de.infinityloop.util.markup
Class XMLTools

java.lang.Object
  extended by de.infinityloop.util.markup.XMLTools

public class XMLTools
extends java.lang.Object

Tools for XML parsing and XSLT application.


Constructor Summary
XMLTools()
          initialize
 
Method Summary
static void addCatalog(java.lang.String catalogLocation)
          adds a single catalog to the existing catalog system
static void chainSAXONWithParamsCatalog(InputProxy inXML, OutputProxy outXML, java.util.Vector transformations, javax.xml.transform.ErrorListener myErrorListener, Pair[] features, Pair[] properties)
          chains several transformationsone one after another.
static void chainSAXONWithParamsCatalog(InputProxy inXML, OutputProxy outXML, java.util.Vector transformations, javax.xml.transform.ErrorListener myErrorListener, Pair[] features, Pair[] properties, java.lang.String intermediateDir)
          chains several transformationsone one after another.
static void chainXALANWithParamsCatalog(InputProxy inXML, OutputProxy outXML, java.util.Vector transformations, javax.xml.transform.ErrorListener myErrorListener)
          chains several transformationsone after another.
static org.w3c.dom.Document chainXALANWithParamsCatalog(InputProxy inXML, java.util.Vector transformations, javax.xml.transform.ErrorListener myErrorListener)
          chains several transformationsone after another.
static java.lang.String collectTextNodesContent(org.w3c.dom.DocumentFragment df, java.lang.String[] elementFilter)
          Collect text-only contents on this DocumentFragment, possibly with filter on element names
static java.lang.String collectTextNodesContent(org.w3c.dom.Node curNode, java.lang.String[] elementFilter)
          Collect text-only contents on this Node, possibly with filter on element names
static void discardCatalogs()
          removes all registered catalogs from the system
static int executeSAXONWithParamsCatalog(org.w3c.dom.Document inDOM, java.lang.String srcSystemID, InputProxy inXSL, OutputProxy outXML, java.util.Vector params, javax.xml.transform.ErrorListener myErrorListener)
          Runs Saxon with specified parameters for the stylesheet and the EntityResolver provided by the Catalog file system, and writes to a stream.
static int executeSAXONWithParamsCatalog(InputProxy inXML, InputProxy inXSL, OutputProxy outXML, java.util.Vector params, javax.xml.transform.ErrorListener myErrorListener)
          Runs Saxon with specified parameters for the stylesheet and the EntityResolver provided by the Catalog file system.
static int executeSAXONWithParamsCatalog(InputProxy inXML, InputProxy inXSL, OutputProxy outXML, java.util.Vector params, javax.xml.transform.ErrorListener myErrorListener, Pair[] features, Pair[] properties)
          Runs Saxon with specified parameters for the stylesheet and the EntityResolver provided by the Catalog file system.
static int executeXALAN(InputProxy inXML, InputProxy inXSL, OutputProxy outXML)
          Runs Xalan
static int executeXALANWithParams(InputProxy inXML, InputProxy inXSL, OutputProxy outXML, java.util.Vector params)
          Runs Xalan with specified parameters for the stylesheet.
static int executeXALANWithParamsCatalog(org.w3c.dom.Document inDOM, java.lang.String srcSystemID, InputProxy inXSL, OutputProxy outXML, java.util.Vector params, javax.xml.transform.ErrorListener myErrorListener)
          Runs Xalan with specified parameters for the stylesheet and the EntityResolver provided by the Catalog file system, and writes to a stream.
static org.w3c.dom.Document executeXALANWithParamsCatalog(org.w3c.dom.Document inDOM, java.lang.String srcSystemID, InputProxy inXSL, java.util.Vector params, javax.xml.transform.ErrorListener myErrorListener)
          Runs Xalan with specified parameters for the stylesheet and the EntityResolver provided by the Catalog file system, and returns a DOM Document tree.
static int executeXALANWithParamsCatalog(InputProxy inXML, InputProxy inXSL, OutputProxy outXML, java.util.Vector params, javax.xml.transform.ErrorListener myErrorListener)
          Runs Xalan with specified parameters for the stylesheet and the EntityResolver provided by the Catalog file system.
