1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.ws.commons.schema.utils;
21
22 import org.apache.ws.commons.schema.constants.Constants;
23 import org.w3c.dom.Node;
24
25 import javax.xml.namespace.NamespaceContext;
26 import java.util.*;
27
28
29
30
31 public class NodeNamespaceContext implements NamespacePrefixList {
32 private static final Collection XML_NS_PREFIX_COLLECTION = Collections.singletonList(Constants.XML_NS_PREFIX);
33 private static final Collection XMLNS_ATTRIBUTE_COLLECTION = Collections.singletonList(Constants.XMLNS_ATTRIBUTE);
34 private Node node;
35 private Map declarations;
36 private String[] prefixes;
37
38
39
40
41 public NodeNamespaceContext(Node pNode) {
42 node = pNode;
43 }
44
45 private Map getDeclarations() {
46 if (declarations == null) {
47 declarations = new HashMap();
48
49
50 new PrefixCollector(){
51 protected void declare(String pPrefix, String pNamespaceURI) {
52 declarations.put(pPrefix, pNamespaceURI);
53 }
54 }.searchAllPrefixDeclarations(node);
55 Collection keys = declarations.keySet();
56 prefixes = (String[]) keys.toArray(new String[keys.size()]);
57 }
58 return declarations;
59 }
60
61 public String getNamespaceURI(String pPrefix) {
62 if (pPrefix == null) {
63 throw new IllegalArgumentException("The prefix must not be null.");
64 }
65 if (Constants.XML_NS_PREFIX.equals(pPrefix)) {
66 return Constants.XML_NS_URI;
67 }
68 if (Constants.XMLNS_ATTRIBUTE.equals(pPrefix)) {
69 return Constants.XMLNS_ATTRIBUTE_NS_URI;
70 }
71 final String uri = (String) getDeclarations().get(pPrefix);
72 return uri == null ? Constants.NULL_NS_URI : uri;
73 }
74
75 public String getPrefix(String pNamespaceURI) {
76 if (pNamespaceURI == null) {
77 throw new IllegalArgumentException("The namespace URI must not be null.");
78 }
79 if (Constants.XML_NS_URI.equals(pNamespaceURI)) {
80 return Constants.XML_NS_PREFIX;
81 }
82 if (Constants.XMLNS_ATTRIBUTE_NS_URI.equals(pNamespaceURI)) {
83 return Constants.XMLNS_ATTRIBUTE;
84 }
85 Map decl = getDeclarations();
86 for (Iterator iter = decl.entrySet().iterator(); iter.hasNext(); ) {
87 Map.Entry entry = (Map.Entry) iter.next();
88 if (pNamespaceURI.equals(entry.getValue())) {
89 return (String) entry.getKey();
90 }
91 }
92 return null;
93 }
94
95 public Iterator getPrefixes(String pNamespaceURI) {
96 if (pNamespaceURI == null) {
97 throw new IllegalArgumentException("The namespace URI must not be null.");
98 }
99 if (Constants.XML_NS_URI.equals(pNamespaceURI)) {
100 return XML_NS_PREFIX_COLLECTION.iterator();
101 }
102 if (Constants.XMLNS_ATTRIBUTE_NS_URI.equals(pNamespaceURI)) {
103 return XMLNS_ATTRIBUTE_COLLECTION.iterator();
104 }
105 final List list = new ArrayList();
106 for (Iterator iter = getDeclarations().entrySet().iterator(); iter.hasNext(); ) {
107 Map.Entry entry = (Map.Entry) iter.next();
108 if (pNamespaceURI.equals(entry.getValue())) {
109 list.add(entry.getKey());
110 }
111 }
112 return list.iterator();
113 }
114
115 public String[] getDeclaredPrefixes() {
116 getDeclarations();
117 return prefixes;
118 }
119 }