NamespaceContextImpl.java
4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Decompiled with CFR 0_118.
*/
package com.adobe.xfa.dom;
import com.adobe.xfa.dom.AttrImpl;
import com.adobe.xfa.dom.ElementImpl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.xml.namespace.NamespaceContext;
import org.w3c.dom.Node;
public class NamespaceContextImpl
implements NamespaceContext {
private final ElementImpl mElementImpl;
private Map<String, String> mNamespaceMappings;
public NamespaceContextImpl(Node node) {
ElementImpl element = null;
if (node instanceof ElementImpl) {
element = (ElementImpl)node;
} else if ((node = node.getParentNode()) instanceof ElementImpl) {
element = (ElementImpl)node;
}
this.mElementImpl = element;
}
@Override
public String getNamespaceURI(String prefix) {
String ns;
if (prefix == null) {
throw new IllegalArgumentException("prefix");
}
if (prefix.equals("xml")) {
return "http://www.w3.org/XML/1998/namespace";
}
if (prefix.equals("xmlns")) {
return "http://www.w3.org/2000/xmlns/";
}
if (this.mElementImpl != null) {
Iterator<AttrImpl> i = this.mElementImpl.getNamespacesInScopeIterator();
while (i.hasNext()) {
AttrImpl attr = i.next();
if (!prefix.equals(NamespaceContextImpl.extractPrefix(attr))) continue;
return attr.getValue();
}
}
if (this.mNamespaceMappings != null && (ns = this.mNamespaceMappings.get(prefix)) != null) {
return ns;
}
return "";
}
@Override
public String getPrefix(String namespaceURI) {
if (namespaceURI == null) {
throw new IllegalArgumentException("namespaceURI");
}
if (namespaceURI.equals("http://www.w3.org/XML/1998/namespace")) {
return "xml";
}
if (namespaceURI.equals("http://www.w3.org/2000/xmlns/")) {
return "xmlns";
}
if (this.mElementImpl != null) {
Iterator<AttrImpl> i = this.mElementImpl.getNamespacesInScopeIterator();
while (i.hasNext()) {
AttrImpl attr = i.next();
if (!attr.getValue().equals(namespaceURI)) continue;
return NamespaceContextImpl.extractPrefix(attr);
}
}
if (this.mNamespaceMappings != null) {
for (Map.Entry<String, String> mapping : this.mNamespaceMappings.entrySet()) {
if (!namespaceURI.equals(mapping.getValue())) continue;
return mapping.getKey();
}
}
if (namespaceURI.equals("")) {
return "";
}
return null;
}
@Override
public Iterator<String> getPrefixes(String namespaceURI) {
if (namespaceURI == null) {
throw new IllegalArgumentException("namespaceURI");
}
ArrayList<String> prefixes = new ArrayList<String>();
if (namespaceURI.equals("http://www.w3.org/XML/1998/namespace")) {
prefixes.add("xml");
} else if (namespaceURI.equals("http://www.w3.org/2000/xmlns/")) {
prefixes.add("xmlns");
} else {
if (this.mElementImpl != null) {
Iterator<AttrImpl> i = this.mElementImpl.getNamespacesInScopeIterator();
while (i.hasNext()) {
AttrImpl attr = i.next();
if (!attr.getValue().equals(namespaceURI)) continue;
prefixes.add(NamespaceContextImpl.extractPrefix(attr));
}
}
if (this.mNamespaceMappings != null) {
for (Map.Entry<String, String> mapping : this.mNamespaceMappings.entrySet()) {
if (!namespaceURI.equals(mapping.getValue())) continue;
prefixes.add(mapping.getKey());
}
}
}
return prefixes.iterator();
}
public void addNamespaceMapping(String prefix, String namespaceURI) {
if (this.mNamespaceMappings == null) {
this.mNamespaceMappings = new HashMap<String, String>();
}
this.mNamespaceMappings.put(prefix, namespaceURI);
}
private static String extractPrefix(AttrImpl attr) {
String prefix = attr.getName();
int index = prefix.indexOf(58);
return index != -1 ? prefix.substring(index + 1) : "";
}
}