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 * Creation of the namespace context for use with static imports.
54 * @return Empty namespace context.
56 public static SimpleNamespaceContext namespaces() {
57 return new SimpleNamespaceContext();
61 * Constructs a context with a single prefix.
62 * @param aPrefix Prefix.
63 * @param aNamespace Namespace.
65 public SimpleNamespaceContext(String aPrefix, String aNamespace) {
67 addPrefix(aPrefix, aNamespace);
71 * Constructs empty context (for use with static imports).
72 * @return Namespace context.
74 public static SimpleNamespaceContext namespaceContext() {
75 return new SimpleNamespaceContext();
79 * Constructs a context with a single prefix (for use with static imports).
80 * @param aPrefix Prefix
81 * @param aNamespace Namespace.
84 public static SimpleNamespaceContext namespaceContext(String aPrefix,
86 return new SimpleNamespaceContext(aPrefix, aNamespace);
90 * Sets the default namespace.
91 * @param aDefaultNs Default namespace.
92 * @return Current object for method chaining.
94 public SimpleNamespaceContext defaultNamespace(String aDefaultNs) {
95 defaultNs = aDefaultNs;
101 * @param aPrefix Prefix to add.
102 * @param aNamespace Namespace to bind prefix to.
103 * @return Current object for method chaining.
105 public SimpleNamespaceContext addPrefix(String aPrefix, String aNamespace) {
106 if (aPrefix == null) {
107 throw new IllegalArgumentException("prefix is null");
109 if (aNamespace == null) {
110 throw new IllegalArgumentException("namespace is null");
112 prefixMap.put(aPrefix, aNamespace);
117 public String getNamespaceURI(String aPrefix) {
118 if (XMLConstants.DEFAULT_NS_PREFIX.equals(aPrefix)) {
119 if (defaultNs == null) {
120 return XMLConstants.NULL_NS_URI;
124 if (XMLConstants.XML_NS_PREFIX.equals(aPrefix)) {
125 return XMLConstants.XML_NS_URI;
127 if (XMLConstants.XMLNS_ATTRIBUTE.equals(aPrefix)) {
128 return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
130 String ns = prefixMap.get(aPrefix);
132 return XMLConstants.NULL_NS_URI;
138 public String getPrefix(String aNamespaceURI) {
139 List<String> prefixes = getPrefixList(aNamespaceURI);
140 if (prefixes.isEmpty()) {
143 return prefixes.get(0);
147 public Iterator getPrefixes(String aNamespaceURI) {
148 return Collections.unmodifiableList(getPrefixList(aNamespaceURI)).iterator();
151 public List<String> getPrefixList(String aNamespaceURI) {
152 List<String> result = new ArrayList<String>();
154 if ((aNamespaceURI == null && defaultNs == null) ||
155 (defaultNs != null && defaultNs.equals(aNamespaceURI))) {
156 result.add(XMLConstants.DEFAULT_NS_PREFIX);
157 return result; // make sure not more prefixes added.
159 if (XMLConstants.XML_NS_URI.equals(aNamespaceURI)) {
160 result.add(XMLConstants.XML_NS_PREFIX);
161 return result; // make sure no more prefixes added.
163 if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(aNamespaceURI)) {
164 result.add(XMLConstants.XMLNS_ATTRIBUTE);
167 for (Map.Entry<String,String> entry: prefixMap.entrySet()) {
168 if (entry.getValue().equals(aNamespaceURI)) {
169 result.add(entry.getKey());