static org.w3c.dom.Document executeXALANWithParamsCatalog(InputProxy inXML, InputProxy inXSL, java.util.Vector params, javax.xml.transform.ErrorListener myErrorListener)
          Runs Xalan with specified parameters for the stylesheet and the EntityResolver provided by the Catalog file system, and returns a DOM Document tree.
static org.w3c.dom.Text findFirstTextNodeStartingWith(org.w3c.dom.Node curNode, java.lang.String prefix, java.lang.String[] elementFilter)
          Finds the first (probably depth-first child-) TextNode and checks if it starts with the specified string.
static org.w3c.dom.Element findNamedAncestor(java.lang.String ancestorName, org.w3c.dom.Element elem)
          Finds the first named ancestor element of the element passed.
static org.xml.sax.EntityResolver getGlobalEntityResolver()
          retrieves the global default entity resolver class.
static java.util.Vector getRegisteredCatalogSearchPaths()
          Returns a Vector of all currently programmatically registered Catalog file search paths (does not include Catalogs set by the xml.catalog.files system property!).
static java.lang.String getXSLTProcessorVersionString(java.lang.String processorToUse)
          Get current XSLT processor used + version String
static void initializeCatalogs(java.util.Vector additionalFiles)
          initializes the default catalog handler, with adding the catalog files as in the passed Vector of String objects.
static java.lang.String makePCDATA_XML(java.lang.String pcdata)
          Turns the passed PCDATA content into a string for writing to UTF-8 XML file, using entities where necessary.
static java.lang.String makePCDATAAttr_XML(java.lang.String pcdata)
          Turns the passed PCDATA content into a string for writing to UTF8 XML file, using entities where necessary.
static java.lang.String makeValidXML_ID(java.lang.String src)
          Transform a passed string in such a way that the result conforms to the rules for an XML 1.0 'Name' (as used e.g. for element names or xml:id).
static java.lang.String makeValidXMLName(java.lang.String src)
          Transform a passed string in such a way that the result conforms to the rules for an XML 1.0 'Name' (as used e.g. for element names or xml:id).
static void moveChildren(org.w3c.dom.Node from, org.w3c.dom.Node to)
          Moves all children of the first node to the second node.
static org.w3c.dom.Document parseDOM(DiskFile file)
          DOM-parses the passed file
static org.w3c.dom.Document parseDOM(java.io.InputStream is, java.lang.String systemIdentifier)
           
static org.w3c.dom.Document parseDOM(java.io.Reader rd, java.lang.String systemIdentifier)
           
static void parseSAX(org.xml.sax.helpers.DefaultHandler handler, DiskFile file)
          SAX-parses the passed file
static void parseSAX(org.xml.sax.helpers.DefaultHandler handler, org.w3c.dom.Document inTree, java.lang.String systemIdentifier, Pair[] features, Pair[] properties)
          Parses as above, with features and properties to be set as parameter.
static void parseSAX(org.xml.sax.helpers.DefaultHandler handler, java.io.InputStream is, java.lang.String systemIdentifier)
           
static void parseSAX(org.xml.sax.helpers.DefaultHandler handler, java.io.InputStream is, java.lang.String systemIdentifier, Pair[] features, Pair[] properties)
          Parses as above, with features and properties to be set as parameter.
static void parseSAX(org.xml.sax.helpers.DefaultHandler handler, java.io.Reader rd, java.lang.String systemIdentifier)
           
static void parseSAX(org.xml.sax.helpers.DefaultHandler handler, java.io.Reader rd, java.lang.String systemIdentifier, Pair[] features, Pair[] properties)
          Parses as above, with features and properties to be set as parameter.
static void removeNode(org.w3c.dom.Node node)
          Removes (only) the specified node from the tree, but not its children.
static void serializeDOM(org.w3c.dom.Document src, OutputProxy dest)
          serializes a DOM tree to the specified OutputProxy.
static void serializeInternalDOM(org.w3c.dom.Document src, OutputProxy dest)
          serializes an internal upCast DOM tree to the specified OutputProxy.
static void serializeNode(org.w3c.dom.Node n, boolean children, java.io.Writer w)
          serializes an internal upCast DOM node to the specified OutputProxy.
