| 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 | |
package com.jcabi.xml; |
| 31 | |
|
| 32 | |
import com.jcabi.immutable.Array; |
| 33 | |
import com.jcabi.immutable.ArrayMap; |
| 34 | |
import com.jcabi.log.Logger; |
| 35 | |
import java.util.Collections; |
| 36 | |
import java.util.Iterator; |
| 37 | |
import java.util.LinkedList; |
| 38 | |
import java.util.List; |
| 39 | |
import java.util.concurrent.ConcurrentHashMap; |
| 40 | |
import java.util.concurrent.ConcurrentMap; |
| 41 | |
import javax.xml.XMLConstants; |
| 42 | |
import javax.xml.namespace.NamespaceContext; |
| 43 | |
import lombok.EqualsAndHashCode; |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | 0 | @EqualsAndHashCode(of = { "map", "contexts" }) |
| 55 | |
public final class XPathContext implements NamespaceContext { |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
private final transient ArrayMap<String, String> map; |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
private final transient Array<NamespaceContext> contexts; |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
public XPathContext() { |
| 77 | 887 | this( |
| 78 | |
new ArrayMap<String, String>() |
| 79 | |
.with("xhtml", "http://www.w3.org/1999/xhtml") |
| 80 | |
.with("xs", "http://www.w3.org/2001/XMLSchema") |
| 81 | |
.with("xsi", "http://www.w3.org/2001/XMLSchema-instance") |
| 82 | |
.with("xsl", "http://www.w3.org/1999/XSL/Transform") |
| 83 | |
.with("svg", "http://www.w3.org/2000/svg"), |
| 84 | |
new Array<NamespaceContext>() |
| 85 | |
); |
| 86 | 886 | } |
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
public XPathContext(final Object... namespaces) { |
| 93 | 1 | this( |
| 94 | |
XPathContext.namespacesAsMap(namespaces), |
| 95 | |
new Array<NamespaceContext>() |
| 96 | |
); |
| 97 | 1 | } |
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
private XPathContext(final ArrayMap<String, String> old, |
| 106 | |
final String prefix, final Object namespace) { |
| 107 | 5 | this( |
| 108 | |
old.with(prefix, namespace.toString()), |
| 109 | |
new Array<NamespaceContext>() |
| 110 | |
); |
| 111 | 5 | } |
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
private XPathContext(final ArrayMap<String, String> mapping, |
| 119 | 1101 | final Array<NamespaceContext> ctxs) { |
| 120 | 1101 | this.map = mapping; |
| 121 | 1101 | this.contexts = ctxs; |
| 122 | 1101 | } |
| 123 | |
|
| 124 | |
@Override |
| 125 | |
public String toString() { |
| 126 | 0 | return this.map.keySet().toString(); |
| 127 | |
} |
| 128 | |
|
| 129 | |
@Override |
| 130 | |
public String getNamespaceURI(final String prefix) { |
| 131 | 16 | String namespace = this.map.get(prefix); |
| 132 | 16 | if (namespace == null) { |
| 133 | 3 | for (final NamespaceContext ctx : this.contexts) { |
| 134 | 0 | namespace = ctx.getNamespaceURI(prefix); |
| 135 | 0 | if (namespace != null) { |
| 136 | 0 | break; |
| 137 | |
} |
| 138 | 0 | } |
| 139 | |
} |
| 140 | 16 | if (namespace == null) { |
| 141 | 3 | if (prefix.equals(XMLConstants.XML_NS_PREFIX)) { |
| 142 | 1 | namespace = XMLConstants.XML_NS_URI; |
| 143 | 2 | } else if (prefix.equals(XMLConstants.XMLNS_ATTRIBUTE)) { |
| 144 | 1 | namespace = XMLConstants.XMLNS_ATTRIBUTE_NS_URI; |
| 145 | |
} else { |
| 146 | 1 | namespace = XMLConstants.NULL_NS_URI; |
| 147 | |
} |
| 148 | |
} |
| 149 | 16 | return namespace; |
| 150 | |
} |
| 151 | |
|
| 152 | |
@Override |
| 153 | |
public String getPrefix(final String namespace) { |
| 154 | 1 | final Iterator<String> prefixes = this.getPrefixes(namespace); |
| 155 | 1 | String prefix = null; |
| 156 | 1 | if (prefixes.hasNext()) { |
| 157 | 1 | prefix = prefixes.next(); |
| 158 | |
} |
| 159 | 1 | return prefix; |
| 160 | |
} |
| 161 | |
|
| 162 | |
@Override |
| 163 | |
public Iterator<String> getPrefixes(final String namespace) { |
| 164 | 2 | final List<String> prefixes = new LinkedList<String>(); |
| 165 | |
for (final ConcurrentMap.Entry<String, String> entry |
| 166 | 2 | : this.map.entrySet()) { |
| 167 | 9 | if (entry.getValue().equals(namespace)) { |
| 168 | 3 | prefixes.add(entry.getKey()); |
| 169 | |
} |
| 170 | 9 | } |
| 171 | 2 | for (final NamespaceContext ctx : this.contexts) { |
| 172 | 0 | final Iterator<?> iterator = ctx.getPrefixes(namespace); |
| 173 | 0 | while (iterator.hasNext()) { |
| 174 | 0 | prefixes.add(iterator.next().toString()); |
| 175 | |
} |
| 176 | 0 | } |
| 177 | 2 | if (namespace.equals(XMLConstants.XML_NS_URI)) { |
| 178 | 0 | prefixes.add(XMLConstants.XML_NS_PREFIX); |
| 179 | |
} |
| 180 | 2 | if (namespace.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) { |
| 181 | 0 | prefixes.add(XMLConstants.XMLNS_ATTRIBUTE); |
| 182 | |
} |
| 183 | 2 | return Collections.unmodifiableList(prefixes).iterator(); |
| 184 | |
} |
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
public XPathContext add(final String prefix, final Object namespace) { |
| 193 | 5 | if (this.map.containsKey(prefix)) { |
| 194 | 0 | throw new IllegalArgumentException( |
| 195 | |
String.format( |
| 196 | |
"prefix '%s' already registered for namespace '%s'", |
| 197 | |
prefix, |
| 198 | |
this.map.get(prefix) |
| 199 | |
) |
| 200 | |
); |
| 201 | |
} |
| 202 | 5 | return new XPathContext(this.map, prefix, namespace); |
| 203 | |
} |
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
public XPathContext merge(final NamespaceContext context) { |
| 211 | 209 | final XPathContext ctx = new XPathContext( |
| 212 | |
this.map, this.contexts.with(context) |
| 213 | |
); |
| 214 | 209 | return ctx; |
| 215 | |
} |
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
private static ArrayMap<String, String> namespacesAsMap( |
| 223 | |
final Object...namespaces) { |
| 224 | 1 | final ConcurrentMap<String, String> map = |
| 225 | |
new ConcurrentHashMap<String, String>(namespaces.length); |
| 226 | 3 | for (int pos = 0; pos < namespaces.length; ++pos) { |
| 227 | 2 | map.put( |
| 228 | |
Logger.format("ns%d", pos + 1), |
| 229 | |
namespaces[pos].toString() |
| 230 | |
); |
| 231 | |
} |
| 232 | 1 | return new ArrayMap<String, String>(map); |
| 233 | |
} |
| 234 | |
|
| 235 | |
} |