001: /*
002: * Copyright 2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.xml.namespace;
018:
019: import java.util.ArrayList;
020: import java.util.Collections;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025: import javax.xml.XMLConstants;
026: import javax.xml.namespace.NamespaceContext;
027:
028: import org.springframework.util.Assert;
029:
030: /**
031: * Simple <code>javax.xml.namespace.NamespaceContext</code> implementation. Follows the standard
032: * <code>NamespaceContext</code> contract, and is loadable via a <code>java.util.Map</code> or
033: * <code>java.util.Properties</code> object
034: *
035: * @author Arjen Poutsma
036: * @since 1.0.0
037: */
038: public class SimpleNamespaceContext implements NamespaceContext {
039:
040: private Map prefixToNamespaceUri = new HashMap();
041:
042: /** Maps a <code>String</code> namespaceUri to a <code>List</code> of prefixes */
043: private Map namespaceUriToPrefixes = new HashMap();
044:
045: private String defaultNamespaceUri = "";
046:
047: public String getNamespaceURI(String prefix) {
048: Assert.notNull(prefix, "prefix is null");
049: if (XMLConstants.XML_NS_PREFIX.equals(prefix)) {
050: return XMLConstants.XML_NS_URI;
051: } else if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) {
052: return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
053: } else if (XMLConstants.DEFAULT_NS_PREFIX.equals(prefix)) {
054: return defaultNamespaceUri;
055: } else if (prefixToNamespaceUri.containsKey(prefix)) {
056: return (String) prefixToNamespaceUri.get(prefix);
057: }
058: return "";
059: }
060:
061: public String getPrefix(String namespaceUri) {
062: List prefixes = getPrefixesInternal(namespaceUri);
063: return prefixes.isEmpty() ? null : (String) prefixes.get(0);
064: }
065:
066: public Iterator getPrefixes(String namespaceUri) {
067: return getPrefixesInternal(namespaceUri).iterator();
068: }
069:
070: /**
071: * Sets the bindings for this namespace context. The supplied map must consist of string key value pairs.
072: *
073: * @param bindings the bindings
074: */
075: public void setBindings(Map bindings) {
076: for (Iterator iterator = bindings.entrySet().iterator(); iterator
077: .hasNext();) {
078: Map.Entry entry = (Map.Entry) iterator.next();
079: bindNamespaceUri((String) entry.getKey(), (String) entry
080: .getValue());
081: }
082: }
083:
084: /**
085: * Binds the given namespace as default namespace.
086: *
087: * @param namespaceUri the namespace uri
088: */
089: public void bindDefaultNamespaceUri(String namespaceUri) {
090: bindNamespaceUri(XMLConstants.DEFAULT_NS_PREFIX, namespaceUri);
091: }
092:
093: /**
094: * Binds the given prefix to the given namespace.
095: *
096: * @param prefix the namespace prefix
097: * @param namespaceUri the namespace uri
098: */
099: public void bindNamespaceUri(String prefix, String namespaceUri) {
100: Assert.notNull(prefix, "No prefix given");
101: Assert.notNull(namespaceUri, "No namespaceUri given");
102: if (XMLConstants.DEFAULT_NS_PREFIX.equals(prefix)) {
103: defaultNamespaceUri = namespaceUri;
104: } else {
105: prefixToNamespaceUri.put(prefix, namespaceUri);
106: getPrefixesInternal(namespaceUri).add(prefix);
107: }
108: }
109:
110: /** Removes all declared prefixes. */
111: public void clear() {
112: prefixToNamespaceUri.clear();
113: }
114:
115: /**
116: * Returns all declared prefixes.
117: *
118: * @return the declared prefixes
119: */
120: public Iterator getBoundPrefixes() {
121: return prefixToNamespaceUri.keySet().iterator();
122: }
123:
124: private List getPrefixesInternal(String namespaceUri) {
125: if (defaultNamespaceUri.equals(namespaceUri)) {
126: return Collections
127: .singletonList(XMLConstants.DEFAULT_NS_PREFIX);
128: } else if (XMLConstants.XML_NS_URI.equals(namespaceUri)) {
129: return Collections
130: .singletonList(XMLConstants.XML_NS_PREFIX);
131: } else if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI
132: .equals(namespaceUri)) {
133: return Collections
134: .singletonList(XMLConstants.XMLNS_ATTRIBUTE);
135: } else {
136: List list = (List) namespaceUriToPrefixes.get(namespaceUri);
137: if (list == null) {
138: list = new ArrayList();
139: namespaceUriToPrefixes.put(namespaceUri, list);
140: }
141: return list;
142: }
143: }
144:
145: /**
146: * Removes the given prefix from this context.
147: *
148: * @param prefix the prefix to be removed
149: */
150: public void removeBinding(String prefix) {
151: if (XMLConstants.DEFAULT_NS_PREFIX.equals(prefix)) {
152: defaultNamespaceUri = "";
153: } else {
154: String namespaceUri = (String) namespaceUriToPrefixes
155: .get(prefix);
156: List prefixes = getPrefixesInternal(namespaceUri);
157: prefixes.remove(prefix);
158: namespaceUriToPrefixes.remove(prefixes);
159: }
160: }
161: }
|