static void setGlobalEntityResolver(org.xml.sax.EntityResolver er)
          sets a global default entity resolver class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLTools

public XMLTools()
initialize

Method Detail

setGlobalEntityResolver

public static void setGlobalEntityResolver(org.xml.sax.EntityResolver er)
sets a global default entity resolver class. Set to null to clear.


getGlobalEntityResolver

public static org.xml.sax.EntityResolver getGlobalEntityResolver()
retrieves the global default entity resolver class.


initializeCatalogs

public static void initializeCatalogs(java.util.Vector additionalFiles)
initializes the default catalog handler, with adding the catalog files as in the passed Vector of String objects.


addCatalog

public static void addCatalog(java.lang.String catalogLocation)
adds a single catalog to the existing catalog system


discardCatalogs

public static void discardCatalogs()
removes all registered catalogs from the system


parseSAX

public static void parseSAX(org.xml.sax.helpers.DefaultHandler handler,
                            DiskFile file)
                     throws org.xml.sax.SAXException,
                            javax.xml.parsers.ParserConfigurationException
SAX-parses the passed file

Throws:
org.xml.sax.SAXException
javax.xml.parsers.ParserConfigurationException

parseSAX

public static void parseSAX(org.xml.sax.helpers.DefaultHandler handler,
                            java.io.InputStream is,
                            java.lang.String systemIdentifier)
                     throws org.xml.sax.SAXException,
                            javax.xml.parsers.ParserConfigurationException
Throws:
org.xml.sax.SAXException
javax.xml.parsers.ParserConfigurationException

parseSAX

public static void parseSAX(org.xml.sax.helpers.DefaultHandler handler,
                            java.io.Reader rd,
                            java.lang.String systemIdentifier)
                     throws org.xml.sax.SAXException,
                            javax.xml.parsers.ParserConfigurationException
Throws:
org.xml.sax.SAXException
javax.xml.parsers.ParserConfigurationException

parseSAX

public static void parseSAX(org.xml.sax.helpers.DefaultHandler handler,
                            java.io.Reader rd,
                            java.lang.String systemIdentifier,
                            Pair[] features,
                            Pair[] properties)
                     throws org.xml.sax.SAXException,
                            javax.xml.parsers.ParserConfigurationException
Parses as above, with features and properties to be set as parameter.

Throws:
org.xml.sax.SAXException
javax.xml.parsers.ParserConfigurationException

parseSAX

public static void parseSAX(org.xml.sax.helpers.DefaultHandler handler,
                            java.io.InputStream is,
                            java.lang.String systemIdentifier,
                            Pair[] features,
                            Pair[] properties)
                     throws org.xml.sax.SAXException,
                            javax.xml.parsers.ParserConfigurationException
Parses as above, with features and properties to be set as parameter.

Throws:
org.xml.sax.SAXException
javax.xml.parsers.ParserConfigurationException

parseSAX

public static void parseSAX(org.xml.sax.helpers.DefaultHandler handler,
                            org.w3c.dom.Document inTree,
                            java.lang.String systemIdentifier,
                            Pair[] features,
                            Pair[] properties)
                     throws org.xml.sax.SAXException,
                            javax.xml.parsers.ParserConfigurationException
Parses as above, with features and properties to be set as parameter. Takes a DOM tree as input!

Throws:
org.xml.sax.SAXException
javax.xml.parsers.ParserConfigurationException

parseDOM

public static org.w3c.dom.Document parseDOM(DiskFile file)
                                     throws org.xml.sax.SAXException,
                                            java.io.IOException,
                                            javax.xml.parsers.ParserConfigurationException
DOM-parses the passed file

Throws:
org.xml.sax.SAXException
java.io.IOException
javax.xml.parsers.ParserConfigurationException

parseDOM

public static org.w3c.dom.Document parseDOM(java.io.InputStream is,
                                            java.lang.String systemIdentifier)
                                     throws org.xml.sax.SAXException,
                                            java.io.IOException,
                                            javax.xml.parsers.ParserConfigurationException
Throws:
org.xml.sax.SAXException
java.io.IOException
javax.xml.parsers.ParserConfigurationException

