public final class XmlToolkit
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
private static java.util.regex.Pattern |
AMP |
private static java.util.regex.Pattern |
APOS |
private static java.util.regex.Pattern |
CR |
private static java.util.regex.Pattern |
GT |
private static java.util.logging.Logger |
LOGGER |
private static java.util.regex.Pattern |
LT |
private static java.util.regex.Pattern |
NL |
private static java.util.regex.Pattern |
QUOT |
private static java.util.regex.Pattern |
TAB |
private static java.lang.String |
XML_PARSER_DISALLOW_DOCTYPE_ATTRIBUTE |
Modifier | Constructor and Description |
---|---|
private |
XmlToolkit()
The constructor is private since no instances should ever be created.
|
Modifier and Type | Method and Description |
---|---|
static javax.xml.parsers.DocumentBuilderFactory |
createDocumentBuildFactoryInstance()
Create a document builder factory with safe settings.
|
static org.w3c.dom.Element |
createElement(org.w3c.dom.Element parentNode,
java.lang.String tagName)
Adds a child element with the name tagName to the parent and returns the new child element.
|
static org.w3c.dom.Document |
createNewDocument(java.lang.String rootElementName)
Creates a new empty XML document.
|
static javax.xml.parsers.SAXParserFactory |
createSAXParserFactory()
Create a SAX parser factory with safe settings.
|
static javax.xml.transform.TransformerFactory |
createTransformerFactory()
Create a transformer factory with safe settings.
|
static java.lang.String |
escapeAll(java.lang.String s) |
static java.lang.String |
escapeTagContent(java.lang.String s) |
static org.w3c.dom.Element |
getChildElementOrNull(org.w3c.dom.Element parent,
java.lang.String name)
Returns the child element with the specified tag name of the specified parent node.
|
static java.util.List<org.w3c.dom.Element> |
getChildElementsByTag(org.w3c.dom.Node contextNode,
java.lang.String tag)
Returns all the children from a node with a tag matching the tag argument.
|
private static javax.xml.parsers.DocumentBuilder |
getDocumentBuilder()
Returns a document builder for XML documents.
|
static org.w3c.dom.Element |
getOrCreateElement(org.w3c.dom.Element parentNode,
java.lang.String tagName)
Returns the child element with the specified tag name of the specified parent node.
|
static java.lang.String |
getSetting(org.w3c.dom.Element parent,
java.lang.String settingName,
java.lang.String defaultValue)
Returns the value of the setting with the specified name or a default value if the setting
had no value.
|
static java.lang.String |
getStringValue(org.w3c.dom.Element element)
Returns the content between the tags of the element, for example <tag>hello
</tag> where the value is "hello".
|
static org.w3c.dom.Document |
loadDocumentFromFile(java.io.File file)
Loads an XML document from the specified file.
|
static org.w3c.dom.Document |
loadDocumentFromStream(java.io.InputStream stream)
Loads an XML document from the specified stream.
|
static org.w3c.dom.Document |
loadDocumentFromString(java.lang.String doc)
Loads an XML document from its string representation.
|
static java.lang.String |
prettyPrint(org.w3c.dom.Element node)
Pretty prints an XML document to a string, starting from the specified element.
|
static void |
prettyPrint(org.w3c.dom.Element node,
java.io.Writer wrt)
Pretty prints an XML document to a writer, starting from the specified element.
|
static void |
setSetting(org.w3c.dom.Element parentNode,
java.lang.String settingName,
java.lang.String settingValue)
Sets the value of a a "setting" element.
|
static void |
setStringValue(org.w3c.dom.Element element,
java.lang.String value)
Sets the text value as a text node child of a an element.
|
static void |
storeDocumentToFile(org.w3c.dom.Document doc,
java.io.File file)
Stores an XML document in a file.
|
static java.lang.String |
storeDocumentToString(org.w3c.dom.Document doc)
Stores an XML document as a string.
|
private static final java.lang.String XML_PARSER_DISALLOW_DOCTYPE_ATTRIBUTE
private static final java.util.logging.Logger LOGGER
private static final java.util.regex.Pattern AMP
private static final java.util.regex.Pattern LT
private static final java.util.regex.Pattern GT
private static final java.util.regex.Pattern QUOT
private static final java.util.regex.Pattern APOS
private static final java.util.regex.Pattern CR
private static final java.util.regex.Pattern NL
private static final java.util.regex.Pattern TAB
private XmlToolkit()
public static org.w3c.dom.Element getOrCreateElement(org.w3c.dom.Element parentNode, java.lang.String tagName)
parentNode
- parent node for the wanted elementtagName
- name of the wanted elementgetChildElementOrNull(Element, String)
public static org.w3c.dom.Element createElement(org.w3c.dom.Element parentNode, java.lang.String tagName)
parentNode
- parent node to add the new element totagName
- the name of the new child elementpublic static void setSetting(org.w3c.dom.Element parentNode, java.lang.String settingName, java.lang.String settingValue)
parentNode
- parent node of the setting elementsettingName
- tag name of the setting elementsettingValue
- the value to setpublic static org.w3c.dom.Document createNewDocument(java.lang.String rootElementName) throws java.io.IOException
rootElementName
- the name of the root elementjava.io.IOException
- if there is a problem creating the XML documentpublic static javax.xml.parsers.SAXParserFactory createSAXParserFactory() throws org.xml.sax.SAXNotRecognizedException, org.xml.sax.SAXNotSupportedException, javax.xml.parsers.ParserConfigurationException
org.xml.sax.SAXNotRecognizedException
org.xml.sax.SAXNotSupportedException
javax.xml.parsers.ParserConfigurationException
public static javax.xml.parsers.DocumentBuilderFactory createDocumentBuildFactoryInstance() throws javax.xml.parsers.ParserConfigurationException
javax.xml.parsers.ParserConfigurationException
public static javax.xml.transform.TransformerFactory createTransformerFactory() throws javax.xml.transform.TransformerFactoryConfigurationError, javax.xml.transform.TransformerConfigurationException
javax.xml.transform.TransformerFactoryConfigurationError
javax.xml.transform.TransformerConfigurationException
public static void setStringValue(org.w3c.dom.Element element, java.lang.String value)
element
- the element to set the text value forvalue
- the new value to setpublic static java.lang.String getSetting(org.w3c.dom.Element parent, java.lang.String settingName, java.lang.String defaultValue)
Since everything should have a default value, no other version of get setting exists. This method implicitly builds the setting node with a default value if none is found.
parent
- the parent element whose children to search for the settings node.settingName
- name of the settingdefaultValue
- default value to return if setting is emptypublic static java.lang.String getStringValue(org.w3c.dom.Element element)
element
- the element from which to extract the text node.public static java.lang.String prettyPrint(org.w3c.dom.Element node)
node
- node from which to start pretty printingpublic static void prettyPrint(org.w3c.dom.Element node, java.io.Writer wrt)
node
- node from which to start pretty printingwrt
- writer to write the document topublic static java.util.List<org.w3c.dom.Element> getChildElementsByTag(org.w3c.dom.Node contextNode, java.lang.String tag)
contextNode
- node whose children to searchtag
- the tag to search forprivate static javax.xml.parsers.DocumentBuilder getDocumentBuilder()
public static org.w3c.dom.Document loadDocumentFromFile(java.io.File file) throws org.xml.sax.SAXException, java.io.IOException
file
- the file from which to read the documentorg.xml.sax.SAXException
- if the document could not be parsedjava.io.IOException
- if the stream could not be readpublic static org.w3c.dom.Document loadDocumentFromStream(java.io.InputStream stream) throws org.xml.sax.SAXException, java.io.IOException
stream
- the input stream from which to read the documentorg.xml.sax.SAXException
- if the document could not be parsedjava.io.IOException
- if the stream could not be readpublic static org.w3c.dom.Document loadDocumentFromString(java.lang.String doc) throws org.xml.sax.SAXException
doc
- the string to read fromorg.xml.sax.SAXException
- if the document could not be parsedjava.lang.NullPointerException
- if the input string is nullpublic static void storeDocumentToFile(org.w3c.dom.Document doc, java.io.File file) throws java.io.IOException
doc
- the XML document to storefile
- the file to store it injava.io.IOException
- if the file could not writtenpublic static java.lang.String storeDocumentToString(org.w3c.dom.Document doc)
doc
- the XML document to storepublic static org.w3c.dom.Element getChildElementOrNull(org.w3c.dom.Element parent, java.lang.String name)
null
is returned.parent
- parent node for the wanted elementname
- name of the wanted elementnull
if no such element existsgetOrCreateElement(Element, String)
public static java.lang.String escapeAll(java.lang.String s)
public static java.lang.String escapeTagContent(java.lang.String s)