Class 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
    • 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 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 - post-translation 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 - 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 number
        prefix -
      • 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 (reference kept until namespaces are closed, values may be modified by this method)
        prefs - array of namespace prefixes mapped by this element (no null 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 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
      • 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 interface IXMLWriter
        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 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. 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 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
      • 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)
      • 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 interface IXMLWriter
        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 interface IXMLWriter