org.milyn.xml
Class XmlUtil

java.lang.Object
  extended by org.milyn.xml.XmlUtil

public class XmlUtil
extends Object

XMl utility methods.

Author:
Tom Fennelly

Nested Class Summary
static class XmlUtil.VALIDATION_TYPE
          Document validation types.
 
Field Summary
static char[] AMP
           
static char[] APOS
           
static DocumentBuilder documentBuilder
           
static char[] GT
           
static char[] LT
           
static char[] QUOT
           
 
Constructor Summary
XmlUtil()
           
 
Method Summary
static Element createElement(String localPart)
          Create an element with the specified name.
static Element createElementNS(String namespace, String localPart)
          Create an element with the specified name and namespace.
static void encodeAttributeValue(char[] characters, int offset, int length, Writer writer)
           
static void encodeTextValue(char[] characters, int offset, int length, Writer writer)
           
static Node getNode(Node node, String xpath)
          Get the W3C Node instance associated with the XPath selection supplied.
static NodeList getNodeList(Node node, String xpath)
          Get the W3C NodeList instance associated with the XPath selection supplied.
static String getString(Node node, String xpath)
          Get the String data associated with the XPath selection supplied.
static String indent(String xml, int indent)
          Indent the supplied XML string by the number of spaces specified in the 'indent' param.
static boolean isTextNode(Node node)
          Is the supplied W3C DOM Node a text node.
static boolean isXMLReservedNamespace(String namespace)
           
static Document parseStream(InputStream stream)
          Basic DOM namespace aware parse.
static Document parseStream(InputStream stream, EntityResolver entityResolver, XmlUtil.VALIDATION_TYPE validation, boolean expandEntityRefs)
          Parse the XML stream and return the associated W3C Document object.
static Document parseStream(InputStream stream, XmlUtil.VALIDATION_TYPE validation, boolean expandEntityRefs)
          Parse the XML stream and return the associated W3C Document object.
static Document parseStream(Reader stream)
          Basic DOM namespace aware parse.
static Document parseStream(Reader stream, EntityResolver entityResolver, XmlUtil.VALIDATION_TYPE validation, boolean expandEntityRefs)
          Parse the XML stream and return the associated W3C Document object.
static Document parseStream(Reader stream, ErrorHandler errorHandler)
          Basic DOM namespace aware parse.
static void removeEntities(Reader reader, Writer writer)
          Remove all entities from the supplied Reader stream replacing them with their actual character values.
static String removeEntities(String string)
          Remove all entities from the supplied String stream replacing them with there actual character values.
static void rewriteEntities(Reader reader, Writer writer)
          Rewrite all entities from the supplied Reader stream replacing them with their character reference equivalents.
static String serialize(Node node, boolean format)
          Serialise the supplied W3C DOM subtree.
static void serialize(Node node, boolean format, Writer writer)
          Serialise the supplied W3C DOM subtree.
static String serialize(NodeList nodeList)
          Serialise the supplied W3C DOM subtree.
static String serialize(NodeList nodeList, boolean format)
          Serialise the supplied W3C DOM subtree.
static void serialize(NodeList nodeList, boolean format, Writer writer)
          Serialise the supplied W3C DOM subtree.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

documentBuilder

public static final DocumentBuilder documentBuilder

LT

public static final char[] LT

GT

public static final char[] GT

AMP

public static final char[] AMP

QUOT

public static final char[] QUOT

APOS

public static final char[] APOS
Constructor Detail

XmlUtil

public XmlUtil()
Method Detail

isXMLReservedNamespace

public static boolean isXMLReservedNamespace(String namespace)

removeEntities

public static void removeEntities(Reader reader,
                                  Writer writer)
                           throws IOException
Remove all entities from the supplied Reader stream replacing them with their actual character values.

Both the read and write streams are returned unclosed.

Parameters:
reader - The read stream.
writer - The write stream.
Throws:
IOException

removeEntities

public static String removeEntities(String string)
Remove all entities from the supplied String stream replacing them with there actual character values.

Parameters:
string - The string on which the operation is to be carried out.
Returns:
The string with its entities rewriten.

rewriteEntities

public static void rewriteEntities(Reader reader,
                                   Writer writer)
                            throws IOException
Rewrite all entities from the supplied Reader stream replacing them with their character reference equivalents.

Example:   is rewriten as  

Both the read and write streams are returned unclosed.

Parameters:
reader - The read stream.
writer - The write stream.
Throws:
IOException

parseStream

public static Document parseStream(InputStream stream,
                                   XmlUtil.VALIDATION_TYPE validation,
                                   boolean expandEntityRefs)
                            throws SAXException,
                                   IOException
Parse the XML stream and return the associated W3C Document object.

Parameters:
stream - The stream to be parsed.
validation - Validation type to be carried out on the document.
expandEntityRefs - Expand entity References as per DocumentBuilderFactory.setExpandEntityReferences(boolean).
Returns:
The W3C Document object associated with the input stream.
Throws:
SAXException
IOException

parseStream

public static Document parseStream(InputStream stream,
                                   EntityResolver entityResolver,
                                   XmlUtil.VALIDATION_TYPE validation,
                                   boolean expandEntityRefs)
                            throws SAXException,
                                   IOException
Parse the XML stream and return the associated W3C Document object.

Parameters:
stream - The stream to be parsed.
entityResolver - Entity resolver to be used during the parse.
validation - Validation type to be carried out on the document.
expandEntityRefs - Expand entity References as per DocumentBuilderFactory.setExpandEntityReferences(boolean).
Returns:
The W3C Document object associated with the input stream.
Throws:
SAXException
IOException

