public final class DomUtilImpl extends AbstractLoggableComponent implements DomUtil
DOM
API.Modifier and Type | Field and Description |
---|---|
private BasicUtil |
basicUtil |
private DocumentBuilderFactory |
documentBuilderFactory
the document builder factory used to read and parse XML
|
private static DomUtil |
instance |
private TransformerFactory |
transformerFactory
the transformer factory used to transform or write XML
|
Constructor and Description |
---|
DomUtilImpl()
The constructor.
|
Modifier and Type | Method and Description |
---|---|
Document |
createDocument()
This method creates a new empty XML DOM document.
|
private DocumentBuilder |
createDocumentBuilder()
This method creates a new document builder.
|
private Transformer |
createTransformer(boolean indent)
This method creates a new transformer.
|
protected void |
doInitialize()
This method performs the actual
initialization . |
boolean |
getAttributeAsBoolean(Element element,
String attribute,
boolean defaultValue)
This method gets the value of the
attribute from the given element as a boolean value. |
protected BasicUtil |
getBasicUtil()
This method gets the
BasicUtil . |
protected DocumentBuilderFactory |
getDocumentBuilderFactory()
This method gets the
DocumentBuilderFactory . |
Element |
getFirstChildElement(Element element,
String tagName)
This method gets the first child-element of the given
element with the specified tagName . |
Element |
getFirstElement(NodeList nodeList)
This method gets the first element of the given node-list.
|
Element |
getFirstElement(NodeList nodeList,
String tagName)
This method gets the first element of the given node-list with the specified tag-name.
|
static DomUtil |
getInstance()
This method gets the singleton instance of this
DomUtilImpl . |
String |
getLocalName(Node node)
This method gets the local name of a given
element . |
String |
getNodeText(Node node)
This method gets the text of the given node excluding the text of child elements (depth=0).
|
void |
getNodeText(Node node,
Appendable buffer,
int depth)
This method appends the text of the given node to the string buffer.
|
String |
getNodeText(Node node,
int depth)
This method gets the text of the given node as string.
|
protected TransformerFactory |
getTransformerFactory()
This method gets the
TransformerFactory . |
protected boolean |
isEqual(CharIterator charIterator1,
CharIterator charIterator2,
XmlCompareMode mode)
This method determines if the given
CharSequence s are equal. |
protected boolean |
isEqual(Element element1,
Element element2,
XmlCompareMode mode)
This method determines if the given
elements are equal. |
protected boolean |
isEqual(NodeList nodeList1,
NodeList nodeList2,
XmlCompareMode mode)
This method determines if the given
NodeList s are equal. |
boolean |
isEqual(Node node1,
Node node2,
XmlCompareMode mode)
This method determines if two
XML-nodes represent the same XML. |
private boolean |
isEqualName(Node node1,
Node node2)
|
Document |
parseDocument(InputSource inputSource)
This method parses an XML document into an DOM document.
|
Document |
parseDocument(InputStream inputStream)
This method parses an XML document from a given input stream.
|
Document |
parseDocument(Reader reader)
This method parses an XML document from a given reader.
|
void |
removeChildren(Element node)
This method removes all
child nodes of the given node. |
Element |
requireFirstChildElement(Element element,
String tagName)
This method requires the first child-element of the given
element with the specified
tagName . |
void |
setBasicUtil(BasicUtil basicUtil)
This method sets the
BasicUtil . |
void |
setDocumentBuilderFactory(DocumentBuilderFactory documentBuilderFactory)
This method sets the
documentBuilderFactory . |
void |
setTransformerFactory(TransformerFactory transformerFactory)
This method sets the
transformerFactory . |
void |
transformXml(Source source,
Result result,
boolean indent)
This method transforms the given XML source to the given result without structural modifications.
|
void |
writeXml(Node xmlNode,
OutputStream outputStream,
boolean indent)
This method writes the XML DOM tree given as node to the given output stream.
|
void |
writeXml(Node xmlNode,
Writer writer,
boolean indent)
This method writes the XML DOM tree given as node to the given writer.
|
createLogger, getLogger
doInitialized, getInitializationState, initialize
private static DomUtil instance
private DocumentBuilderFactory documentBuilderFactory
private TransformerFactory transformerFactory
private BasicUtil basicUtil
protected DocumentBuilderFactory getDocumentBuilderFactory()
DocumentBuilderFactory
.DocumentBuilderFactory
to use.public void setDocumentBuilderFactory(DocumentBuilderFactory documentBuilderFactory)
documentBuilderFactory
.documentBuilderFactory
- is the documentBuilderFactory to set.protected TransformerFactory getTransformerFactory()
TransformerFactory
.TransformerFactory
to use.public void setTransformerFactory(TransformerFactory transformerFactory)
transformerFactory
.transformerFactory
- is the transformerFactory to setprotected BasicUtil getBasicUtil()
BasicUtil
.BasicUtil
to use.@Inject public void setBasicUtil(BasicUtil basicUtil)
BasicUtil
.basicUtil
- is the BasicUtil
to setpublic static DomUtil getInstance()
DomUtilImpl
. Cdi.GET_INSTANCE
before using.protected void doInitialize()
AbstractComponent
initialization
. It is called when AbstractComponent.initialize()
is
invoked for the first time. super.
AbstractComponent.doInitialize()
.doInitialize
in class AbstractLoggableComponent
private DocumentBuilder createDocumentBuilder()
private Transformer createTransformer(boolean indent)
indent
- - true
if the XML should be indented (automatically add linebreaks before opening
tags), false
otherwise.public Element getFirstElement(NodeList nodeList)
DomUtil
getFirstElement
in interface DomUtil
nodeList
- is the node-list (potentially) containing the requested element.null
, if the node-list contains NO element.DomUtil.getFirstElement(NodeList, String)
public Element getFirstElement(NodeList nodeList, String tagName)
DomUtil
getFirstElement
in interface DomUtil
nodeList
- is the node-list (potentially) containing the requested element.tagName
- is the tag-name
of the requested element or null
if
any element is acceptable.null
). If no such element exists in the node-list, null
is returned.public Element getFirstChildElement(Element element, String tagName)
DomUtil
element
with the specified tagName
.getFirstChildElement
in interface DomUtil
element
- is the element (potentially) containing the requested child-element.tagName
- is the tag-name
of the requested element or null
if
any element is acceptable.null
). If no such element exists in the node-list, null
is returned.public Element requireFirstChildElement(Element element, String tagName) throws IllegalArgumentException
DomUtil
element
with the specified
tagName
.requireFirstChildElement
in interface DomUtil
element
- is the element (potentially) containing the requested child-element.tagName
- is the tag-name
of the requested element or null
if
any element is acceptable.null
).IllegalArgumentException
- if the requested child element does NOT exist.public boolean getAttributeAsBoolean(Element element, String attribute, boolean defaultValue) throws IllegalArgumentException
DomUtil
attribute
from the given element
as a boolean value.getAttributeAsBoolean
in interface DomUtil
element
- is the element potentially containing the requested boolean attribute.attribute
- is the name of the requested attribute.defaultValue
- is the default returned if the attribute is NOT present.attribute
or the defaultValue
if the attribute is NOT
present.IllegalArgumentException
- if the value of the specified attribute does NOT represent a boolean
value.public String getNodeText(Node node)
DomUtil
getNodeText
in interface DomUtil
node
- is the xml node containing the text.DomUtil.getNodeText(Node, Appendable, int)
public String getNodeText(Node node, int depth)
DomUtil
getNodeText
in interface DomUtil
node
- is the xml node containing the text.depth
- specifies how deep to step into child elements. If less than 1, the text of child elements
is ignored.DomUtil.getNodeText(Node, Appendable, int)
public void getNodeText(Node node, Appendable buffer, int depth)
DomUtil
Integer.MAX_VALUE
. E.g. for the a element a
in
<a>123<b/>4<c>5<d>6</d></c><![CDATA[7]]>8</a>
getNodeText(a, buffer, depth)
will append the following text to the buffer according
to the given depth.
getNodeText
in interface DomUtil
node
- is the xml node containing the text.buffer
- is the where to append the text to.depth
- specifies how deep to step into child elements. If less than 1, the text of child elements
is ignored.public void removeChildren(Element node)
child nodes
of the given node.removeChildren
in interface DomUtil
node
- is the node to clean of children.public Document createDocument()
DomUtil
createDocument
in interface DomUtil
public Document parseDocument(InputStream inputStream) throws XmlException, IOException
DomUtil
parseDocument
in interface DomUtil
inputStream
- is the input stream to the XML data. If will be closed at the end of this method (on
success as well as in an exceptional state).XmlException
- if the XML is invalid and could NOT be parsed.IOException
- if the input stream produced an error while reading.public Document parseDocument(Reader reader) throws XmlException
DomUtil
parseDocument
in interface DomUtil
reader
- is the reader to the XML data.XmlException
- if the input stream produced an IOException or the XML is invalid.public Document parseDocument(InputSource inputSource) throws XmlException
DomUtil
parseDocument
in interface DomUtil
inputSource
- is the input source to the XML data.XmlException
- if the XML is invalid.public void writeXml(Node xmlNode, OutputStream outputStream, boolean indent) throws XmlException
DomUtil
writeXml
in interface DomUtil
xmlNode
- is the "root" node of the XML tree to write (e.g. the Document or just a single Element).outputStream
- is where the serialized XML is written to.indent
- - true
if the XML should be indented (automatically add line-breaks before opening
tags), false
otherwise.XmlException
- if XML is inconsistent.public void writeXml(Node xmlNode, Writer writer, boolean indent) throws XmlException
DomUtil
writeXml
in interface DomUtil
xmlNode
- is the "root" node of the XML tree to write (e.g. the Document or just a single Element).writer
- is where the serialized XML is written to.indent
- - true
if the XML should be indented (automatically add line-breaks before opening
tags), false
otherwise.XmlException
- if XML is inconsistent.public void transformXml(Source source, Result result, boolean indent) throws XmlException
DomUtil
transformXml
in interface DomUtil
source
- is a source (e.g. DomSource, etc.)result
- is a result (e.g. DomResult, StreamResult, etc.)indent
- - true
if the XML should be indented (automatically add line-breaks before opening
tags), false
otherwise.XmlException
- if the transformation failed (e.g. invalid XML).public String getLocalName(Node node)
DomUtil
element
. This is the name of the element without any
namespace prefix. Node.getLocalName()
will return null
if the Node
has no
namespace
. Therefore this method makes your life a little easier.getLocalName
in interface DomUtil
node
- the Node
to get the local name from.public boolean isEqual(Node node1, Node node2, XmlCompareMode mode)
DomUtil
XML-nodes
represent the same XML. This is quite a complex
operation since the two given nodes may have different segmentations of the text they contain and one
might use Text
while the other may use a CDATASection
.protected boolean isEqual(CharIterator charIterator1, CharIterator charIterator2, XmlCompareMode mode)
CharSequence
s are equal.charIterator1
- is the first CharIterator
.charIterator2
- is the second CharIterator
.mode
- is the mode of comparison.true
if equal, false
otherwise.isEqual(Node, Node, XmlCompareMode)
protected boolean isEqual(Element element1, Element element2, XmlCompareMode mode)
elements
are equal.element1
- is the first Element
.element2
- is the second Element
.mode
- is the mode of comparison.true
if equal, false
otherwise.isEqual(Node, Node, XmlCompareMode)
protected boolean isEqual(NodeList nodeList1, NodeList nodeList2, XmlCompareMode mode)
NodeList
s are equal.Copyright © 2001–2016 mmm-Team. All rights reserved.