FileDocCategorySizeDatePackage
NamespaceSupport.javaAPI DocApache Xerces 3.0.110214Fri Sep 14 20:33:54 BST 2007org.apache.xerces.util

NamespaceSupport

public class NamespaceSupport extends Object implements org.apache.xerces.xni.NamespaceContext
Namespace support for XML document handlers. This class doesn't perform any error checking and assumes that all strings passed as arguments to methods are unique symbols. The SymbolTable class can be used for this purpose.
author
Andy Clark, IBM
version
$Id: NamespaceSupport.java 447241 2006-09-18 05:12:57Z mrglavas $

Fields Summary
protected String[]
fNamespace
Namespace binding information. This array is composed of a series of tuples containing the namespace binding information: <prefix, uri>. The default size can be set to anything as long as it is a power of 2 greater than 1.
protected int
fNamespaceSize
The top of the namespace information array.
protected int[]
fContext
Context indexes. This array contains indexes into the namespace information array. The index at the current context is the start index of declared namespace bindings and runs to the size of the namespace information array.
protected int
fCurrentContext
The current context.
protected String[]
fPrefixes
Constructors Summary
public NamespaceSupport()
Default constructor.

    
    //
    // Constructors
    //

       
      
    
public NamespaceSupport(org.apache.xerces.xni.NamespaceContext context)
Constructs a namespace context object and initializes it with the prefixes declared in the specified context.

        pushContext();
        // copy declaration in the context
        Enumeration prefixes = context.getAllPrefixes();
        while (prefixes.hasMoreElements()){
            String prefix = (String)prefixes.nextElement();
            String uri = context.getURI(prefix);
            declarePrefix(prefix, uri);
        }
      
Methods Summary
public booleancontainsPrefix(java.lang.String prefix)
Checks whether a binding or unbinding for the given prefix exists in the context.

param
prefix The prefix to look up.
return
true if the given prefix exists in the context


        // find prefix in current context
        for (int i = fNamespaceSize; i > 0; i -= 2) {
            if (fNamespace[i - 2] == prefix) {
                return true;
            }
        }
        
        // prefix not found
        return false;
    
public booleandeclarePrefix(java.lang.String prefix, java.lang.String uri)

see
org.apache.xerces.xni.NamespaceContext#declarePrefix(String, String)

        // ignore "xml" and "xmlns" prefixes
        if (prefix == XMLSymbols.PREFIX_XML || prefix == XMLSymbols.PREFIX_XMLNS) {
            return false;
        }

        // see if prefix already exists in current context
        for (int i = fNamespaceSize; i > fContext[fCurrentContext]; i -= 2) {
            if (fNamespace[i - 2] == prefix) {
                // REVISIT: [Q] Should the new binding override the
                //          previously declared binding or should it
                //          it be ignored? -Ac
                // NOTE:    The SAX2 "NamespaceSupport" helper allows
                //          re-bindings with the new binding overwriting
                //          the previous binding. -Ac
                fNamespace[i - 1] = uri;
                return true;
            }
        }

        // resize array, if needed
        if (fNamespaceSize == fNamespace.length) {
            String[] namespacearray = new String[fNamespaceSize * 2];
            System.arraycopy(fNamespace, 0, namespacearray, 0, fNamespaceSize);
            fNamespace = namespacearray;
        }

        // bind prefix to uri in current context
        fNamespace[fNamespaceSize++] = prefix;
        fNamespace[fNamespaceSize++] = uri;

        return true;

    
public java.util.EnumerationgetAllPrefixes()

see
org.apache.xerces.xni.NamespaceContext#getAllPrefixes()

        int count = 0;
        if (fPrefixes.length < (fNamespace.length/2)) {
            // resize prefix array          
            String[] prefixes = new String[fNamespaceSize];
            fPrefixes = prefixes;
        }
        String prefix = null;
        boolean unique = true;
        for (int i = 2; i < (fNamespaceSize-2); i += 2) {
            prefix = fNamespace[i + 2];            
            for (int k=0;k<count;k++){
                if (fPrefixes[k]==prefix){
                    unique = false;
                    break;
                }               
            }
            if (unique){
                fPrefixes[count++] = prefix;
            }
            unique = true;
        }
		return new Prefixes(fPrefixes, count);
	
public java.lang.StringgetDeclaredPrefixAt(int index)

see
org.apache.xerces.xni.NamespaceContext#getDeclaredPrefixAt(int)

        return fNamespace[fContext[fCurrentContext] + index * 2];
    
public intgetDeclaredPrefixCount()

see
org.apache.xerces.xni.NamespaceContext#getDeclaredPrefixCount()

        return (fNamespaceSize - fContext[fCurrentContext]) / 2;
    
public java.lang.StringgetPrefix(java.lang.String uri)

see
org.apache.xerces.xni.NamespaceContext#getPrefix(String)


        // find uri in current context
        for (int i = fNamespaceSize; i > 0; i -= 2) {
            if (fNamespace[i - 1] == uri) {
                if (getURI(fNamespace[i - 2]) == uri)
                    return fNamespace[i - 2];
            }
        }

        // uri not found
        return null;

    
public java.lang.StringgetURI(java.lang.String prefix)

see
org.apache.xerces.xni.NamespaceContext#getURI(String)

        
        // find prefix in current context
        for (int i = fNamespaceSize; i > 0; i -= 2) {
            if (fNamespace[i - 2] == prefix) {
                return fNamespace[i - 1];
            }
        }

        // prefix not found
        return null;

    
public voidpopContext()

see
org.apache.xerces.xni.NamespaceContext#popContext()

        fNamespaceSize = fContext[fCurrentContext--];
    
public voidpushContext()

see
org.apache.xerces.xni.NamespaceContext#pushContext()


        // extend the array, if necessary
        if (fCurrentContext + 1 == fContext.length) {
            int[] contextarray = new int[fContext.length * 2];
            System.arraycopy(fContext, 0, contextarray, 0, fContext.length);
            fContext = contextarray;
        }

        // push context
        fContext[++fCurrentContext] = fNamespaceSize;

    
public voidreset()

see
org.apache.xerces.xni.NamespaceContext#reset()


        // reset namespace and context info
        fNamespaceSize = 0;
        fCurrentContext = 0;
        fContext[fCurrentContext] = fNamespaceSize;

        // bind "xml" prefix to the XML uri
        fNamespace[fNamespaceSize++] = XMLSymbols.PREFIX_XML;
        fNamespace[fNamespaceSize++] = NamespaceContext.XML_URI;
        // bind "xmlns" prefix to the XMLNS uri
        fNamespace[fNamespaceSize++] = XMLSymbols.PREFIX_XMLNS;
        fNamespace[fNamespaceSize++] = NamespaceContext.XMLNS_URI;
        ++fCurrentContext;