1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
package org.apache.camel.builder.xml; |
19 |
|
|
20 |
|
import org.w3c.dom.Element; |
21 |
|
import org.w3c.dom.NamedNodeMap; |
22 |
|
import org.w3c.dom.Node; |
23 |
|
import org.w3c.dom.Attr; |
24 |
|
|
25 |
|
import javax.xml.namespace.NamespaceContext; |
26 |
|
import javax.xml.xpath.XPathFactory; |
27 |
|
import java.util.HashMap; |
28 |
|
import java.util.HashSet; |
29 |
|
import java.util.Iterator; |
30 |
|
import java.util.Map; |
31 |
|
import java.util.Set; |
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
public class DefaultNamespaceContext implements NamespaceContext { |
40 |
|
|
41 |
|
private final Map map; |
42 |
|
private final NamespaceContext parent; |
43 |
|
|
44 |
|
public DefaultNamespaceContext() { |
45 |
0 |
this(XPathFactory.newInstance()); |
46 |
0 |
} |
47 |
|
|
48 |
16 |
public DefaultNamespaceContext(XPathFactory factory) { |
49 |
16 |
this.parent = factory.newXPath().getNamespaceContext(); |
50 |
16 |
this.map = new HashMap(); |
51 |
16 |
} |
52 |
|
|
53 |
0 |
public DefaultNamespaceContext(NamespaceContext parent, Map map) { |
54 |
0 |
this.parent = parent; |
55 |
0 |
this.map = map; |
56 |
0 |
} |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
public DefaultNamespaceContext add(String prefix, String uri) { |
62 |
9 |
map.put(prefix, uri); |
63 |
9 |
return this; |
64 |
|
} |
65 |
|
|
66 |
|
public String getNamespaceURI(String prefix) { |
67 |
6 |
String answer = (String) map.get(prefix); |
68 |
6 |
if (answer == null && parent != null) { |
69 |
0 |
return parent.getNamespaceURI(prefix); |
70 |
|
} |
71 |
6 |
return answer; |
72 |
|
} |
73 |
|
|
74 |
|
public String getPrefix(String namespaceURI) { |
75 |
0 |
for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) { |
76 |
0 |
Map.Entry entry = (Map.Entry) iter.next(); |
77 |
0 |
if (namespaceURI.equals(entry.getValue())) { |
78 |
0 |
return (String) entry.getKey(); |
79 |
|
} |
80 |
0 |
} |
81 |
0 |
if (parent != null) { |
82 |
0 |
return parent.getPrefix(namespaceURI); |
83 |
|
} |
84 |
0 |
return null; |
85 |
|
} |
86 |
|
|
87 |
|
public Iterator getPrefixes(String namespaceURI) { |
88 |
0 |
Set set = new HashSet(); |
89 |
0 |
for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) { |
90 |
0 |
Map.Entry entry = (Map.Entry) iter.next(); |
91 |
0 |
if (namespaceURI.equals(entry.getValue())) { |
92 |
0 |
set.add(entry.getKey()); |
93 |
|
} |
94 |
0 |
} |
95 |
0 |
if (parent != null) { |
96 |
0 |
Iterator iter = parent.getPrefixes(namespaceURI); |
97 |
0 |
while (iter.hasNext()) { |
98 |
0 |
set.add(iter.next()); |
99 |
0 |
} |
100 |
|
} |
101 |
0 |
return set.iterator(); |
102 |
|
} |
103 |
|
|
104 |
|
public void setNamespacesFromDom(Element element) { |
105 |
|
|
106 |
2 |
Node parentNode = element.getParentNode(); |
107 |
2 |
if (parentNode instanceof Element) { |
108 |
1 |
setNamespacesFromDom((Element) parentNode); |
109 |
|
} |
110 |
2 |
NamedNodeMap attributes = element.getAttributes(); |
111 |
6 |
for (int i = 0, size = attributes.getLength(); i < size; i++) { |
112 |
4 |
Attr node = (Attr) attributes.item(i); |
113 |
4 |
String name = node.getName(); |
114 |
4 |
if (name.startsWith("xmlns:")) { |
115 |
3 |
String prefix = name.substring("xmlns:".length()); |
116 |
3 |
String uri = node.getValue(); |
117 |
3 |
add(prefix, uri); |
118 |
|
} |
119 |
|
} |
120 |
2 |
} |
121 |
|
} |