org.osesb.utilities.xml
Class DocumentNamespaceHelper

java.lang.Object
  extended by org.osesb.utilities.xml.DocumentNamespaceHelper

public class DocumentNamespaceHelper
extends java.lang.Object

This Class uses SAX and SAXNamespaceExtractorHandler to extract namespace(s) from XML using the folowing methods:

This Class is parser pool-aware.

Since:
Version .9

Field Summary
protected  boolean debugFlag
          The debugFlag for this Object.
protected static org.apache.commons.logging.Log log
          The logging implementation for this class.
protected  boolean parseWholeDocument
          If parseWholeDocument is true, DocumentNamespaceHelper will parse the whole document.
protected  SAXNamespaceExtractorHandler saxNamespaceExtractorHandler
          SAX handler that extracts all namespaces from a XML document.
protected  java.lang.String thisClassName
          The full name of this class.
 
Constructor Summary
DocumentNamespaceHelper()
          Creates a new instance of DocumentNamespaceHelper.
 
Method Summary
 Namespace[] getDeclaredNamespaces(java.lang.String xmlText)
          Get the namespace declartions for the XML as an array of org.osesb.utilities.xml.Namespace(s).
 java.lang.String getDefaultNamespaceURI(java.lang.String xmlText)
          Get the default namespace URI for the XML; that is, the namespace that is given by a root element 'xmlns' attribute with no prefix.
 java.lang.String getDocumentNamespaceURI(java.lang.String xmlText)
          Get the namespace for a XML document according to the following ordered lookup:

The default namespace, which is the namespace given by a root element 'xmlns' attribute with no prefix. The target namespace, which is the value of the 'targetNamespace' root attribute. The namespace portion of the value of a 'schemaLocation' root attribute.

 Namespace[] getNamespaces(java.lang.String xmlText)
          Get all namespaces associated with the XML as an array of org.osesb.utilities.xml.Namespace(s), including:

'xmlns' declarations. A 'targetNamespace' declaration. The namespace portion of a 'schemaLocation' declaration.

 boolean getParseWholeDocument()
          Get parseWholeDocument property.
 java.lang.String getSchemaLocationNamespaceURI(java.lang.String xmlText)
          Get the namespace portion of the value of a 'schemaLocation' root attribute.
 java.lang.String getTargetNamespaceURI(java.lang.String xmlText)
          Get the targtetNamespace URI for the XML; that is, the namespace that is the value of the 'targetNamespace' root attribute.
static void main(java.lang.String[] args)
          Main method for testing and sandboxing.
 void setDebugFlag(boolean debugFlag)
          Set the debugFlag for this object.
 void setParseWholeDocument(boolean parseWholeDocument)
          Set parseWholeDocument property.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected static org.apache.commons.logging.Log log
The logging implementation for this class.


thisClassName

protected java.lang.String thisClassName
The full name of this class.


saxNamespaceExtractorHandler

protected SAXNamespaceExtractorHandler saxNamespaceExtractorHandler
SAX handler that extracts all namespaces from a XML document.


parseWholeDocument

protected boolean parseWholeDocument
If parseWholeDocument is true, DocumentNamespaceHelper will parse the whole document. If parseWholeDocument is false, DocumentNamespaceHelper stops processing XML after processing the root.

Set parseWholeDocument to true to find namespace declarations in elements after the root or to cacth XML errors.

Default is false.


debugFlag

protected boolean debugFlag
The debugFlag for this Object.

Constructor Detail

DocumentNamespaceHelper

public DocumentNamespaceHelper()
Creates a new instance of DocumentNamespaceHelper.

Method Detail

getNamespaces

public Namespace[] getNamespaces(java.lang.String xmlText)
                          throws java.lang.Exception
Get all namespaces associated with the XML as an array of org.osesb.utilities.xml.Namespace(s), including:

Parameters:
xmlText - The XML text to extract the namespaces from.
Returns:
an array of org.osesb.utilities.xml.Namespace objects, one for each namespace found in the XML document.
Throws:
java.lang.Exception - if invoked with a null xmlText or if there is a problem parsing the XML.