parseDOM

public static org.w3c.dom.Document parseDOM(java.io.Reader rd,
                                            java.lang.String systemIdentifier)
                                     throws org.xml.sax.SAXException,
                                            java.io.IOException,
                                            javax.xml.parsers.ParserConfigurationException
Throws:
org.xml.sax.SAXException
java.io.IOException
javax.xml.parsers.ParserConfigurationException

makePCDATA_XML

public static java.lang.String makePCDATA_XML(java.lang.String pcdata)
Turns the passed PCDATA content into a string for writing to UTF-8 XML file, using entities where necessary. Use this for plain text only.


executeXALAN

public static int executeXALAN(InputProxy inXML,
                               InputProxy inXSL,
                               OutputProxy outXML)
                        throws java.lang.Exception
Runs Xalan

Throws:
java.lang.Exception

executeXALANWithParams

public static int executeXALANWithParams(InputProxy inXML,
                                         InputProxy inXSL,
                                         OutputProxy outXML,
                                         java.util.Vector params)
                                  throws java.lang.Exception
Runs Xalan with specified parameters for the stylesheet.

Throws:
java.lang.Exception

executeXALANWithParamsCatalog

public static int executeXALANWithParamsCatalog(InputProxy inXML,
                                                InputProxy inXSL,
                                                OutputProxy outXML,
                                                java.util.Vector params,
                                                javax.xml.transform.ErrorListener myErrorListener)
                                         throws java.lang.Exception
Runs Xalan with specified parameters for the stylesheet and the EntityResolver provided by the Catalog file system.

Throws:
java.lang.Exception

executeSAXONWithParamsCatalog

public static int executeSAXONWithParamsCatalog(InputProxy inXML,
                                                InputProxy inXSL,
                                                OutputProxy outXML,
                                                java.util.Vector params,
                                                javax.xml.transform.ErrorListener myErrorListener)
                                         throws java.lang.Exception
Runs Saxon with specified parameters for the stylesheet and the EntityResolver provided by the Catalog file system.

Throws:
java.lang.Exception

executeSAXONWithParamsCatalog

public static int executeSAXONWithParamsCatalog(InputProxy inXML,
                                                InputProxy inXSL,
                                                OutputProxy outXML,
                                                java.util.Vector params,
                                                javax.xml.transform.ErrorListener myErrorListener,
                                                Pair[] features,
                                                Pair[] properties)
                                         throws java.lang.Exception
Runs Saxon with specified parameters for the stylesheet and the EntityResolver provided by the Catalog file system. You can provide an array of Pair()s for features and properties for the underlying parser. These will be applied to the input source XML document.

Throws:
java.lang.Exception

executeXALANWithParamsCatalog

public static org.w3c.dom.Document executeXALANWithParamsCatalog(InputProxy inXML,
                                                                 InputProxy inXSL,
                                                                 java.util.Vector params,
                                                                 javax.xml.transform.ErrorListener myErrorListener)
                                                          throws java.lang.Exception
Runs Xalan with specified parameters for the stylesheet and the EntityResolver provided by the Catalog file system, and returns a DOM Document tree.

Throws:
java.lang.Exception

executeXALANWithParamsCatalog

public static org.w3c.dom.Document executeXALANWithParamsCatalog(org.w3c.dom.Document inDOM,
                                                                 java.lang.String srcSystemID,
                                                                 InputProxy inXSL,
                                                                 java.util.Vector params,
                                                                 javax.xml.transform.ErrorListener myErrorListener)
                                                          throws java.lang.Exception
Runs Xalan with specified parameters for the stylesheet and the EntityResolver provided by the Catalog file system, and returns a DOM Document tree. Takes a DOM tree as input source.

Throws:
java.lang.Exception

executeXALANWithParamsCatalog

public static int executeXALANWithParamsCatalog(org.w3c.dom.Document inDOM,
                                                java.lang.String srcSystemID,
                                                InputProxy inXSL,
                                                OutputProxy outXML,
                                                java.util.Vector params,
                                                javax.xml.transform.ErrorListener myErrorListener)
                                         throws java.lang.Exception
