All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.saxon.instruct.SavedNamespaceContext Maven / Gradle / Ivy

package net.sf.saxon.instruct;
import net.sf.saxon.om.NamePool;
import net.sf.saxon.om.NamespaceConstant;
import net.sf.saxon.om.NamespaceResolver;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;

/**
  * An object representing a list of Namespaces. Used when the namespace
  * controller in the stylesheet needs to be kept for use at run-time. The list of namespaces
  * is maintained in the form of numeric prefix/uri codes, which are only meaningful
  * in the context of a name pool
  */

public final class SavedNamespaceContext implements Serializable, NamespaceResolver {

    // TODO: static context can't vary within an XPath expression. Therefore, save the
    // NamespaceContext at the outermost expression level if any subexpression needs it.
    // (Or put a namespace context expression on the expression tree, which has no effect
    // at run-time, but is available to descendant expressions).

    private int[] namespaceCodes;
    private NamePool namePool;

    /**
     * Create a NamespaceContext object
     * @param nscodes an array of namespace codes. Each namespace code is an integer
     * in which the first 16 bits represent the prefix (zero if it's the default namespace)
     * and the next 16 bits represent the uri. These are codes held in the NamePool. The
     * list will be searched from the "high" end.
     * @param pool the namepool
    */

    public SavedNamespaceContext(int[] nscodes, NamePool pool) {
        namespaceCodes = nscodes;
        namePool = pool;
    }

    /**
     * Create a SavedNamespaceContext that captures all the information in a given NamespaceResolver
     * @param resolver the NamespaceResolver
     * @param pool the NamePool
     */

    public SavedNamespaceContext(NamespaceResolver resolver, NamePool pool) {
        namePool = pool;
        ArrayList list = new ArrayList();
        Iterator iter = resolver.iteratePrefixes();
        while (iter.hasNext()) {
            String prefix = (String)iter.next();
            String uri = resolver.getURIForPrefix(prefix, true);
            int nscode = pool.getNamespaceCode(prefix, uri);
            list.add(new Integer(nscode));
        }
        namespaceCodes = new int[list.size()];
        for (int i=0; i=0; i--) {
            // TODO: isn't it better to turn the prefix into a code and compare the codes?
            if (namePool.getPrefixFromNamespaceCode(namespaceCodes[i]).equals(prefix)) {
                return namePool.getURIFromNamespaceCode(namespaceCodes[i]);
            }
        }

        if (prefix.length() == 0) {
            // use the "default default namespace" - namely ""
            return "";
        } else {
            return null;
        }
    }

    /**
     * Get an iterator over all the prefixes declared in this namespace context. This will include
     * the default namespace (prefix="") and the XML namespace where appropriate
     */

    public Iterator iteratePrefixes() {
        ArrayList prefixes = new ArrayList(namespaceCodes.length);
        for (int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy