KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  *
21  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
22  *
23  * Portions Copyright Apache Software Foundation.
24  */

25 package org.apache.taglibs.standard.tag.common.xml;
26
27 import java.util.Arrays JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 import javax.xml.namespace.NamespaceContext JavaDoc;
33 import javax.xml.XMLConstants JavaDoc;
34
35 /**
36   * Implemenation of XML Namespace context processing given a URI or prefix
37   */

38 public class JSTLXPathNamespaceContext implements NamespaceContext JavaDoc {
39
40     //*********************************************************************
41
// Constructor
42

43     /**
44      * No-arg constructor which would create empty HashMap of namespaces
45      */

46     public JSTLXPathNamespaceContext() {
47         namespaces = new HashMap JavaDoc();
48     }
49
50     public JSTLXPathNamespaceContext(HashMap JavaDoc nses) {
51         namespaces = nses;
52     }
53
54     /**
55      * The context to resolve the prefix from, if the context
56      * is not given.
57      */

58     HashMap JavaDoc namespaces;
59
60     /**
61      * Get Namespace URI bound to a prefix in the current scope
62      *
63      * @param Prefix Parameter
64      *
65      * @return Namespace URI bound to prefix
66      *
67      * @throws IllegalArgumentException if prefix is null
68      */

69     public String JavaDoc getNamespaceURI(String JavaDoc prefix)
70         throws IllegalArgumentException JavaDoc {
71         // p("[getNamespaceURI] prefix: " + prefix);
72
if (prefix == null) {
73             throw new IllegalArgumentException JavaDoc("Cannot get Namespace URI for null prefix");
74         }
75
76         if (prefix.equals(XMLConstants.XML_NS_PREFIX)) {
77             return XMLConstants.XML_NS_URI;
78         }
79         if (prefix.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
80             return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
81         }
82
83         String JavaDoc namespaceURI = (String JavaDoc)namespaces.get(prefix);
84         // p("[getNamespaceURI] namespaceURI: " + namespaceURI);
85
if (namespaceURI != null) {
86             return namespaceURI;
87         }
88
89         return XMLConstants.NULL_NS_URI;
90     }
91
92     /**
93      * Get Prefix bound to Namespace URI in the current scope
94      *
95      * @param Namespace URI
96      *
97      * @return Prefix bound to Namespace URI
98      *
99      * @throws IllegalArgumentException if Namespace URI is null
100      */

101     public String JavaDoc getPrefix(String JavaDoc namespaceURI) {
102         // p("[getPrefix] namespaceURI: " + namespaceURI);
103
if (namespaceURI == null) {
104             throw new IllegalArgumentException JavaDoc("Cannot get prefix for null NamespaceURI");
105         }
106
107         if (namespaceURI.equals(XMLConstants.XML_NS_URI)) {
108             return XMLConstants.XML_NS_PREFIX;
109         }
110         if (namespaceURI.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
111             return XMLConstants.XMLNS_ATTRIBUTE;
112         }
113
114         Iterator JavaDoc iter = namespaces.keySet().iterator();
115         while (iter.hasNext()) {
116             String JavaDoc key = (String JavaDoc)iter.next();
117             String JavaDoc value = (String JavaDoc)namespaces.get(key);
118             if (value.equals(namespaceURI)) {
119                 // p("[getPrefix] value: " + value);
120
return value;
121             }
122         }
123
124         // p("[getPrefix] returning null");
125
return null;
126     }
127
128     /**
129      * Get all Prefixes bound to Namespace URI in the current scope
130      *
131      * @param Namespace URI
132      *
133      * @return Iterator of Prefixes bound to Namespace URI
134      *
135      * @throws IllegalArgumentException if Namespace URI is null
136      */

137     public Iterator JavaDoc getPrefixes(String JavaDoc namespaceURI) {
138         // p("[getPrefixes] namespaceURI: " + namespaceURI);
139
if (namespaceURI == null) {
140             throw new IllegalArgumentException JavaDoc("Cannot get prefix for null NamespaceURI");
141         }
142
143         if (namespaceURI.equals(XMLConstants.XML_NS_URI)) {
144             return Arrays.asList(new String JavaDoc[] {XMLConstants.XML_NS_PREFIX}).iterator();
145         }
146         if (namespaceURI.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
147             return Arrays.asList(new String JavaDoc[] {XMLConstants.XMLNS_ATTRIBUTE}).iterator();
148         }
149
150         ArrayList JavaDoc prefixList = new ArrayList JavaDoc();
151         Iterator JavaDoc iter = namespaces.keySet().iterator();
152         while (iter.hasNext()) {
153             String JavaDoc key = (String JavaDoc)iter.next();
154             String JavaDoc value = (String JavaDoc)namespaces.get(key);
155             if (value.equals(namespaceURI)) {
156                 prefixList.add(key);
157             }
158         }
159
160         // p("[getPrefixes] prefixList: " + prefixList);
161
return prefixList.iterator();
162     }
163
164     /**
165      * Populate map of Prefix and NameSpace URI's entries
166      */

167     protected void addNamespace(String JavaDoc prefix, String JavaDoc uri ) {
168         namespaces.put(prefix, uri );
169     }
170
171     //*********************************************************************
172
// Utility methods
173

174     private static void p(String JavaDoc s) {
175         System.out.println("[JSTLXPathNameContext] " + s);
176     }
177 }
178
Popular Tags