Class NamespacesTable


  • @Deprecated(since="2022-01-27")
    public class NamespacesTable
    extends Object
    Deprecated.
    This API is deprecated, migrate code to the XML APIs provided by the JDK.
    Keeps track of namespaces declarations and resolve namespaces names.

    This class also provides a very convenient and safe way of handling namespace declarations in SAX pipes. It also allows to filter duplicate namespace declarations that too often clutter up XML documents that went through several transformations, and avoid useless namespace declarations that aren't followed by element events.

    Usage example in a SAX pipe:

        NamespacesTable namespaces = new NamespacesTable();
        ContentHandler nextHandler;
    
        public void startPrefixMapping(String prefix, String uri) throws SAXException {
            namespaces.addDeclaration(prefix, uri);
        }
    
        public void startElement(...) throws SAXException {
            // automatically start mappings for this scope
            namespaces.enterScope(nextHandler);
            nextHandler.startElement(...);
        }
    
        public void endElement(...) throws SAXException {
            nextHandler.endElement(...);
            // automatically end mappings for this scope
            namespaces.leaveScope(nextHandler);
        }
    
        public void endPrefixMapping(String prefix) throws SAXException {
            // Ignore, it is handled by leaveScope()
        }
      
    • Constructor Detail

      • NamespacesTable

        public NamespacesTable()
        Deprecated.
        Construct a new NamespacesTable instance.
    • Method Detail

      • clear

        public void clear()
        Deprecated.
        Clear and reinitialize this namespace table before reuse.
        Since:
        2.1.8
      • removeDeclaration

        public NamespacesTable.Declaration removeDeclaration​(String prefix)
        Deprecated.
        Undeclare a namespace prefix-uri mapping. If the prefix was previously declared mapping another URI, its value is restored.

        When using enterScope()/leaveScope(), this method does nothing and always returns null, as declaration removal is handled in leaveScope().

        Returns:
        the removed Declaration or null.
      • enterScope

        public void enterScope()
        Deprecated.
        Enter a new scope. This starts a new, empty list of declarations for the new scope.

        Typically called in a SAX handler before sending a startElement() event.

        Since:
        2.1.8
      • enterScope

        public void enterScope​(ContentHandler handler)
                        throws SAXException
        Deprecated.
        Start all declared mappings of the current scope and enter a new scope. This starts a new, empty list of declarations for the new scope.

        Typically called in a SAX handler before sending a startElement() event.

        Parameters:
        handler - the handler that will receive startPrefixMapping events.
        Throws:
        SAXException
        Since:
        2.1.8
      • leaveScope

        public void leaveScope()
        Deprecated.
        Leave a scope. The namespace declarations that occured before the corresponding enterScope() are no more visible using the resolution methods, but still available using getCurrentScopeDeclarations() until the next call to addDeclaration(String, String) or enterScope().

        Typically called in a SAX handler after sending a endElement() event.

        Since:
        2.1.8
      • leaveScope

        public void leaveScope​(ContentHandler handler)
                        throws SAXException
        Deprecated.
        Leave a scope. The namespace declarations that occured before the corresponding enterScope() are no more visible using the resolution methods, but still available using getCurrentScopeDeclarations() until the next call to addDeclaration(String, String) or enterScope().

        Typically called in a SAX handler after sending a endElement() event.

        Parameters:
        handler - the handler that will receive endPrefixMapping events.
        Throws:
        SAXException
        Since:
        2.1.8
      • getCurrentScopeDeclarations

        public NamespacesTable.Declaration[] getCurrentScopeDeclarations()
        Deprecated.
        Get the declarations that were declared within the current scope.
        Returns:
        the declarations (possibly empty, but never null)
        Since:
        2.1.8
      • getUri

        public String getUri​(String prefix)
        Deprecated.
        Return the URI associated with the given prefix or null if the prefix was not mapped.
      • getPrefixes

        public String[] getPrefixes​(String uri)
        Deprecated.
        Return an array with all prefixes currently mapped to the specified URI.
        The array length might be zero if no prefixes are associated with the specified uri.
        Returns:
        A non-null String array.
      • getPrefix

        public String getPrefix​(String uri)
        Deprecated.
        Return one of the prefixes currently mapped to the specified URI or null.
      • resolve

        public NamespacesTable.Name resolve​(String uri,
                                            String raw,
                                            String prefix,
                                            String local)
                                     throws SAXException
        Deprecated.
        Resolve a namespace-aware name against the current namespaces declarations.
        Parameters:
        uri - The namespace URI or null if not known.
        raw - The raw (complete) name or null if not known.
        prefix - The namespace prefix or null if not known.
        local - The local name or null if not known.
        Returns:
        A non-null Name.
        Throws:
        SAXException - If the name cannot be resolved.