KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > util > NamespaceSupport


1 /*
2  * Copyright 2003,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 package org.apache.ws.jaxme.util;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import javax.xml.XMLConstants JavaDoc;
24 import javax.xml.namespace.NamespaceContext JavaDoc;
25
26
27 /** <p>Similar to org.xml.sax.NamespaceSupport, but
28  * for marshalling and not for parsing XML.</p>
29  *
30  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
31  * @version $Id: NamespaceSupport.java,v 1.6 2005/03/10 10:14:03 jochen Exp $
32  */

33 public class NamespaceSupport implements NamespaceContext JavaDoc {
34     List JavaDoc prefixList;
35     String JavaDoc cachedURI, cachedPrefix;
36     
37     /** <p>Creates a new instance of NamespaceSupport.</p> */
38     public NamespaceSupport() {
39     }
40     
41     /** <p>Resets the NamespaceSupport's state for reuse.</p>
42      */

43     public void reset() {
44         cachedURI = cachedPrefix = null;
45         if (prefixList != null) {
46             prefixList.clear();
47         }
48     }
49     
50     /** <p>Declares a new prefix.</p>
51      */

52     public void declarePrefix(String JavaDoc pPrefix, String JavaDoc pURI) {
53         if (pURI == null) { pURI = ""; }
54         if (cachedURI == null) {
55             cachedURI = pURI;
56             cachedPrefix = pPrefix;
57         } else {
58             if (prefixList == null) { prefixList = new ArrayList JavaDoc(); }
59             prefixList.add(cachedPrefix);
60             prefixList.add(cachedURI);
61             cachedPrefix = pPrefix;
62             cachedURI = pURI;
63         }
64     }
65
66     /** <p>Removes a prefix declaration. Assumes that the prefix is the
67      * current prefix. If not, throws a IllegalStateException.</p>
68      */

69     public void undeclarePrefix(String JavaDoc pPrefix) {
70         if (pPrefix.equals(cachedPrefix)) {
71             if (prefixList != null && prefixList.size() > 0) {
72                 cachedURI = prefixList.remove(prefixList.size()-1).toString();
73                 cachedPrefix = prefixList.remove(prefixList.size()-1).toString();
74             } else {
75                 cachedPrefix = cachedURI = null;
76             }
77         } else {
78             for (int i = prefixList.size()-2; i >= 0; i -= 2) {
79                 if (pPrefix.equals(prefixList.get(i))) {
80                     prefixList.remove(i);
81                     prefixList.remove(i);
82                     return;
83                 }
84             }
85             throw new IllegalStateException JavaDoc("Undeclared prefix: " + pPrefix);
86         }
87     }
88     
89     /** <p>Given a prefix, returns the URI to which the prefix is
90      * currently mapped or null, if there is no such mapping.</p>
91      * <p><em>Note</em>: This methods behaviour is precisely
92      * defined by {@link NamespaceContext#getNamespaceURI(java.lang.String)}.</p>
93      *
94      * @param pPrefix The prefix in question
95      */

96     public String JavaDoc getNamespaceURI(String JavaDoc pPrefix) {
97         if (pPrefix == null) {
98             throw new IllegalArgumentException JavaDoc("Namespace prefix must not be null");
99         }
100         if (cachedURI != null) {
101             if (cachedPrefix.equals(pPrefix)) { return cachedURI; }
102             if (prefixList != null) {
103                 for (int i = prefixList.size(); i > 0; i -= 2) {
104                     if (pPrefix.equals(prefixList.get(i-2))) {
105                         return (String JavaDoc) prefixList.get(i-1);
106                     }
107                 }
108             }
109         }
110         if (XMLConstants.XML_NS_PREFIX.equals(pPrefix)) {
111             return XMLConstants.XML_NS_URI;
112         } else if (XMLConstants.XMLNS_ATTRIBUTE.equals(pPrefix)) {
113             return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
114         }
115         return null;
116     }
117     
118     /** <p>Returns a prefix currently mapped to the given URI or
119      * null, if there is no such mapping. This method may be used
120      * to find a possible prefix for an elements namespace URI. For
121      * attributes you should use {@link #getAttributePrefix(String)}.</p>
122      * <p><em>Note</em>: This methods behaviour is precisely
123      * defined by {@link NamespaceContext#getPrefix(java.lang.String)}.</p>
124      *
125      * @param pURI The namespace URI in question
126      * @throws IllegalArgumentException The namespace URI is null.
127      */

128     public String JavaDoc getPrefix(String JavaDoc pURI) {
129         if (pURI == null) {
130             throw new IllegalArgumentException JavaDoc("Namespace URI must not be null");
131         }
132         if (cachedURI != null) {
133             if (cachedURI.equals(pURI)) { return cachedPrefix; }
134             if (prefixList != null) {
135                 for (int i = prefixList.size(); i > 0; i -= 2) {
136                     if (pURI.equals(prefixList.get(i-1))) {
137                         return (String JavaDoc) prefixList.get(i-2);
138                     }
139                 }
140             }
141         }
142         if (XMLConstants.XML_NS_URI.equals(pURI)) {
143             return XMLConstants.XML_NS_PREFIX;
144         } else if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(pURI)) {
145             return XMLConstants.XMLNS_ATTRIBUTE;
146         }
147         return null;
148     }
149     
150     /** <p>Returns a non-empty prefix currently mapped to the given
151      * URL or null, if there is no such mapping. This method may be
152      * used to find a possible prefix for an attributes namespace
153      * URI. For elements you should use {@link #getPrefix(String)}.</p>
154      *
155      * @param pURI Thhe namespace URI in question
156      * @throws IllegalArgumentException The namespace URI is null.
157      */

