1 /*
2  * reserved comment block
3  * DO NOT REMOVE OR ALTER!
4  */
5 /*
6  * Licensed to the Apache Software Foundation (ASF) under one or more
7  * contributor license agreements.  See the NOTICE file distributed with
8  * this work for additional information regarding copyright ownership.
9  * The ASF licenses this file to You under the Apache License, Version 2.0
10  * (the "License"); you may not use this file except in compliance with
11  * the License.  You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 package com.sun.org.apache.xml.internal.utils;
23 
24 import org.w3c.dom.NamedNodeMap;
25 import org.w3c.dom.Node;
26 
27 /**
28  * This class implements a generic PrefixResolver that
29  * can be used to perform prefix-to-namespace lookup
30  * for the XPath object.
31  * @xsl.usage general
32  */
33 public class PrefixResolverDefault implements PrefixResolver
34 {
35 
36   /**
37    * The context to resolve the prefix from, if the context
38    * is not given.
39    */
40   Node m_context;
41 
42   /**
43    * Construct a PrefixResolverDefault object.
44    * @param xpathExpressionContext The context from
45    * which XPath expression prefixes will be resolved.
46    * Warning: This will not work correctly if xpathExpressionContext
47    * is an attribute node.
48    */
PrefixResolverDefault(Node xpathExpressionContext)49   public PrefixResolverDefault(Node xpathExpressionContext)
50   {
51     m_context = xpathExpressionContext;
52   }
53 
54   /**
55    * Given a namespace, get the corrisponding prefix.  This assumes that
56    * the PrevixResolver hold's it's own namespace context, or is a namespace
57    * context itself.
58    * @param prefix Prefix to resolve.
59    * @return Namespace that prefix resolves to, or null if prefix
60    * is not bound.
61    */
getNamespaceForPrefix(String prefix)62   public String getNamespaceForPrefix(String prefix)
63   {
64     return getNamespaceForPrefix(prefix, m_context);
65   }
66 
67   /**
68    * Given a namespace, get the corrisponding prefix.
69    * Warning: This will not work correctly if namespaceContext
70    * is an attribute node.
71    * @param prefix Prefix to resolve.
72    * @param namespaceContext Node from which to start searching for a
73    * xmlns attribute that binds a prefix to a namespace.
74    * @return Namespace that prefix resolves to, or null if prefix
75    * is not bound.
76    */
getNamespaceForPrefix(String prefix, org.w3c.dom.Node namespaceContext)77   public String getNamespaceForPrefix(String prefix,
78                                       org.w3c.dom.Node namespaceContext)
79   {
80 
81     Node parent = namespaceContext;
82     String namespace = null;
83 
84     if (prefix.equals("xml"))
85     {
86       namespace = Constants.S_XMLNAMESPACEURI;
87     }
88     else
89     {
90       int type;
91 
92       while ((null != parent) && (null == namespace)
93              && (((type = parent.getNodeType()) == Node.ELEMENT_NODE)
94                  || (type == Node.ENTITY_REFERENCE_NODE)))
95       {
96         if (type == Node.ELEMENT_NODE)
97         {
98                 if (parent.getNodeName().indexOf(prefix+":") == 0)
99                         return parent.getNamespaceURI();
100           NamedNodeMap nnm = parent.getAttributes();
101 
102           for (int i = 0; i < nnm.getLength(); i++)
103           {
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             {
110               int index = aname.indexOf(':');
111               String p = isPrefix ? aname.substring(index + 1) : "";
112 
113               if (p.equals(prefix))
114               {
115                 namespace = attr.getNodeValue();
116 
117                 break;
118               }
119             }
120           }
121         }
122 
123         parent = parent.getParentNode();
124       }
125     }
126 
127     return namespace;
128   }
129 
130   /**
131    * Return the base identifier.
132    *
133    * @return null
134    */
getBaseIdentifier()135   public String getBaseIdentifier()
136   {
137     return null;
138   }
139         /**
140          * @see PrefixResolver#handlesNullPrefixes()
141          */
handlesNullPrefixes()142         public boolean handlesNullPrefixes() {
143                 return false;
144         }
145 
146 }
147