getDeclaredNamespaces

public Namespace[] getDeclaredNamespaces(java.lang.String xmlText)
                                  throws java.lang.Exception
Get the namespace declartions for the XML as an array of org.osesb.utilities.xml.Namespace(s). These are the namespaces declared using a 'xmlns' attribute.

Parameters:
xmlText - The XML text to extract the namespaces from.
Returns:
an array of org.osesb.utilities.xml.Namespace objects, one for each declared namespace in the XML document. getNamespaceDeclarations() may return valid Namespace(s) after parsing completes.
Throws:
java.lang.Exception - if invoked with a null xmlText or if there is a problem parsing the XML.

getTargetNamespaceURI

public java.lang.String getTargetNamespaceURI(java.lang.String xmlText)
                                       throws java.lang.Exception
Get the targtetNamespace URI for the XML; that is, the namespace that is the value of the 'targetNamespace' root attribute.

Parameters:
xmlText - The XML text to extract the namespace from.
Returns:
the target namespace URI.
Throws:
java.lang.Exception - if invoked with a null xmlText or if there is a problem parsing the XML.

getDefaultNamespaceURI

public java.lang.String getDefaultNamespaceURI(java.lang.String xmlText)
                                        throws java.lang.Exception
Get the default namespace URI for the XML; that is, the namespace that is given by a root element 'xmlns' attribute with no prefix.

Parameters:
xmlText - The XML text to extract the namespace from.
Returns:
the default namespace URI defined by the root element.
Throws:
java.lang.Exception - if invoked with a null xmlText or if there is a problem parsing the XML.

getSchemaLocationNamespaceURI

public java.lang.String getSchemaLocationNamespaceURI(java.lang.String xmlText)
                                               throws java.lang.Exception
Get the namespace portion of the value of a 'schemaLocation' root attribute.

Parameters:
xmlText - The XML text to extract the namespace from.
Returns:
The namespace portion of the value of a 'schemaLocation' root attribute. getSchemaLocationNamespace() may return a valid namespaceURI after parsing completes.
Throws:
java.lang.Exception - if invoked with a null xmlText or if there is a problem parsing the XML.

getDocumentNamespaceURI

public java.lang.String getDocumentNamespaceURI(java.lang.String xmlText)
                                         throws java.lang.Exception
Get the namespace for a XML document according to the following ordered lookup:

  1. The default namespace, which is the namespace given by a root element 'xmlns' attribute with no prefix.
  2. The target namespace, which is the value of the 'targetNamespace' root attribute.
  3. The namespace portion of the value of a 'schemaLocation' root attribute.

Parameters:
xmlText - The XML text to extract the namespace from.
Returns:
the namespaceURI that is the best-fit namespace for the XML.
Throws:
java.lang.Exception - if invoked with a null xmlText or if there is a problem parsing the XML.

getParseWholeDocument

public boolean getParseWholeDocument()
Get parseWholeDocument property.

If parseWholeDocument is true, DocumentNamespaceHelper will parse the whole document. If parseWholeDocument is false, DocumentNamespaceHelper stops processing XML after processing the root.

Set parseWholeDocument to true to find namespace declarations in elements after the root or to cacth XML errors.

Default is false.

Returns:
parseWholeDocument property.

setParseWholeDocument

public void setParseWholeDocument(boolean parseWholeDocument)
Set parseWholeDocument property.

If parseWholeDocument is true, DocumentNamespaceHelper will parse the whole document. If parseWholeDocument is false, DocumentNamespaceHelper stops processing XML after processing the root.

Set parseWholeDocument to true to find namespace declarations in elements after the root or to cacth XML errors.

Default is false.

Parameters:
parseWholeDocument - property.

setDebugFlag

public void setDebugFlag(boolean debugFlag)
Set the debugFlag for this object.

Parameters:
debugFlag - If true, debug messages are logged.

main

public static void main(java.lang.String[] args)
Main method for testing and sandboxing.

Parameters:
args - The test data directory.