Top Description Inners Fields Constructors Methods
org.apache.tools.ant.util

public Class DOMElementWriter

extends Object
Class Inheritance
Imports
java.io.IOException, .OutputStream, .OutputStreamWriter, .StringWriter, .Writer, java.nio.charset.StandardCharsets, java.util.ArrayList, .HashMap, .List, .Map, org.w3c.dom.Attr, .Element, .NamedNodeMap, .Node, .NodeList, .Text

Writes a DOM tree to a given Writer.

Warning

this utility currently does not declare XML Namespaces.

Utility class used by XmlLogger and org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter XMLJUnitResultFormatter}.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
DOMElementWriter.XmlNamespacePolicy

Whether namespaces should be ignored for elements and attributes.

Field Summary

Modifier and TypeField and Description
private static final int
protected String[]
knownEntities

Don't try to be too smart but at least recognize the predefined entities.

private DOMElementWriter.XmlNamespacePolicy
namespacePolicy

XML Namespaces are ignored by default.

private int
nextPrefix

Number of generated prefix to use next.

private static final String
NS

prefix for generated prefixes

private Map<String, String>
nsPrefixMap

Map (URI to prefix) of known namespaces.

private Map<Element, List<String>>
nsURIByElement

Map (Element to URI) of namespaces defined on a given element.

private static final String[]
private boolean
xmlDeclaration

xml declaration is on by default

Constructor Summary

AccessConstructor and Description
public
DOMElementWriter()

Create an element writer.

public
DOMElementWriter(boolean
flag to indicate whether the ?xml? declaration should be included.
xmlDeclaration
)

Create an element writer XML namespaces will be ignored.

public
DOMElementWriter(boolean
flag to indicate whether the ?xml? declaration should be included.
xmlDeclaration
,
DOMElementWriter.XmlNamespacePolicy
the policy to use.
namespacePolicy
)

Create an element writer XML namespaces will be ignored.

Method Summary

Modifier and TypeMethod and Description
private void
public void
closeElement(Element
the Root DOM element of the tree
element
,
Writer
where to send the output
out
,
int
number of
indent
,
String
string that should be used to indent the corresponding tag.
indentWith
,
boolean
if true indent.
hasChildren
)

Writes a DOM tree to a stream.

public String

Returns:

the encoded string.
encode
(String
the string to encode.
value
)

Escape <, >, &, ', " as their entities and drop characters that are illegal in XML documents.

private String
encode(final String value, final boolean encodeWhitespace)

public String

Returns:

the encoded string.
encodeAttributeValue
(String
the string to encode.
value
)

Escape <, >, &, ', " as their entities, \n, \r and \t as numeric entities and drop characters that are illegal in XML documents.

public String

Returns:

the encoded value.
encodedata
(final String
the value to be encoded.
value
)

Drop characters that are illegal in XML documents.

public void
encodedata(final Writer
where to write the encoded data to.
out
,
final String
the value to be encoded.
value
)

Drop characters that are illegal in XML documents and write the rest to the given writer.

private static String
public boolean

Returns:

true if the character is allowed.
isLegalCharacter
(final char
the character to test.
c
)

Is the given character allowed inside an XML document?

public static boolean

Returns:

true if the character is allowed.
isLegalXmlCharacter
(final char
the character to test.
c
)

Is the given character allowed inside an XML document?

public boolean

Returns:

true if it is an entity.
isReference
(String
the value to be checked.
ent
)

Is the given argument a character or entity reference?

public void
openElement(Element
the DOM element to write
element
,
Writer
where to send the output
out
,
int
number of
indent
,
String
string that should be used to indent the corresponding tag.
indentWith
)

Writes the opening tag - including all attributes - corresponding to a DOM element.

public void
openElement(Element
the DOM element to write
element
,
Writer
where to send the output
out
,
int
number of
indent
,
String
string that should be used to indent the corresponding tag.
indentWith
,
boolean
whether this element has children.
hasChildren
)

Writes the opening tag - including all attributes - corresponding to a DOM element.

private void
public void
write(Element
the root element of the DOM tree.
root
,
OutputStream
the outputstream to write to.
out
)

Writes a DOM tree to a stream in UTF8 encoding.

public void
write(Element
the Root DOM element of the tree
element
,
Writer
where to send the output
out
,
int
number of
indent
,
String
string that should be used to indent the corresponding tag.
indentWith
)

Writes a DOM tree to a stream.