parseStream

public static Document parseStream(Reader stream,
                                   EntityResolver entityResolver,
                                   XmlUtil.VALIDATION_TYPE validation,
                                   boolean expandEntityRefs)
                            throws SAXException,
                                   IOException
Parse the XML stream and return the associated W3C Document object.

Parameters:
stream - The stream to be parsed.
entityResolver - Entity resolver to be used during the parse.
validation - Validation type to be carried out on the document.
expandEntityRefs - Expand entity References as per DocumentBuilderFactory.setExpandEntityReferences(boolean).
Returns:
The W3C Document object associated with the input stream.
Throws:
SAXException
IOException

parseStream

public static Document parseStream(InputStream stream)
                            throws ParserConfigurationException,
                                   IOException,
                                   SAXException
Basic DOM namespace aware parse.

Parameters:
stream - Document stream.
Returns:
Document instance.
Throws:
ParserConfigurationException
IOException
SAXException

parseStream

public static Document parseStream(Reader stream)
                            throws ParserConfigurationException,
                                   IOException,
                                   SAXException
Basic DOM namespace aware parse.

Parameters:
stream - Document stream.
Returns:
Document instance.
Throws:
ParserConfigurationException
IOException
SAXException

parseStream

public static Document parseStream(Reader stream,
                                   ErrorHandler errorHandler)
                            throws ParserConfigurationException,
                                   IOException,
                                   SAXException
Basic DOM namespace aware parse.

Parameters:
stream - Document stream.
errorHandler - ErrorHandler to be set on the DocumentBuilder. This can be used to controll error reporting. If null the default error handler will be used.
Returns:
Document instance.
Throws:
ParserConfigurationException
IOException
SAXException

getNodeList

public static NodeList getNodeList(Node node,
                                   String xpath)
Get the W3C NodeList instance associated with the XPath selection supplied.

Parameters:
node - The document node to be searched.
xpath - The XPath String to be used in the selection.
Returns:
The W3C NodeList instance at the specified location in the document, or null.

getNode

public static Node getNode(Node node,
                           String xpath)
Get the W3C Node instance associated with the XPath selection supplied.

Parameters:
node - The document node to be searched.
xpath - The XPath String to be used in the selection.
Returns:
The W3C Node instance at the specified location in the document, or null.

getString

public static String getString(Node node,
                               String xpath)
Get the String data associated with the XPath selection supplied.

Parameters:
node - The node to be searched.
xpath - The XPath String to be used in the selection.
Returns:
The string data located at the specified location in the document, or an empty string for an empty resultset query.

createElement

public static Element createElement(String localPart)
Create an element with the specified name.

Parameters:
localPart - The localPart name.
Returns:
Element instance.

createElementNS

public static Element createElementNS(String namespace,
                                      String localPart)
Create an element with the specified name and namespace.

Parameters:
namespace - The namespace.
localPart - The localPart name.
Returns:
Element instance.

serialize

public static String serialize(NodeList nodeList)
                        throws DOMException
Serialise the supplied W3C DOM subtree.

The output is unformatted.

Parameters:
nodeList - The DOM subtree as a NodeList.
Returns:
The subtree in serailised form.
Throws:
DOMException - Unable to serialise the DOM.

serialize

public static String serialize(Node node,
                               boolean format)
                        throws DOMException
Serialise the supplied W3C DOM subtree.

Parameters:
node - The DOM node to be serialized.
format - Format the output.
Returns:
The subtree in serailised form.
Throws:
DOMException - Unable to serialise the DOM.

serialize

public static void serialize(Node node,
                             boolean format,
                             Writer writer)
                      throws DOMException
Serialise the supplied W3C DOM subtree.

Parameters:
node - The DOM node to be serialized.
format - Format the output.
writer - The target writer for serialization.
Throws:
DOMException - Unable to serialise the DOM.

serialize

public static String serialize(NodeList nodeList,
                               boolean format)
                        throws DOMException
Serialise the supplied W3C DOM subtree.

Parameters:
nodeList - The DOM subtree as a NodeList.
format - Format the output.
Returns:
The subtree in serailised form.
Throws:
DOMException - Unable to serialise the DOM.

serialize

public static void serialize(NodeList nodeList,
                             boolean format,
                             Writer writer)
                      throws DOMException
Serialise the supplied W3C DOM subtree.

Parameters:
nodeList - The DOM subtree as a NodeList.
format - Format the output.
writer - The target writer for serialization.
Throws:
DOMException - Unable to serialise the DOM.

indent

public static String indent(String xml,
                            int indent)
Indent the supplied XML string by the number of spaces specified in the 'indent' param.

The indents are only inserted after newlines, where the first non-whitespace character is '<'.

Parameters:
xml - The XML to indent.
indent - The number of spaces to insert as the indent.
Returns:
The indented XML string.

isTextNode

public static boolean isTextNode(Node node)
Is the supplied W3C DOM Node a text node.

Parameters:
node - The node to be tested.
Returns:
True if the node is a text node, otherwise false.

encodeTextValue

public static void encodeTextValue(char[] characters,
                                   int offset,
                                   int length,
                                   Writer writer)
                            throws IOException
Throws:
IOException

encodeAttributeValue

public static void encodeAttributeValue(char[] characters,
                                        int offset,
                                        int length,
                                        Writer writer)
                                 throws IOException
Throws:
IOException


Copyright © 2018. All rights reserved.