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