KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > utils > PrefixResolverDefault


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 /*
17  * $Id: PrefixResolverDefault.java,v 1.9 2004/02/20 20:32:51 jycli Exp $
18  */

19 package com.sun.org.apache.xml.internal.utils;
20
21 import org.w3c.dom.NamedNodeMap JavaDoc;
22 import org.w3c.dom.Node JavaDoc;
23
24 /**
25  * This class implements a generic PrefixResolver that
26  * can be used to perform prefix-to-namespace lookup
27  * for the XPath object.
28  * @xsl.usage general
29  */

30 public class PrefixResolverDefault implements PrefixResolver
31 {
32
33   /**
34    * The context to resolve the prefix from, if the context
35    * is not given.
36    */

37   Node JavaDoc m_context;
38
39   /**
40    * Construct a PrefixResolverDefault object.
41    * @param xpathExpressionContext The context from
42    * which XPath expression prefixes will be resolved.
43    * Warning: This will not work correctly if xpathExpressionContext
44    * is an attribute node.
45    * @param xpathExpressionContext Node from which to start searching for a
46    * xmlns attribute that binds a prefix to a namespace (when the namespace
47    * context is not specified in the getNamespaceForPrefix call).
48    */

49   public PrefixResolverDefault(Node JavaDoc 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    */

62   public String JavaDoc getNamespaceForPrefix(String JavaDoc 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    */

77   public String JavaDoc getNamespaceForPrefix(String JavaDoc prefix,
78                                       org.w3c.dom.Node JavaDoc namespaceContext)
79   {
80
81     Node JavaDoc parent = namespaceContext;
82     String JavaDoc 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 JavaDoc nnm = parent.getAttributes();
101
102           for (int i = 0; i < nnm.getLength(); i++)
103           {
104             Node JavaDoc attr = nnm.item(i);
105             String JavaDoc aname = attr.getNodeName();
106             boolean isPrefix = aname.startsWith("xmlns:");
107
108             if (isPrefix || aname.equals("xmlns"))
109             {
110               int index = aname.indexOf(':');
111               String JavaDoc 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    */

135   public String JavaDoc getBaseIdentifier()
136   {
137     return null;
138   }
139     /**
140      * @see PrefixResolver#handlesNullPrefixes()
141      */

142     public boolean handlesNullPrefixes() {
143         return false;
144     }
145
146 }
147
Popular Tags