public void
writeXMLDeclaration(Writer
the writer to write to.
wri
)

Writes the XML declaration if xmlDeclaration is true.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

HEXback to summary
private static final int HEX
knownEntitiesback to summary
protected String[] knownEntities

Don't try to be too smart but at least recognize the predefined entities.

namespacePolicyback to summary
private DOMElementWriter.XmlNamespacePolicy namespacePolicy

XML Namespaces are ignored by default.

nextPrefixback to summary
private int nextPrefix

Number of generated prefix to use next.

NSback to summary
private static final String NS

prefix for generated prefixes

nsPrefixMapback to summary
private Map<String, String> nsPrefixMap

Map (URI to prefix) of known namespaces.

nsURIByElementback to summary
private Map<Element, List<String>> nsURIByElement

Map (Element to URI) of namespaces defined on a given element.

WS_ENTITIESback to summary
private static final String[] WS_ENTITIES
xmlDeclarationback to summary
private boolean xmlDeclaration

xml declaration is on by default

Constructor Detail

DOMElementWriterback to summary
public DOMElementWriter()

Create an element writer. The ?xml? declaration will be included, namespaces ignored.

DOMElementWriterback to summary
public DOMElementWriter(boolean xmlDeclaration)

Create an element writer XML namespaces will be ignored.

Parameters
xmlDeclaration:boolean

flag to indicate whether the ?xml? declaration should be included.

Since
Ant1.7
DOMElementWriterback to summary
public DOMElementWriter(boolean xmlDeclaration, DOMElementWriter.XmlNamespacePolicy namespacePolicy)

Create an element writer XML namespaces will be ignored.

Parameters
xmlDeclaration:boolean

flag to indicate whether the ?xml? declaration should be included.

namespacePolicy:DOMElementWriter.XmlNamespacePolicy

the policy to use.

Since
Ant1.7

Method Detail

addNSDefinitionback to summary
private void addNSDefinition(Element element, String uri)
closeElementback to summary
public void closeElement(Element element, Writer out, int indent, String indentWith, boolean hasChildren) throws IOException

Writes a DOM tree to a stream.

Parameters
element:Element

the Root DOM element of the tree

out:Writer

where to send the output

indent:int

number of

indentWith:String

string that should be used to indent the corresponding tag.

hasChildren:boolean

if true indent.

Exceptions
IOException:
if an error happens while writing to the stream.
encodeback to summary
public String encode(String value)

Escape <, >, &, ', " as their entities and drop characters that are illegal in XML documents.

Parameters
value:String

the string to encode.

Returns:String

the encoded string.

encodeback to summary
private String encode(final String value, final boolean encodeWhitespace)
encodeAttributeValueback to summary
public String encodeAttributeValue(String value)

Escape <, >, &, ', " as their entities, \n, \r and \t as numeric entities and drop characters that are illegal in XML documents.

Parameters
value:String

the string to encode.

Returns:String

the encoded string.

encodedataback to summary
public String encodedata(final String value)

Drop characters that are illegal in XML documents.

Also ensure that we are not including an ]]> marker by replacing that sequence with &#x5d;&#x5d;&gt;.

See XML 1.0 2.2 https://www.w3.org/TR/1998/REC-xml-19980210#charsets and 2.7 https://www.w3.org/TR/1998/REC-xml-19980210#sec-cdata-sect.

Parameters
value:String

the value to be encoded.

Returns:String

the encoded value.

encodedataback to summary
public void encodedata(final Writer out, final String value) throws IOException

Drop characters that are illegal in XML documents and write the rest to the given writer.

Also ensure that we are not including an ]]> marker by replacing that sequence with &#x5d;&#x5d;&gt;.

See XML 1.0 2.2 https://www.w3.org/TR/1998/REC-xml-19980210#charsets and 2.7 https://www.w3.org/TR/1998/REC-xml-19980210#sec-cdata-sect.

Parameters
out:Writer

where to write the encoded data to.

value:String

the value to be encoded.

Exceptions
IOException:
if data cannot be written
getNamespaceURIback to summary
private static String getNamespaceURI(Node n)
isLegalCharacterback to summary
public boolean isLegalCharacter(final char c)

Is the given character allowed inside an XML document?

See XML 1.0 2.2 https://www.w3.org/TR/1998/REC-xml-19980210#charsets.

Parameters
c:char

the character to test.

Returns:boolean

true if the character is allowed.

Since
1.10, Ant 1.5
isLegalXmlCharacterback to summary
public static boolean isLegalXmlCharacter(final char c)

