1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| package org.apache.xindice.xml.dom; |
21 |
| |
22 |
| import org.apache.commons.logging.Log; |
23 |
| import org.apache.commons.logging.LogFactory; |
24 |
| import org.apache.xindice.core.data.Key; |
25 |
| import org.apache.xindice.util.ByteArrayInput; |
26 |
| import org.apache.xindice.xml.NodeSource; |
27 |
| import org.apache.xindice.xml.SymbolTable; |
28 |
| import org.apache.xindice.xml.XMLCompressedInput; |
29 |
| |
30 |
| import org.w3c.dom.Attr; |
31 |
| import org.w3c.dom.DOMException; |
32 |
| import org.w3c.dom.Element; |
33 |
| import org.w3c.dom.NamedNodeMap; |
34 |
| import org.w3c.dom.Node; |
35 |
| import org.w3c.dom.TypeInfo; |
36 |
| import org.w3c.dom.UserDataHandler; |
37 |
| |
38 |
| import java.io.IOException; |
39 |
| import java.util.HashSet; |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| public final class ElementImpl extends ContainerNodeImpl |
47 |
| implements Element { |
48 |
| |
49 |
| private static final Log log = LogFactory.getLog(ElementImpl.class); |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| private NamedNodeMapImpl attributes = new NamedNodeMapImpl(this); |
56 |
| private short symbolID = -1; |
57 |
| |
58 |
| |
59 |
0
| public ElementImpl() {
|
60 |
| } |
61 |
| |
62 |
130981
| public ElementImpl(NodeImpl parent, byte[] data, int pos, int len) {
|
63 |
130981
| super(parent, data, pos, len);
|
64 |
130981
| DocumentImpl doc = (DocumentImpl) getOwnerDocument();
|
65 |
130981
| try {
|
66 |
130981
| loadAttributes(doc.getSymbols());
|
67 |
| } catch (Exception e) { |
68 |
0
| if (log.isWarnEnabled()) {
|
69 |
0
| log.warn("ignored exception", e);
|
70 |
| } |
71 |
| } |
72 |
| } |
73 |
| |
74 |
0
| public ElementImpl(NodeImpl parent, boolean dirty) {
|
75 |
0
| super(parent, dirty);
|
76 |
| } |
77 |
| |
78 |
145696
| public ElementImpl(NodeImpl parent, String nodeName) {
|
79 |
145696
| super(parent, true);
|
80 |
145696
| this.nodeName = nodeName;
|
81 |
| } |
82 |
| |
83 |
236663
| protected boolean isNodeTypeValid(short type) {
|
84 |
236663
| return type == Node.ELEMENT_NODE
|
85 |
| || type == Node.COMMENT_NODE |
86 |
| || type == Node.TEXT_NODE |
87 |
| || type == Node.CDATA_SECTION_NODE |
88 |
| || type == Node.ENTITY_REFERENCE_NODE; |
89 |
| } |
90 |
| |
91 |
2772674
| protected void checkLoaded() {
|
92 |
2772675
| if (loaded) {
|
93 |
2503607
| return;
|
94 |
| } |
95 |
| |
96 |
269058
| loaded = true;
|
97 |
269058
| try {
|
98 |
269058
| if (data != null) {
|
99 |
130901
| DocumentImpl doc = (DocumentImpl) getOwnerDocument();
|
100 |
130901
| SymbolTable st = doc.getSymbols();
|
101 |
| |
102 |
130901
| ByteArrayInput bis = new ByteArrayInput(data, pos, len);
|
103 |
130901
| XMLCompressedInput in = new XMLCompressedInput(bis, st);
|
104 |
| |
105 |
130901
| in.readSignature();
|
106 |
130901
| in.readContentSize();
|
107 |
| |
108 |
130901
| symbolID = in.readShort();
|
109 |
130901
| SymbolTable.SymbolInfo si = st.getSymbolInfo(symbolID);
|
110 |
130901
| nodeName = si.getQName();
|
111 |
130901
| nsURI = si.getNamespaceURI();
|
112 |
| |
113 |
130901
| loadChildren(st);
|
114 |
| } |
115 |
| } catch (IOException e) { |
116 |
0
| if (log.isWarnEnabled()) {
|
117 |
0
| log.warn("ignored exception", e);
|
118 |
| } |
119 |
| } |
120 |
| } |
121 |
| |
122 |
0
| public short getSymbolID() {
|
123 |
0
| return symbolID;
|
124 |
| } |
125 |
| |
126 |
| |
127 |
| |
128 |
| |
129 |
543
| public void expandSource() {
|
130 |
543
| NodeSource src = getSource();
|
131 |
543
| if (src != null) {
|
132 |
519
| final String prefix = sourcePrefix("src", NodeSource.SOURCE_NS);
|
133 |
| |
134 |
519
| setAttribute(XMLNS_PREFIX + ":" + prefix, NodeSource.SOURCE_NS);
|
135 |
519
| setAttribute(prefix + ":" + NodeSource.SOURCE_COL, src.getCollection().getCanonicalName());
|
136 |
519
| Key k = src.getKey();
|
137 |
519
| if (k != null) {
|
138 |
519
| setAttribute(prefix + ":" + NodeSource.SOURCE_KEY, k.toString());
|
139 |
| } |
140 |
| } |
141 |
| } |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
| |
147 |
519
| private String sourcePrefix(final String candidatePrefix, final String nsuri) {
|
148 |
519
| Element element = this;
|
149 |
519
| HashSet prefixes = new HashSet();
|
150 |
519
| while (element != null) {
|
151 |
939
| NamedNodeMap nm = element.getAttributes();
|
152 |
939
| for (int i = 0; i < nm.getLength(); i++) {
|
153 |
846
| final Attr a = (Attr) nm.item(i);
|
154 |
846
| final String name = a.getNodeName();
|
155 |
846
| if (name.startsWith("xmlns:")) {
|
156 |
72
| final String prefix = name.substring(6);
|
157 |
72
| if (nsuri.equals(a.getValue())) {
|
158 |
63
| return prefix;
|
159 |
| } |
160 |
9
| prefixes.add(prefix);
|
161 |
| } |
162 |
| } |
163 |
876
| element = element.getParentNode().getNodeType() == ELEMENT_NODE ? (Element) element.getParentNode() : null;
|
164 |
| } |
165 |
| |
166 |
456
| String result = candidatePrefix;
|
167 |
456
| while (prefixes.contains(result)) {
|
168 |
3
| result = candidatePrefix + System.currentTimeMillis();
|
169 |
| } |
170 |
| |
171 |
456
| return result;
|
172 |
| } |
173 |
| |
174 |
130981
| protected void loadAttributes(SymbolTable st) throws IOException {
|
175 |
130981
| ByteArrayInput bis = new ByteArrayInput(data, pos, len);
|
176 |
130981
| XMLCompressedInput in = new XMLCompressedInput(bis, st);
|
177 |
| |
178 |
130981
| in.readSignature();
|
179 |
130981
| in.readContentSize();
|
180 |
130981
| in.readShort();
|
181 |
130981
| int attrCount = in.readAttributeCount();
|
182 |
| |
183 |
130981
| for (int i = 0; i < attrCount; i++) {
|
184 |
80745
| short symbol = in.readShort();
|
185 |
80745
| short strLen = in.readShort();
|
186 |
80745
| byte[] b = new byte[strLen];
|
187 |
80745
| in.read(b);
|
188 |
80745
| SymbolTable.SymbolInfo si = st.getSymbolInfo(symbol);
|
189 |
80745
| String name = si.getQName();
|
190 |
80745
| String nsURI = si.getNamespaceURI();
|
191 |
80745
| AttrImpl attr = new AttrImpl(this, name, nsURI, symbol, new String(b, "UTF-8"));
|
192 |
80745
| attributes.setNamedItem(attr);
|
193 |
| } |
194 |
| } |
195 |
| |
196 |
6122300
| public short getNodeType() {
|
197 |
6122295
| return Node.ELEMENT_NODE;
|
198 |
| } |
199 |
| |
200 |
| |
201 |
| |
202 |
| |
203 |
| |
204 |
463501
| public NamedNodeMap getAttributes() {
|
205 |
463501
| return attributes;
|
206 |
| } |
207 |
| |
208 |
48
| public boolean hasAttributes() {
|
209 |
48
| return attributes.size() > 0;
|
210 |
| } |
211 |
| |
212 |
0
| public boolean hasAttribute(String name) {
|
213 |
0
| return attributes.getNamedItem(name) != null;
|
214 |
| } |
215 |
| |
216 |
| |
217 |
| |
218 |
| |
219 |
| |
220 |
| |
221 |
| |
222 |
| |
223 |
| |
224 |
| |
225 |
0
| public void removeAttribute(String name) throws DOMException {
|
226 |
0
| checkReadOnly();
|
227 |
0
| AttrImpl attr = (AttrImpl) attributes.removeNamedItem(name);
|
228 |
0
| if (attr != null) {
|
229 |
0
| attr.setParentNode((NodeImpl) this.getOwnerDocument());
|
230 |
| } |
231 |
0
| setDirty();
|
232 |
| } |
233 |
| |
234 |
| |
235 |
| |
236 |
| |
237 |
| |
238 |
| |
239 |
| |
240 |
| |
241 |
| |
242 |
| |
243 |
18994
| public String getTagName() {
|
244 |
18994
| return getNodeName();
|
245 |
| } |
246 |
| |
247 |
| |
248 |
| |
249 |
| |
250 |
| |
251 |
| |
252 |
| |
253 |
761499
| public String getAttribute(String name) {
|
254 |
761499
| Attr attr = (Attr) attributes.getNamedItem(name);
|
255 |
761500
| return attr != null ? attr.getValue() : "";
|
256 |
| } |
257 |
| |
258 |
| |
259 |
| |
260 |
| |
261 |
| |
262 |
| |
263 |
| |
264 |
| |
265 |
| |
266 |
| |
267 |
| |
268 |
| |
269 |
| |
270 |
| |
271 |
| |
272 |
| |
273 |
| |
274 |
| |
275 |
137282
| public Attr setAttributeNode(Attr newAttr) throws DOMException {
|
276 |
137282
| checkReadOnly();
|
277 |
137282
| Attr oldAttr = (Attr) attributes.getNamedItem(newAttr.getName());
|
278 |
137282
| DocumentImpl doc = (DocumentImpl) getOwnerDocument();
|
279 |
137282
| if (newAttr.getParentNode().getNodeType() == Node.ELEMENT_NODE) {
|
280 |
0
| throw EX_INUSE_ATTRIBUTE;
|
281 |
| } |
282 |
137282
| if (doc != newAttr.getOwnerDocument()) {
|
283 |
0
| throw EX_WRONG_DOCUMENT;
|
284 |
| } |
285 |
137282
| ((AttrImpl) newAttr).setParentNode(this);
|
286 |
137282
| attributes.setNamedItem(newAttr);
|
287 |
137282
| setDirty();
|
288 |
137282
| return oldAttr;
|
289 |
| } |
290 |
| |
291 |
| |
292 |
| |
293 |
| |
294 |
| |
295 |
| |
296 |
| |
297 |
| |
298 |
| |
299 |
| |
300 |
| |
301 |
| |
302 |
| |
303 |
| |
304 |
| |
305 |
| |
306 |
| |
307 |
| |
308 |
| |
309 |
| |
310 |
131154
| public void setAttribute(String name, String value) throws DOMException {
|
311 |
131154
| Attr attr = getOwnerDocument().createAttribute(name);
|
312 |
131154
| attr.setValue(value);
|
313 |
131154
| setAttributeNode(attr);
|
314 |
| } |
315 |
| |
316 |
| |
317 |
| |
318 |
| |
319 |
| |
320 |
| |
321 |
| |
322 |
4
| public Attr getAttributeNode(String name) {
|
323 |
4
| return (Attr) attributes.getNamedItem(name);
|
324 |
| } |
325 |
| |
326 |
| |
327 |
| |
328 |
| |
329 |
| |
330 |
| |
331 |
| |
332 |
| |
333 |
| |
334 |
| |
335 |
| |
336 |
| |
337 |
0
| public Attr removeAttributeNode(Attr oldAttr) throws DOMException {
|
338 |
0
| checkReadOnly();
|
339 |
0
| if (oldAttr.getParentNode() != this) {
|
340 |
0
| throw EX_NOT_FOUND;
|
341 |
| } |
342 |
0
| attributes.remove(oldAttr.getName());
|
343 |
0
| ((AttrImpl) oldAttr).setParentNode((NodeImpl) this.getOwnerDocument());
|
344 |
0
| setDirty();
|
345 |
0
| return oldAttr;
|
346 |
| } |
347 |
| |
348 |
| |
349 |
| |
350 |
| |
351 |
| |
352 |
| |
353 |
| |
354 |
| |
355 |
| |
356 |
| |
357 |
| |
358 |
354
| public String getAttributeNS(String namespaceURI, String localName) {
|
359 |
354
| Attr attr = (Attr) attributes.getNamedItemNS(namespaceURI, localName);
|
360 |
354
| return attr != null ? attr.getValue() : "";
|
361 |
| } |
362 |
| |
363 |
| |
364 |
| |
365 |
| |
366 |
| |
367 |
| |
368 |
| |
369 |
| |
370 |
| |
371 |
| |
372 |
| |
373 |
| |
374 |
| |
375 |
| |
376 |
| |
377 |
| |
378 |
| |
379 |
| |
380 |
| |
381 |
| |
382 |
| |
383 |
| |
384 |
| |
385 |
| |
386 |
| |
387 |
| |
388 |
| |
389 |
| |
390 |
| |
391 |
| |
392 |
| |
393 |
| |
394 |
| |
395 |
| |
396 |
| |
397 |
| |
398 |
| |
399 |
| |
400 |
986
| public void setAttributeNS(String namespaceURI, String qualifiedName, String value) {
|
401 |
986
| Attr attr = getOwnerDocument().createAttributeNS(namespaceURI, qualifiedName);
|
402 |
986
| attr.setValue(value);
|
403 |
986
| setAttributeNodeNS(attr);
|
404 |
| } |
405 |
| |
406 |
| |
407 |
| |
408 |
| |
409 |
| |
410 |
| |
411 |
| |
412 |
| |
413 |
| |
414 |
| |
415 |
| |
416 |
| |
417 |
| |
418 |
| |
419 |
| |
420 |
| |
421 |
2
| public void removeAttributeNS(String namespaceURI, String localName) {
|
422 |
2
| checkReadOnly();
|
423 |
2
| AttrImpl attr = (AttrImpl) attributes.removeNamedItemNS(namespaceURI, localName);
|
424 |
2
| if (attr != null) {
|
425 |
0
| attr.setParentNode((NodeImpl) this.getOwnerDocument());
|
426 |
| } |
427 |
2
| setDirty();
|
428 |
| } |
429 |
| |
430 |
| |
431 |
| |
432 |
| |
433 |
| |
434 |
| |
435 |
| |
436 |
| |
437 |
| |
438 |
| |
439 |
| |
440 |
0
| public Attr getAttributeNodeNS(String namespaceURI, String localName) {
|
441 |
0
| return (Attr) attributes.getNamedItemNS(namespaceURI, localName);
|
442 |
| } |
443 |
| |
444 |
| |
445 |
| |
446 |
| |
447 |
| |
448 |
| |
449 |
| |
450 |
| |
451 |
| |
452 |
| |
453 |
| |
454 |
| |
455 |
| |
456 |
| |
457 |
| |
458 |
| |
459 |
| |
460 |
| |
461 |
| |
462 |
| |
463 |
| |
464 |
986
| public Attr setAttributeNodeNS(Attr newAttr) {
|
465 |
986
| return setAttributeNode(newAttr);
|
466 |
| } |
467 |
| |
468 |
| |
469 |
| |
470 |
| |
471 |
0
| public boolean hasAttributeNS(String namespaceURI, String localName) {
|
472 |
0
| return attributes.getNamedItemNS(namespaceURI, localName) != null;
|
473 |
| } |
474 |
| |
475 |
| |
476 |
| |
477 |
| |
478 |
| |
479 |
| |
480 |
| |
481 |
| |
482 |
0
| public TypeInfo getSchemaTypeInfo() {
|
483 |
0
| return null;
|
484 |
| } |
485 |
| |
486 |
| |
487 |
| |
488 |
| |
489 |
0
| public void setIdAttribute(String name, boolean isId) throws DOMException {
|
490 |
| } |
491 |
| |
492 |
| |
493 |
| |
494 |
| |
495 |
0
| public void setIdAttributeNS(String namespaceURI, String localName, boolean isId) throws DOMException {
|
496 |
| } |
497 |
| |
498 |
| |
499 |
| |
500 |
| |
501 |
0
| public void setIdAttributeNode(Attr idAttr, boolean isId) throws DOMException {
|
502 |
| } |
503 |
| |
504 |
| |
505 |
| |
506 |
| |
507 |
18
| public boolean isEqualNode(Node other) {
|
508 |
18
| if (!super.isEqualNode(other)) {
|
509 |
4
| return false;
|
510 |
| } |
511 |
| |
512 |
| |
513 |
14
| if (!hasAttributes() && !other.hasAttributes()) {
|
514 |
0
| return true;
|
515 |
| } |
516 |
14
| if (!hasAttributes() || !other.hasAttributes()) {
|
517 |
0
| return false;
|
518 |
| } |
519 |
| |
520 |
14
| NamedNodeMap attrMap = getAttributes();
|
521 |
14
| NamedNodeMap otherAttrMap = other.getAttributes();
|
522 |
14
| if (attrMap.getLength() != otherAttrMap.getLength()) {
|
523 |
0
| return false;
|
524 |
| } |
525 |
| |
526 |
14
| for (int i = 0; i < attrMap.getLength(); i++) {
|
527 |
28
| String name = attrMap.item(i).getNodeName();
|
528 |
28
| Node attr = otherAttrMap.getNamedItem(name);
|
529 |
28
| if (attr == null || !attrMap.item(i).getNodeValue().equals(attr.getNodeValue())) {
|
530 |
2
| return false;
|
531 |
| } |
532 |
| } |
533 |
| |
534 |
12
| return true;
|
535 |
| } |
536 |
| |
537 |
6
| Node renameNode(String namespaceURI, String qualifiedName, String prefix) throws DOMException {
|
538 |
6
| checkLoaded();
|
539 |
6
| checkReadOnly();
|
540 |
| |
541 |
6
| nodeName = qualifiedName;
|
542 |
6
| if (prefix != null) {
|
543 |
4
| setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:" + prefix, namespaceURI);
|
544 |
4
| nsURI = namespaceURI;
|
545 |
| } else { |
546 |
2
| removeAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:" + nsURI);
|
547 |
2
| nsURI = null;
|
548 |
| } |
549 |
| |
550 |
6
| invokeHandlers(UserDataHandler.NODE_RENAMED, this, null);
|
551 |
6
| return this;
|
552 |
| } |
553 |
| } |