Class DOMElementWriter


  • public class DOMElementWriter
    extends Object
    Writes a DOM tree to a given Writer.

    Utility class used by XMLUtils.

    Author:
    Lubomir Marinov
    • Field Detail

      • knownEntities

        protected String[] knownEntities
        Don't try to be too smart but at least recognize the predefined entities.
    • Constructor Detail

      • DOMElementWriter

        public DOMElementWriter()
    • Method Detail

      • decodeName

        public static String decodeName​(String name)
        Decodes an XML (element) name according to http://www.w3.org/TR/xml/#NT-Name.
        Parameters:
        name - the XML (element) name to be decoded
        Returns:
        a String which represents name decoded according to http://www.w3.org/TR/xml/#NT-Name
      • encodeName

        public static String encodeName​(String value)
        Encodes a specific String so that it is a valid XML (element) name according to http://www.w3.org/TR/xml/#NT-Name.
        Parameters:
        value - the String to be encoded so that it is a valid XML name
        Returns:
        a String which represents value encoded so that it is a valid XML (element) name
      • write

        public void write​(Element root,
                          OutputStream out)
                   throws IOException
        Writes a DOM tree to a stream in UTF8 encoding. Note that it prepends the <?xml version='1.0' encoding='UTF-8'?>. The indent number is set to 0 and a 2-space indent.
        Parameters:
        root - the root element of the DOM tree.
        out - the outputstream to write to.
        Throws:
        IOException - if an error happens while writing to the stream.
      • write

        public void write​(Node element,
                          Writer out,
                          int indent,
                          String indentWith)
                   throws IOException
        Writes a DOM tree to a stream.
        Parameters:
        element - the Root DOM element of the tree
        out - where to send the output
        indent - number of
        indentWith - string that should be used to indent the corresponding tag.
        Throws:
        IOException - if an error happens while writing to the stream.
      • encode

        public String encode​(String value)
        Escape <, > & &apos;, " as their entities and drop characters that are illegal in XML documents.
        Parameters:
        value - the value to encode
        Returns:
        a String containing the encoded element.
      • isReference

        public boolean isReference​(String ent)
        Is the given argument a character or entity reference?
        Parameters:
        ent - the string whose nature we need to determine.
        Returns:
        true if ent is an entity reference and false otherwise.
      • isLegalCharacter

        public boolean isLegalCharacter​(char c)
        Is the given character allowed inside an XML document?

        See XML 1.0 2.2 http://www.w3.org/TR/1998/REC-xml-19980210#charsets.

        Parameters:
        c - the character whose nature we'd like to determine.
        Returns:
        true if c is a legal character and false otherwise
        Since:
        1.10, Ant 1.5