Is the given character allowed inside an XML document?

See XML 1.0 2.2 https://www.w3.org/TR/1998/REC-xml-19980210#charsets.

Parameters
c:char

the character to test.

Returns:boolean

true if the character is allowed.

Since
1.10.10
isReferenceback to summary
public boolean isReference(String ent)

Is the given argument a character or entity reference?

Parameters
ent:String

the value to be checked.

Returns:boolean

true if it is an entity.

openElementback to summary
public void openElement(Element element, Writer out, int indent, String indentWith) throws IOException

Writes the opening tag - including all attributes - corresponding to a DOM element.

Parameters
element:Element

the DOM element to write

out:Writer

where to send the output

indent:int

number of

indentWith:String

string that should be used to indent the corresponding tag.

Exceptions
IOException:
if an error happens while writing to the stream.
openElementback to summary
public void openElement(Element element, Writer out, int indent, String indentWith, boolean hasChildren) throws IOException

Writes the opening tag - including all attributes - corresponding to a DOM element.

Parameters
element:Element

the DOM element to write

out:Writer

where to send the output

indent:int

number of

indentWith:String

string that should be used to indent the corresponding tag.

hasChildren:boolean

whether this element has children.

Exceptions
IOException:
if an error happens while writing to the stream.
Since
Ant 1.7
removeNSDefinitionsback to summary
private void removeNSDefinitions(Element element)
writeback to summary
public void write(Element root, OutputStream out) throws IOException

Writes a DOM tree to a stream in UTF8 encoding. Note that it prepends the <?xml version='1.0' encoding='UTF-8'?> if the xmlDeclaration field is true. The indent number is set to 0 and a 2-space indent.

Parameters
root:Element

the root element of the DOM tree.

out:OutputStream

the outputstream to write to.

Exceptions
IOException:
if an error happens while writing to the stream.
writeback to summary
public void write(Element element, Writer out, int indent, String indentWith) throws IOException

Writes a DOM tree to a stream.

Parameters
element:Element

the Root DOM element of the tree

out:Writer

where to send the output

indent:int

number of

indentWith:String

string that should be used to indent the corresponding tag.

Exceptions
IOException:
if an error happens while writing to the stream.
writeXMLDeclarationback to summary
public void writeXMLDeclaration(Writer wri) throws IOException

Writes the XML declaration if xmlDeclaration is true.

Parameters
wri:Writer

the writer to write to.

Exceptions
IOException:
if there is an error.
Since
Ant 1.7.0
org.apache.tools.ant.util back to summary

public Class DOMElementWriter.XmlNamespacePolicy

extends Object
Class Inheritance
  • java.lang.Object
  • org.apache.tools.ant.util.DOMElementWriter.XmlNamespacePolicy

Whether namespaces should be ignored for elements and attributes.
Since
Ant 1.7

Field Summary

Modifier and TypeField and Description
public static final DOMElementWriter.XmlNamespacePolicy
IGNORE

Ignores namespaces for elements and attributes, the default.

public static final DOMElementWriter.XmlNamespacePolicy
ONLY_QUALIFY_ELEMENTS

Ignores namespaces for attributes.

public static final DOMElementWriter.XmlNamespacePolicy
QUALIFY_ALL

Qualifies namespaces for elements and attributes.

private boolean
private boolean

Constructor Summary

AccessConstructor and Description
public
XmlNamespacePolicy(boolean
whether to qualify elements
qualifyElements
,
boolean
whether to qualify elements
qualifyAttributes
)

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

IGNOREback to summary
public static final DOMElementWriter.XmlNamespacePolicy IGNORE

Ignores namespaces for elements and attributes, the default.

ONLY_QUALIFY_ELEMENTSback to summary
public static final DOMElementWriter.XmlNamespacePolicy ONLY_QUALIFY_ELEMENTS

Ignores namespaces for attributes.

QUALIFY_ALLback to summary
public static final DOMElementWriter.XmlNamespacePolicy QUALIFY_ALL

Qualifies namespaces for elements and attributes.

qualifyAttributesback to summary
private boolean qualifyAttributes
qualifyElementsback to summary
private boolean qualifyElements

Constructor Detail

XmlNamespacePolicyback to summary
public XmlNamespacePolicy(boolean qualifyElements, boolean qualifyAttributes)
Parameters
qualifyElements:boolean

whether to qualify elements

qualifyAttributes:boolean

whether to qualify elements