Package org.apache.uima.pear.util
Class XMLUtil
java.lang.Object
org.apache.uima.pear.util.XMLUtil
The
XMLUtil
class provides miscellaneous XML utilities.-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
static final String
protected static final boolean
protected static final boolean
protected static final boolean
protected static final boolean
protected static final boolean
protected static final boolean
static final String
protected static final String
static final String
static final String
XML parser feature idsstatic final String
static final String
static final String
static final String
static final String
static final String
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic SAXParser
Gets new instance of theSAXParser
class and sets standard features.static String
detectXmlFileEncoding
(File xmlFile) Attempts to detect file encoding of a given XML file by analyzing it's first characters.static boolean
isValidXmlFile
(File xmlFile) Tries to parse a given XML file using SAX parser.static void
printAllXMLElements
(Properties elements, PrintWriter oWriter, int level) Prints entries of a givenProperties
object as XML elements to a givenPrintWriter
, maintaining a given indentation level.static void
printAllXMLElements
(Properties elements, String[] tagOrder, PrintWriter oWriter, int level) Prints entries of a givenProperties
object as XML elements to a givenPrintWriter
, maintaining a specified tag order and a given indentation level.static void
printAllXMLElements
(Properties elements, String valueDelimiter, String[] tagOrder, PrintWriter oWriter, int level) Prints entries of a givenProperties
object as XML elements to a givenPrintWriter
, maintaining a specified tag order and a given indentation level.static void
printError
(String type, SAXParseException ex) Prints SAX error message.static void
printXMLElement
(String tag, String elemValue, boolean putInCdataSection, PrintWriter oWriter, int level) Prints a given XML element, which contains only a given string value, to a givenPrintWriter
, maintaining a given indentation level.static void
printXMLElement
(String tag, String elemValue, PrintWriter oWriter, int level) Prints a given XML element, which contains only a given string value, to a givenPrintWriter
, maintaining a given indentation level.static void
printXMLElement
(String tag, Properties attributes, PrintWriter oWriter, int level) Prints a given XML element, which contains only given attributes, to a givenPrintWriter
, maintaining a given indentation level.static void
printXMLElement
(String tag, Properties attributes, String elemValue, boolean putInCdataSection, PrintWriter oWriter, int level) Prints a given XML element, which contains given attributes and a given string value, to a givenPrintWriter
, maintaining a given indentation level.static void
printXMLElement
(String tag, Properties attributes, String elemValue, boolean putInCdataSection, PrintWriter oWriter, int level, boolean useNewLine4Value) Prints a given XML element, which contains given attributes and a given string value, to a givenPrintWriter
, maintaining a given indentation level.static void
printXMLElement
(String tag, Properties attributes, String elemValue, PrintWriter oWriter, int level) Prints a given XML element, which contains given attributes and a given string value, to a givenPrintWriter
, maintaining a given indentation level.static void
printXMLElements
(String tag, String elemValue, String valueDelimiter, PrintWriter oWriter, int level) Prints multiple values of a given XML element, separated with a specified delimiter, to a givenPrintWriter
, maintaining a given indentation level.static void
printXMLElementValue
(String elemValue, boolean putInCdataSection, PrintWriter oWriter, int level) Prints a given element value to a givenPrintWriter
, maintaining a given indentation level.static void
printXMLElementValue
(String elemValue, PrintWriter oWriter, int level) Prints a given element value to a givenPrintWriter
, maintaining a given indentation level.static void
printXMLHeader
(String encoding, PrintWriter oWriter) Prints standard XML 1.0 header with a specified encoding to a givenPrintStream
.static void
printXMLTag
(String tag, PrintWriter oWriter, boolean tagEnd, int level) Prints a given XML tag to a givenPrintWriter
, maintaining a given indentation level.static void
printXMLTag
(String tag, Properties attributes, PrintWriter oWriter, boolean tagEnd, int level) Prints a given XML tag with given element attributes to a givenPrintWriter
, maintaining a given indentation level.
-
Field Details
-
XML_HEADER_BEG
- See Also:
-
XML_ENCODING_TAG
- See Also:
-
XML_HEADER_END
- See Also:
-
CDATA_SECTION_BEG
- See Also:
-
CDATA_SECTION_END
- See Also:
-
NAMESPACES_FEATURE_ID
XML parser feature ids- See Also:
-
NAMESPACE_PREFIXES_FEATURE_ID
- See Also:
-
VALIDATION_FEATURE_ID
- See Also:
-
SCHEMA_VALIDATION_FEATURE_ID
- See Also:
-
SCHEMA_FULL_CHECKING_FEATURE_ID
- See Also:
-
DYNAMIC_VALIDATION_FEATURE_ID
- See Also:
-
DEFAULT_NAMESPACES
protected static final boolean DEFAULT_NAMESPACES- See Also:
-
DEFAULT_NAMESPACE_PREFIXES
protected static final boolean DEFAULT_NAMESPACE_PREFIXES- See Also:
-
DEFAULT_VALIDATION
protected static final boolean DEFAULT_VALIDATION- See Also:
-
DEFAULT_SCHEMA_VALIDATION
protected static final boolean DEFAULT_SCHEMA_VALIDATION- See Also:
-
DEFAULT_SCHEMA_FULL_CHECKING
protected static final boolean DEFAULT_SCHEMA_FULL_CHECKING- See Also:
-
DEFAULT_DYNAMIC_VALIDATION
protected static final boolean DEFAULT_DYNAMIC_VALIDATION- See Also:
-
FIRST_XML_CHARS
- See Also:
-
-
Constructor Details
-
XMLUtil
public XMLUtil()XMLUtil constructor comment.
-
-
Method Details
-
createSAXParser
Gets new instance of theSAXParser
class and sets standard features.- Returns:
- The new instance of the
SAXParser
class. - Throws:
SAXException
- if any parser exception occurred.
-
detectXmlFileEncoding
Attempts to detect file encoding of a given XML file by analyzing it's first characters. This method can recognize the following 2 standard encodings: UTF-8 (ASCII) and UTF-16 (LE and BE). If the given XML file is not valid or its encoding cannot be recognized, the method returnsnull
, otherwise it returns the detected encoding name. For more on UTF encodings and its signatures see FAQ - UTF and BOM.- Parameters:
xmlFile
- The given XML file.- Returns:
- Detected XML file encoding name or
null
, if the file encoding cannot be recognized or the file is not a valid XML file. - Throws:
IOException
- If the given file cannot be read.
-
isValidXmlFile
Tries to parse a given XML file using SAX parser. Returnstrue
, if the parser does not encounter fatal error, otherwise returnsfalse
.- Parameters:
xmlFile
- The given XML file to be tested.- Returns:
true
, if the given XML file can be parsed,false
otherwise.- Throws:
IOException
- If the given file cannot be read.
-
printError
Prints SAX error message.- Parameters:
type
- typeex
- exception
-
printAllXMLElements
public static void printAllXMLElements(Properties elements, PrintWriter oWriter, int level) throws IOException Prints entries of a givenProperties
object as XML elements to a givenPrintWriter
, maintaining a given indentation level.- Parameters:
elements
- The givenProperties
object.oWriter
- The givenPrintWriter
object.level
- The given indentation level.- Throws:
IOException
- if any I/O exception occurred.
-
printAllXMLElements
public static void printAllXMLElements(Properties elements, String valueDelimiter, String[] tagOrder, PrintWriter oWriter, int level) throws IOException Prints entries of a givenProperties
object as XML elements to a givenPrintWriter
, maintaining a specified tag order and a given indentation level. Some elements may contain multiple values delimited by a specified value delimiter. Inserts new line after each printed element.- Parameters:
elements
- The givenProperties
object.valueDelimiter
- The specified value delimiter for multi-valued elements.tagOrder
- TheString
array that specifies the tag order.oWriter
- The givenPrintWriter
object.level
- The given indentation level.- Throws:
IOException
- if any I/O exception occurred.
-
printAllXMLElements
public static void printAllXMLElements(Properties elements, String[] tagOrder, PrintWriter oWriter, int level) throws IOException Prints entries of a givenProperties
object as XML elements to a givenPrintWriter
, maintaining a specified tag order and a given indentation level.- Parameters:
elements
- The givenProperties
object.tagOrder
- TheString
array that specifies the tag order.oWriter
- The givenPrintWriter
object.level
- The given indentation level.- Throws:
IOException
- if any I/O exception occurred.
-
printXMLElement
public static void printXMLElement(String tag, Properties attributes, PrintWriter oWriter, int level) throws IOException Prints a given XML element, which contains only given attributes, to a givenPrintWriter
, maintaining a given indentation level.- Parameters:
tag
- The given XML tag.attributes
- The given XML element attributes.oWriter
- The givenPrintWriter
object.level
- The given indentation level (number of marginal '\t' symbols).- Throws:
IOException
- if any I/O exception occurred.
-
printXMLElement
public static void printXMLElement(String tag, Properties attributes, String elemValue, PrintWriter oWriter, int level) throws IOException Prints a given XML element, which contains given attributes and a given string value, to a givenPrintWriter
, maintaining a given indentation level. The string element value (if exists) is printed 'as is' - without a CDATA section.- Parameters:
tag
- The given XML element tag.attributes
- The given XML element attributes.elemValue
- The given XML element value.oWriter
- The givenPrintWriter
object.level
- The given indentation level (number of marginal '\t' symbols).- Throws:
IOException
- if any I/O exception occurred.
-
printXMLElement
public static void printXMLElement(String tag, Properties attributes, String elemValue, boolean putInCdataSection, PrintWriter oWriter, int level) throws IOException Prints a given XML element, which contains given attributes and a given string value, to a givenPrintWriter
, maintaining a given indentation level. The string element value (if exists) is printed within or without the CDATA section, depending on a givenboolean
flag value.- Parameters:
tag
- The given XML element tag.attributes
- The given XML element tag.elemValue
- The given XML element value.putInCdataSection
- Iftrue
, puts the element value in theCDATA
section, otherwise prints the element value without theCDATA
section.oWriter
- The givenPrintWriter
object.level
- The given indentation level (number of marginal '\t' symbols).- Throws:
IOException
- if any I/O exception occurred.
-
printXMLElement
public static void printXMLElement(String tag, Properties attributes, String elemValue, boolean putInCdataSection, PrintWriter oWriter, int level, boolean useNewLine4Value) throws IOException Prints a given XML element, which contains given attributes and a given string value, to a givenPrintWriter
, maintaining a given indentation level. The string element value (if exists) may be printed inside a CDATA section, depending on the value of a given 'CDATA'boolean
flag. The element value may be printed on the same line as the element tags, or on a new line, depending on the value of a given 'new-line'boolean
flag.- Parameters:
tag
- The given XML element tag.attributes
- The given XML element attributes.elemValue
- The given XML element value.putInCdataSection
- Iftrue
, the given element value is printed inside the CDATA section, otherwise it's printed 'as is'.oWriter
- The givenPrintWriter
object.level
- The given indentation level (number of marginal '\t' symbols).useNewLine4Value
- Iftrue
, the given element value is printed on a new line, otherwise it's printed on the same line as the element tags.- Throws:
IOException
- if any I/O exception occurred.
-
printXMLElement
public static void printXMLElement(String tag, String elemValue, PrintWriter oWriter, int level) throws IOException Prints a given XML element, which contains only a given string value, to a givenPrintWriter
, maintaining a given indentation level. The string element value is printed 'as-is' without the CDATA block.- Parameters:
tag
- The given XML tag.elemValue
- The given XML element value.oWriter
- The givenPrintWriter
object.level
- The given indentation level (number of marginal '\t' symbols).- Throws:
IOException
- if any I/O exception occurred.
-
printXMLElement
public static void printXMLElement(String tag, String elemValue, boolean putInCdataSection, PrintWriter oWriter, int level) throws IOException Prints a given XML element, which contains only a given string value, to a givenPrintWriter
, maintaining a given indentation level. The string element value (if exists) is put into the CDATA block, if a givenboolean
flag istrue
.- Parameters:
tag
- The given XML tag.elemValue
- The given XML element value.putInCdataSection
- Iftrue
, puts the element value in theCDATA
section, otherwise prints the element value without theCDATA
section.oWriter
- The givenPrintWriter
object.level
- The given indentation level (number of marginal '\t' symbols).- Throws:
IOException
- if any I/O exception occurred.
-
printXMLElements
public static void printXMLElements(String tag, String elemValue, String valueDelimiter, PrintWriter oWriter, int level) throws IOException Prints multiple values of a given XML element, separated with a specified delimiter, to a givenPrintWriter
, maintaining a given indentation level. Inserts new line after each printed element value.- Parameters:
tag
- The given XML element tag.elemValue
- The given XML element values (multi-value), separated with the given delimiter.valueDelimiter
- The given delimiter for multi-value elements.oWriter
- The givenPrintWriter
object.level
- The given indentation level (number of marginal '\t' symbols).- Throws:
IOException
- if any I/O exception occurred.
-
printXMLElementValue
public static void printXMLElementValue(String elemValue, PrintWriter oWriter, int level) throws IOException Prints a given element value to a givenPrintWriter
, maintaining a given indentation level. By default, prints the element value 'as is' - not using theCDATA
section.- Parameters:
elemValue
- The given element value.oWriter
- The givenPrintWriter
object.level
- The given indentation level.- Throws:
IOException
- if any I/O exception occurred.
-
printXMLElementValue
public static void printXMLElementValue(String elemValue, boolean putInCdataSection, PrintWriter oWriter, int level) throws IOException Prints a given element value to a givenPrintWriter
, maintaining a given indentation level. If a givenboolean
'CDATA' flag istrue
, puts the element value in theCDATA
section, otherwise prints the element value 'as is'.- Parameters:
elemValue
- The given element value.putInCdataSection
- Iftrue
, puts the element value in theCDATA
section, otherwise prints the element value without theCDATA
section.oWriter
- The givenPrintWriter
object.level
- The given indentation level.- Throws:
IOException
- if any I/O exception occurred.
-
printXMLHeader
Prints standard XML 1.0 header with a specified encoding to a givenPrintStream
. If no encoding is specified (null
or empty string), does not include the encoding name in the header.- Parameters:
encoding
- The given XML encoding name ornull
.oWriter
- The given outputPrintStream
.- Throws:
IOException
- if any I/O exception occurred.
-
printXMLTag
public static void printXMLTag(String tag, PrintWriter oWriter, boolean tagEnd, int level) throws IOException Prints a given XML tag to a givenPrintWriter
, maintaining a given indentation level.- Parameters:
tag
- The given XML tag.oWriter
- The givenPrintWriter
object.tagEnd
- Iffalse
prints the XML tag beginning brackets, otherwise prints the the XML tag ending brackets.level
- The given indentation level (number of marginal '\t' symbols).- Throws:
IOException
- if any I/O exception occurred.
-
printXMLTag
public static void printXMLTag(String tag, Properties attributes, PrintWriter oWriter, boolean tagEnd, int level) throws IOException Prints a given XML tag with given element attributes to a givenPrintWriter
, maintaining a given indentation level.- Parameters:
tag
- The given XML tag.attributes
- The given XML element attributes.oWriter
- The givenPrintWriter
object.tagEnd
- Iffalse
prints the XML tag beginning brackets, otherwise prints the the XML tag ending brackets.level
- The given indentation level (number of marginal '\t' symbols).- Throws:
IOException
- if any I/O exception occurred.
-