KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > om > InscopeNamespaceResolver


1 package net.sf.saxon.om;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6
7 /**
8  * A NamespaceResolver that resolves namespace prefixes by reference to a node in a document for which
9  * those namespaces are in-scope.
10  */

11 public class InscopeNamespaceResolver implements NamespaceResolver {
12
13     private NodeInfo node;
14
15     public InscopeNamespaceResolver(NodeInfo node) {
16         this.node = node;
17     }
18
19     /**
20      * Get the namespace URI corresponding to a given prefix. Return null
21      * if the prefix is not in scope.
22      *
23      * @param prefix the namespace prefix
24      * @param useDefault true if the default namespace is to be used when the
25      * prefix is ""
26      * @return the uri for the namespace, or null if the prefix is not in scope
27      * Return "" for the no-namespace.
28      */

29
30     public String JavaDoc getURIForPrefix(String JavaDoc prefix, boolean useDefault) {
31         if ("".equals(prefix) && !useDefault) {
32             return "";
33         }
34         AxisIterator iter = node.iterateAxis(Axis.NAMESPACE);
35         while (true) {
36             NodeInfo node = (NodeInfo)iter.next();
37             if (node == null) {
38                 break;
39             }
40             if (node.getLocalPart().equals(prefix)) {
41                 return node.getStringValue();
42             }
43         }
44         if ("".equals(prefix)) {
45             return "";
46         } else {
47             return null;
48         }
49     }
50
51     /**
52      * Get an iterator over all the prefixes declared in this namespace context. This will include
53      * the default namespace (prefix="") and the XML namespace where appropriate
54      */

55
56     public Iterator JavaDoc iteratePrefixes() {
57         List JavaDoc list = new ArrayList JavaDoc(16);
58         AxisIterator iter = node.iterateAxis(Axis.NAMESPACE);
59         while (true) {
60             NodeInfo node = (NodeInfo)iter.next();
61             if (node == null) {
62                 break;
63             }
64             list.add(node.getLocalPart());
65         }
66         return list.iterator();
67     }
68
69     /**
70      * Get the node on which this namespace resolver is based
71      */

72
73     public NodeInfo getNode() {
74         return node;
75     }
76 }
77
78
79 //
80
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
81
// you may not use this file except in compliance with the License. You may obtain a copy of the
82
// License at http://www.mozilla.org/MPL/
83
//
84
// Software distributed under the License is distributed on an "AS IS" basis,
85
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
86
// See the License for the specific language governing rights and limitations under the License.
87
//
88
// The Original Code is: all this file.
89
//
90
// The Initial Developer of the Original Code is Michael H. Kay
91
//
92
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
93
//
94
// Contributor(s): none
95
//
Popular Tags