public abstract class XMLUtils extends Object
Constructor and Description |
---|
XMLUtils() |
Modifier and Type | Method and Description |
---|---|
static int |
checkForNonXmlCharacters(char[] ch,
int start,
int length,
boolean xml11)
Check the input character array for non-XML characters.
|
static int |
checkForNonXmlCharacters(String s)
Check the input string for non-XML 1.0 characters.
|
static int |
checkForNonXmlCharacters(String s,
boolean xml11)
Check the input string for non-XML characters.
|
static Element |
getChildByTagName(Element aElem,
String aName)
Gets the first child of the given Element with the given tag name.
|
static Element |
getFirstChildElement(Element aElem)
Gets the first child of the given Element.
|
static String |
getText(Element aElem)
Gets the text of this Element.
|
static String |
getText(Element aElem,
boolean aExpandEnvVarRefs)
Gets the text of this Element.
|
static void |
normalize(String aStr,
StringBuffer aResultBuf)
Normalizes the given string for output to XML.
|
static void |
normalize(String aStr,
StringBuffer aResultBuf,
boolean aNewlinesToSpaces)
Normalizes the given string for output to XML.
|
static Object |
readPrimitiveValue(Element aElem)
Reads a primitive value from its standard DOM representation.
|
static void |
writeNormalizedString(String aStr,
Writer aWriter,
boolean aNewlinesToSpaces)
Normalizes the given string for output to XML, and writes the normalized string to the given
Writer.
|
static void |
writePrimitiveValue(Object aObj,
Writer aWriter)
Writes a standard XML representation of the specified Object, in the form:
<className>string value%lt;/className%gt; |
public static void normalize(String aStr, StringBuffer aResultBuf)
aStr
- input stringaResultBuf
- the StringBuffer to which the normalized string will be appendedpublic static void normalize(String aStr, StringBuffer aResultBuf, boolean aNewlinesToSpaces)
aNewlinesToSpaces
parameter. The normalized
string is appended to the specified StringBuffer.aStr
- input stringaNewlinesToSpaces
- iff true, newlines (\r and \n) will be converted to spacesaResultBuf
- the StringBuffer to which the normalized string will be appendedpublic static void writeNormalizedString(String aStr, Writer aWriter, boolean aNewlinesToSpaces) throws IOException
aNewlinesToSpaces
parameter.aStr
- input stringaWriter
- a Writer to which the normalized string will be writtenaNewlinesToSpaces
- iff true, newlines (\r and \n) will be converted to spacesIOException
- if an I/O failure occurs when writing to aWriter
public static void writePrimitiveValue(Object aObj, Writer aWriter) throws IOException
<className>string value%lt;/className%gt;
where className
is the object's java class name without the package and made
lowercase, e.g. "string","integer", "boolean" and string value
is the result of
Object.toString()
.
This is intended to be used for Java Strings and wrappers for primitive value classes (e.g. Integer, Boolean).
aObj
- the object to writeaWriter
- a Writer to which the XML will be writtenIOException
- if an I/O failure occurs when writing to aWriter
public static Element getChildByTagName(Element aElem, String aName)
aElem
- the parent elementaName
- tag name of the child to retrieveaElem
with tag name aName
,
null
if there is no such child.public static Element getFirstChildElement(Element aElem)
aElem
- the parent elementaElem
, null
if it has no children.public static Object readPrimitiveValue(Element aElem)
writePrimitiveValue(Object, Writer)
.
This is intended to be used for Java Strings and wrappers for primitive value classes (e.g. Integer, Boolean).
aElem
- the element representing the valuenull
if a primitive value could not be
constructed from the elementpublic static String getText(Element aElem)
aElem
- the elementaElem
public static String getText(Element aElem, boolean aExpandEnvVarRefs)
aElem
- the elementaExpandEnvVarRefs
- whether to expand environment variable references. Defaults to false.aElem
public static final int checkForNonXmlCharacters(String s)
-1
.
From the XML 1.0 spec:
Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] // any Unicode character, excluding the surrogate blocks, FFFE, and FFFF.
And from the UTF-16 spec:
Characters with values between 0x10000 and 0x10FFFF are represented by a 16-bit integer with a value between 0xD800 and 0xDBFF (within the so-called high-half zone or high surrogate area) followed by a 16-bit integer with a value between 0xDC00 and 0xDFFF (within the so-called low-half zone or low surrogate area).
s
- Input string-1
if no
invalid XML characters found.public static final int checkForNonXmlCharacters(String s, boolean xml11)
-1
.
The definition of an XML character is different for
XML 1.0 and 1.1. This method will check either version, depending on the value of the
xml11
argument.
From the XML 1.0 spec:
Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] // any Unicode character, excluding the surrogate blocks, FFFE, and FFFF.
From the XML 1.1 spec:
Char ::= [#x1-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
And from the UTF-16 spec:
Characters with values between 0x10000 and 0x10FFFF are represented by a 16-bit integer with a value between 0xD800 and 0xDBFF (within the so-called high-half zone or high surrogate area) followed by a 16-bit integer with a value between 0xDC00 and 0xDFFF (within the so-called low-half zone or low surrogate area).
s
- Input stringxml11
- true to check for invalid XML 1.1 characters, false to check for invalid XML 1.0 characters.
The default is false.-1
if no
invalid XML characters found.public static final int checkForNonXmlCharacters(char[] ch, int start, int length, boolean xml11)
-1
.ch
- Input character arraystart
- offset of first char to checklength
- number of chars to checkxml11
- true to check for invalid XML 1.1 characters, false to check for invalid XML 1.0 characters.
The default is false.-1
if no
invalid XML characters found.checkForNonXmlCharacters(String, boolean)
Copyright © 2017. All rights reserved.