Package org.smooks.support
Class XmlUtils
- java.lang.Object
-
- org.smooks.support.XmlUtils
-
public final class XmlUtils extends Object
XMl utility methods.- Author:
- Tom Fennelly
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
XmlUtils.VALIDATION_TYPE
Document validation types.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description 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 Document
parseStream(InputStream stream)
Basic DOM namespace aware parse.static Document
parseStream(InputStream stream, XmlUtils.VALIDATION_TYPE validation, boolean expandEntityRefs)
Parse the XML stream and return the associated W3C Document object.static Document
parseStream(InputStream stream, EntityResolver entityResolver, XmlUtils.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, XmlUtils.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 suppliedReader
stream replacing them with their actual character values.static String
removeEntities(String string)
Remove all entities from the suppliedString
stream replacing them with there actual character values.static void
rewriteEntities(Reader reader, Writer writer)
Rewrite all entities from the suppliedReader
stream replacing them with their character reference equivalents.static String
serialize(Node node)
static String
serialize(NodeList nodeList, boolean closeEmptyElements)
Serialise the supplied W3C DOM subtree.static String
serialize(NodeList nodeList, boolean format, boolean closeEmptyElements)
Serialise the supplied W3C DOM subtree.static void
serialize(NodeList nodeList, boolean format, Writer writer, boolean closeEmptyElements)
Serialise the supplied W3C DOM subtree.static String
serialize(Node node, boolean format, boolean closeEmptyElements)
Serialise the supplied W3C DOM subtree.static void
serialize(Node node, boolean format, Writer writer, boolean closeEmptyElements)
Serialise the supplied W3C DOM subtree.static QName
toQName(String namespaceURI, String localName, String qName)
-
-
-
Method Detail
-
removeEntities
public static void removeEntities(Reader reader, Writer writer) throws IOException
Remove all entities from the suppliedReader
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 suppliedString
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 suppliedReader
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, XmlUtils.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 perDocumentBuilderFactory.setExpandEntityReferences(boolean)
.- Returns:
- The W3C Document object associated with the input stream.
- Throws:
SAXException
IOException
-
parseStream
public static Document parseStream(InputStream stream, EntityResolver entityResolver, XmlUtils.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 perDocumentBuilderFactory.setExpandEntityReferences(boolean)
.- Returns:
- The W3C Document object associated with the input stream.
- Throws:
SAXException
IOException
-
parseStream
public static Document parseStream(Reader stream, EntityResolver entityResolver, XmlUtils.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 perDocumentBuilderFactory.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.
-
serialize
public static String serialize(Node node) throws DOMException
- Throws:
DOMException
-
serialize
public static String serialize(NodeList nodeList, boolean closeEmptyElements) 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, boolean closeEmptyElements) 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, boolean closeEmptyElements) 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, boolean closeEmptyElements) 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, boolean closeEmptyElements) 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
-
-