public final class XMLTools extends Object
Modifier and Type | Class and Description |
---|---|
static interface |
XMLTools.SchemaReader
A schema reader can provide
InputStream s for certain XML schemas. |
Modifier and Type | Method and Description |
---|---|
static String |
avoidUndeclaredNamespaces(String xml)
Replaces NS:tag with NS_tag for undeclared namespaces
|
static DocumentBuilder |
createBuilder()
|
static Document |
createDocument()
Calls
DocumentBuilder.newDocument() on a
new builder . |
static String |
dumpXML(String schemaLocation,
Document doc,
Element r)
Dumps the given OME-XML DOM tree to a string.
|
static String |
dumpXML(String schemaLocation,
Document doc,
Element r,
boolean includeXMLDeclaration)
Dumps the given OME-XML DOM tree to a string.
|
static String |
escapeXML(String s)
Escape special characters.
|
static Templates |
getStylesheet(String resourcePath,
Class<?> sourceClass)
Gets an XSLT template from the given resource location.
|
static String |
getXML(Document doc)
Converts the given DOM back to a string.
|
static String |
indentXML(String xml)
Indents XML to be more readable.
|
static String |
indentXML(String xml,
boolean preserveCData)
Indents XML to be more readable, avoiding any whitespace
injection into CDATA if the preserveCData flag is set.
|
static String |
indentXML(String xml,
int spacing)
Indents XML by the given spacing to be more readable.
|
static String |
indentXML(String xml,
int spacing,
boolean preserveCData)
Indents XML by the given spacing to be more readable, avoiding any
whitespace injection into CDATA if the preserveCData flag is set.
|
static Document |
parseDOM(File file)
Parses a DOM from the given XML file on disk.
|
static Document |
parseDOM(InputStream is)
Parses a DOM from the given XML input stream.
|
static Document |
parseDOM(String xml)
Parses a DOM from the given XML string.
|
static void |
parseXML(byte[] xml,
DefaultHandler handler)
Parses the XML contained in the given byte array into
using the specified XML handler.
|
static void |
parseXML(InputStream xml,
DefaultHandler handler)
Parses the XML contained in the given InputStream using the
specified XML handler.
|
static void |
parseXML(RandomAccessInputStream stream,
DefaultHandler handler)
Parses the XML contained in the given input stream into
using the specified XML handler.
|
static Hashtable<String,String> |
parseXML(String xml)
Parses the given XML string into a list of key/value pairs.
|
static void |
parseXML(String xml,
DefaultHandler handler)
Parses the given XML string using the specified XML handler.
|
static String |
sanitizeXML(String s)
Remove invalid characters from an XML string.
|
static String |
transformXML(Source xmlSource,
Templates xslt)
Transforms the given XML data using the specified XSLT stylesheet.
|
static String |
transformXML(String xml,
Templates xslt)
Transforms the given XML string using the specified XSLT stylesheet.
|
static boolean |
validateXML(String xml)
Attempts to validate the given XML string using
Java's XML validation facility.
|
static boolean |
validateXML(String xml,
String label)
Attempts to validate the given XML string using
Java's XML validation facility.
|
static boolean |
validateXML(String xml,
String label,
XMLTools.SchemaReader schemaReader)
Attempts to validate the given XML string using
Java's XML validation facility.
|
static void |
writeXML(OutputStream os,
Document doc)
Writes the specified DOM to the given output stream.
|
static void |
writeXML(OutputStream os,
Document doc,
boolean includeXMLDeclaration)
Writes the specified DOM to the given output stream.
|
static void |
writeXML(Result output,
Document doc,
boolean includeXMLDeclaration)
Writes the specified DOM to the given stream.
|
public static DocumentBuilder createBuilder()
DocumentBuilder
instancepublic static Document createDocument()
DocumentBuilder.newDocument()
on a
new builder
.Document
public static Document parseDOM(File file) throws ParserConfigurationException, SAXException, IOException
file
- a file on disk that contains XMLDocument
reflecting the contents of the fileParserConfigurationException
- if the XML parser cannot be createdSAXException
- if there is an error parsing the XMLIOException
- if there is an error reading from the filepublic static Document parseDOM(String xml) throws ParserConfigurationException, SAXException, IOException
xml
- XML dataDocument
reflecting the XML stringParserConfigurationException
- if the XML parser cannot be createdSAXException
- if there is an error parsing the XMLIOException
- if there is an error reading from the filepublic static Document parseDOM(InputStream is) throws ParserConfigurationException, SAXException, IOException
is
- the InputStream
containing XMLDocument
representing the XMLParserConfigurationException
- if the XML parser cannot be createdSAXException
- if there is an error parsing the XMLIOException
- if there is an error reading from the streampublic static String getXML(Document doc) throws TransformerConfigurationException, TransformerException
doc
- the Document
representing the XMLTransformerConfigurationException
- if there is an error
initializing the conversionTransformerException
- if there is an error during writingpublic static String dumpXML(String schemaLocation, Document doc, Element r)
public static String dumpXML(String schemaLocation, Document doc, Element r, boolean includeXMLDeclaration)
schemaLocation
- if null, no xmlns attribute will be added.doc
- the Document
representing the XML to be writtenr
- the Element
to use for adding the schema locationincludeXMLDeclaration
- false if the XML declaration should
be omitted, see OutputKeys.OMIT_XML_DECLARATION
public static String escapeXML(String s)
s
- input string containing characters to escapepublic static String sanitizeXML(String s)
s
- the input string from which to remove invalid characterspublic static String indentXML(String xml)
xml
- the XML string to formatindentXML(String, int, boolean)
public static String indentXML(String xml, int spacing)
xml
- the XML string to formatspacing
- the number of spaces by which to indentindentXML(String, int, boolean)
public static String indentXML(String xml, boolean preserveCData)
xml
- the XML string to formatpreserveCData
- true if CDATA nodes should be preserved
with no indentingindentXML(String, int, boolean)
public static String indentXML(String xml, int spacing, boolean preserveCData)
xml
- the XML string to formatspacing
- the number of spaces by which to indentpreserveCData
- true if CDATA nodes should be preserved
with no indentingpublic static Hashtable<String,String> parseXML(String xml) throws IOException
xml
- the InputStream
representing the XMLIOException
- if there is an error during parsingMetadataHandler
public static void parseXML(String xml, DefaultHandler handler) throws IOException
xml
- the string representing the XMLhandler
- the DefaultHandler
to use for parsingIOException
- if there is an error during parsingpublic static void parseXML(RandomAccessInputStream stream, DefaultHandler handler) throws IOException
stream
- the RandomAccessInputStream
representing the XMLhandler
- the DefaultHandler
to use for parsingIOException
- if there is an error during parsingpublic static void parseXML(byte[] xml, DefaultHandler handler) throws IOException
xml
- the byte array representing the XMLhandler
- the DefaultHandler
to use for parsingIOException
- if there is an error during parsingpublic static void parseXML(InputStream xml, DefaultHandler handler) throws IOException
xml
- the InputStream
representing the XMLhandler
- the DefaultHandler
to use for parsingIOException
- if there is an error during parsingpublic static void writeXML(OutputStream os, Document doc) throws TransformerException
os
- the OutputStream
to which XML should be writtendoc
- the Document
representing the XML to writeTransformerException
- if there is an error during writingpublic static void writeXML(OutputStream os, Document doc, boolean includeXMLDeclaration) throws TransformerException
os
- the OutputStream
to which XML should be writtendoc
- the Document
representing the XML to writeincludeXMLDeclaration
- false if the XML declaration should
be omitted, see OutputKeys.OMIT_XML_DECLARATION
TransformerException
- if there is an error during writingpublic static void writeXML(Result output, Document doc, boolean includeXMLDeclaration) throws TransformerException
output
- the Result
to which XML should be writtendoc
- the Document
representing the XML to writeincludeXMLDeclaration
- false if the XML declaration should
be omitted, see OutputKeys.OMIT_XML_DECLARATION
TransformerException
- if there is an error during writingpublic static Templates getStylesheet(String resourcePath, Class<?> sourceClass)
resourcePath
- the name of the stylesheet resourcesourceClass
- the class to use when searching for the resourceTemplates
object representing the stylesheetpublic static String avoidUndeclaredNamespaces(String xml)
xml
- the XML string whose namespaces need to be replacedpublic static String transformXML(String xml, Templates xslt) throws IOException
xml
- the XML string to be transformedxslt
- the Templates
object representing an XSLT stylesheetIOException
- if there is an error parsing the XMLgetStylesheet(String, Class)
public static String transformXML(Source xmlSource, Templates xslt) throws IOException
xmlSource
- the Source
object representing
the XML to be transformedxslt
- the Templates
object representing an XSLT stylesheetIOException
- if there is an error parsing the XMLgetStylesheet(String, Class)
public static boolean validateXML(String xml)
xml
- The XML string to validate.public static boolean validateXML(String xml, String label)
xml
- The XML string to validate.label
- String describing the type of XML being validated.public static boolean validateXML(String xml, String label, XMLTools.SchemaReader schemaReader)
xml
- The XML string to validate.label
- String describing the type of XML being validated.schemaReader
- turns schema system IDs into input streams,
may be null
Copyright © 2005–2022 Open Microscopy Environment. All rights reserved.