1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the  "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 // $Id: JAXPPrefixResolver.java 468655 2006-10-28 07:12:06Z minchau $
19 
20 package org.apache.xpath.jaxp;
21 
22 import org.w3c.dom.Node;
23 import org.w3c.dom.NamedNodeMap;
24 import org.apache.xml.utils.PrefixResolver;
25 
26 import javax.xml.namespace.NamespaceContext;
27 
28 /**
29  * <meta name="usage" content="general"/>
30  * This class implements a Default PrefixResolver which
31  * can be used to perform prefix-to-namespace lookup
32  * for the XPath object.
33  * This class delegates the resolution to the passed NamespaceContext
34  */
35 public class JAXPPrefixResolver implements PrefixResolver
36 {
37 
38     private NamespaceContext namespaceContext;
39 
40 
JAXPPrefixResolver( NamespaceContext nsContext )41     public JAXPPrefixResolver ( NamespaceContext nsContext ) {
42         this.namespaceContext = nsContext;
43     }
44 
45 
getNamespaceForPrefix( String prefix )46     public String getNamespaceForPrefix( String prefix ) {
47         return namespaceContext.getNamespaceURI( prefix );
48     }
49 
50     /**
51      * Return the base identifier.
52      *
53      * @return null
54      */
getBaseIdentifier()55     public String getBaseIdentifier() {
56         return null;
57     }
58 
59     /**
60      * @see PrefixResolver#handlesNullPrefixes()
61      */
handlesNullPrefixes()62     public boolean handlesNullPrefixes() {
63         return false;
64     }
65 
66 
67     /**
68      * The URI for the XML namespace.
69      * (Duplicate of that found in org.apache.xpath.XPathContext).
70      */
71 
72     public static final String S_XMLNAMESPACEURI =
73         "http://www.w3.org/XML/1998/namespace";
74 
75 
76     /**
77      * Given a prefix and a Context Node, get the corresponding namespace.
78      * Warning: This will not work correctly if namespaceContext
79      * is an attribute node.
80      * @param prefix Prefix to resolve.
81      * @param namespaceContext Node from which to start searching for a
82      * xmlns attribute that binds a prefix to a namespace.
83      * @return Namespace that prefix resolves to, or null if prefix
84      * is not bound.
85      */
getNamespaceForPrefix(String prefix, org.w3c.dom.Node namespaceContext)86     public String getNamespaceForPrefix(String prefix,
87                                       org.w3c.dom.Node namespaceContext) {
88         Node parent = namespaceContext;
89         String namespace = null;
90 
91         if (prefix.equals("xml")) {
92             namespace = S_XMLNAMESPACEURI;
93         } else {
94             int type;
95 
96             while ((null != parent) && (null == namespace)
97                 && (((type = parent.getNodeType()) == Node.ELEMENT_NODE)
98                     || (type == Node.ENTITY_REFERENCE_NODE))) {
99 
100                 if (type == Node.ELEMENT_NODE) {
101                     NamedNodeMap nnm = parent.getAttributes();
102 
103                     for (int i = 0; i < nnm.getLength(); i++) {
104                         Node attr = nnm.item(i);
105                         String aname = attr.getNodeName();
106                         boolean isPrefix = aname.startsWith("xmlns:");
107 
108                         if (isPrefix || aname.equals("xmlns")) {
109                             int index = aname.indexOf(':');
110                             String p =isPrefix ?aname.substring(index + 1) :"";
111 
112                             if (p.equals(prefix)) {
113                                 namespace = attr.getNodeValue();
114                                 break;
115                             }
116                         }
117                     }
118                 }
119 
120                 parent = parent.getParentNode();
121             }
122         }
123         return namespace;
124     }
125 
126 }
127 
128