Package fi.luomus.commons.xml

Examples of fi.luomus.commons.xml.Document


      return DEFAULT_LIMIT;
   
  }

  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


  }
 
  @Override
  public void execute() throws Exception {
    Query query = parseQuery();
    Document response = generateResponse(query);
    String format = req.getParameter("format");
    if (format == null) {
      writeXMLToOut(response);
    } else if (format.equals("json")) {
      writeJSONToOut(response);
View Full Code Here

public class XmlToJsonTests {

  @Test
  public void test_it_doesnt_convert_numbers_to_numbers_and_drop_front_zeros() throws JSONException {
    Document doc = new Document("root");
    doc.getRootNode().addChildNode("int").setContents("0100");
    doc.getRootNode().addChildNode("int").setContents("00100");
    doc.getRootNode().addChildNode("int").setContents("000100");
    doc.getRootNode().addChildNode("int").setContents("000000");
    doc.getRootNode().addChildNode("int").setContents("1");
    String xml = new XMLWriter(doc).generateXML();

    JSONObject json = XML.toJSONObject(xml);
    String expected = "{\"root\":{\"int\":[\"0100\",\"00100\",\"000100\",\"000000\",\"1\"]}}";
    Assert.assertEquals(expected, json.toString());
View Full Code Here

   * </login>
   */
  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());
    }
View Full Code Here

  }

  @Override
  public Integer getCount() throws Exception {
    HttpGet request = new HttpGet(uri + "/get/count/" + systemID);
    Document document = httpclient.contentAsDocument(request);
    String count = document.getRootNode().getContents();
    return Integer.valueOf(count);
  }
View Full Code Here

  @Override
  public List<FormData> getForms() throws Exception {
    List<FormData> list = new LinkedList<FormData>();

    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")) {
        owners.add(owner.getAttribute("name"));
      }
View Full Code Here

  @Override
  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);
View Full Code Here

      super(msg);
    }
  }

  public static FormData readFormData(String xml, String systemId) throws MalformedFormDataException {
    Document document = null;
    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());
        }
      }
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());
View Full Code Here

      assertEquals("value with \"quotes\" & 'apostrophes'", root.getAttributes().get(0).getValue());
      assertEquals("other <value>", root.getAttributes().get(1).getValue());
    }
   
    public void test__2() {
      Document document = new Document("root");
      Node root = document.getRootNode();
      root.addChildNode("node").setContents("value's öäÖÄ jes").addAttribute("a1", "v1");
      root.addChildNode("other-node").addChildNode("sub-node").addAttribute("a2", "v2");
      root.addChildNode("data").setCDATA("1234567890 &ent; &amp; & // &quot;    ");
      String xml = new XMLWriter(document).generateXML();
      String expected = "" +
      "<?xml version='1.0' encoding='utf-8'?>\n" +
      "<root>\n" +
      "\n" +
      "  <node a1=\"v1\">value&apos;s öäÖÄ jes</node>\n" +
      "\n" +
      "  <other-node>\n" +
      "    <sub-node a2=\"v2\" />\n" +
      "  </other-node>\n" +
      "\n" +
      "  <data>\n" +
      "<![CDATA[1234567890 &ent; &amp; & // &quot;    ]]>\n" +
      "  </data>\n" +
      "\n" +
      "</root>";
      assertEquals(expected.trim(), xml.trim());
     
      XMLReader reader = new XMLReader();
      document = reader.parse(xml);
      root = document.getRootNode();
      assertEquals(true, root.hasChildNodes());
      assertEquals(false, root.hasContents());
      assertEquals(3, root.getChildNodes().size());
      assertEquals(false, root.hasAttributes());
      assertEquals(0, root.getAttributes().size());
View Full Code Here

TOP

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

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.