public abstract class XMLUtils
extends java.lang.Object
parse() methods must be given a wellformatted XML rescource, in which the encoding is set
forth, since none of them recieves such an parameter. If no encoding is specified in the XML recource, UTF-8 is
assumed as set forth by the W3C recommendation.
For all stream() methods without the encoding-paramter the default encoding UTF-8 is assumed (W3C
Recommendation?).
| Modifier and Type | Method and Description |
|---|---|
static void |
clearGrammarCache()
Clear grammar cache.
|
static boolean |
decodeBoolean(java.lang.String xmlBoolean)
Decodes a Boolean.
|
static java.util.Date |
decodeDate(java.lang.String xmlDate)
Decodes a Date.
|
static java.util.Date |
decodeDateTime(java.lang.String xmlDate)
Decodes a DateTime.
|
static int |
decodeInteger(java.lang.String xmlInteger)
Decodes an Integer.
|
static byte[] |
documentToBytes(org.w3c.dom.Document document)
Converts a Document into a byte[].
|
static java.lang.String |
documentToString(org.w3c.dom.Document document)
Converts a Document to a String.
|
static java.lang.String |
encodeBoolean(boolean javaBoolean)
Encodes a boolean.
|
static java.lang.String |
encodeDate(java.util.Date javaDate)
Encodes a date.
|
static java.lang.String |
encodeDateTime(java.util.Date javaDate)
Encodes a DateTime.
|
static java.lang.String |
encodeInteger(int value)
Encodes and Integer.
|
static java.lang.String |
encodeInteger(java.lang.Integer value)
Encodes and Integer.
|
static java.lang.String |
generateGUID(java.lang.Object o)
A 32 byte GUID generator (Globally Unique ID).
|
static org.w3c.dom.Document |
getDocument()
Returns an empty document whose document element is not set !
|
static org.w3c.dom.Document |
getDocument(java.lang.String docElement)
Creates a document with a new document element with it's name set to the given text.
|
static java.lang.String |
getLocalPart(java.lang.String xml)
This method converts a given XML attribute or element name to a local representation by stripping it of its
namespace prefix.
|
static boolean |
isXML(byte[] message)
Checks if the given byte[] is XML.
|
static boolean |
isXML(java.io.File file)
Checks if the given file is XML.
|
static boolean |
isXML(java.io.InputStream is)
Checks if the given InputStream is XML.
|
static void |
loadSchema(java.lang.String schemaName,
BundleContext context) |
static void |
loadSchemas(java.io.File folder,
java.lang.String filter,
java.lang.String schemaUrlPrefix)
Load schemas.
|
static org.w3c.dom.Document |
newDocument()
Creates a new Document.
|
static org.w3c.dom.Document |
parse(byte[] bytes,
boolean validate)
Shorthand for
parse(bytes, new XMLUtilsConfig(validate,true)). |
static org.w3c.dom.Document |
parse(byte[] bytes,
XMLUtilsConfig cfg)
Parses a document from the given byte-array.The applied encoding must be declared within the XML-document-instance
or otherwise UTF-8 is assumed according to the W3C recommendation.
|
static org.w3c.dom.Document |
parse(java.io.File file,
boolean validate)
Shorthand for
parse(file, new XMLUtilsConfig(validate,true)). |
static org.w3c.dom.Document |
parse(java.io.File file,
XMLUtilsConfig cfg)
Parses a document from the given File.
|
static org.w3c.dom.Document |
parse(java.io.InputStream is,
boolean validate)
Shorthand for
parse(is , new XMLUtilsConfig(validate,true)). |
static org.w3c.dom.Document |
parse(java.io.InputStream is,
XMLUtilsConfig cfg)
Parses a document from the given InputStream.
|
static void |
removeWhitespaceTextNodes(org.w3c.dom.Element parent)
Traverses through a DOM tree starting at the given element and removes all those text-nodes from it that only
contain white spaces.
|
static void |
setGrammarCacheLock(boolean lockState)
Sets the grammar cache lock.
|
static byte[] |
stream(org.w3c.dom.Element el,
boolean validate)
Shorthand for
stream(el , validate, "UTF-8")). |
static byte[] |
stream(org.w3c.dom.Element el,
boolean validate,
boolean preserveSpace)
Shorthand for
stream(el , validate,
"UTF-8")). |
static byte[] |
stream(org.w3c.dom.Element el,
boolean validate,
java.lang.String enc,
boolean preserveSpace)
Streams the given DOM-(Sub)tree starting at the given Element into the returned byte-array.
|
static void |
stream(org.w3c.dom.Element el,
boolean validate,
java.lang.String enc,
java.io.File file)
Streams the given DOM-(Sub)tree starting at the given Element into the given File
|
static void |
stream(org.w3c.dom.Element el,
boolean validate,
java.lang.String enc,
java.io.OutputStream os)
Streams the given DOM-(Sub)tree starting at the given Element into the returned byte-array.
|
static boolean |
validate(org.w3c.dom.Element el)
Validates the given element and it's decendants against the XML-Schema as specified in the given element.
|
public static org.w3c.dom.Document newDocument()
throws XMLUtilsException
XMLUtilsException - if any error occurspublic static java.lang.String documentToString(org.w3c.dom.Document document)
throws XMLUtilsException
document - the Document to convertXMLUtilsException - if any error occurspublic static byte[] documentToBytes(org.w3c.dom.Document document)
throws XMLUtilsException
document - the DocumentXMLUtilsException - if any error occurspublic static java.util.Date decodeDate(java.lang.String xmlDate)
throws java.text.ParseException
xmlDate - the xmlDatejava.text.ParseException - if any error occurspublic static java.lang.String encodeDate(java.util.Date javaDate)
javaDate - a Datepublic static java.util.Date decodeDateTime(java.lang.String xmlDate)
throws java.text.ParseException
xmlDate - the xmlDatejava.text.ParseException - if any error occurspublic static java.lang.String encodeDateTime(java.util.Date javaDate)
javaDate - a datepublic static boolean decodeBoolean(java.lang.String xmlBoolean)
xmlBoolean - the xmlBooleanpublic static java.lang.String encodeBoolean(boolean javaBoolean)
javaBoolean - a booleanpublic static int decodeInteger(java.lang.String xmlInteger)
xmlInteger - the xmlIntegerpublic static java.lang.String encodeInteger(int value)
value - a intpublic static java.lang.String encodeInteger(java.lang.Integer value)
value - Integerpublic static final java.lang.String generateGUID(java.lang.Object o)
o - a Objectpublic static org.w3c.dom.Document getDocument()
public static org.w3c.dom.Document getDocument(java.lang.String docElement)
docElement - a docElementpublic static java.lang.String getLocalPart(java.lang.String xml)
xml - the xmlpublic static boolean isXML(java.io.File file)
file - a Filepublic static boolean isXML(byte[] message)
message - a byte[]public static boolean isXML(java.io.InputStream is)
is - a InputStreampublic static void loadSchema(java.lang.String schemaName,
BundleContext context)
throws XMLUtilsException
schemaName - Schema name.XMLUtilsException - Unable to resolve schema.public static void loadSchemas(java.io.File folder,
java.lang.String filter,
java.lang.String schemaUrlPrefix)
throws XMLUtilsException
folder - the folderfilter - the filterschemaUrlPrefix - the schemaUrlPrefixXMLUtilsException - if any error occurspublic static void clearGrammarCache()
public static void setGrammarCacheLock(boolean lockState)
lockState - boolean flagpublic static org.w3c.dom.Document parse(byte[] bytes,
boolean validate)
throws XMLUtilsException
parse(bytes, new XMLUtilsConfig(validate,true)).bytes - a byte[]validate - boolean flagXMLUtilsException - if any error occurspublic static org.w3c.dom.Document parse(byte[] bytes,
XMLUtilsConfig cfg)
throws XMLUtilsException
bytes - a byte[]cfg - a XMLUtilsConfigXMLUtilsException - if cgf contains invalid values or parsing results in an error.public static org.w3c.dom.Document parse(java.io.File file,
boolean validate)
throws XMLUtilsException
parse(file, new XMLUtilsConfig(validate,true)).file - a Filevalidate - boolean flagXMLUtilsException - if any error occurspublic static org.w3c.dom.Document parse(java.io.File file,
XMLUtilsConfig cfg)
throws XMLUtilsException
file - a Filecfg - the XMLUtilsConfigXMLUtilsException - if the File dosn't point to a valid location, cgf contains invalid values, or parsing results in an
error.public static org.w3c.dom.Document parse(java.io.InputStream is,
boolean validate)
throws XMLUtilsException
parse(is , new XMLUtilsConfig(validate,true)).is - a InputStreamvalidate - boolean flagXMLUtilsException - if any error occurspublic static org.w3c.dom.Document parse(java.io.InputStream is,
XMLUtilsConfig cfg)
throws XMLUtilsException
is - a InputStreamcfg - the XMLUtilsConfigXMLUtilsException - if cfg contains invalid values or parsing results in an error.public static void removeWhitespaceTextNodes(org.w3c.dom.Element parent)
parent - the parent Elementpublic static byte[] stream(org.w3c.dom.Element el,
boolean validate)
throws XMLUtilsException
stream(el , validate, "UTF-8")).el - a Elementvalidate - boolean flagXMLUtilsException - if any exception occurspublic static byte[] stream(org.w3c.dom.Element el,
boolean validate,
boolean preserveSpace)
throws XMLUtilsException
stream(el , validate,
"UTF-8")).el - a Elementvalidate - boolean flagpreserveSpace - boolean flagXMLUtilsException - if any exception occurspublic static byte[] stream(org.w3c.dom.Element el,
boolean validate,
java.lang.String enc,
boolean preserveSpace)
throws XMLUtilsException
el - a Elementvalidate - whether the given DOM-(sub)tree shall be validated against an XML-Schema that is referenced in the given
element.enc - the encoding that shall be applied. The given String must conform to the encodings as set forth by the W3C
and are somewhat different to that of Java.preserveSpace - boolean flagXMLUtilsException - if any exception occurspublic static void stream(org.w3c.dom.Element el,
boolean validate,
java.lang.String enc,
java.io.File file)
throws XMLUtilsException
el - a Elementenc - the encoding that shall be applied. The given String must conform to the encodings as set forth by the W3C
and are somewhat different to that of Java.validate - whether the given DOM-(sub)tree shall be validated against an XML-Schema that is referenced in the given
element.file - if it exist it will be overwritten otherwise created. If access rights are violated an XMLUtilsExcpetion
is thrown.XMLUtilsException - if any error occurspublic static void stream(org.w3c.dom.Element el,
boolean validate,
java.lang.String enc,
java.io.OutputStream os)
throws XMLUtilsException
el - a Elementenc - the encoding that shall be applied. The given String must conform to the encodings as set forth by the W3C
and are somewhat different to that of Java.validate - whether the given DOM-(sub)tree shall be validated against an XML-Schema that is referenced in the given
element.os - a OutputStreamXMLUtilsException - if any error occurspublic static boolean validate(org.w3c.dom.Element el)
throws XMLUtilsException
Unless someone finds a way to validate a life DOM-Tree against a given schema, the quickest way to do this is to, write the damn thing into an ByteArrayOutputStream and to read from that again, and while reading it perform a validation.
el - a ElementXMLUtilsException - if any error occurs