org.jibx.runtime.impl
Class XMLWriterNamespaceBase

java.lang.Object
  extended by org.jibx.runtime.impl.XMLWriterNamespaceBase
All Implemented Interfaces:
IXMLWriter
Direct Known Subclasses:
JDOMWriter, StAXWriter, XMLWriterBase

public abstract class XMLWriterNamespaceBase
extends java.lang.Object
implements IXMLWriter

Base implementation of XML writer interface namespace handling. This tracks only the namespace declarations and the element nesting depth. It can be used as a base class for all forms of output.

Version:
1.0
Author:
Dennis M. Sosnoski

Nested Class Summary
private static class XMLWriterNamespaceBase.DeclarationInfo
          Namespace declaration tracking information.
 
Field Summary
private static int[] EMPTY_INT_ARRAY
          Empty array for default return.
private  java.lang.String[][] m_extensionPrefixes
          Extension namespace prefixes (null if not in use).
private  java.lang.String[][] m_extensionUris
          Extension namespace URIs (null if not in use).
private  int m_namespaceDepth
          Depth of top namespace declaration level.
private  java.util.Stack m_namespaceStack
          Stack of information for namespace declarations.
private  int m_nestingDepth
          Depth of nested tags.
protected  java.lang.String[] m_prefixes
          Prefixes currently defined for namespaces.
protected  java.lang.String[] m_uris
          URIs for namespaces.
 
Constructor Summary
XMLWriterNamespaceBase(java.lang.String[] uris)
          Constructor.
XMLWriterNamespaceBase(XMLWriterNamespaceBase base, java.lang.String[] uris)
          Copy constructor.
 
Method Summary
private  void closeNamespaces()
          Ends the current innermost set of nested namespace definitions.
protected  void decrementNesting()
          Decrement the current nesting depth.
protected abstract  void defineNamespace(int index, java.lang.String prefix)
          Report to subclass that namespace has been defined.
 java.lang.String[][] getExtensionNamespaces()
          Get extension namespace URIs added to those in mapping.
 int getNamespaceCount()
          Get the number of namespaces currently defined.
 java.lang.String getNamespacePrefix(int index)
          Get current prefix defined for namespace.
 java.lang.String[] getNamespaces()
          Get namespace URIs for mapping.
 java.lang.String getNamespaceUri(int index)
          Get URI for namespace.
 int getNestingDepth()
          Get the current element nesting depth.
 int getPrefixIndex(java.lang.String prefix)
          Get index of namespace mapped to prefix.
protected static java.lang.String[][] growArray(java.lang.String[][] base, java.lang.String[] items)
          Grow array of array of strings.
protected  void incrementNesting()
          Increment the current nesting depth.
 int[] openNamespaces(int[] nums, java.lang.String[] prefs)
          Open the specified namespaces.
 void popExtensionNamespaces()
          Remove extension namespace URIs.
 void pushExtensionNamespaces(java.lang.String[] uris)
          Append extension namespace URIs to those in mapping.
 void reset()
          Reset to initial state for reuse.
private  void setNamespacePrefix(int index, java.lang.String prefix)
          Set prefix for namespace.
protected static java.lang.String[][] shrinkArray(java.lang.String[][] base)
          Shrink array of array of strings.
protected abstract  void undefineNamespace(int index)
          Report to subclass that namespace has been undefined.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.jibx.runtime.IXMLWriter
addAttribute, close, closeEmptyTag, closeStartTag, endTag, flush, indent, setIndentSpaces, startTagClosed, startTagNamespaces, startTagOpen, writeCData, writeComment, writeDocType, writeEntityRef, writePI, writeTextContent, writeXMLDecl
 

Field Detail

EMPTY_INT_ARRAY

private static final int[] EMPTY_INT_ARRAY
Empty array for default return.


m_uris

protected java.lang.String[] m_uris
URIs for namespaces.


m_prefixes

protected java.lang.String[] m_prefixes
Prefixes currently defined for namespaces.


m_nestingDepth

private int m_nestingDepth
Depth of nested tags.


m_namespaceStack

private java.util.Stack m_namespaceStack
Stack of information for namespace declarations.


m_namespaceDepth

private int m_namespaceDepth
Depth of top namespace declaration level.


m_extensionUris

private java.lang.String[][] m_extensionUris
Extension namespace URIs (null if not in use).


m_extensionPrefixes

private java.lang.String[][] m_extensionPrefixes
Extension namespace prefixes (null if not in use).

Constructor Detail

XMLWriterNamespaceBase

public XMLWriterNamespaceBase(java.lang.String[] uris)
Constructor.

Parameters:
uris - ordered array of URIs for namespaces used in document (must be constant; the value in position 0 must always be the empty string "", and the value in position 1 must always be the XML namespace "http://www.w3.org/XML/1998/namespace")

XMLWriterNamespaceBase

public XMLWriterNamespaceBase(XMLWriterNamespaceBase base,
                              java.lang.String[] uris)
Copy constructor. This initializes the extension namespace information from an existing instance.

