Package fi.luomus.commons.xml.Document

Examples of fi.luomus.commons.xml.Document.Node


    for (Node resource : document.getRootNode()) {
      String id = resource.getNode("id").getContents();
      StringBuilder desc = new StringBuilder();
      List<Node> childNodes = resource.getChildNodes();
      for (int index : fieldIndexes) {
        Node resourceChild = childNodes.get(index);
        String contents = resourceChild.getContents();
        if (contents == null) continue;
        desc.append(contents).append(", ");
      }
      Utils.removeLastChar(desc);
      Utils.removeLastChar(desc);
View Full Code Here


    return sdf;
  }

  public static Document createSimpleDarwinRecordSet() {
    Document doc = new Document("SimpleDarwinRecordSet");
    Node root = doc.getRootNode();
    root.addAttribute("xmlns", "http://rs.tdwg.org/dwc/xsd/simpledarwincore/");
    root.addAttribute("xmlns:dc", "http://purl.org/dc/terms/");
    root.addAttribute("xmlns:dwc", "http://rs.tdwg.org/dwc/terms/");
    root.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
    root.addAttribute("xsi:schemaLocation", "http://rs.tdwg.org/dwc/xsd/simpledarwincore/ http://rs.tdwg.org/dwc/xsd/tdwg_dwc_simple.xsd");
    return doc;
  }
View Full Code Here

    root.addAttribute("xsi:schemaLocation", "http://rs.tdwg.org/dwc/xsd/simpledarwincore/ http://rs.tdwg.org/dwc/xsd/tdwg_dwc_simple.xsd");
    return doc;
  }

  public static void addRecordToRecordSet(Node recordSet, SimpleDarwinRecord record) {
    Node entry = recordSet.addChildNode("SimpleDarwinRecord");
    entry.addChildNode("dwc:occurrenceID").setContents(record.getOccurrenceID());
    entry.addChildNode("dwc:recordedBy").setContents(record.getRecordedBy());
    entry.addChildNode("dwc:basisOfRecord").setContents(record.getBasisOfRecord());
    entry.addChildNode("dwc:scientificName").setContents(record.getScientificName());
    for (VernacularName vernacularName : record.getVernacularNames()) {
      if (vernacularName.given()) {
        entry.addChildNode("dwc:vernacularName").setContents(vernacularName.getVernacularName()).addAttribute("xml:lang", vernacularName.getLangcode());
      }
    }
    entry.addChildNode("dwc:individualCount").setContents(record.getIndividualCount());
    entry.addChildNode("dwc:eventDate").setContents(record.getEventStartEndDate());
    entry.addChildNode("dwc:locality").setContents(record.getLocality());
    entry.addChildNode("dwc:decimalLatitude").setContents(round(record.getDecimalLatitude()));
    entry.addChildNode("dwc:decimalLongitude").setContents(round(record.getDecimalLongitude()));
    entry.addChildNode("dwc:coordinatePrecision").setContents(round(record.getCoordinatePrecision()));
    entry.addChildNode("dwc:geodeticDatum").setContents("WGS84");
    entry.addChildNode("dwc:collectionID").setContents(record.getCollectionID());
    entry.addChildNode("dwc:collectionCode").setContents(record.getCollectionCode());
    entry.addChildNode("dwc:occurrenceRemarks").setContents(record.getOccurrenceRemarks());
  }
View Full Code Here

    return response;
  }
 
  private Document generateResultXMLDocument(List<AggregateResponseEntry> results) {
    Document response = new Document("response");
    Node root = response.getRootNode();
    int totalCount = 0;
    List<Double> counts = new LinkedList<Double>();
    int maxCount = 0;
    for (AggregateResponseEntry entry : results) {
      Node entryNode = new Node("entry");
      for (KeyValuePair aggregatedBy : entry.getAggregatedByValues()) {
        entryNode.addAttribute(aggregatedBy.getKey(), aggregatedBy.getValue());
      }
      entryNode.addChildNode(new Node("count").setContents(entry.getCount()));
      entryNode.addChildNode(new Node("individual-count-sum").setContents(entry.getIndividualCountSum()));
      entryNode.addChildNode(new Node("individual-count-max").setContents(entry.getIndividualCountMax()));
      entryNode.addChildNode(new Node("oldest-record").setContents(Utils.toIsoDateFormat(entry.getOldestRecord())));
      entryNode.addChildNode(new Node("newest-record").setContents(Utils.toIsoDateFormat(entry.getNewestRecord())));
      root.addChildNode(entryNode);
      totalCount += entry.getCount();
      counts.add((double) entry.getCount());
      maxCount = Math.max(entry.getCount(), maxCount);
    }
View Full Code Here

   
  }

  private Document generateResultXMLDocument(List<SimpleDarwinRecord> results) {
    Document doc = Utils.createSimpleDarwinRecordSet();
    Node root = doc.getRootNode();
    for (SimpleDarwinRecord record : results) {
      Utils.addRecordToRecordSet(root, record);
    }
    return doc;
  }
