1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jxpath.ri;
18
19 import java.io.Serializable;
20 import java.util.HashMap;
21
22 import org.apache.commons.jxpath.Pointer;
23 import org.apache.commons.jxpath.ri.model.NodeIterator;
24 import org.apache.commons.jxpath.ri.model.NodePointer;
25
26
27
28
29
30
31
32 public class NamespaceResolver implements Cloneable, Serializable {
33 private static final long serialVersionUID = 1085590057838651311L;
34
35
36 protected final NamespaceResolver parent;
37
38 protected HashMap namespaceMap = new HashMap();
39
40 protected HashMap reverseMap = new HashMap();
41
42 protected NodePointer pointer;
43 private boolean sealed;
44
45
46
47
48
49
50
51
52 protected static String getPrefix(final NodePointer pointer, String namespaceURI) {
53 NodePointer currentPointer = pointer;
54 while (currentPointer != null) {
55 NodeIterator ni = currentPointer.namespaceIterator();
56 for (int position = 1; ni != null && ni.setPosition(position); position++) {
57 NodePointer nsPointer = ni.getNodePointer();
58 String uri = nsPointer.getNamespaceURI();
59 if (uri.equals(namespaceURI)) {
60 String prefix = nsPointer.getName().getName();
61 if (!"".equals(prefix)) {
62 return prefix;
63 }
64 }
65 }
66 currentPointer = currentPointer.getParent();
67 }
68 return null;
69 }
70
71
72
73
74 public NamespaceResolver() {
75 this(null);
76 }
77
78
79
80
81
82 public NamespaceResolver(NamespaceResolver parent) {
83 this.parent = parent;
84 }
85
86
87
88
89
90
91
92 public synchronized void registerNamespace(String prefix, String namespaceURI) {
93 if (isSealed()) {
94 throw new IllegalStateException(
95 "Cannot register namespaces on a sealed NamespaceResolver");
96 }
97 namespaceMap.put(prefix, namespaceURI);
98 reverseMap.put(namespaceURI, prefix);
99 }
100
101
102
103
104
105 public void setNamespaceContextPointer(NodePointer pointer) {
106 this.pointer = pointer;
107 }
108
109
110
111
112
113 public Pointer getNamespaceContextPointer() {
114 if (pointer == null && parent != null) {
115 return parent.getNamespaceContextPointer();
116 }
117 return pointer;
118 }
119
120
121
122
123
124
125
126
127
128
129
130 public synchronized String getNamespaceURI(String prefix) {
131 String uri = getExternallyRegisteredNamespaceURI(prefix);
132 return uri == null && pointer != null ? pointer.getNamespaceURI(prefix)
133 : uri;
134 }
135
136
137
138
139
140
141
142
143 protected synchronized String getExternallyRegisteredNamespaceURI(
144 String prefix) {
145 String uri = (String) namespaceMap.get(prefix);
146 return uri == null && parent != null ? parent
147 .getExternallyRegisteredNamespaceURI(prefix) : uri;
148 }
149
150
151
152
153
154
155 public synchronized String getPrefix(String namespaceURI) {
156 String prefix = getExternallyRegisteredPrefix(namespaceURI);
157 return prefix == null && pointer != null ? getPrefix(pointer,
158 namespaceURI) : prefix;
159 }
160
161
162
163
164
165
166
167 protected synchronized String getExternallyRegisteredPrefix(String namespaceURI) {
168 String prefix = (String) reverseMap.get(namespaceURI);
169 return prefix == null && parent != null ? parent
170 .getExternallyRegisteredPrefix(namespaceURI) : prefix;
171 }
172
173
174
175
176
177 public boolean isSealed() {
178 return sealed;
179 }
180
181
182
183
184 public void seal() {
185 sealed = true;
186 if (parent != null) {
187 parent.seal();
188 }
189 }
190
191 public Object clone() {
192 try {
193 NamespaceResolver result = (NamespaceResolver) super.clone();
194 result.sealed = false;
195 return result;
196 }
197 catch (CloneNotSupportedException e) {
198
199 e.printStackTrace();
200 return null;
201 }
202 }
203 }