Runs Xalan with specified parameters for the stylesheet and the EntityResolver provided by the Catalog file system, and writes to a stream. Takes a DOM tree as input source.

Throws:
java.lang.Exception

executeSAXONWithParamsCatalog

public static int executeSAXONWithParamsCatalog(org.w3c.dom.Document inDOM,
                                                java.lang.String srcSystemID,
                                                InputProxy inXSL,
                                                OutputProxy outXML,
                                                java.util.Vector params,
                                                javax.xml.transform.ErrorListener myErrorListener)
                                         throws java.lang.Exception
Runs Saxon with specified parameters for the stylesheet and the EntityResolver provided by the Catalog file system, and writes to a stream. Takes a DOM tree as input source.

Throws:
java.lang.Exception

chainXALANWithParamsCatalog

public static void chainXALANWithParamsCatalog(InputProxy inXML,
                                               OutputProxy outXML,
                                               java.util.Vector transformations,
                                               javax.xml.transform.ErrorListener myErrorListener)
                                        throws java.lang.Exception
chains several transformationsone after another. The transformations Vector is a Vector of Pair()s holding (InputProxy xslSource, Vector params) tuples. Transformations are performed in the order specified by the Vector elements.

Throws:
java.lang.Exception

chainSAXONWithParamsCatalog

public static void chainSAXONWithParamsCatalog(InputProxy inXML,
                                               OutputProxy outXML,
                                               java.util.Vector transformations,
                                               javax.xml.transform.ErrorListener myErrorListener,
                                               Pair[] features,
                                               Pair[] properties,
                                               java.lang.String intermediateDir)
                                        throws java.lang.Exception
chains several transformationsone one after another. The transformations Vector is a Vector of Pair()s holding (InputProxy xslSource, Vector params) tuples. Transformations are performed in the order specified by the Vector elements.

Parameters:
features - These features are set for the initial reading of the XML source
properties - These properties are set for the initial reading of the XML source
intermediateDir - directory where intermediate files should be written; when null, transformations are internally chained
Throws:
java.lang.Exception

chainSAXONWithParamsCatalog

public static void chainSAXONWithParamsCatalog(InputProxy inXML,
                                               OutputProxy outXML,
                                               java.util.Vector transformations,
                                               javax.xml.transform.ErrorListener myErrorListener,
                                               Pair[] features,
                                               Pair[] properties)
                                        throws java.lang.Exception
chains several transformationsone one after another. The transformations Vector is a Vector of Pair()s holding (InputProxy xslSource, Vector params) tuples. Transformations are performed in the order specified by the Vector elements.

Parameters:
features - These features are set for the initial reading of the XML source
properties - These properties are set for the initial reading of the XML source
Throws:
java.lang.Exception

chainXALANWithParamsCatalog

public static org.w3c.dom.Document chainXALANWithParamsCatalog(InputProxy inXML,
                                                               java.util.Vector transformations,
                                                               javax.xml.transform.ErrorListener myErrorListener)
                                                        throws java.lang.Exception
chains several transformationsone after another. The transformations Vector is a Vector of Pair()s holding (InputProxy xslSource, Vector params) tuples. Transformations are performed in the order specified by the Vector elements.

Throws:
java.lang.Exception

serializeInternalDOM

public static void serializeInternalDOM(org.w3c.dom.Document src,
                                        OutputProxy dest)
                                 throws java.lang.Exception
serializes an internal upCast DOM tree to the specified OutputProxy.

Parameters:
src - the Document to serialize
dest - the destination to serialize to
Throws:
java.lang.Exception

serializeNode

public static void serializeNode(org.w3c.dom.Node n,
                                 boolean children,
                                 java.io.Writer w)
                          throws java.lang.Exception
serializes an internal upCast DOM node to the specified OutputProxy.

Parameters:
n - the node to (start) serialize
children - when true, all the nodes descendants are also serialized
w - the Writer to serialize to
Throws:
java.lang.Exception

serializeDOM

public static void serializeDOM(org.w3c.dom.Document src,
                                OutputProxy dest)
                         throws java.lang.Exception
serializes a DOM tree to the specified OutputProxy.

Throws:
java.lang.Exception

collectTextNodesContent

