KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > eip > support > NamespaceContextImpl


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.eip.support;
18
19 import java.util.Collections JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.LinkedHashMap JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import javax.xml.XMLConstants JavaDoc;
27 import javax.xml.namespace.NamespaceContext JavaDoc;
28
29 /**
30  * A simple namespace context with a clean xbean configuration.
31  *
32  * @org.apache.xbean.XBean element="namespace-context"
33  * description="A NamespaceContext implementation"
34  * @author gnodet
35  * @version $Revision: 397796 $
36  */

37 public class NamespaceContextImpl implements NamespaceContext JavaDoc {
38
39     /**
40      * map containing bound namespaces, keyed by their prefix. A LinkedHashMap
41      * is used to ensure that {@link #getPrefix(String)} always returns the same
42      * prefix, unless that prefix is removed.
43      */

44     private Map JavaDoc namespaces = new LinkedHashMap JavaDoc();
45     
46     /**
47      * Constructs a SimpleNamespaceContext with no parent context or namespace
48      * declarations.
49      */

50     public NamespaceContextImpl() {
51     }
52     
53     /**
54      * Constructs a SimpleNamespaceContext with no parent context that contains
55      * the specified prefixes.
56      *
57      * @param namespaces A Map of namespace URIs, keyed by their prefixes.
58      */

59     public NamespaceContextImpl(Map JavaDoc namespaces) {
60         setNamespaces(namespaces);
61     }
62     
63     /**
64      * @org.apache.xbean.Map entryName="namespace" keyName="prefix"
65      * @return Returns the namespaces.
66      */

67     public Map JavaDoc getNamespaces() {
68         return namespaces;
69     }
70
71     /**
72      * @param namespaces The namespaces to set.
73      */

74     public void setNamespaces(Map JavaDoc namespaces) {
75         this.namespaces.clear();
76         if (namespaces != null) {
77             this.namespaces.putAll(namespaces);
78         }
79     }
80
81     /* (non-Javadoc)
82      * @see javax.xml.namespace.NamespaceContext#getNamespaceURI(java.lang.String)
83      */

84     public String JavaDoc getNamespaceURI(String JavaDoc prefix) {
85         if (prefix == null) {
86             throw new IllegalArgumentException JavaDoc("prefix argument was null");
87         } else if (prefix.equals(XMLConstants.XML_NS_PREFIX)) {
88             return XMLConstants.XML_NS_URI;
89         } else if (prefix.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
90             return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
91         } else if (namespaces.containsKey(prefix)) {
92             String JavaDoc uri = (String JavaDoc) namespaces.get(prefix);
93             if (uri.length() == 0) {
94                 return null;
95             } else {
96                 return uri;
97             }
98         } else {
99             return null;
100         }
101     }
102
103     /* (non-Javadoc)
104      * @see javax.xml.namespace.NamespaceContext#getPrefix(java.lang.String)
105      */

106     public String JavaDoc getPrefix(String JavaDoc nsURI) {
107         if (nsURI == null) {
108             throw new IllegalArgumentException JavaDoc("nsURI was null");
109         } else if (nsURI.length() == 0) {
110             throw new IllegalArgumentException JavaDoc("nsURI was empty");
111         } else if (nsURI.equals(XMLConstants.XML_NS_URI)) {
112             return XMLConstants.XML_NS_PREFIX;
113         } else if (nsURI.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
114             return XMLConstants.XMLNS_ATTRIBUTE;
115         }
116         Iterator JavaDoc iter = namespaces.entrySet().iterator();
117         while (iter.hasNext()) {
118             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
119             String JavaDoc uri = (String JavaDoc) entry.getValue();
120             if (uri.equals(nsURI)) {
121                 return (String JavaDoc) entry.getKey();
122             }
123         }
124         if (nsURI.length() == 0) {
125             return "";
126         } else {
127             return null;
128         }
129     }
130
131     /* (non-Javadoc)
132      * @see javax.xml.namespace.NamespaceContext#getPrefixes(java.lang.String)
133      */

134     public Iterator JavaDoc getPrefixes(String JavaDoc nsURI) {
135         if (nsURI == null) {
136             throw new IllegalArgumentException JavaDoc("nsURI was null");
137         } else if (nsURI.length() == 0) {
138             throw new IllegalArgumentException JavaDoc("nsURI was empty");
139         } else if (nsURI.equals(XMLConstants.XML_NS_URI)) {
140             return Collections.singleton(XMLConstants.XML_NS_PREFIX).iterator();
141         } else if (nsURI.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
142             return Collections.singleton(XMLConstants.XMLNS_ATTRIBUTE).iterator();
143         }
144         Set JavaDoc prefixes = null;
145         Iterator JavaDoc iter = namespaces.entrySet().iterator();
146         while (iter.hasNext()) {
147             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
148             String JavaDoc uri = (String JavaDoc) entry.getValue();
149             if (uri.equals(nsURI)) {
150                 if (prefixes == null) {
151                     prefixes = new HashSet JavaDoc();
152                 }
153                 prefixes.add(entry.getKey());
154             }
155         }
156         if (prefixes != null) {
157             return Collections.unmodifiableSet(prefixes).iterator();
158         } else if (nsURI.length() == 0) {
159             return Collections.singleton("").iterator();
160         } else {
161             return Collections.EMPTY_LIST.iterator();
162         }
163     }
164     
165 }
166
Popular Tags