2 * Copyright 2005-2011 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.xml;
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.Iterator;
22 import java.util.List;
25 import javax.xml.XMLConstants;
26 import javax.xml.namespace.NamespaceContext;
29 * Implementation of {@link NamespaceContext} for binding namespace prefixes to
30 * namespaces. This class implements the full contract as defined by that interface.
32 * It provides a fluent interface style. In its simplest use, it can be constructed
33 * empty or with a single prefix using a constructor. In addition, the methods
34 * {@link #addPrefix(String, String)} and {@link #defaultNamespace(String)} can be used
35 * to add prefixes and to define the default namespace respectively.
37 * @author Erik Brakkee
39 public class SimpleNamespaceContext implements NamespaceContext {
41 private String defaultNs;
42 private Map<String, String> prefixMap;
45 * Constructs an empty context without prefixes.
47 public SimpleNamespaceContext() {
49 prefixMap = new HashMap<String, String>();
53 * Constructs a context with a single prefix.
54 * @param aPrefix Prefix.
55 * @param aNamespace Namespace.
57 public SimpleNamespaceContext(String aPrefix, String aNamespace) {
59 addPrefix(aPrefix, aNamespace);
63 * Constructs empty context (for use with static imports).
64 * @return Namespace context.
66 public static SimpleNamespaceContext namespaceContext() {
67 return new SimpleNamespaceContext();
71 * Constructs a context with a single prefix (for use with static imports).
72 * @param aPrefix Prefix
73 * @param aNamespace Namespace.
76 public static SimpleNamespaceContext namespaceContext(String aPrefix,
78 return new SimpleNamespaceContext(aPrefix, aNamespace);
82 * Sets the default namespace.
83 * @param aDefaultNs Default namespace.
84 * @return Current object for method chaining.
86 public SimpleNamespaceContext defaultNamespace(String aDefaultNs) {
87 defaultNs = aDefaultNs;
93 * @param aPrefix Prefix to add.
94 * @param aNamespace Namespace to bind prefix to.
95 * @return Current object for method chaining.
97 public SimpleNamespaceContext addPrefix(String aPrefix, String aNamespace) {
98 if (aPrefix == null) {
99 throw new IllegalArgumentException("prefix is null");
101 if (aNamespace == null) {
102 throw new IllegalArgumentException("namespace is null");
104 prefixMap.put(aPrefix, aNamespace);
109 public String getNamespaceURI(String aPrefix) {
110 if (XMLConstants.DEFAULT_NS_PREFIX.equals(aPrefix)) {
111 if (defaultNs == null) {
112 return XMLConstants.NULL_NS_URI;
116 if (XMLConstants.XML_NS_PREFIX.equals(aPrefix)) {
117 return XMLConstants.XML_NS_URI;
119 if (XMLConstants.XMLNS_ATTRIBUTE.equals(aPrefix)) {
120 return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
122 String ns = prefixMap.get(aPrefix);
124 return XMLConstants.NULL_NS_URI;
130 public String getPrefix(String aNamespaceURI) {
131 List<String> prefixes = getPrefixList(aNamespaceURI);
132 if (prefixes.isEmpty()) {
135 return prefixes.get(0);
139 public Iterator getPrefixes(String aNamespaceURI) {
140 return Collections.unmodifiableList(getPrefixList(aNamespaceURI)).iterator();
143 public List<String> getPrefixList(String aNamespaceURI) {
144 List<String> result = new ArrayList<String>();
146 if ((aNamespaceURI == null && defaultNs == null) ||
147 (defaultNs != null && defaultNs.equals(aNamespaceURI))) {
148 result.add(XMLConstants.DEFAULT_NS_PREFIX);
149 return result; // make sure not more prefixes added.
151 if (XMLConstants.XML_NS_URI.equals(aNamespaceURI)) {
152 result.add(XMLConstants.XML_NS_PREFIX);
153 return result; // make sure no more prefixes added.
155 if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(aNamespaceURI)) {
156 result.add(XMLConstants.XMLNS_ATTRIBUTE);
159 for (Map.Entry<String,String> entry: prefixMap.entrySet()) {
160 if (entry.getValue().equals(aNamespaceURI)) {
161 result.add(entry.getKey());