Package org.jibx.runtime.impl
Class XMLWriterNamespaceBase
- java.lang.Object
-
- 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.- Author:
- Dennis M. Sosnoski
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
XMLWriterNamespaceBase.DeclarationInfo
Namespace declaration tracking information.
-
Field Summary
Fields Modifier and Type Field Description 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.private int[]
m_translateTable
Current namespace translation table (null
if none).private java.util.Stack
m_translateTableStack
Stack of namespace translation tables in use.protected java.lang.String[]
m_uris
URIs for namespaces.
-
Constructor Summary
Constructors Constructor Description XMLWriterNamespaceBase(java.lang.String[] uris)
Constructor.XMLWriterNamespaceBase(XMLWriterNamespaceBase base, java.lang.String[] uris)
Copy constructor.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description 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.protected java.lang.String
internalNamespacePrefix(int index)
Get current prefix defined for translated namespace index.protected java.lang.String
internalNamespaceUri(int index)
Get URI for translated namespace index.protected void
internalSetUris(java.lang.String[] uris)
Set namespace URIs.int[]
openNamespaces(int[] nums, java.lang.String[] prefs)
Open the specified namespaces.void
popExtensionNamespaces()
Remove extension namespace URIs.void
popTranslationTable()
Pop a translation table used for converting namespace index numbers to values matching the internal lookup.void
pushExtensionNamespaces(java.lang.String[] uris)
Append extension namespace URIs to those in mapping.void
pushTranslationTable(int[] table)
Push a translation table to be used for converting namespace index numbers passed as arguments to values used for internal lookup.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.int
translateNamespace(int index)
Translate a namespace index number to match internal tables.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, init, 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).
-
m_translateTable
private int[] m_translateTable
Current namespace translation table (null
if none).
-
m_translateTableStack
private java.util.Stack m_translateTableStack
Stack of namespace translation tables 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 instanceuris
- 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
- post-translation namespace URI index numberprefix
- 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
- post-translation namespace URI index number
-
internalSetUris
protected void internalSetUris(java.lang.String[] uris)
Set namespace URIs. It is provided so that subclasses can implement easy reuse.- Parameters:
uris
- ordered array of URIs for namespaces used in document
-
setNamespacePrefix
private void setNamespacePrefix(int index, java.lang.String prefix)
Set prefix for namespace.- Parameters:
index
- post-translation namespace URI index numberprefix
-
-
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 interfaceIXMLWriter
- Parameters:
nums
- array of namespace indexes defined by this element (reference kept until namespaces are closed, values may be modified by this method)prefs
- array of namespace prefixes mapped by this element (nonull
values, use "" for default namespace declaration)- Returns:
- array of translated 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 interfaceIXMLWriter
- 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 interfaceIXMLWriter
- 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 interfaceIXMLWriter
-
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 interfaceIXMLWriter
- Returns:
- array of namespaces
-
internalNamespaceUri
protected final java.lang.String internalNamespaceUri(int index)
Get URI for translated namespace index.- Parameters:
index
- namespace URI index number (post-translation)- Returns:
- namespace URI text, or
null
if the namespace index is invalid
-
getNamespaceUri
public final java.lang.String getNamespaceUri(int index)
Get URI for namespace.- Specified by:
getNamespaceUri
in interfaceIXMLWriter
- Parameters:
index
- namespace URI index number- Returns:
- namespace URI text, or
null
if the namespace index is invalid
-
internalNamespacePrefix
protected final java.lang.String internalNamespacePrefix(int index)
Get current prefix defined for translated namespace index.- Parameters:
index
- namespace URI index number (post-translation)- Returns:
- current prefix text, or
null
if the namespace is not currently mapped
-
getNamespacePrefix
public final java.lang.String getNamespacePrefix(int index)
Get current prefix defined for namespace.- Specified by:
getNamespacePrefix
in interfaceIXMLWriter
- 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. If namespace translation is in use, the returned index will be the value prior to translation. This can be an expensive operation (regardless of whether translation is used or not) with time proportional to the number of namespaces defined, so it should be used with care.- Specified by:
getPrefixIndex
in interfaceIXMLWriter
- 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 interfaceIXMLWriter
- 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 usingpushExtensionNamespaces(java.lang.String[])
.- Specified by:
popExtensionNamespaces
in interfaceIXMLWriter
-
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 interfaceIXMLWriter
- Returns:
- array of arrays of extension namespaces (
null
if none)
-
translateNamespace
public int translateNamespace(int index)
Translate a namespace index number to match internal tables.- Parameters:
index
- raw namespace index- Returns:
- namespace index number for internal tables
-
pushTranslationTable
public void pushTranslationTable(int[] table)
Push a translation table to be used for converting namespace index numbers passed as arguments to values used for internal lookup. This allows a layer of indirection between the client code and the namespace definitions, designed for use in supporting precompiled bindings. The translated values must match the internal tables.- Specified by:
pushTranslationTable
in interfaceIXMLWriter
- Parameters:
table
- translation table to be used (null
if no change)
-
popTranslationTable
public void popTranslationTable()
Pop a translation table used for converting namespace index numbers to values matching the internal lookup.- Specified by:
popTranslationTable
in interfaceIXMLWriter
-
-