示例#1
0
 /**
  * Converts a W3C Element into an XML.E.
  *
  * @param w3cElement the W3C Element
  * @return the equivalent XML.E
  */
 public static ERXML.E e(Element w3cElement) {
   ERXML.E e = ERXML.e(w3cElement.getNodeName());
   org.w3c.dom.NamedNodeMap attributes = w3cElement.getAttributes();
   for (int i = 0; i < attributes.getLength(); i++) {
     org.w3c.dom.Node w3cAttribute = attributes.item(i);
     String attributeName = w3cAttribute.getNodeName();
     String attributeValue = w3cAttribute.getNodeValue();
     e.set(attributeName, attributeValue);
   }
   org.w3c.dom.NodeList w3cChildren = w3cElement.getChildNodes();
   for (int i = 0; i < w3cChildren.getLength(); i++) {
     org.w3c.dom.Node w3cChild = w3cChildren.item(i);
     if (w3cChild instanceof org.w3c.dom.Text) {
       e.text(((org.w3c.dom.Text) w3cChild).getNodeValue());
     } else if (w3cChild instanceof org.w3c.dom.CDATASection) {
       e.cdata(((org.w3c.dom.CDATASection) w3cChild).getNodeValue());
     } else if (w3cChild instanceof org.w3c.dom.Comment) {
       e.comment(((org.w3c.dom.Comment) w3cChild).getNodeValue());
     } else if (w3cChild instanceof org.w3c.dom.Element) {
       e.add(ERXML.e((org.w3c.dom.Element) w3cChild));
     } else {
       throw new IllegalArgumentException("Unable to handle nodes of type '" + w3cChild + "'.");
     }
   }
   return e;
 }
示例#2
0
 /**
  * Creates and returns a new Document. The first item that is an element will be set as the root
  * element of the document.
  *
  * @param children the children to add to the document
  * @return a new element
  */
 public static ERXML.Doc doc(ERXML.Item... children) {
   ERXML.Doc doc = ERXML.doc();
   for (ERXML.Item child : children) {
     doc.add(child);
   }
   return doc;
 }
示例#3
0
 /**
  * Creates and return a document parsed from the given file.
  *
  * @param file the file to parse from
  * @return a new parsed document
  */
 public static ERXML.Doc doc(File file) {
   try {
     Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
     return ERXML.doc(document);
   } catch (Throwable t) {
     throw new IllegalArgumentException("Failed to parse a document from the provided file.", t);
   }
 }
示例#4
0
 /**
  * Creates and return a document parsed from the given string.
  *
  * @param documentString the string to parse as XML
  * @return a new parsed document
  */
 public static ERXML.Doc doc(String documentString) {
   try {
     ERXML.Doc doc;
     if (documentString == null || documentString.trim().length() == 0) {
       doc = ERXML.doc();
     } else {
       Document document =
           DocumentBuilderFactory.newInstance()
               .newDocumentBuilder()
               .parse(new InputSource(new StringReader(documentString)));
       doc = ERXML.doc(document);
     }
     return doc;
   } catch (Throwable t) {
     throw new IllegalArgumentException("Failed to parse a document from the provided string.", t);
   }
 }
示例#5
0
 /**
  * Creates and return a document parsed from the given reader.
  *
  * @param reader the reader to parse from
  * @return a new parsed document
  */
 public static ERXML.Doc doc(Reader reader) {
   try {
     Document document =
         DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(reader));
     return ERXML.doc(document);
   } catch (Throwable t) {
     throw new IllegalArgumentException("Failed to parse a document from the provided reader.", t);
   }
 }
示例#6
0
 /**
  * Creates and returns a new Element.
  *
  * @param name the name of the element
  * @param children the children to add to this element (String, XML.Node, or XML.Attr)
  * @return a new element
  */
 public static ERXML.E e(String name, Object... children) {
   ERXML.E e = ERXML.e(name);
   for (Object child : children) {
     if (child instanceof String) {
       e.text((String) child);
     } else if (child instanceof ERXML.Node) {
       e.add((ERXML.Node) child);
     } else if (child instanceof ERXML.Attr) {
       e.add((ERXML.Attr) child);
     } else {
       throw new IllegalArgumentException(
           "Unable to add the object '" + child + "' to an XML element.");
     }
   }
   return e;
 }
示例#7
0
 /**
  * Creates and returns a new comment for this document. If you want comments above the root
  * element, you should call doc.comment(..) prior to calling .root(..).
  *
  * @param comment a new comment for this document
  * @return this document
  */
 public ERXML.Doc comment(String comment) {
   _add(ERXML.comment(comment));
   return this;
 }
示例#8
0
 /**
  * Creates a new root element with the given name and initial text contents and returns it. If
  * there is already a root element, this will throw an exception.
  *
  * @param name the name of the new root element
  * @param value the initial text value of this element
  * @return the new root element
  */
 public ERXML.E root(String name, String value) {
   checkNullRoot();
   ERXML.E root = ERXML.e(name, value);
   setRoot(root);
   return root;
 }
示例#9
0
 /**
  * Converts a W3C Document into an XML.Doc.
  *
  * @param w3cDocument the W3C Document
  * @return the equivalent XML.Doc
  */
 public static ERXML.Doc doc(org.w3c.dom.Document w3cDocument) {
   org.w3c.dom.Element w3cElement = w3cDocument.getDocumentElement();
   Doc doc = ERXML.doc();
   doc.setRoot(ERXML.e(w3cElement));
   return doc;
 }
示例#10
0
 /**
  * Creates and appends a new comment node to this element.
  *
  * @param comment the comment value to append
  * @return the new comment node
  */
 public ERXML.Comment comment(String comment) {
   return add(ERXML.comment(comment));
 }
示例#11
0
 /**
  * Creates and appends a new CDATA node to this element.
  *
  * @param cdata the cdata value to append
  * @return the new cdata node
  */
 public ERXML.CDATA cdata(String cdata) {
   return add(ERXML.cdata(cdata));
 }
示例#12
0
 /**
  * Creates and appends a new text node to this element.
  *
  * @param text the text value to append
  * @return the new text node
  */
 public ERXML.Text text(String text) {
   return add(ERXML.text(text));
 }
示例#13
0
 /**
  * Creates and appends a new element to this element.
  *
  * @param name the name of the new element
  * @param value the text value of the new element
  * @return the new element
  */
 public ERXML.E e(String name, String value) {
   return add(ERXML.e(name, value));
 }
示例#14
0
 /**
  * Creates and appends a new element to this element.
  *
  * @param name the name of the new element
  * @return the new element
  */
 public ERXML.E e(String name) {
   return add(ERXML.e(name));
 }