KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > xpath > NamespaceContextImpl


1 package net.sf.saxon.xpath;
2
3 import net.sf.saxon.om.NamespaceResolver;
4
5 import javax.xml.namespace.NamespaceContext JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.List JavaDoc;
9
10 /**
11  * This class bridges between the JAXP 1.3 NamespaceContext interface and Saxon's
12  * equivalent NamespaceResolver interface. It allows any implementation of the Saxon
13  * NamespaceResolver to be wrapped as a JAXP NamespaceContext.
14  */

15
16 public class NamespaceContextImpl implements NamespaceContext JavaDoc, NamespaceResolver {
17
18     NamespaceResolver resolver;
19
20     /**
21      * Constructor: wrap a Saxon NamespaceResolver as a JAXP NamespaceContext
22      * @param resolver the Saxon NamespaceResolver
23      */

24
25     public NamespaceContextImpl(NamespaceResolver resolver) {
26         this.resolver = resolver;
27     }
28
29     /**
30     * Get the namespace URI corresponding to a given prefix. Return null
31     * if the prefix is not in scope.
32     * @param prefix the namespace prefix
33     * @param useDefault true if the default namespace is to be used when the
34     * prefix is ""
35     * @return the uri for the namespace, or null if the prefix is not in scope
36     */

37
38     public String JavaDoc getURIForPrefix(String JavaDoc prefix, boolean useDefault) {
39         return resolver.getURIForPrefix(prefix, useDefault);
40     }
41
42     /**
43      * Get an iterator over all the prefixes declared in this namespace context. This will include
44      * the default namespace (prefix="") and the XML namespace where appropriate
45      */

46
47     public Iterator JavaDoc iteratePrefixes() {
48         return resolver.iteratePrefixes();
49     }
50
51     /**
52      * Implement the JAXP getNamespaceURI() method in terms of the Saxon-specific methods
53      * @param prefix a namespace prefix
54      * @return the corresponding URI, if the prefix is bound, or "" otherwise
55      */

56
57     public String JavaDoc getNamespaceURI(String JavaDoc prefix) {
58         if (prefix.equals("xmlns")) {
59             return "http://www.w3.org/2000/xmlns/";
60         }
61         return resolver.getURIForPrefix(prefix, true);
62     }
63
64     /**
65      * Get the prefix bound to a particular namespace URI, if there is one, or null if not (JAXP method)
66      * @param uri the namespace URI
67      * @return the prefix bound to the URI if there is one, or null if not
68      */

69
70     public String JavaDoc getPrefix(String JavaDoc uri) {
71         Iterator JavaDoc prefixes = iteratePrefixes();
72         while (prefixes.hasNext()) {
73             String JavaDoc p = (String JavaDoc)prefixes.next();
74             String JavaDoc u = resolver.getURIForPrefix(p, true);
75             if (u.equals(uri)) {
76                 return p;
77             }
78         }
79         return null;
80     }
81
82     /**
83      * Get all the prefixes mapped to a given namespace URI (JAXP method)
84      * @param uri the namespace URI
85      * @return an iterator over all the prefixes bound to this namespace URI
86      */

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