public static java.lang.String collectTextNodesContent(org.w3c.dom.DocumentFragment df,
                                                       java.lang.String[] elementFilter)
Collect text-only contents on this DocumentFragment, possibly with filter on element names


collectTextNodesContent

public static java.lang.String collectTextNodesContent(org.w3c.dom.Node curNode,
                                                       java.lang.String[] elementFilter)
Collect text-only contents on this Node, possibly with filter on element names


findFirstTextNodeStartingWith

public static org.w3c.dom.Text findFirstTextNodeStartingWith(org.w3c.dom.Node curNode,
                                                             java.lang.String prefix,
                                                             java.lang.String[] elementFilter)
Finds the first (probably depth-first child-) TextNode and checks if it starts with the specified string. If it does, that text node is returned, otherwise we return null.

Parameters:
curNode - the node to start the search with. This node is already considered itself
prefix - the prefix string to match against
elementFilter - a list of element names that should be skipped when searching for an appropriate text node
Returns:
the matching text node (if any), null otherwise

getRegisteredCatalogSearchPaths

public static java.util.Vector getRegisteredCatalogSearchPaths()
Returns a Vector of all currently programmatically registered Catalog file search paths (does not include Catalogs set by the xml.catalog.files system property!).


makePCDATAAttr_XML

public static java.lang.String makePCDATAAttr_XML(java.lang.String pcdata)
Turns the passed PCDATA content into a string for writing to UTF8 XML file, using entities where necessary. Use this for all Attribute values only (does NOT write elements).


getXSLTProcessorVersionString

public static java.lang.String getXSLTProcessorVersionString(java.lang.String processorToUse)
Get current XSLT processor used + version String

Parameters:
processorToUse - identifies the processor for which the version string should be built: "xalan" (the default when null) or "saxon".

findNamedAncestor

public static org.w3c.dom.Element findNamedAncestor(java.lang.String ancestorName,
                                                    org.w3c.dom.Element elem)
Finds the first named ancestor element of the element passed. If that isn't found, null is returned.

Parameters:
ancestorName - the name of the ancestor element to look for
elem - the start element
Returns:
the desired ancestor Element or null, if it could not be found

removeNode

public static void removeNode(org.w3c.dom.Node node)
                       throws java.lang.Exception
Removes (only) the specified node from the tree, but not its children. They are moved to the deleted node's former place instead.

Parameters:
node - the node to remove from the tree
Throws:
java.lang.Exception

moveChildren

public static void moveChildren(org.w3c.dom.Node from,
                                org.w3c.dom.Node to)
                         throws java.lang.Exception
Moves all children of the first node to the second node.

Parameters:
from - the node from which the children should be moved
to - the node to which the children are moved
Throws:
java.lang.Exception

makeValidXML_ID

public static final java.lang.String makeValidXML_ID(java.lang.String src)
Transform a passed string in such a way that the result conforms to the rules for an XML 1.0 'Name' (as used e.g. for element names or xml:id). Invalid characters are replaced by '-' if within the name, and prepended by an additional '_' when at the beginning. This is essentially the same method as makeValidXMLName(), but additionally allows the colon character ':' .

Important Note: This method does not guarantee that for different source strings the output will be different as well! For example, both 1elem and _1elem will be converted to _1elem: The first will be modified by prepending a '_', while the second is already a valid XML Name and therefore passed unchanged.

Parameters:
src - the source string
Returns:
a string that matches the XML 1.0 Name production.

makeValidXMLName

public static java.lang.String makeValidXMLName(java.lang.String src)
Transform a passed string in such a way that the result conforms to the rules for an XML 1.0 'Name' (as used e.g. for element names or xml:id). Invalid characters are replaced by '-' if within the name, and prepended by an additional '_' when at the beginning. Also note that a starting colon ':' will not be allowed by this function, as it has special meaning in XML Namespaces. Therefore, consider the passed string as only the local name of an element.

Important Note: This method does not guarantee that for different source strings the output will be different as well! For example, both 1elem and _1elem will be converted to _1elem: The first will be modified by prepending a '_', while the second is already a valid XML Name and therefore passed unchanged.

Parameters:
src - the source string
Returns:
a string that matches the XML 1.0 Name production.