View Full Code Here

   */
  private Map<String, String> parseUserModel(String xml) throws Exception {
    Map<String, String> usermodel = new HashMap<String, String>();
    XMLReader reader = new XMLReader();
    Document document = reader.parse(xml);
    Node rootNode = document.getRootNode();
    String userType = rootNode.getAttribute("type");
    usermodel.put(USER_TYPE, userType);
    for (Node node : rootNode.getChildNodes()) {
      usermodel.put(node.getName(), node.getContents());
    }
    return usermodel;
  }
View Full Code Here

    HttpGet request = new HttpGet(uri + "/get/list/" + systemID);
    Document document = httpclient.contentAsDocument(request);
    if (document == null) {
      throw new IllegalStateException("Api not working");
    }
    Node root = document.getRootNode();
    checkFormTypeIsForThisSystem(root);
    for (Node formNode : document.getRootNode().getChildNodes()) {
      FormData form = new FormData(formNode.getAttribute("id"), systemID);
      List<String> owners = new ArrayList<String>();
      for (Node owner : formNode.getNode("owners")) {
View Full Code Here

  public void sendForCorrections(String id, String message) throws Exception {
    HttpPost request = new HttpPost(uri + "/post/update/" + systemID);

    String xml = this.getXML(id);
    Document document = new XMLReader().parse(xml);
    Node root = document.getRootNode();
    if (root.hasChildNodes("message")) {
      Node messageNode = root.getNode("message");
      messageNode.setContents(messageNode.getContents() + " \n " + message);
    } else {
      root.addChildNode("message").setContents(message);
    }
    xml = new XMLWriter(document).generateXML();
View Full Code Here

    try {
      document = new XMLReader().parse(xml);
    } catch (Exception e) {
      throw new MalformedFormDataException("Form data was malformed: " + xml);
    }
    Node formData = document.getRootNode();
    String id = null;
    try {
      id = formData.getAttribute("id");
    } catch (IllegalArgumentException e) {
      id = "undefined";
    }

    Map<String, String> formDataMap = new HashMap<String, String>();
    if (formData.hasChildNodes("data")) {
      for (Node node : formData.getNode("data")) {
        if (node.hasContents()) {
          formDataMap.put(node.getName(), node.getContents());
        }
      }
    }
    FormData form = new FormData(id, systemId).setData(formDataMap);

    if (formData.hasAttribute("returned-empty")) {
      String attrValue = formData.getAttribute("returned-empty");
      if (attrValue.equalsIgnoreCase("yes")) {
        form.setReturnedEmpty(true);
      }
    }

    if (document.getRootNode().hasChildNodes("log")) {
      for (Node node : document.getRootNode().getNode("log")) {
        String user = node.getAttribute("owner");
        Double time = Double.valueOf(node.getAttribute("time"));
        String action = node.getContents();
        form.addLogEntry(new FormData.LogEntry(user, time, action));
      }
    }

    if (formData.hasChildNodes("message")) {
      for (Node node : document.getRootNode().getChildNodes("message")) {
        String contents = node.getContents().trim();
        if (contents.length() > 0) {
          form.addMessage(node.getContents());
        }
      }
    }

    List<String> owners = new ArrayList<String>();
    if (formData.hasChildNodes("owners")) {
      for (Node owner : formData.getNode("owners")) {
        owners.add(owner.getContents());
      }
      form.setOwners(owners);
    }
   
View Full Code Here

 
  public static class UsingXMLWriterAndReader extends TestCase {
   
    public void test_() {
      Document document = new Document("root");
      Node root = document.getRootNode();
      root.addAttribute("attribute", "value with \"quotes\" & 'apostrophes' ");
      root.addAttribute("second", "other <value> ");
      String xml = new XMLWriter(document).generateXML();
      String expected = "" +
      "<?xml version='1.0' encoding='utf-8'?>\n" +
      "<root attribute=\"value with &quot;quotes&quot; &amp; &apos;apostrophes&apos;\" second=\"other &lt;value&gt;\" />";
      assertEquals(expected.trim(), xml.trim());
     
      XMLReader reader = new XMLReader();
      document = reader.parse(xml);
      root = document.getRootNode();
      assertEquals("root", root.getName());
      assertEquals("", root.getContents());
      assertEquals(false, root.hasChildNodes());
      assertEquals(false, root.hasContents());
      assertEquals(0, root.getChildNodes().size());
      assertEquals(true, root.hasAttributes());
      assertEquals(2, root.getAttributes().size());
      assertEquals("attribute", root.getAttributes().get(0).getName());
      assertEquals("second", root.getAttributes().get(1).getName());
      assertEquals("value with \"quotes\" & 'apostrophes'", root.getAttributes().get(0).getValue());
      assertEquals("other <value>", root.getAttributes().get(1).getValue());
    }
View Full Code Here

TOP

Related Classes of fi.luomus.commons.xml.Document.Node

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.