public abstract class BaseXMLBuilder extends Object
Modifier | Constructor and Description |
---|---|
protected |
BaseXMLBuilder(Document xmlDocument)
Construct a new builder object that wraps the given XML document.
|
protected |
BaseXMLBuilder(Node myNode,
Node parentNode)
Construct a new builder object that wraps the given XML document and node.
|
Modifier and Type | Method and Description |
---|---|
abstract BaseXMLBuilder |
a(String name,
String value)
Synonym for
attribute(String, String) . |
protected void |
assertElementContainsNoOrWhitespaceOnlyTextNodes(Node anXmlElement) |
String |
asString()
Serialize the XML document to a string excluding the XML declaration.
|
String |
asString(Properties outputProperties)
Serialize the XML document to a string by delegating to the
toWriter(Writer, Properties) method. |
abstract BaseXMLBuilder |
attr(String name,
String value)
Synonym for
attribute(String, String) . |
abstract BaseXMLBuilder |
attribute(String name,
String value)
Add a named attribute value to the element represented by this builder
node, and return the node representing the element to which the
attribute was added (not the new attribute node).
|
protected void |
attributeImpl(String name,
String value)
Add a named attribute value to the element for this builder node.
|
protected NamespaceContextImpl |
buildDocumentNamespaceContext() |
abstract BaseXMLBuilder |
c(String comment)
Synonym for
comment(String) . |
abstract BaseXMLBuilder |
cdata(byte[] data)
Add a CDATA node with Base64-encoded byte data content to the element represented
by this builder node, and return the node representing the element to which the
data was added (not the new CDATA node).
|
abstract BaseXMLBuilder |
cdata(String data)
Add a CDATA node with String content to the element represented by this
builder node, and return the node representing the element to which the
data was added (not the new CDATA node).
|
protected void |
cdataImpl(byte[] data)
Add a CDATA node with Base64-encoded byte data content to the element
for this builder node.
|
protected void |
cdataImpl(String data)
Add a CDATA node with String content to the element for this builder node.
|
abstract BaseXMLBuilder |
cmnt(String comment)
Synonym for
comment(String) . |
abstract BaseXMLBuilder |
comment(String comment)
Add a comment to the element represented by this builder node, and
return the node representing the element to which the comment
was added (not the new comment node).
|
protected void |
commentImpl(String comment)
Add a comment to the element represented by this builder node.
|
protected static Document |
createDocumentImpl(String name,
String namespaceURI)
Construct an XML Document with a default namespace with the given
root element.
|
abstract BaseXMLBuilder |
d(byte[] data)
Synonym for
cdata(byte[]) . |
abstract BaseXMLBuilder |
d(String data)
Synonym for
cdata(String) . |
abstract BaseXMLBuilder |
data(byte[] data)
Synonym for
cdata(byte[]) . |
abstract BaseXMLBuilder |
data(String data)
Synonym for
cdata(String) . |
abstract BaseXMLBuilder |
document()
BEWARE: The builder returned by this method represents a Document node, not
an Element node as is usually the case, so attempts to use the attribute or
namespace methods on this builder will likely fail.
|
abstract BaseXMLBuilder |
e(String name)
Synonym for
element(String) . |
abstract BaseXMLBuilder |
elem(String name)
Synonym for
element(String) . |
abstract BaseXMLBuilder |
element(String name)
Add a named XML element to the document as a child of this builder node,
and return the builder node representing the new child.
|
abstract BaseXMLBuilder |
element(String name,
String namespaceURI)
Add a named and namespaced XML element to the document as a child of
this builder node, and return the builder node representing the new child.
|
String |
elementAsString()
Serialize the current XML Element and its descendants to a string
excluding the XML declaration.
|
String |
elementAsString(Properties outputProperties)
Serialize the current XML Element and its descendants to a string by
delegating to the
toWriter(Writer, Properties) method. |
abstract BaseXMLBuilder |
elementBefore(String name)
Add a named XML element to the document as a sibling element
that precedes the position of this builder node, and return the builder node
representing the new child.
|
abstract BaseXMLBuilder |
elementBefore(String name,
String namespaceURI)
Add a named and namespaced XML element to the document as a sibling element
that precedes the position of this builder node, and return the builder node
representing the new child.
|
protected Element |
elementBeforeImpl(String name)
Add a named XML element to the document as a sibling element
that precedes the position of this builder node.
|
protected Element |
elementBeforeImpl(String name,
String namespaceURI)
Add a named and namespaced XML element to the document as a sibling element
that precedes the position of this builder node.
|
protected Element |
elementImpl(String name,
String namespaceURI)
Add a named and namespaced XML element to the document as a child of
this builder's node.
|
boolean |
equals(Object obj) |
Document |
getDocument() |
Element |
getElement() |
protected String |
getPrefixFromQualifiedName(String qualifiedName) |
abstract BaseXMLBuilder |
i(String target,
String data)
Synonym for
instruction(String, String) . |
abstract BaseXMLBuilder |
importXMLBuilder(BaseXMLBuilder builder)
Imports another XMLBuilder document into this document at the
current position.
|
protected void |
importXMLBuilderImpl(BaseXMLBuilder builder)
Imports another BaseXMLBuilder document into this document at the
current position.
|
abstract BaseXMLBuilder |
insertInstruction(String target,
String data)
Insert an instruction before the element represented by this builder node,
and return the node representing that same element
(not the new instruction node).
|
protected void |
insertInstructionImpl(String target,
String data)
Insert an instruction before the element represented by this builder node.
|
abstract BaseXMLBuilder |
inst(String target,
String data)
Synonym for
instruction(String, String) . |
abstract BaseXMLBuilder |
instruction(String target,
String data)
Add an instruction to the element represented by this builder node, and
return the node representing the element to which the instruction
was added (not the new instruction node).
|
protected void |
instructionImpl(String target,
String data)
Add an instruction to the element represented by this builder node.
|
protected String |
lookupNamespaceURIImpl(String name)
Look up the namespace matching the current builder node's qualified
name prefix (if any) or the document's default namespace.
|
abstract BaseXMLBuilder |
namespace(String namespaceURI)
Add an XML namespace attribute to this builder's element node
without a prefix.
|
abstract BaseXMLBuilder |
namespace(String prefix,
String namespaceURI)
Add an XML namespace attribute to this builder's element node.
|
protected void |
namespaceImpl(String namespaceURI)
Add an XML namespace attribute to this builder's element node
without a prefix.
|
protected void |
namespaceImpl(String prefix,
String namespaceURI)
Add an XML namespace attribute to this builder's element node.
|
abstract BaseXMLBuilder |
ns(String namespaceURI)
Synonym for
namespace(String) . |
abstract BaseXMLBuilder |
ns(String prefix,
String namespaceURI)
Synonym for
namespace(String, String) . |
protected static Document |
parseDocumentImpl(InputSource inputSource)
Return an XML Document parsed from the given input source.
|
abstract BaseXMLBuilder |
r(String name)
Synonym for
reference(String) . |
abstract BaseXMLBuilder |
ref(String name)
Synonym for
reference(String) . |
abstract BaseXMLBuilder |
reference(String name)
Add a reference to the element represented by this builder node, and
return the node representing the element to which the reference
was added (not the new reference node).
|
protected void |
referenceImpl(String name)
Add a reference to the element represented by this builder node.
|
abstract BaseXMLBuilder |
root() |
abstract BaseXMLBuilder |
stripWhitespaceOnlyTextNodes()
Find and delete from the underlying Document any text nodes that
contain nothing but whitespace, such as newlines and tab or space
characters used to indent or pretty-print an XML document.
|
protected void |
stripWhitespaceOnlyTextNodesImpl()
Find and delete from the underlying Document any text nodes that
contain nothing but whitespace, such as newlines and tab or space
characters used to indent or pretty-print an XML document.
|
abstract BaseXMLBuilder |
t(String value)
Synonym for
text(String) . |
abstract BaseXMLBuilder |
text(String value)
Add a text value to the element represented by this builder node, and
return the node representing the element to which the text
was added (not the new text node).
|
abstract BaseXMLBuilder |
text(String value,
boolean replaceText)
Add or replace the text value of an element represented by this builder
node, and return the node representing the element to which the text
was added (not the new text node).
|
protected void |
textImpl(String value,
boolean replaceText)
Add or replace the text value of an element for this builder node.
|
void |
toWriter(boolean wholeDocument,
Writer writer,
Properties outputProperties)
Serialize either the specific Element wrapped by this BaseXMLBuilder,
or its entire XML document, to the given writer using the default
TransformerFactory and Transformer classes. |
void |
toWriter(Writer writer,
Properties outputProperties)
Serialize the XML document to the given writer using the default
TransformerFactory and Transformer classes. |
abstract BaseXMLBuilder |
up()
Return the builder node representing the parent of the current node.
|
abstract BaseXMLBuilder |
up(int steps)
Return the builder node representing the nth ancestor element
of this node, or the root node if n exceeds the document's depth.
|
protected Node |
upImpl(int steps)
Return the Document node representing the nth ancestor element
of this node, or the root node if n exceeds the document's depth.
|
abstract BaseXMLBuilder |
xpathFind(String xpath)
Find the first element in the builder's DOM matching the given
XPath expression.
|
abstract BaseXMLBuilder |
xpathFind(String xpath,
NamespaceContext nsContext)
Find the first element in the builder's DOM matching the given
XPath expression, where the expression may include namespaces if
a
NamespaceContext is provided. |
protected Node |
xpathFindImpl(String xpath,
NamespaceContext nsContext)
Find the first element in the builder's DOM matching the given
XPath expression, where the expression may include namespaces if
a
NamespaceContext is provided. |
Object |
xpathQuery(String xpath,
QName type)
Return the result of evaluating an XPath query on the builder's DOM.
|
Object |
xpathQuery(String xpath,
QName type,
NamespaceContext nsContext)
Return the result of evaluating an XPath query on the builder's DOM
using the given namespace.
|
protected BaseXMLBuilder(Document xmlDocument)
xmlDocument
- an XML document that the builder will manage and manipulate.protected BaseXMLBuilder(Node myNode, Node parentNode)
myNode
- the XML node that this builder node will wrap. This node may
be part of the XML document, or it may be a new element that is to be
added to the document.parentNode
- If not null, the given myElement will be appended as child node of the
parentNode node.protected static Document createDocumentImpl(String name, String namespaceURI) throws ParserConfigurationException, FactoryConfigurationError
name
- the name of the document's root element.namespaceURI
- default namespace URI for document, ignored if null or empty.FactoryConfigurationError
ParserConfigurationException
protected static Document parseDocumentImpl(InputSource inputSource) throws ParserConfigurationException, SAXException, IOException
inputSource
- an XML document input source that will be parsed into a DOM.ParserConfigurationException
FactoryConfigurationError
ParserConfigurationException
IOException
SAXException
protected void stripWhitespaceOnlyTextNodesImpl() throws XPathExpressionException
XPathExpressionException
public abstract BaseXMLBuilder stripWhitespaceOnlyTextNodes() throws XPathExpressionException
XPathExpressionException
protected void importXMLBuilderImpl(BaseXMLBuilder builder)
builder
- the BaseXMLBuilder document to be imported.public boolean equals(Object obj)
public Element getElement()
public Document getDocument()
public abstract BaseXMLBuilder importXMLBuilder(BaseXMLBuilder builder)
builder
- the XMLBuilder document to be imported.public abstract BaseXMLBuilder root()
public Object xpathQuery(String xpath, QName type, NamespaceContext nsContext) throws XPathExpressionException
xpath
- an XPath expressiontype
- the type the XPath is expected to resolve to, e.g:
XPathConstants.NODE
, XPathConstants.NODESET
,
XPathConstants.STRING
.nsContext
- a mapping of prefixes to namespace URIs that allows the XPath expression
to use namespaces, or null for a non-namespaced document.XPathExpressionException
- If the XPath is invalid, or if does not resolve to at least one
Node.ELEMENT_NODE
.public Object xpathQuery(String xpath, QName type) throws XPathExpressionException
xpath
- an XPath expressiontype
- the type the XPath is expected to resolve to, e.g:
XPathConstants.NODE
, XPathConstants.NODESET
,
XPathConstants.STRING
XPathExpressionException
- If the XPath is invalid, or if does not resolve to at least one
Node.ELEMENT_NODE
.public abstract BaseXMLBuilder xpathFind(String xpath, NamespaceContext nsContext) throws XPathExpressionException
NamespaceContext
is provided.xpath
- An XPath expression that *must* resolve to an existing Element within
the document object model.nsContext
- a mapping of prefixes to namespace URIs that allows the XPath expression
to use namespaces.XPathExpressionException
- If the XPath is invalid, or if does not resolve to at least one
Node.ELEMENT_NODE
.public abstract BaseXMLBuilder xpathFind(String xpath) throws XPathExpressionException
xpath
- An XPath expression that *must* resolve to an existing Element within
the document object model.XPathExpressionException
- If the XPath is invalid, or if does not resolve to at least one
Node.ELEMENT_NODE
.protected Node xpathFindImpl(String xpath, NamespaceContext nsContext) throws XPathExpressionException
NamespaceContext
is provided.xpath
- An XPath expression that *must* resolve to an existing Element within
the document object model.nsContext
- a mapping of prefixes to namespace URIs that allows the XPath expression
to use namespaces.XPathExpressionException
- If the XPath is invalid, or if does not resolve to at least one
Node.ELEMENT_NODE
.protected String lookupNamespaceURIImpl(String name)
name
- the name of the XML element.protected Element elementImpl(String name, String namespaceURI)
name
- the name of the XML element.namespaceURI
- a namespace URIIllegalStateException
- if you attempt to add a child element to an XML node that already
contains a text node value.public abstract BaseXMLBuilder element(String name)
name
- the name of the XML element.IllegalStateException
- if you attempt to add a child element to an XML node that already
contains a text node value.public abstract BaseXMLBuilder elem(String name)
element(String)
.name
- the name of the XML element.IllegalStateException
- if you attempt to add a child element to an XML node that already
contains a text node value.public abstract BaseXMLBuilder e(String name)
element(String)
.name
- the name of the XML element.IllegalStateException
- if you attempt to add a child element to an XML node that already
contains a text node value.public abstract BaseXMLBuilder element(String name, String namespaceURI)
name
- the name of the XML element.namespaceURI
- a namespace URIIllegalStateException
- if you attempt to add a child element to an XML node that already
contains a text node value.public abstract BaseXMLBuilder elementBefore(String name)
name
- the name of the XML element.IllegalStateException
- if you attempt to add a sibling element to a node where there are already
one or more siblings that are text nodes.public abstract BaseXMLBuilder elementBefore(String name, String namespaceURI)
name
- the name of the XML element.namespaceURI
- a namespace URIIllegalStateException
- if you attempt to add a sibling element to a node where there are already
one or more siblings that are text nodes.public abstract BaseXMLBuilder attribute(String name, String value)
name
- the attribute's name.value
- the attribute's value.public abstract BaseXMLBuilder attr(String name, String value)
attribute(String, String)
.name
- the attribute's name.value
- the attribute's value.public abstract BaseXMLBuilder a(String name, String value)
attribute(String, String)
.name
- the attribute's name.value
- the attribute's value.public abstract BaseXMLBuilder text(String value, boolean replaceText)
value
- the text value to set or add to the element.replaceText
- if True any existing text content of the node is replaced with the
given text value, if the given value is appended to any existing text.public abstract BaseXMLBuilder text(String value)
value
- the text value to add to the element.public abstract BaseXMLBuilder t(String value)
text(String)
.value
- the text value to add to the element.protected Element elementBeforeImpl(String name)
name
- the name of the XML element.IllegalStateException
- if you attempt to add a sibling element to a node where there are already
one or more siblings that are text nodes.protected Element elementBeforeImpl(String name, String namespaceURI)
name
- the name of the XML element.namespaceURI
- a namespace URIIllegalStateException
- if you attempt to add a sibling element to a node where there are already
one or more siblings that are text nodes.protected void attributeImpl(String name, String value)
name
- the attribute's name.value
- the attribute's value.protected void textImpl(String value, boolean replaceText)
value
- the text value to set or add to the element.replaceText
- if True any existing text content of the node is replaced with the
given text value, if the given value is appended to any existing text.protected void cdataImpl(String data)
data
- the String value that will be added to a CDATA element.protected void cdataImpl(byte[] data)
data
- the data value that will be Base64-encoded and added to a CDATA element.protected void commentImpl(String comment)
comment
- the comment to add to the element.protected void instructionImpl(String target, String data)
target
- the target value for the instruction.data
- the data value for the instructionprotected void insertInstructionImpl(String target, String data)
target
- the target value for the instruction.data
- the data value for the instructionprotected void referenceImpl(String name)
name
- the name value for the reference.protected void namespaceImpl(String prefix, String namespaceURI)
prefix
- a prefix for the namespace URI within the document, may be null
or empty in which case a default "xmlns" attribute is created.namespaceURI
- a namespace uriprotected void namespaceImpl(String namespaceURI)
namespaceURI
- a namespace uripublic abstract BaseXMLBuilder cdata(String data)
data
- the String value that will be added to a CDATA element.public abstract BaseXMLBuilder data(String data)
cdata(String)
.data
- the String value that will be added to a CDATA element.public abstract BaseXMLBuilder d(String data)
cdata(String)
.data
- the String value that will be added to a CDATA element.public abstract BaseXMLBuilder cdata(byte[] data)
data
- the data value that will be Base64-encoded and added to a CDATA element.public abstract BaseXMLBuilder data(byte[] data)
cdata(byte[])
.data
- the data value that will be Base64-encoded and added to a CDATA element.public abstract BaseXMLBuilder d(byte[] data)
cdata(byte[])
.data
- the data value that will be Base64-encoded and added to a CDATA element.public abstract BaseXMLBuilder comment(String comment)
comment
- the comment to add to the element.public abstract BaseXMLBuilder cmnt(String comment)
comment(String)
.comment
- the comment to add to the element.public abstract BaseXMLBuilder c(String comment)
comment(String)
.comment
- the comment to add to the element.public abstract BaseXMLBuilder instruction(String target, String data)
target
- the target value for the instruction.data
- the data value for the instructionpublic abstract BaseXMLBuilder inst(String target, String data)
instruction(String, String)
.target
- the target value for the instruction.data
- the data value for the instructionpublic abstract BaseXMLBuilder i(String target, String data)
instruction(String, String)
.target
- the target value for the instruction.data
- the data value for the instructionpublic abstract BaseXMLBuilder insertInstruction(String target, String data)
target
- the target value for the instruction.data
- the data value for the instructionpublic abstract BaseXMLBuilder reference(String name)
name
- the name value for the reference.public abstract BaseXMLBuilder ref(String name)
reference(String)
.name
- the name value for the reference.public abstract BaseXMLBuilder r(String name)
reference(String)
.name
- the name value for the reference.public abstract BaseXMLBuilder namespace(String prefix, String namespaceURI)
prefix
- a prefix for the namespace URI within the document, may be null
or empty in which case a default "xmlns" attribute is created.namespaceURI
- a namespace uripublic abstract BaseXMLBuilder ns(String prefix, String namespaceURI)
namespace(String, String)
.prefix
- a prefix for the namespace URI within the document, may be null
or empty in which case a default xmlns attribute is created.namespaceURI
- a namespace uripublic abstract BaseXMLBuilder namespace(String namespaceURI)
namespaceURI
- a namespace uripublic abstract BaseXMLBuilder ns(String namespaceURI)
namespace(String)
.namespaceURI
- a namespace uripublic abstract BaseXMLBuilder up(int steps)
steps
- the number of parent elements to step over while navigating up the chain
of node ancestors. A steps value of 1 will find a node's parent, 2 will
find its grandparent etc.public abstract BaseXMLBuilder up()
public abstract BaseXMLBuilder document()
protected Node upImpl(int steps)
steps
- the number of parent elements to step over while navigating up the chain
of node ancestors. A steps value of 1 will find a node's parent, 2 will
find its grandparent etc.protected void assertElementContainsNoOrWhitespaceOnlyTextNodes(Node anXmlElement)
IllegalStateException
- if the current element contains any child text nodes that aren't pure whitespace.
We allow whitespace so parsed XML documents containing indenting or pretty-printing
can still be amended, per issue #17.public void toWriter(boolean wholeDocument, Writer writer, Properties outputProperties) throws TransformerException
TransformerFactory
and Transformer
classes.
If output options are provided, these options are provided to the
Transformer
serializer.wholeDocument
- if true the whole XML document (i.e. the document root) is serialized,
if false just the current Element and its descendants are serialized.writer
- a writer to which the serialized document is written.outputProperties
- settings for the Transformer
serializer. This parameter may be
null or an empty Properties object, in which case the default output
properties will be applied.TransformerException
public void toWriter(Writer writer, Properties outputProperties) throws TransformerException
TransformerFactory
and Transformer
classes. If output
options are provided, these options are provided to the
Transformer
serializer.writer
- a writer to which the serialized document is written.outputProperties
- settings for the Transformer
serializer. This parameter may be
null or an empty Properties object, in which case the default output
properties will be applied.TransformerException
public String asString(Properties outputProperties) throws TransformerException
toWriter(Writer, Properties)
method. If output options are
provided, these options are provided to the Transformer
serializer.outputProperties
- settings for the Transformer
serializer. This parameter may be
null or an empty Properties object, in which case the default output
properties will be applied.TransformerException
public String elementAsString(Properties outputProperties) throws TransformerException
toWriter(Writer, Properties)
method.
If output options are provided, these options are provided to the
Transformer
serializer.outputProperties
- settings for the Transformer
serializer. This parameter may be
null or an empty Properties object, in which case the default output
properties will be applied.TransformerException
public String asString() throws TransformerException
TransformerException
public String elementAsString() throws TransformerException
TransformerException
protected NamespaceContextImpl buildDocumentNamespaceContext()
Copyright © 2024. All rights reserved.