Parameters:
base - existing instance
uris - ordered array of URIs for namespaces used in document
Method Detail

defineNamespace

protected abstract void defineNamespace(int index,
                                        java.lang.String prefix)
                                 throws java.io.IOException
Report to subclass that namespace has been defined.

Parameters:
index - namespace URI index number
prefix - prefix used for namespace
Throws:
java.io.IOException - if error writing to document

undefineNamespace

protected abstract void undefineNamespace(int index)
Report to subclass that namespace has been undefined.

Parameters:
index - namespace URI index number

setNamespacePrefix

private void setNamespacePrefix(int index,
                                java.lang.String prefix)
Set prefix for namespace.

Parameters:
index - namespace URI index number

openNamespaces

public int[] openNamespaces(int[] nums,
                            java.lang.String[] prefs)
                     throws java.io.IOException
Open the specified namespaces. Previously active namespace declarations are not duplicated.

Specified by:
openNamespaces in interface IXMLWriter
Parameters:
nums - array of namespace indexes defined by this element (must be constant, reference is kept until end of element)
prefs - array of namespace prefixes mapped by this element (no null values, use "" for default namespace declaration)
Returns:
array of indexes for namespaces not previously active (the ones actually needing to be declared, in the case of text output)
Throws:
java.io.IOException - on error writing to document

closeNamespaces

private void closeNamespaces()
Ends the current innermost set of nested namespace definitions. Reverts the namespaces involved to their previously-declared prefixes, and sets up for ending the new innermost set.


getNestingDepth

public final int getNestingDepth()
Get the current element nesting depth. Elements are only counted in the depth returned when they're officially open - after the start tag has been output and before the end tag has been output.

Specified by:
getNestingDepth in interface IXMLWriter
Returns:
number of nested elements at current point in output

getNamespaceCount

public final int getNamespaceCount()
Get the number of namespaces currently defined. This is equivalent to the index of the next extension namespace added.

Specified by:
getNamespaceCount in interface IXMLWriter
Returns:
namespace count

incrementNesting

protected void incrementNesting()
Increment the current nesting depth. Subclasses need to call this method whenever an element start tag is written.


decrementNesting

protected void decrementNesting()
Decrement the current nesting depth. Subclasses need to call this method whenever an element end tag is written.


reset

public void reset()
Reset to initial state for reuse. Subclasses overriding this method need to call this base class implementation during their processing.

Specified by:
reset in interface IXMLWriter

getNamespaces

public final java.lang.String[] getNamespaces()
Get namespace URIs for mapping. This gets the full ordered array of namespaces known in the binding used for this marshalling, where the index number of each namespace URI is the namespace index used to lookup the prefix when marshalling a name in that namespace. The returned array must not be modified.

Specified by:
getNamespaces in interface IXMLWriter
Returns:
array of namespaces

getNamespaceUri

public final java.lang.String getNamespaceUri(int index)
Get URI for namespace.

Specified by:
getNamespaceUri in interface IXMLWriter
Parameters:
index - namespace URI index number
Returns:
namespace URI text, or null if the namespace index is invalid

getNamespacePrefix

public final java.lang.String getNamespacePrefix(int index)
Get current prefix defined for namespace.

Specified by:
getNamespacePrefix in interface IXMLWriter
Parameters:
index - namespace URI index number
Returns:
current prefix text, or null if the namespace is not currently mapped

getPrefixIndex

public final int getPrefixIndex(java.lang.String prefix)
Get index of namespace mapped to prefix. This can be an expensive operation with time proportional to the number of namespaces defined, so it should be used with care.

Specified by:
getPrefixIndex in interface IXMLWriter
Parameters:
prefix - text to match (non-null, use "" for default prefix)
Returns:
index namespace URI index number mapped to prefix

growArray

protected static java.lang.String[][] growArray(java.lang.String[][] base,
                                                java.lang.String[] items)
Grow array of array of strings.

Parameters:
base - array to be grown (null is treated as zero length)
items - array of strings to be added at end of base array
Returns:
array with added array of items

shrinkArray

protected static java.lang.String[][] shrinkArray(java.lang.String[][] base)
Shrink array of array of strings.

Parameters:
base - array to be shrunk
Returns:
array with last set of items eliminated (null if empty)

pushExtensionNamespaces

public void pushExtensionNamespaces(java.lang.String[] uris)
Append extension namespace URIs to those in mapping.

Specified by:
pushExtensionNamespaces in interface IXMLWriter
Parameters:
uris - namespace URIs to extend those in mapping

popExtensionNamespaces

public void popExtensionNamespaces()
Remove extension namespace URIs. This removes the last set of extension namespaces pushed using pushExtensionNamespaces(java.lang.String[]).

Specified by:
popExtensionNamespaces in interface IXMLWriter

getExtensionNamespaces

public final java.lang.String[][] getExtensionNamespaces()
Get extension namespace URIs added to those in mapping. This gets the current set of extension definitions. The returned arrays must not be modified.

Specified by:
getExtensionNamespaces in interface IXMLWriter
Returns:
array of arrays of extension namespaces (null if none)


Project Web Site