KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > instruct > SavedNamespaceContext


1 package net.sf.saxon.instruct;
2 import net.sf.saxon.om.NamePool;
3 import net.sf.saxon.om.NamespaceConstant;
4 import net.sf.saxon.om.NamespaceResolver;
5
6 import java.io.Serializable JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.Iterator JavaDoc;
9
10 /**
11   * An object representing a list of Namespaces. Used when the namespace
12   * controller in the stylesheet needs to be kept for use at run-time. The list of namespaces
13   * is maintained in the form of numeric prefix/uri codes, which are only meaningful
14   * in the controller of a name pool; however, in order to save space, the NamespaceContext
15   * object does not keep a reference to the namepool, it requires this to be supplied
16   * by the caller.
17   */

18
19 public final class SavedNamespaceContext implements Serializable JavaDoc, NamespaceResolver {
20
21     // TODO: static context can't vary within an XPath expression. Therefore, save the
22
// NamespaceContext at the outermost expression level if any subexpression needs it.
23
// (Or put a namespace context expression on the expression tree, which has no effect
24
// at run-time, but is available to descendant expressions).
25

26     private int[] namespaceCodes;
27     private NamePool namePool;
28
29     /**
30     * Create a NamespaceContext object
31     * @param nscodes an array of namespace codes. Each namespace code is an integer
32     * in which the first 16 bits represent the prefix (zero if it's the default namespace)
33     * and the next 16 bits represent the uri. These are codes held in the NamePool. The
34     * list will be searched from the "high" end.
35     */

36
37     public SavedNamespaceContext(int[] nscodes, NamePool pool) {
38         namespaceCodes = nscodes;
39         namePool = pool;
40     }
41
42     /**
43     * Get the list of in-scope namespaces held in this NamespaceContext
44     * @return the list of namespaces
45     */

46
47     public int[] getNamespaceCodes() {
48         return namespaceCodes;
49     }
50
51     /**
52     * Get the namespace URI corresponding to a given prefix. Return null
53     * if the prefix is not in scope.
54     * @param prefix the namespace prefix
55     * @param useDefault true if the default namespace is to be used when the
56     * prefix is ""
57     * @return the uri for the namespace, or null if the prefix is not in scope
58     */

59
60     public String JavaDoc getURIForPrefix(String JavaDoc prefix, boolean useDefault) {
61
62         if (prefix.equals("") && !useDefault) {
63             return "";
64         }
65
66         if (prefix.equals("xml")) {
67             return NamespaceConstant.XML;
68         }
69
70         for (int i=namespaceCodes.length-1; i>=0; i--) {
71             if (namePool.getPrefixFromNamespaceCode(namespaceCodes[i]).equals(prefix)) {
72                 return namePool.getURIFromNamespaceCode(namespaceCodes[i]);
73             }
74         }
75
76         if (prefix.equals("") && useDefault) {
77             // use the "default default namespace" - namely ""
78
return "";
79         } else {
80             return null;
81         }
82     }
83
84     /**
85      * Get an iterator over all the prefixes declared in this namespace context. This will include
86      * the default namespace (prefix="") and the XML namespace where appropriate
87      */

88
89     public Iterator JavaDoc iteratePrefixes() {
90         ArrayList JavaDoc prefixes = new ArrayList JavaDoc(namespaceCodes.length);
91         for (int i=0; i<namespaceCodes.length; i++) {
92             prefixes.add(namePool.getPrefixFromNamespaceCode(namespaceCodes[i]));
93         }
94         return prefixes.iterator();
95     }
96
97
98 }
99
100 //
101
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
102
// you may not use this file except in compliance with the License. You may obtain a copy of the
103
// License at http://www.mozilla.org/MPL/
104
//
105
// Software distributed under the License is distributed on an "AS IS" basis,
106
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
107
// See the License for the specific language governing rights and limitations under the License.
108
//
109
// The Original Code is: all this file.
110
//
111
// The Initial Developer of the Original Code is Michael H. Kay
112
//
113
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
114
//
115
// Contributor(s): none.
116
//
117
Popular Tags