KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xpath > internal > jaxp > JAXPPrefixResolver


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 // $Id: JAXPPrefixResolver.java,v 1.2 2004/07/10 03:27:20 rameshm Exp $
17

18 package com.sun.org.apache.xpath.internal.jaxp;
19
20 import org.w3c.dom.Node JavaDoc;
21 import org.w3c.dom.NamedNodeMap JavaDoc;
22 import com.sun.org.apache.xml.internal.utils.PrefixResolver;
23
24 import javax.xml.namespace.NamespaceContext JavaDoc;
25
26 /**
27  * <meta name="usage" content="general"/>
28  * This class implements a Default PrefixResolver which
29  * can be used to perform prefix-to-namespace lookup
30  * for the XPath object.
31  * This class delegates the resolution to the passed NamespaceContext
32  */

33 public class JAXPPrefixResolver implements PrefixResolver
34 {
35
36     private NamespaceContext JavaDoc namespaceContext;
37     
38
39     public JAXPPrefixResolver ( NamespaceContext JavaDoc nsContext ) {
40         this.namespaceContext = nsContext;
41     }
42
43
44     public String JavaDoc getNamespaceForPrefix( String JavaDoc prefix ) {
45         return namespaceContext.getNamespaceURI( prefix );
46     }
47
48     /**
49      * Return the base identifier.
50      *
51      * @return null
52      */

53     public String JavaDoc getBaseIdentifier() {
54         return null;
55     }
56
57     /**
58      * @see PrefixResolver#handlesNullPrefixes()
59      */

60     public boolean handlesNullPrefixes() {
61         return false;
62     }
63
64
65     /**
66      * The URI for the XML namespace.
67      * (Duplicate of that found in com.sun.org.apache.xpath.internal.XPathContext).
68      */

69      
70     public static final String JavaDoc S_XMLNAMESPACEURI =
71         "http://www.w3.org/XML/1998/namespace";
72
73
74     /**
75      * Given a prefix and a Context Node, get the corresponding namespace.
76      * Warning: This will not work correctly if namespaceContext
77      * is an attribute node.
78      * @param prefix Prefix to resolve.
79      * @param namespaceContext Node from which to start searching for a
80      * xmlns attribute that binds a prefix to a namespace.
81      * @return Namespace that prefix resolves to, or null if prefix
82      * is not bound.
83      */

84     public String JavaDoc getNamespaceForPrefix(String JavaDoc prefix,
85                                       org.w3c.dom.Node JavaDoc namespaceContext) {
86         Node JavaDoc parent = namespaceContext;
87         String JavaDoc namespace = null;
88
89         if (prefix.equals("xml")) {
90             namespace = S_XMLNAMESPACEURI;
91         } else {
92             int type;
93
94             while ((null != parent) && (null == namespace)
95                 && (((type = parent.getNodeType()) == Node.ELEMENT_NODE)
96                     || (type == Node.ENTITY_REFERENCE_NODE))) {
97
98                 if (type == Node.ELEMENT_NODE) {
99                     NamedNodeMap JavaDoc nnm = parent.getAttributes();
100
101                     for (int i = 0; i < nnm.getLength(); i++) {
102                         Node JavaDoc attr = nnm.item(i);
103                         String JavaDoc aname = attr.getNodeName();
104                         boolean isPrefix = aname.startsWith("xmlns:");
105
106                         if (isPrefix || aname.equals("xmlns")) {
107                             int index = aname.indexOf(':');
108                             String JavaDoc p =isPrefix ?aname.substring(index + 1) :"";
109
110                             if (p.equals(prefix)) {
111                                 namespace = attr.getNodeValue();
112                                 break;
113                             }
114                         }
115                     }
116                 }
117
118                 parent = parent.getParentNode();
119             }
120         }
121         return namespace;
122     }
123
124 }
125
126
Popular Tags