Debian Codegraph @

rhino/xmlimplsrc/org/mozilla/javascript/xmlimpl/XMLList.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 package org.mozilla.javascript.xmlimpl;
8
9 import org.mozilla.javascript.*;
10 import org.mozilla.javascript.xml.*;
11 import java.util.ArrayList;
12
13 class XMLList extends XMLObjectImpl implements Function {
14 static final long serialVersionUID = -4543618751670781135L;
15
16 private XmlNode.InternalList _annos;
17 private XMLObjectImpl targetObject = null;
18 private XmlNode.QName targetProperty = null;
19
20 XMLList(XMLLibImpl lib, Scriptable scope, XMLObject prototype) {
21 super(lib, scope, prototype);
22 _annos = new XmlNode.InternalList();
23 }
24
25 /* TODO Will probably end up unnecessary as we move things around */
26 XmlNode.InternalList getNodeList() {
27 return _annos;
28 }
29
30 // TODO Should be XMLObjectImpl, XMLName?
31 void setTargets(XMLObjectImpl object, XmlNode.QName property) {
32 targetObject = object;
33 targetProperty = property;
34 }
35
36 /* TODO: original author marked this as deprecated */
37 private XML getXmlFromAnnotation(int index) {
38 return getXML(_annos, index);
39 }
40
41 @Override
42 XML getXML() {
43 if (length() == 1) return getXmlFromAnnotation(0);
44 return null;
45 }
46
47 private void internalRemoveFromList(int index) {
48 _annos.remove(index);
49 }
50
51 void replace(int index, XML xml) {
52 if (index < length()) {
53 XmlNode.InternalList newAnnoList = new XmlNode.InternalList();
54 newAnnoList.add(_annos, 0, index);
55 newAnnoList.add(xml);
56 newAnnoList.add(_annos, index+1, length());
57 _annos = newAnnoList;
58 }
59 }
60
61 private void insert(int index, XML xml) {
62 if (index < length()) {
63 XmlNode.InternalList newAnnoList = new XmlNode.InternalList();
64 newAnnoList.add(_annos, 0, index);
65 newAnnoList.add(xml);
66 newAnnoList.add(_annos, index, length());
67 _annos = newAnnoList;
68 }
69 }
70
71 //
72 //
73 // methods overriding ScriptableObject
74 //
75 //
76
77 @Override
78 public String getClassName() {
79 return "XMLList";
80 }
81
82 //
83 //
84 // methods overriding IdScriptableObject
85 //
86 //
87
88 @Override
89 public Object get(int index, Scriptable start) {
90 //Log("get index: " + index);
91
92 if (index >= 0 && index < length()) {
93 return getXmlFromAnnotation(index);
94 } else {
95 return Scriptable.NOT_FOUND;
96 }
97 }
98
99 @Override
100 boolean hasXMLProperty(XMLName xmlName) {
101 // Has should return true if the property would have results > 0
102 return getPropertyList(xmlName).length() > 0;
103 }
104
105 @Override
106 public boolean has(int index, Scriptable start) {
107 return 0 <= index && index < length();
108 }
109
110 @Override
111 void putXMLProperty(XMLName xmlName, Object value) {
112 //Log("put property: " + name);
113
114 // Special-case checks for undefined and null
115 if (value == null) {
116 value = "null";
117 } else if (value instanceof Undefined) {
118 value = "undefined";
119 }
120
121 if (length() > 1) {
122 throw ScriptRuntime.typeError(
123 "Assignment to lists with more than one item is not supported");
124 } else if (length() == 0) {
125 // Secret sauce for super-expandos.
126 // We set an element here, and then add ourselves to our target.
127 if (targetObject != null && targetProperty != null &&
128 targetProperty.getLocalName() != null &&
129 targetProperty.getLocalName().length() > 0)
130 {
131 // Add an empty element with our targetProperty name and
132 // then set it.
133 XML xmlValue = newTextElementXML(null, targetProperty, null);
134 addToList(xmlValue);
135
136 if(xmlName.isAttributeName()) {
137 setAttribute(xmlName, value);
138 } else {
139 XML xml = item(0);
140 xml.putXMLProperty(xmlName, value);
141
142 // Update the list with the new item at location 0.
143 replace(0, item(0));
144 }
145
146 // Now add us to our parent
147 XMLName name2 = XMLName.formProperty(
148 targetProperty.getNamespace().getUri(),
149 targetProperty.getLocalName());
150 targetObject.putXMLProperty(name2, this);
151 replace(0, targetObject.getXML().getLastXmlChild());
152 } else {
153 throw ScriptRuntime.typeError(
154 "Assignment to empty XMLList without targets not supported");
155 }
156 } else if(xmlName.isAttributeName()) {
157 setAttribute(xmlName, value);
158 } else {
159 XML xml = item(0);
160 xml.putXMLProperty(xmlName, value);
161
162 // Update the list with the new item at location 0.
163 replace(0, item(0));
164 }
165 }
166
167 @Override
168 Object getXMLProperty(XMLName name) {
169 return getPropertyList(name);
170 }
171
172 private void replaceNode(XML xml, XML with) {
173 xml.replaceWith(with);
174 }
175
176 @Override
177 public void put(int index, Scriptable start, Object value) {
178 Object parent = Undefined.instance;
179 // Convert text into XML if needed.
180 XMLObject xmlValue;
181
182 // Special-case checks for undefined and null
183 if (value == null) {
184 value = "null";
185 } else if (value instanceof Undefined) {
186 value = "undefined";
187 }
188
189 if (value instanceof XMLObject) {
190 xmlValue = (XMLObject) value;
191 } else {
192 if (targetProperty == null) {
193 xmlValue = newXMLFromJs(value.toString());
194 } else {
195 // Note that later in the code, we will use this as an argument to replace(int,value)
196 // So we will be "replacing" this element with itself
197 // There may well be a better way to do this
198 // TODO Find a way to refactor this whole method and simplify it
199 xmlValue = item(index);
200 if (xmlValue == null) {
201 XML x = item(0);
202 xmlValue = x == null
203 ? newTextElementXML(null,targetProperty,null)
204 : x.copy();
205 }
206 ((XML)xmlValue).setChildren(value);
207 }
208 }
209
210 // Find the parent
211 if (index < length()) {
212 parent = item(index).parent();
213 } else if (length() == 0) {
214 parent = targetObject != null ? targetObject.getXML() : parent();
215 } else {
216 // Appending
217 parent = parent();
218 }
219
220 if (parent instanceof XML) {
221 // found parent, alter doc
222 XML xmlParent = (XML) parent;
223
224 if (index < length()) {
225 // We're replacing the the node.
226 XML xmlNode = getXmlFromAnnotation(index);
227
228 if (xmlValue instanceof XML) {
229 replaceNode(xmlNode, (XML) xmlValue);
230 replace(index, xmlNode);
231 } else if (xmlValue instanceof XMLList) {
232 // Replace the first one, and add the rest on the list.
233 XMLList list = (XMLList) xmlValue;
234
235 if (list.length() > 0) {
236 int lastIndexAdded = xmlNode.childIndex();
237 replaceNode(xmlNode, list.item(0));
238 replace(index, list.item(0));
239
240 for (int i = 1; i < list.length(); i++) {
241 xmlParent.insertChildAfter(xmlParent.getXmlChild(lastIndexAdded), list.item(i));
242 lastIndexAdded++;
243 insert(index + i, list.item(i));
244 }
245 }
246 }
247 } else {
248 // Appending
249 xmlParent.appendChild(xmlValue);
250 addToList(xmlParent.getLastXmlChild());
251 }
252 } else {
253 // Don't all have same parent, no underlying doc to alter
254 if (index < length()) {
255 XML xmlNode = getXML(_annos, index);
256
257 if (xmlValue instanceof XML) {
258 replaceNode(xmlNode, (XML) xmlValue);
259 replace(index, xmlNode);
260 } else if (xmlValue instanceof XMLList) {
261 // Replace the first one, and add the rest on the list.
262 XMLList list = (XMLList) xmlValue;
263
264 if (list.length() > 0) {
265 replaceNode(xmlNode, list.item(0));
266 replace(index, list.item(0));
267
268 for (int i = 1; i < list.length(); i++) {
269 insert(index + i, list.item(i));
270 }
271 }
272 }
273 } else {
274 addToList(xmlValue);
275 }
276 }
277 }
278
279 private XML getXML(XmlNode.InternalList _annos, int index) {
280 if (index >= 0 && index < length()) {
281 return xmlFromNode(_annos.item(index));
282 } else {
283 return null;
284 }
285 }
286
287 @Override
288 void deleteXMLProperty(XMLName name) {
289 for (int i = 0; i < length(); i++) {
290 XML xml = getXmlFromAnnotation(i);
291
292 if (xml.isElement()) {
293 xml.deleteXMLProperty(name);
294 }
295 }
296 }
297
298 @Override
299 public void delete(int index) {
300 if (index >= 0 && index < length()) {
301 XML xml = getXmlFromAnnotation(index);
302
303 xml.remove();
304
305 internalRemoveFromList(index);
306 }
307 }
308
309 @Override
310 public Object[] getIds() {
311 Object enumObjs[];
312
313 if (isPrototype()) {
314 enumObjs = new Object[0];
315 } else {
316 enumObjs = new Object[length()];
317
318 for (int i = 0; i < enumObjs.length; i++) {
319 enumObjs[i] = Integer.valueOf(i);
320 }
321 }
322
323 return enumObjs;
324 }
325
326 public Object[] getIdsForDebug() {
327 return getIds();
328 }
329
330
331 // XMLList will remove will delete all items in the list (a set delete) this differs from the XMLList delete operator.
332 void remove() {
333 int nLen = length();
334 for (int i = nLen - 1; i >= 0; i--) {
335 XML xml = getXmlFromAnnotation(i);
336 if (xml != null) {
337 xml.remove();
338 internalRemoveFromList(i);
339 }
340 }
341 }
342
343 XML item(int index) {
344 return _annos != null
345 ? getXmlFromAnnotation(index) : createEmptyXML();
346 }
347
348 private void setAttribute(XMLName xmlName, Object value) {
349 for (int i = 0; i < length(); i++) {
350 XML xml = getXmlFromAnnotation(i);
351 xml.setAttribute(xmlName, value);
352 }
353 }
354
355 void addToList(Object toAdd) {
356 _annos.addToList(toAdd);
357 }
358
359 //
360 //
361 // Methods from section 12.4.4 in the spec
362 //
363 //
364
365 @Override
366 XMLList child(int index) {
367 XMLList result = newXMLList();
368
369 for (int i = 0; i < length(); i++) {
370 result.addToList(getXmlFromAnnotation(i).child(index));
371 }
372
373 return result;
374 }
375
376 @Override
377 XMLList child(XMLName xmlName) {
378 XMLList result = newXMLList();
379
380 for (int i = 0; i < length(); i++) {
381 result.addToList(getXmlFromAnnotation(i).child(xmlName));
382 }
383
384 return result;
385 }
386
387 @Override
388 void addMatches(XMLList rv, XMLName name) {
389 for (int i=0; i<length(); i++) {
390 getXmlFromAnnotation(i).addMatches(rv, name);
391 }
392 }
393
394 @Override
395 XMLList children() {
396 ArrayList<XML> list = new ArrayList<XML>();
397
398 for (int i = 0; i < length(); i++) {
399 XML xml = getXmlFromAnnotation(i);
400
401 if (xml != null) {
402 XMLList childList = xml.children();
403
404 int cChildren = childList.length();
405 for (int j = 0; j < cChildren; j++) {
406 list.add(childList.item(j));
407 }
408 }
409 }
410
411 XMLList allChildren = newXMLList();
412 int sz = list.size();
413
414 for (int i = 0; i < sz; i++) {
415 allChildren.addToList(list.get(i));
416 }
417
418 return allChildren;
419 }
420
421 @Override
422 XMLList comments() {
423 XMLList result = newXMLList();
424
425 for (int i = 0; i < length(); i++) {
426 XML xml = getXmlFromAnnotation(i);
427 result.addToList(xml.comments());
428 }
429
430 return result;
431 }
432
433 @Override
434 XMLList elements(XMLName name) {
435 XMLList rv = newXMLList();
436 for (int i=0; i<length(); i++) {
437 XML xml = getXmlFromAnnotation(i);
438 rv.addToList(xml.elements(name));
439 }
440 return rv;
441 }
442
443 @Override
444 boolean contains(Object xml) {
445 boolean result = false;
446
447 for (int i = 0; i < length(); i++) {
448 XML member = getXmlFromAnnotation(i);
449
450 if (member.equivalentXml(xml)) {
451 result = true;
452 break;
453 }
454 }
455
456 return result;
457 }
458
459 @Override
460 XMLObjectImpl copy() {
461 XMLList result = newXMLList();
462
463 for (int i = 0; i < length(); i++) {
464 XML xml = getXmlFromAnnotation(i);
465 result.addToList(xml.copy());
466 }
467
468 return result;
469 }
470
471 @Override
472 boolean hasOwnProperty(XMLName xmlName) {
473 if (isPrototype()) {
474 String property = xmlName.localName();
475 return (findPrototypeId(property) != 0);
476 } else {
477 return (getPropertyList(xmlName).length() > 0);
478 }
479 }
480
481 @Override
482 boolean hasComplexContent() {
483 boolean complexContent;
484 int length = length();
485
486 if (length == 0) {
487 complexContent = false;
488 } else if (length == 1) {
489 complexContent = getXmlFromAnnotation(0).hasComplexContent();
490 } else {
491 complexContent = false;
492
493 for (int i = 0; i < length; i++) {
494 XML nextElement = getXmlFromAnnotation(i);
495 if (nextElement.isElement()) {
496 complexContent = true;
497 break;
498 }
499 }
500 }
501
502 return complexContent;
503 }
504
505 @Override
506 boolean hasSimpleContent() {
507 if (length() == 0) {
508 return true;
509 } else if (length() == 1) {
510 return getXmlFromAnnotation(0).hasSimpleContent();
511 } else {
512 for (int i=0; i<length(); i++) {
513 XML nextElement = getXmlFromAnnotation(i);
514 if (nextElement.isElement()) {
515 return false;
516 }
517 }
518 return true;
519 }
520 }
521
522 @Override
523 int length() {
524 int result = 0;
525
526 if (_annos != null) {
527 result = _annos.length();
528 }
529
530 return result;
531 }
532
533 @Override
534 void normalize() {
535 for (int i = 0; i < length(); i++) {
536 getXmlFromAnnotation(i).normalize();
537 }
538 }
539
540 /**
541 * If list is empty, return undefined, if elements have different parents return undefined,
542 * If they all have the same parent, return that parent
543 */
544 @Override
545 Object parent() {
546 if (length() == 0) return Undefined.instance;
547
548 XML candidateParent = null;
549
550 for (int i = 0; i < length(); i++) {
551 Object currParent = getXmlFromAnnotation(i).parent();
552 if (!(currParent instanceof XML)) return Undefined.instance;
553 XML xml = (XML)currParent;
554 if (i == 0) {
555 // Set the first for the rest to compare to.
556 candidateParent = xml;
557 } else {
558 if (candidateParent.is(xml)) {
559 // keep looking
560 } else {
561 return Undefined.instance;
562 }
563 }
564 }
565 return candidateParent;
566 }
567
568 @Override
569 XMLList processingInstructions(XMLName xmlName) {
570 XMLList result = newXMLList();
571
572 for (int i = 0; i < length(); i++) {
573 XML xml = getXmlFromAnnotation(i);
574
575 result.addToList(xml.processingInstructions(xmlName));
576 }
577
578 return result;
579 }
580
581 @Override
582 boolean propertyIsEnumerable(Object name) {
583 long index;
584 if (name instanceof Integer) {
585 index = ((Integer)name).intValue();
586 } else if (name instanceof Number) {
587 double x = ((Number)name).doubleValue();
588 index = (long)x;
589 if (index != x) {
590 return false;
591 }
592 if (index == 0 && 1.0 / x < 0) {
593 // Negative 0
594 return false;
595 }
596 } else {
597 String s = ScriptRuntime.toString(name);
598 index = ScriptRuntime.testUint32String(s);
599 }
600 return (0 <= index && index < length());
601 }
602
603 @Override
604 XMLList text() {
605 XMLList result = newXMLList();
606
607 for (int i = 0; i < length(); i++) {
608 result.addToList(getXmlFromAnnotation(i).text());
609 }
610
611 return result;
612 }
613
614 @Override
615 public String toString() {
616 // ECMA357 10.1.2
617 if (hasSimpleContent()) {
618 StringBuilder sb = new StringBuilder();
619
620 for(int i = 0; i < length(); i++) {
621 XML next = getXmlFromAnnotation(i);
622 if (next.isComment() || next.isProcessingInstruction()) {
623 // do nothing
624 } else {
625 sb.append(next.toString());
626 }
627 }
628
629 return sb.toString();
630 } else {
631 return toXMLString();
632 }
633 }
634
635 @Override
636 String toSource(int indent) {
637 return toXMLString();
638 }
639
640 @Override
641 String toXMLString() {
642 // See ECMA 10.2.1
643 StringBuilder sb = new StringBuilder();
644
645 for (int i=0; i<length(); i++) {
646 if (getProcessor().isPrettyPrinting() && i != 0) {
647 sb.append('\n');
648 }
649 sb.append(getXmlFromAnnotation(i).toXMLString());
650 }
651 return sb.toString();
652 }
653
654 @Override
655 Object valueOf() {
656 return this;
657 }
658
659 //
660 // Other public Functions from XMLObject
661 //
662
663 @Override
664 boolean equivalentXml(Object target) {
665 boolean result = false;
666
667 // Zero length list should equate to undefined
668 if (target instanceof Undefined && length() == 0) {
669 result = true;
670 } else if (length() == 1) {
671 result = getXmlFromAnnotation(0).equivalentXml(target);
672 } else if (target instanceof XMLList) {
673 XMLList otherList = (XMLList) target;
674
675 if (otherList.length() == length()) {
676 result = true;
677
678 for (int i = 0; i < length(); i++) {
679 if (!getXmlFromAnnotation(i).equivalentXml(otherList.getXmlFromAnnotation(i))) {
680 result = false;
681 break;
682 }
683 }
684 }
685 }
686
687 return result;
688 }
689
690 private XMLList getPropertyList(XMLName name) {
691 XMLList propertyList = newXMLList();
692 XmlNode.QName qname = null;
693
694 if (!name.isDescendants() && !name.isAttributeName()) {
695 // Only set the targetProperty if this is a regular child get
696 // and not a descendant or attribute get
697 qname = name.toQname();
698 }
699
700 propertyList.setTargets(this, qname);
701
702 for (int i = 0; i < length(); i++) {
703 propertyList.addToList(
704 getXmlFromAnnotation(i).getPropertyList(name));
705 }
706
707 return propertyList;
708 }
709
710 private Object applyOrCall(boolean isApply,
711 Context cx, Scriptable scope,
712 Scriptable thisObj, Object[] args) {
713 String methodName = isApply ? "apply" : "call";
714 if(!(thisObj instanceof XMLList) ||
715 ((XMLList)thisObj).targetProperty == null)
716 throw ScriptRuntime.typeError1("msg.isnt.function",
717 methodName);
718
719 return ScriptRuntime.applyOrCall(isApply, cx, scope, thisObj, args);
720 }
721
722 @Override
723 protected Object jsConstructor(Context cx, boolean inNewExpr,
724 Object[] args)
725 {
726 if (args.length == 0) {
727 return newXMLList();
728 } else {
729 Object arg0 = args[0];
730 if (!inNewExpr && arg0 instanceof XMLList) {
731 // XMLList(XMLList) returns the same object.
732 return arg0;
733 }
734 return newXMLListFrom(arg0);
735 }
736 }
737
738 /**
739 * See ECMA 357, 11_2_2_1, Semantics, 3_e.
740 */
741 @Override
742 public Scriptable getExtraMethodSource(Context cx) {
743 if (length() == 1) {
744 return getXmlFromAnnotation(0);
745 }
746 return null;
747 }
748
749 public Object call(Context cx, Scriptable scope, Scriptable thisObj,
750 Object[] args) {
751 // This XMLList is being called as a Function.
752 // Let's find the real Function object.
753 if(targetProperty == null)
754 throw ScriptRuntime.notFunctionError(this);
755
756 String methodName = targetProperty.getLocalName();
757
758 boolean isApply = methodName.equals("apply");
759 if(isApply || methodName.equals("call"))
760 return applyOrCall(isApply, cx, scope, thisObj, args);
761
762 if (!(thisObj instanceof XMLObject)) {
763 throw ScriptRuntime.typeError1("msg.incompat.call", methodName);
764 }
765 Object func = null;
766 Scriptable sobj = thisObj;
767
768 while (sobj instanceof XMLObject) {
769 XMLObject xmlObject = (XMLObject) sobj;
770 func = xmlObject.getFunctionProperty(cx, methodName);
771 if (func != Scriptable.NOT_FOUND) {
772 break;
773 }
774 sobj = xmlObject.getExtraMethodSource(cx);
775 if (sobj != null) {
776 thisObj = sobj;
777 if (!(sobj instanceof XMLObject)) {
778 func = ScriptableObject.getProperty(sobj, methodName);
779 }
780 }
781 }
782
783 if (!(func instanceof Callable)) {
784 throw ScriptRuntime.notFunctionError(thisObj, func, methodName);
785 }
786 return ((Callable)func).call(cx, scope, thisObj, args);
787 }
788
789 public Scriptable construct(Context cx, Scriptable scope, Object[] args) {
790 throw ScriptRuntime.typeError1("msg.not.ctor", "XMLList");
791 }
792 }
793
794