1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.ken.util;
17
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.Iterator;
21 import java.util.Map;
22
23 import javax.xml.namespace.NamespaceContext;
24
25 import org.apache.log4j.Logger;
26
27
28
29
30
31 public class ConfiguredNamespaceContext implements NamespaceContext {
32 private static final Logger LOG = Logger.getLogger(ConfiguredNamespaceContext.class);
33
34 private Map<String, String> prefixToNamespace = new HashMap<String, String>();
35 private Map<String, HashSet<String>> namespaceToPrefix = new HashMap<String, HashSet<String>>();
36
37
38
39
40
41 public ConfiguredNamespaceContext(Map<String, String> prefixToNamespace) {
42 this.prefixToNamespace = prefixToNamespace;
43
44 for (Map.Entry<String, String> entry: prefixToNamespace.entrySet()) {
45 String namespace = entry.getValue();
46 String prefix = entry.getKey();
47 HashSet<String> prefixes = namespaceToPrefix.get(namespace);
48 if (prefixes == null) {
49 prefixes = new HashSet<String>(4);
50 namespaceToPrefix.put(namespace, prefixes);
51 }
52 prefixes.add(prefix);
53 }
54 }
55
56
57
58
59 public String getNamespaceURI(String prefix) {
60
61 if (prefix == null) {
62 throw new IllegalArgumentException("The prefix cannot be null.");
63 }
64
65 return prefixToNamespace.get(prefix);
66 }
67
68
69
70
71 public String getPrefix(String namespaceURI) {
72
73 if (namespaceURI == null) {
74 throw new IllegalArgumentException("The namespace uri cannot be null.");
75 }
76 Iterator<String> prefixes = getPrefixes(namespaceURI);
77 if (prefixes != null) {
78 return prefixes.next();
79 } else {
80 return null;
81 }
82 }
83
84
85
86
87 public Iterator<String> getPrefixes(String namespaceURI) {
88
89 if (namespaceURI == null) {
90 throw new IllegalArgumentException("The namespace uri cannot be null.");
91 }
92
93 HashSet<String> prefixes = namespaceToPrefix.get(namespaceURI);
94 if (prefixes != null && prefixes.size() > 0) {
95 return prefixes.iterator();
96 } else {
97 return null;
98 }
99 }
100 }