Package org.xwiki.xml

Class XMLUtils


  • public final class XMLUtils
    extends Object
    XML Utility methods.
    Since:
    1.6M1
    Version:
    $Id: 3ec80ca576373b194219647394c28a669d01e0df $
    • Method Detail

      • extractXML

        public static String extractXML​(Node node,
                                        int start,
                                        int length)
        Extracts a well-formed XML fragment from the given DOM tree.
        Parameters:
        node - the root of the DOM tree where the extraction takes place
        start - the index of the first character
        length - the maximum number of characters in text nodes to include in the returned fragment
        Returns:
        a well-formed XML fragment starting at the given character index and having up to the specified length, summing only the characters in text nodes
        Since:
        1.6M2
      • escapeXMLComment

        public static String escapeXMLComment​(String content)
        XML comment does not support some characters inside its content but there is no official escaping/unescaping for it so we made our own.
        • 1) Escape existing \
        • 2) Escape --
        • 3) Escape > or - at the start of the comment
        • 4) Escape { to prevent XWiki macro syntax
        • 5) Add \ (unescaped as "") at the end if the last char is -
        Parameters:
        content - the XML comment content to escape
        Returns:
        the escaped content.
        Since:
        1.9M2
      • unescapeXMLComment

        public static String unescapeXMLComment​(String content)
        XML comment does not support some characters inside its content but there is no official escaping/unescaping for it so we made our own.
        Parameters:
        content - the XML comment content to unescape
        Returns:
        the unescaped content.
        Since:
        1.9M2
        See Also:
        escapeXMLComment(String)
      • escape

        @Deprecated
        public static String escape​(Object content)
        Deprecated.
        since 12.8RC1, use escape(String) instead
        Escapes all the XML special characters and a XWiki Syntax 2.0+ special character (i.e., {, to protect against {{/html}}) in a String. The escaping is done using numerical XML entities to allow the content to be used as an XML attribute value or as an XML element text. For instance, <b>{{html}}$x{{/html}}</b> will be escaped and can thus be put inside an XML attribute. To illustrate, the value can be used in a div tag <div>&#60;b&#62;&#123;&#123;html}}$x&#123;&#123;/html}}&#60;/b&#62; </div> or in the attribute of an input tag <input value="&#60;b&#62;&#123;&#123;html}}$x&#123;&#123;/html}}&#60;/b&#62;" />.

        Specifically, escapes <, >, ", ', & and {.

        Note that is is preferable to use escapeAttributeValue(String) when the content is used as an XML tag attribute, and escapeElementText(String) when the content is used as an XML text.

        Parameters:
        content - the text to escape, may be null. The content is converted to String using Objects.toString(Object, String), where the second parameter is null
        Returns:
        a new escaped String, null if null input
        See Also:
        escapeAttributeValue(String), escapeElementText(String)
      • escape

        public static String escape​(String content)
        Escapes all the XML special characters and a XWiki Syntax 2.0+ special character (i.e., {, to protect against {{/html}}) in a String. The escaping is done using numerical XML entities to allow the content to be used as an XML attribute value or as an XML element text. For instance, <b>{{html}}$x{{/html}}</b> will be escaped and can thus be put inside as XML attribute. To illustrate, the value can be used in a div tag <div>&#60;b&#62;&#123;&#123;html}}$x&#123;&#123;/html}}&#60;/b&#62; </div> or in the attribute of an input tag <input value="&#60;b&#62;&#123;&#123;html}}$x&#123;&#123;/html}}&#60;/b&#62;" />.

        Specifically, escapes <, >, ", ', & and {.

        Note that is is preferable to use escapeAttributeValue(String) when the content is used as an XML tag attribute, and escapeElementText(String) when the content is used as an XML text.

        Parameters:
        content - the text to escape, may be null
        Returns:
        a new escaped String, null if null input
        Since:
        12.8RC1, 12.6.3, 11.10.11
        See Also:
        escapeAttributeValue(String), escapeElementText(String)
      • escapeAttributeValue

        @Deprecated
        public static String escapeAttributeValue​(Object content)
        Deprecated.
        since 12.8RC1, use escapeAttributeValue(String) instead
        Escapes all the XML special characters and a XWiki Syntax 2.0+ special character (i.e., {, to protect against {{/html}}) in a String. The escaping is done using numerical XML entities to allow the content to be used inside XML attributes. For instance, <b>{{html}}$x{{/html}}</b> will be escaped and can thus be put inside an XML attribute. To illustrate, the value can be used in the attribute of an input tag <input value="&#60;b&#62;&#123;&#123;html}}$x&#123;&#123;/html}}&#60;/b&#62;" />.

        Specifically, escapes <, >, ", ', & and {.

        Parameters:
        content - the text to escape, may be null. The content is converted to String using String.valueOf(Object) before escaping.
        Returns:
        a new escaped String, null if null input
      • escapeAttributeValue

        public static String escapeAttributeValue​(String content)
        Escapes all the XML special characters and a XWiki Syntax 2.0+ special character (i.e., {, to protect against {{/html}}) in a String. The escaping is done using numerical XML entities to allow the content to be used inside XML attributes. For instance, <b>{{html}}$x{{/html}}</b> will be escaped and can thus be put inside an XML attribute. To illustrate, the value can be used in the attribute of an input tag <input value="&#60;b&#62;&#123;&#123;html}}$x&#123;&#123;/html}}&#60;/b&#62;" />.

        Specifically, escapes <, >, ", ', & and {.

        Parameters:
        content - the text to escape, may be null
        Returns:
        a new escaped String, null if null input
        Since:
        12.8RC1, 12.6.3, 11.10.11
      • escapeElementText

        public static String escapeElementText​(String content)
        Escapes XML special characters in a String using numerical XML entities, so that the resulting string can safely be used as an XML element text value. For instance, Jim & John will be escaped and can thus be put inside an XML tag, such as the p tag, as in <p>Jim &amp; John</p>. Specifically, escapes < to &lt;, and & to &amp;.

        Since 13.10.9, 14.4.4 and 14.7RC1 the character { is also escaped.

        Parameters:
        content - the text to escape, may be null.
        Returns:
        a new escaped String, null if null input
        Since:
        12.8RC1, 12.6.3, 11.10.11
      • containsElementText

        public static boolean containsElementText​(CharSequence content)
        Same logic as escapeElementText(String) but only indicate if there is something to escape.
        Parameters:
        content - the content to parse
        Returns:
        true if the passed content contains content that can be interpreted as XML syntax
        Since:
        12.10, 12.6.5
        See Also:
        escapeElementText(String)
      • escapeElementContent

        @Deprecated
        public static String escapeElementContent​(Object content)
        Deprecated.
        since 12.8RC1, use escapeElementText(String) instead.
        Escapes the XML special characters in a String using numerical XML entities, so that the resulting string can safely be used as an XML text node. Specifically, escapes <, >, and &.
        Parameters:
        content - the text to escape, may be null. The content is converted to String using String.valueOf(Object) before escaping.
        Returns:
        a new escaped String, null if null input
      • unescape

        @Deprecated
        public static String unescape​(Object content)
        Deprecated.
        since 12.8RC1, use StringEscapeUtils.unescapeXml(String) instead
        Unescape encoded special XML characters. Only >, < &, ", ' and { are unescaped, since they are the only ones that affect the resulting markup.
        Parameters:
        content - the text to decode, may be null. The content is converted to String using String.valueOf(Object) before escaping
        Returns:
        unescaped content, null if null input
      • createDOMDocument

        public static Document createDOMDocument()
        Construct a new (empty) DOM Document and return it.
        Returns:
        an empty DOM Document
      • parse

        public static Document parse​(LSInput source)
        Parse a DOM Document from a source.
        Parameters:
        source - the source input to parse
        Returns:
        the equivalent DOM Document, or null if the parsing failed.
      • serialize

        public static String serialize​(Node node)
        Serialize a DOM Node into a string, including the XML declaration at the start.
        Parameters:
        node - the node to export
        Returns:
        the serialized node, or an empty string if the serialization fails
      • serialize

        public static String serialize​(Node node,
                                       boolean withXmlDeclaration)
        Serialize a DOM Node into a string, with an optional XML declaration at the start.
        Parameters:
        node - the node to export
        withXmlDeclaration - whether to output the XML declaration or not
        Returns:
        the serialized node, or an empty string if the serialization fails or the node is null
      • transform

        public static String transform​(Source xml,
                                       Source xslt)
        Apply an XSLT transformation to a Document.
        Parameters:
        xml - the document to transform
        xslt - the stylesheet to apply
        Returns:
        the transformation result, or null if an error occurs or null xml or xslt input