Package org.xwiki.xml
Class XMLUtils
- java.lang.Object
-
- org.xwiki.xml.XMLUtils
-
public final class XMLUtils extends Object
XML Utility methods.- Since:
- 1.6M1
- Version:
- $Id: 3ec80ca576373b194219647394c28a669d01e0df $
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static boolean
containsElementText(CharSequence content)
Same logic asescapeElementText(String)
but only indicate if there is something to escape.static Document
createDOMDocument()
Construct a new (empty) DOM Document and return it.static String
escape(Object content)
Deprecated.since 12.8RC1, useescape(String)
insteadstatic 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 aString
.static String
escapeAttributeValue(Object content)
Deprecated.since 12.8RC1, useescapeAttributeValue(String)
insteadstatic 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 aString
.static String
escapeElementContent(Object content)
Deprecated.since 12.8RC1, useescapeElementText(String)
instead.static String
escapeElementText(String content)
Escapes XML special characters in aString
using numerical XML entities, so that the resulting string can safely be used as an XML element text value.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.static String
extractXML(Node node, int start, int length)
Extracts a well-formed XML fragment from the given DOM tree.static String
formatXMLContent(String content)
Parse and pretty print a XML content.static Document
parse(LSInput source)
Parse a DOM Document from a source.static String
serialize(Node node)
Serialize a DOM Node into a string, including the XML declaration at the start.static String
serialize(Node node, boolean withXmlDeclaration)
Serialize a DOM Node into a string, with an optional XML declaration at the start.static String
transform(Source xml, Source xslt)
Apply an XSLT transformation to a Document.static String
unescape(Object content)
Deprecated.since 12.8RC1, useStringEscapeUtils.unescapeXml(String)
insteadstatic 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.
-
-
-
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 placestart
- the index of the first characterlength
- 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, useescape(String)
insteadEscapes all the XML special characters and a XWiki Syntax 2.0+ special character (i.e.,{
, to protect against{{/html}}
) in aString
. 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><b>{{html}}$x{{/html}}</b> </div>
or in the attribute of an input tag<input value="<b>{{html}}$x{{/html}}</b>" />
.Specifically, escapes <, >, ", ', & and {.
Note that is is preferable to use
escapeAttributeValue(String)
when the content is used as an XML tag attribute, andescapeElementText(String)
when the content is used as an XML text.- Parameters:
content
- the text to escape, may benull
. The content is converted toString
usingObjects.toString(Object, String)
, where the second parameter isnull
- Returns:
- a new escaped
String
,null
ifnull
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 aString
. 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><b>{{html}}$x{{/html}}</b> </div>
or in the attribute of an input tag<input value="<b>{{html}}$x{{/html}}</b>" />
.Specifically, escapes <, >, ", ', & and {.
Note that is is preferable to use
escapeAttributeValue(String)
when the content is used as an XML tag attribute, andescapeElementText(String)
when the content is used as an XML text.- Parameters:
content
- the text to escape, may benull
- Returns:
- a new escaped
String
,null
ifnull
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, useescapeAttributeValue(String)
insteadEscapes all the XML special characters and a XWiki Syntax 2.0+ special character (i.e.,{
, to protect against {{/html}}) in aString
. 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="<b>{{html}}$x{{/html}}</b>" />
.Specifically, escapes <, >, ", ', & and {.
- Parameters:
content
- the text to escape, may benull
. The content is converted toString
usingString.valueOf(Object)
before escaping.- Returns:
- a new escaped
String
,null
ifnull
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 aString
. 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="<b>{{html}}$x{{/html}}</b>" />
.Specifically, escapes <, >, ", ', & and {.
- Parameters:
content
- the text to escape, may benull
- Returns:
- a new escaped
String
,null
ifnull
input - Since:
- 12.8RC1, 12.6.3, 11.10.11
-
escapeElementText
public static String escapeElementText(String content)
Escapes XML special characters in aString
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 thep
tag, as in<p>Jim & John</p>
. Specifically, escapes < to<
, and & to&
.Since 13.10.9, 14.4.4 and 14.7RC1 the character { is also escaped.
- Parameters:
content
- the text to escape, may benull
.- Returns:
- a new escaped
String
,null
ifnull
input - Since:
- 12.8RC1, 12.6.3, 11.10.11
-
containsElementText
public static boolean containsElementText(CharSequence content)
Same logic asescapeElementText(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, useescapeElementText(String)
instead.Escapes the XML special characters in aString
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 benull
. The content is converted toString
usingString.valueOf(Object)
before escaping.- Returns:
- a new escaped
String
,null
ifnull
input
-
unescape
@Deprecated public static String unescape(Object content)
Deprecated.since 12.8RC1, useStringEscapeUtils.unescapeXml(String)
insteadUnescape 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 benull
. The content is converted toString
usingString.valueOf(Object)
before escaping- Returns:
- unescaped content,
null
ifnull
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 exportwithXmlDeclaration
- 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 transformxslt
- the stylesheet to apply- Returns:
- the transformation result, or
null
if an error occurs ornull
xml or xslt input
-
formatXMLContent
public static String formatXMLContent(String content) throws TransformerFactoryConfigurationError, TransformerException
Parse and pretty print a XML content.- Parameters:
content
- the XML content to format- Returns:
- the formatted version of the passed XML content
- Throws:
TransformerFactoryConfigurationError
- when failing to create aTransformerFactoryConfigurationError
TransformerException
- when failing to transform the content- Since:
- 5.2M1
-
-