158     public String JavaDoc getAttributePrefix(String JavaDoc pURI) {
159         if (pURI == null) {
160             throw new IllegalArgumentException JavaDoc("Namespace URI must not be null");
161         }
162         if (pURI.length() == 0) {
163             return "";
164         }
165         if (cachedURI != null) {
166             if (cachedURI.equals(pURI) && cachedPrefix.length() > 0) {
167                 return cachedPrefix;
168             }
169             if (prefixList != null) {
170                 for (int i = prefixList.size(); i > 0; i -= 2) {
171                     if (pURI.equals(prefixList.get(i-1))) {
172                         String JavaDoc prefix = (String JavaDoc) prefixList.get(i-2);
173                         if (prefix.length() > 0) {
174                             return prefix;
175                         }
176                     }
177                 }
178             }
179         }
180         if (XMLConstants.XML_NS_URI.equals(pURI)) {
181             return XMLConstants.XML_NS_PREFIX;
182         } else if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(pURI)) {
183             return XMLConstants.XMLNS_ATTRIBUTE;
184         }
185         return null;
186     }
187     
188     
189     
190     /** <p>Returns a collection to all prefixes bound to the given
191      * namespace URI.
192      * <p><em>Note</em>: This methods behaviour is precisely
193      * defined by {@link NamespaceContext#getPrefixes(java.lang.String)}.</p>
194      *
195      * @param pURI The namespace prefix in question
196      */

197     public Iterator JavaDoc getPrefixes(String JavaDoc pURI) {
198         if (pURI == null) {
199             throw new IllegalArgumentException JavaDoc("Namespace URI must not be null");
200         }
201         List JavaDoc list = new ArrayList JavaDoc();
202         if (cachedURI != null) {
203             if (cachedURI.equals(pURI)) { list.add(cachedPrefix); }
204             if (prefixList != null) {
205                 for (int i = prefixList.size(); i > 0; i -= 2) {
206                     if (pURI.equals(prefixList.get(i-1))) {
207                         list.add(prefixList.get(i-2));
208                     }
209                 }
210             }
211         }
212         if (pURI.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
213             list.add(XMLConstants.XMLNS_ATTRIBUTE);
214         } else if (pURI.equals(XMLConstants.XML_NS_URI)) {
215             list.add(XMLConstants.XML_NS_PREFIX);
216         }
217         return list.iterator();
218     }
219     
220     /** <p>Returns whether a given prefix is currently declared.</p>
221      */

222     public boolean isPrefixDeclared(String JavaDoc pPrefix) {
223         if (cachedURI != null) {
224             if (cachedPrefix != null && cachedPrefix.equals(pPrefix)) { return true; }
225             if (prefixList != null) {
226                 for (int i = prefixList.size(); i > 0; i -= 2) {
227                     if (prefixList.get(i-2).equals(pPrefix)) {
228                         return true;
229                     }
230                 }
231             }
232         }
233         return "xml".equals(pPrefix);
234     }
235
236     /** Returns the current number of assigned prefixes.
237      * Note, that a prefix may be assigned in several nested
238      * elements, in which case every assignment is counted.<br>
239      * This method is typically called before invoking the
240      * method
241      * {@link org.xml.sax.ContentHandler#startElement(String, String, String, org.xml.sax.Attributes)}.
242      * The return value is used as a saveable state. After
243      * invoking
244      * {@link org.xml.sax.ContentHandler#endElement(String, String, String)},
245      * the state is restored by calling {@link #checkContext(int)}.
246      */

247     public int getContext() {
248         return (prefixList == null ? 0 : prefixList.size()) +
249             (cachedURI == null ? 0 : 2);
250     }
251
252     /** This method is used to restore the namespace state
253      * after an element is created. It takes as input a state,
254      * as returned by {@link #getContext()}.<br>
255      * For any prefix, which was since saving the state,
256      * the prefix is returned and deleted from the internal
257      * list. In other words, a typical use looks like this:
258      * <pre>
259      * NamespaceSupport nss;
260      * ContentHandler h;
261      * int context = nss.getContext();
262      * h.startElement("foo", "bar", "f:bar", new AttributesImpl());
263      * ...
264      * h.endElement("foo", "bar", "f:bar");
265      * for (;;) {
266      * String prefix = nss.checkContext(context);
267      * if (prefix == null) {
268      * break;
269      * }
270      * h.endPrefixMapping(prefix);
271      * }
272      * </pre>
273      */

274     public String JavaDoc checkContext(int i) {
275         if (getContext() == i) {
276             return null;
277         }
278         String JavaDoc result = cachedPrefix;
279         if (prefixList != null && prefixList.size() > 0) {
280             cachedURI = prefixList.remove(prefixList.size()-1).toString();
281             cachedPrefix = prefixList.remove(prefixList.size()-1).toString();
282         } else {
283             cachedURI = null;
284             cachedPrefix = null;
285         }
286         return result;
287     }
288 }
289
Popular Tags