|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnet.sf.mmm.util.component.base.AbstractComponent
net.sf.mmm.util.component.base.AbstractLoggableComponent
net.sf.mmm.util.xml.base.DomUtilImpl
@Singleton @Named public final class DomUtilImpl
This utility class contains methods that help to deal with the
DOM
API.
Field Summary | |
---|---|
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 Summary | |
---|---|
DomUtilImpl()
The constructor. |
Method Summary | |
---|---|
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)
This method determines if the given nodes have the same name and namespace . |
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. |
Methods inherited from class net.sf.mmm.util.component.base.AbstractLoggableComponent |
---|
getLogger, setLogger |
Methods inherited from class net.sf.mmm.util.component.base.AbstractComponent |
---|
doInitialized, getInitializationState, initialize |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static DomUtil instance
getInstance()
private DocumentBuilderFactory documentBuilderFactory
private TransformerFactory transformerFactory
private BasicUtil basicUtil
getBasicUtil()
Constructor Detail |
---|
public DomUtilImpl()
Method Detail |
---|
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
.getInstance()
methods and
construct new instances via the container-framework of your choice (like
plexus, pico, springframework, etc.). To wire up the dependent components
everything is properly annotated using common-annotations (JSR-250). If
your container does NOT support this, you should consider using a better
one.
protected void doInitialize()
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)
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)
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)
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
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
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)
getNodeText
in interface DomUtil
node
- is the xml node containing the text.
DomUtil.getNodeText(Node, Appendable, int)
public String getNodeText(Node node, int depth)
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)
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()
createDocument
in interface DomUtil
public Document parseDocument(InputStream inputStream) throws XmlException, IOException
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
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
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
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
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
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)
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.
private boolean isEqualName(Node node1, Node node2)
name
and namespace
.
node1
- is the first node.node2
- is the second node.
true
if both nodes have equal
name
and namespace
.public boolean isEqual(Node node1, Node node2, XmlCompareMode mode)
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
.
isEqual
in interface DomUtil
node1
- is the first Node
to compare.node2
- is the second Node
to compare.mode
- is the mode
that determines how to do
the comparison.
true
if the XML fragment represented by the given
nodes equals, false
otherwise.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.
nodeList1
- is the first NodeList
.nodeList2
- is the second NodeList
.mode
- is the mode of comparison.
true
if equal, false
otherwise.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |