KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > tag > common > xml > JSTLPrefixResolver


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 package org.apache.taglibs.standard.tag.common.xml;
17
18 import java.util.HashMap JavaDoc;
19
20 import org.apache.xml.utils.PrefixResolver;
21 import org.w3c.dom.NamedNodeMap JavaDoc;
22 import org.w3c.dom.Node JavaDoc;
23
24 /**
25  * <meta name="usage" content="general"/>
26  * This class implements a JSTL PrefixResolver that
27  * can be used to perform prefix-to-namespace lookup
28  * for the XPath object.
29  */

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

36
37     HashMap JavaDoc namespaces;
38
39   /**
40    * The URI for the XML namespace.
41    * (Duplicate of that found in org.apache.xpath.XPathContext). */

42      
43   public static final String JavaDoc S_XMLNAMESPACEURI =
44     "http://www.w3.org/XML/1998/namespace";
45
46   /**
47    * No-arg constructor which would create empty HashMap of namespaces
48    */

49   public JSTLPrefixResolver()
50   {
51     namespaces = new HashMap JavaDoc();
52   }
53
54   public JSTLPrefixResolver( HashMap JavaDoc nses )
55   {
56     namespaces = nses;
57   }
58
59   /**
60    * Given a namespace, get the corresponding prefix. This assumes that
61    * the PrevixResolver hold's it's own namespace context, or is a namespace
62    * context itself.
63    * @param prefix Prefix to resolve.
64    * @return Namespace that prefix resolves to, or null if prefix
65    * is not bound.
66    */

67   public String JavaDoc getNamespaceForPrefix(String JavaDoc prefix)
68   {
69     return (String JavaDoc)namespaces.get(prefix);
70   }
71
72   /**
73    * Given a prefix and a Context Node, get the corresponding namespace.
74    * Warning: This will not work correctly if namespaceContext
75    * is an attribute node.
76    * @param prefix Prefix to resolve.
77    * @param namespaceContext Node from which to start searching for a
78    * xmlns attribute that binds a prefix to a namespace.
79    * @return Namespace that prefix resolves to, or null if prefix
80    * is not bound.
81    */

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

138   public String JavaDoc getBaseIdentifier()
139   {
140     return null;
141   }
142
143   /**
144    * @see PrefixResolver#handlesNullPrefixes() */

145   public boolean handlesNullPrefixes() {
146     return false;
147   }
148
149   public void addNamespace ( String JavaDoc prefix, String JavaDoc uri ) {
150     namespaces.put( prefix, uri );
151   }
152
153 }
154
Popular Tags