org.xwiki.xml
Class XMLUtils

java.lang.Object
  extended by org.xwiki.xml.XMLUtils

public final class XMLUtils
extends Object

XML Utility methods.

Since:
1.6M1
Version:
$Id$

Method Summary
static Document createDOMDocument()
          Construct a new (empty) DOM Document and return it.
static String escape(Object content)
          Escapes all the XML special characters in a String using numerical XML entities.
static String escapeAttributeValue(Object content)
          Escapes all the XML special characters in a String using numerical XML entities, so that the resulting string can safely be used as an XML attribute value.
static String escapeElementContent(Object content)
          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.
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 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)
          Unescape encoded special XML characters.
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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.

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

public static String escape(Object content)
Escapes all the XML special characters in a String using numerical XML entities. Specifically, escapes <, >, ", ' and &.

Parameters:
content - the text to escape, may be null
Returns:
a new escaped String, null if null input

escapeAttributeValue

public static String escapeAttributeValue(Object content)
Escapes all the XML special characters in a String using numerical XML entities, so that the resulting string can safely be used as an XML attribute value. Specifically, escapes <, >, ", ' and &.

Parameters:
content - the text to escape, may be null
Returns:
a new escaped String, null if null input

escapeElementContent

public static String escapeElementContent(Object content)
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
Returns:
a new escaped String, null if null input

unescape

public static String unescape(Object content)
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
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


Copyright © 2004-2011 XWiki. All Rights Reserved.