01: /*
02: * This file is part of PFIXCORE.
03: *
04: * PFIXCORE is free software; you can redistribute it and/or modify
05: * it under the terms of the GNU Lesser General Public License as published by
06: * the Free Software Foundation; either version 2 of the License, or
07: * (at your option) any later version.
08: *
09: * PFIXCORE is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public License
15: * along with PFIXCORE; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: */
18:
19: package de.schlund.pfixxml.config.includes;
20:
21: import java.util.Collections;
22: import java.util.HashMap;
23: import java.util.HashSet;
24: import java.util.Iterator;
25: import java.util.Map;
26: import java.util.Set;
27:
28: import javax.xml.XMLConstants;
29: import javax.xml.namespace.NamespaceContext;
30:
31: class SimpleNamespaceContext implements NamespaceContext {
32: private Map<String, String> prefixToURI = new HashMap<String, String>();
33:
34: protected SimpleNamespaceContext() {
35: prefixToURI.put(XMLConstants.DEFAULT_NS_PREFIX,
36: XMLConstants.NULL_NS_URI);
37: prefixToURI.put(XMLConstants.XML_NS_PREFIX,
38: XMLConstants.XML_NS_URI);
39: prefixToURI.put(XMLConstants.XMLNS_ATTRIBUTE,
40: XMLConstants.XMLNS_ATTRIBUTE_NS_URI);
41: }
42:
43: protected void addNamespace(String prefix, String uri) {
44: if (prefix == null) {
45: throw new IllegalArgumentException(
46: "Prefix must not be null!");
47: }
48: if (uri == null) {
49: throw new IllegalArgumentException(
50: "Namespace URI must not be null!");
51: }
52: prefixToURI.put(prefix, uri);
53: }
54:
55: public String getNamespaceURI(String prefix) {
56: if (prefix == null) {
57: throw new IllegalArgumentException(
58: "Prefix must not be null!");
59: }
60: String uri = prefixToURI.get(prefix);
61: if (uri == null) {
62: return XMLConstants.NULL_NS_URI;
63: } else {
64: return uri;
65: }
66: }
67:
68: public String getPrefix(String namespaceURI) {
69: if (namespaceURI == null) {
70: throw new IllegalArgumentException(
71: "Namespace URI must not be null!");
72: }
73: for (String prefix : prefixToURI.keySet()) {
74: if (prefixToURI.get(prefix).equals(namespaceURI)) {
75: return prefix;
76: }
77: }
78: return null;
79: }
80:
81: public Iterator<String> getPrefixes(String namespaceURI) {
82: if (namespaceURI == null) {
83: throw new IllegalArgumentException(
84: "Namespace URI must not be null!");
85: }
86: Set<String> prefixes = new HashSet<String>();
87: for (String prefix : prefixToURI.keySet()) {
88: if (prefixToURI.get(prefix).equals(namespaceURI)) {
89: prefixes.add(prefix);
90: }
91: }
92: return Collections.unmodifiableSet(prefixes).iterator();
93: }
94:
95: }
|