Debian Codegraph @

rhino/xmlimplsrc/org/mozilla/javascript/xmlimpl/XMLObjectImpl.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
12 /**
13 * This abstract class describes what all XML objects (XML, XMLList) should
14 * have in common.
15 *
16 * @see XML
17 */
18 abstract class XMLObjectImpl extends XMLObject {
19 private static final Object XMLOBJECT_TAG = "XMLObject";
20 private XMLLibImpl lib;
21 private boolean prototypeFlag;
22
23 protected XMLObjectImpl(XMLLibImpl lib, Scriptable scope,
24 XMLObject prototype)
25 {
26 initialize(lib, scope, prototype);
27 }
28
29 final void initialize(XMLLibImpl lib, Scriptable scope,
30 XMLObject prototype)
31 {
32 setParentScope(scope);
33 setPrototype(prototype);
34 prototypeFlag = (prototype == null);
35 this.lib = lib;
36 }
37
38 final boolean isPrototype() {
39 return prototypeFlag;
40 }
41
42 XMLLibImpl getLib() {
43 return lib;
44 }
45
46 final XML newXML(XmlNode node) {
47 return lib.newXML(node);
48 }
49
50 XML xmlFromNode(XmlNode node) {
51 if (node.getXml() == null) {
52 node.setXml( newXML(node) );
53 }
54 return node.getXml();
55 }
56
57 final XMLList newXMLList() {
58 return lib.newXMLList();
59 }
60
61 final XMLList newXMLListFrom(Object o) {
62 return lib.newXMLListFrom(o);
63 }
64
65 final XmlProcessor getProcessor() {
66 return lib.getProcessor();
67 }
68
69 final QName newQName(String uri, String localName, String prefix) {
70 return lib.newQName(uri, localName, prefix);
71 }
72
73 final QName newQName(XmlNode.QName name) {
74 return lib.newQName(name);
75 }
76
77 final Namespace createNamespace(XmlNode.Namespace declaration) {
78 if (declaration == null) return null;
79 return lib.createNamespaces( new XmlNode.Namespace[] { declaration } )[0];
80 }
81
82 final Namespace[] createNamespaces(XmlNode.Namespace[] declarations) {
83 return lib.createNamespaces(declarations);
84 }
85
86
87 @Override
88 public final Scriptable getPrototype() {
89 return super.getPrototype();
90 }
91
92 @Override
93 public final void setPrototype(Scriptable prototype) {
94 super.setPrototype(prototype);
95 }
96
97 @Override
98 public final Scriptable getParentScope() {
99 return super.getParentScope();
100 }
101
102 @Override
103 public final void setParentScope(Scriptable parent) {
104 super.setParentScope(parent);
105 }
106
107 @Override
108 public final Object getDefaultValue(Class<?> hint) {
109 return this.toString();
110 }
111
112 @Override
113 public final boolean hasInstance(Scriptable scriptable) {
114 return super.hasInstance(scriptable);
115 }
116
117 /**
118 * ecmaHas(cx, id) calls this after resolving when id to XMLName
119 * and checking it is not Uint32 index.
120 */
121 abstract boolean hasXMLProperty(XMLName name);
122
123 /**
124 * ecmaGet(cx, id) calls this after resolving when id to XMLName
125 * and checking it is not Uint32 index.
126 */
127 abstract Object getXMLProperty(XMLName name);
128
129 /**
130 * ecmaPut(cx, id, value) calls this after resolving when id to XMLName
131 * and checking it is not Uint32 index.
132 */
133 abstract void putXMLProperty(XMLName name, Object value);
134
135 /**
136 * ecmaDelete(cx, id) calls this after resolving when id to XMLName
137 * and checking it is not Uint32 index.
138 */
139 abstract void deleteXMLProperty(XMLName name);
140
141 /**
142 * Test XML equality with target the target.
143 */
144 abstract boolean equivalentXml(Object target);
145
146 abstract void addMatches(XMLList rv, XMLName name);
147
148 private XMLList getMatches(XMLName name) {
149 XMLList rv = newXMLList();
150 addMatches(rv, name);
151 return rv;
152 }
153
154 abstract XML getXML();
155
156 // Methods from section 12.4.4 in the spec
157 abstract XMLList child(int index);
158 abstract XMLList child(XMLName xmlName);
159 abstract XMLList children();
160 abstract XMLList comments();
161 abstract boolean contains(Object xml);
162 abstract XMLObjectImpl copy();
163 abstract XMLList elements(XMLName xmlName);
164 abstract boolean hasOwnProperty(XMLName xmlName);
165 abstract boolean hasComplexContent();
166 abstract boolean hasSimpleContent();
167 abstract int length();
168 abstract void normalize();
169 abstract Object parent();
170 abstract XMLList processingInstructions(XMLName xmlName);
171 abstract boolean propertyIsEnumerable(Object member);
172 abstract XMLList text();
173 @Override
174 public abstract String toString();
175 abstract String toSource(int indent);
176 abstract String toXMLString();
177 abstract Object valueOf();
178
179 protected abstract Object jsConstructor(Context cx, boolean inNewExpr, Object[] args);
180
181 //
182 //
183 // Methods overriding ScriptableObject
184 //
185 //
186
187 /**
188 * XMLObject always compare with any value and equivalentValues
189 * never returns {@link Scriptable#NOT_FOUND} for them but rather
190 * calls equivalentXml(value) and wrap the result as Boolean.
191 */
192 @Override
193 protected final Object equivalentValues(Object value) {
194 boolean result = equivalentXml(value);
195 return result ? Boolean.TRUE : Boolean.FALSE;
196 }
197
198 //
199 //
200 // Methods overriding XMLObject
201 //
202 //
203
204 /**
205 * Implementation of ECMAScript [[Has]]
206 */
207 @Override
208 public final boolean has(Context cx, Object id) {
209 if (cx == null) cx = Context.getCurrentContext();
210 XMLName xmlName = lib.toXMLNameOrIndex(cx, id);
211 if (xmlName == null) {
212 long index = ScriptRuntime.lastUint32Result(cx);
213 // XXX Fix this cast
214 return has((int)index, this);
215 }
216 return hasXMLProperty(xmlName);
217 }
218
219 @Override
220 public boolean has(String name, Scriptable start) {
221 Context cx = Context.getCurrentContext();
222 return hasXMLProperty(lib.toXMLNameFromString(cx, name));
223 }
224 /**
225 * Implementation of ECMAScript [[Get]]
226 */
227 @Override
228 public final Object get(Context cx, Object id) {
229 if (cx == null) cx = Context.getCurrentContext();
230 XMLName xmlName = lib.toXMLNameOrIndex(cx, id);
231 if (xmlName == null) {
232 long index = ScriptRuntime.lastUint32Result(cx);
233 // XXX Fix this cast
234 Object result = get((int)index, this);
235 if (result == Scriptable.NOT_FOUND) {
236 result = Undefined.instance;
237 }
238 return result;
239 }
240 return getXMLProperty(xmlName);
241 }
242
243 @Override
244 public Object get(String name, Scriptable start) {
245 Context cx = Context.getCurrentContext();
246 return getXMLProperty(lib.toXMLNameFromString(cx, name));
247 }
248 /**
249 * Implementation of ECMAScript [[Put]]
250 */
251 @Override
252 public final void put(Context cx, Object id, Object value) {
253 if (cx == null) cx = Context.getCurrentContext();
254 XMLName xmlName = lib.toXMLNameOrIndex(cx, id);
255 if (xmlName == null) {
256 long index = ScriptRuntime.lastUint32Result(cx);
257 // XXX Fix this cast
258 put((int)index, this, value);
259 return;
260 }
261 putXMLProperty(xmlName, value);
262 }
263
264 @Override
265 public void put(String name, Scriptable start, Object value) {
266 Context cx = Context.getCurrentContext();
267 putXMLProperty(lib.toXMLNameFromString(cx, name), value);
268 }
269 /**
270 * Implementation of ECMAScript [[Delete]].
271 */
272 @Override
273 public final boolean delete(Context cx, Object id) {
274 if (cx == null) cx = Context.getCurrentContext();
275 XMLName xmlName = lib.toXMLNameOrIndex(cx, id);
276 if (xmlName == null) {
277 long index = ScriptRuntime.lastUint32Result(cx);
278 // XXX Fix this
279 delete((int)index);
280 return true;
281 }
282 deleteXMLProperty(xmlName);
283 return true;
284 }
285
286
287 @Override
288 public void delete(String name) {
289 Context cx = Context.getCurrentContext();
290 deleteXMLProperty(lib.toXMLNameFromString(cx, name));
291 }
292
293 @Override
294 public Object getFunctionProperty(Context cx, int id) {
295 if (isPrototype()) {
296 return super.get(id, this);
297 } else {
298 Scriptable proto = getPrototype();
299 if (proto instanceof XMLObject) {
300 return ((XMLObject)proto).getFunctionProperty(cx, id);
301 }
302 }
303 return NOT_FOUND;
304 }
305
306 @Override
307 public Object getFunctionProperty(Context cx, String name) {
308 if (isPrototype()) {
309 return super.get(name, this);
310 } else {
311 Scriptable proto = getPrototype();
312 if (proto instanceof XMLObject) {
313 return ((XMLObject)proto).getFunctionProperty(cx, name);
314 }
315 }
316 return NOT_FOUND;
317 }
318
319 // TODO Can this be made more strongly typed?
320 @Override
321 public Ref memberRef(Context cx, Object elem, int memberTypeFlags) {
322 boolean attribute = (memberTypeFlags & Node.ATTRIBUTE_FLAG) != 0;
323 boolean descendants = (memberTypeFlags & Node.DESCENDANTS_FLAG) != 0;
324 if (!attribute && !descendants) {
325 // Code generation would use ecma(Get|Has|Delete|Set) for
326 // normal name identifiers so one ATTRIBUTE_FLAG
327 // or DESCENDANTS_FLAG has to be set
328 throw Kit.codeBug();
329 }
330 XmlNode.QName qname = lib.toNodeQName(cx, elem, attribute);
331 XMLName rv = XMLName.create(qname, attribute, descendants);
332 rv.initXMLObject(this);
333 return rv;
334 }
335
336 /**
337 * Generic reference to implement x::ns, x.@ns::y, x..@ns::y etc.
338 */
339 @Override
340 public Ref memberRef(Context cx, Object namespace, Object elem,
341 int memberTypeFlags)
342 {
343 boolean attribute = (memberTypeFlags & Node.ATTRIBUTE_FLAG) != 0;
344 boolean descendants = (memberTypeFlags & Node.DESCENDANTS_FLAG) != 0;
345 XMLName rv = XMLName.create(lib.toNodeQName(cx, namespace, elem),
346 attribute, descendants);
347 rv.initXMLObject(this);
348 return rv;
349 }
350
351 @Override
352 public NativeWith enterWith(Scriptable scope) {
353 return new XMLWithScope(lib, scope, this);
354 }
355
356 @Override
357 public NativeWith enterDotQuery(Scriptable scope) {
358 XMLWithScope xws = new XMLWithScope(lib, scope, this);
359 xws.initAsDotQuery();
360 return xws;
361 }
362
363 @Override
364 public final Object addValues(Context cx, boolean thisIsLeft,
365 Object value) {
366 if (value instanceof XMLObject) {
367 XMLObject v1, v2;
368 if (thisIsLeft) {
369 v1 = this;
370 v2 = (XMLObject)value;
371 } else {
372 v1 = (XMLObject)value;
373 v2 = this;
374 }
375 return lib.addXMLObjects(cx, v1, v2);
376 }
377 if (value == Undefined.instance) {
378 // both "xml + undefined" and "undefined + xml" gives String(xml)
379 return ScriptRuntime.toString(this);
380 }
381
382 return super.addValues(cx, thisIsLeft, value);
383 }
384
385 //
386 //
387 // IdScriptableObject machinery
388 //
389 //
390
391 final void exportAsJSClass(boolean sealed) {
392 prototypeFlag = true;
393 exportAsJSClass(MAX_PROTOTYPE_ID, getParentScope(), sealed);
394 }
395
396 // #string_id_map#
397 private final static int
398 Id_constructor = 1,
399
400 Id_addNamespace = 2,
401 Id_appendChild = 3,
402 Id_attribute = 4,
403 Id_attributes = 5,
404 Id_child = 6,
405 Id_childIndex = 7,
406 Id_children = 8,
407 Id_comments = 9,
408 Id_contains = 10,
409 Id_copy = 11,
410 Id_descendants = 12,
411 Id_elements = 13,
412 Id_inScopeNamespaces = 14,
413 Id_insertChildAfter = 15,
414 Id_insertChildBefore = 16,
415 Id_hasOwnProperty = 17,
416 Id_hasComplexContent = 18,
417 Id_hasSimpleContent = 19,
418 Id_length = 20,
419 Id_localName = 21,
420 Id_name = 22,
421 Id_namespace = 23,
422 Id_namespaceDeclarations = 24,
423 Id_nodeKind = 25,
424 Id_normalize = 26,
425 Id_parent = 27,
426 Id_prependChild = 28,
427 Id_processingInstructions = 29,
428 Id_propertyIsEnumerable = 30,
429 Id_removeNamespace = 31,
430 Id_replace = 32,
431 Id_setChildren = 33,
432 Id_setLocalName = 34,
433 Id_setName = 35,
434 Id_setNamespace = 36,
435 Id_text = 37,
436 Id_toString = 38,
437 Id_toSource = 39,
438 Id_toXMLString = 40,
439 Id_valueOf = 41,
440
441 MAX_PROTOTYPE_ID = 41;
442
443 @Override
444 protected int findPrototypeId(String s) {
445 int id;
446 // #generated# Last update: 2008-10-21 12:32:31 MESZ
447 L0: { id = 0; String X = null; int c;
448 L: switch (s.length()) {
449 case 4: c=s.charAt(0);
450 if (c=='c') { X="copy";id=Id_copy; }
451 else if (c=='n') { X="name";id=Id_name; }
452 else if (c=='t') { X="text";id=Id_text; }
453 break L;
454 case 5: X="child";id=Id_child; break L;
455 case 6: c=s.charAt(0);
456 if (c=='l') { X="length";id=Id_length; }
457 else if (c=='p') { X="parent";id=Id_parent; }
458 break L;
459 case 7: c=s.charAt(0);
460 if (c=='r') { X="replace";id=Id_replace; }
461 else if (c=='s') { X="setName";id=Id_setName; }
462 else if (c=='v') { X="valueOf";id=Id_valueOf; }
463 break L;
464 case 8: switch (s.charAt(2)) {
465 case 'S': c=s.charAt(7);
466 if (c=='e') { X="toSource";id=Id_toSource; }
467 else if (c=='g') { X="toString";id=Id_toString; }
468 break L;
469 case 'd': X="nodeKind";id=Id_nodeKind; break L;
470 case 'e': X="elements";id=Id_elements; break L;
471 case 'i': X="children";id=Id_children; break L;
472 case 'm': X="comments";id=Id_comments; break L;
473 case 'n': X="contains";id=Id_contains; break L;
474 } break L;
475 case 9: switch (s.charAt(2)) {
476 case 'c': X="localName";id=Id_localName; break L;
477 case 'm': X="namespace";id=Id_namespace; break L;
478 case 'r': X="normalize";id=Id_normalize; break L;
479 case 't': X="attribute";id=Id_attribute; break L;
480 } break L;
481 case 10: c=s.charAt(0);
482 if (c=='a') { X="attributes";id=Id_attributes; }
483 else if (c=='c') { X="childIndex";id=Id_childIndex; }
484 break L;
485 case 11: switch (s.charAt(0)) {
486 case 'a': X="appendChild";id=Id_appendChild; break L;
487 case 'c': X="constructor";id=Id_constructor; break L;
488 case 'd': X="descendants";id=Id_descendants; break L;
489 case 's': X="setChildren";id=Id_setChildren; break L;
490 case 't': X="toXMLString";id=Id_toXMLString; break L;
491 } break L;
492 case 12: c=s.charAt(0);
493 if (c=='a') { X="addNamespace";id=Id_addNamespace; }
494 else if (c=='p') { X="prependChild";id=Id_prependChild; }
495 else if (c=='s') {
496 c=s.charAt(3);
497 if (c=='L') { X="setLocalName";id=Id_setLocalName; }
498 else if (c=='N') { X="setNamespace";id=Id_setNamespace; }
499 }
500 break L;
501 case 14: X="hasOwnProperty";id=Id_hasOwnProperty; break L;
502 case 15: X="removeNamespace";id=Id_removeNamespace; break L;
503 case 16: c=s.charAt(0);
504 if (c=='h') { X="hasSimpleContent";id=Id_hasSimpleContent; }
505 else if (c=='i') { X="insertChildAfter";id=Id_insertChildAfter; }
506 break L;
507 case 17: c=s.charAt(3);
508 if (c=='C') { X="hasComplexContent";id=Id_hasComplexContent; }
509 else if (c=='c') { X="inScopeNamespaces";id=Id_inScopeNamespaces; }
510 else if (c=='e') { X="insertChildBefore";id=Id_insertChildBefore; }
511 break L;
512 case 20: X="propertyIsEnumerable";id=Id_propertyIsEnumerable; break L;
513 case 21: X="namespaceDeclarations";id=Id_namespaceDeclarations; break L;
514 case 22: X="processingInstructions";id=Id_processingInstructions; break L;
515 }
516 if (X!=null && X!=s && !X.equals(s)) id = 0;
517 break L0;
518 }
519 // #/generated#
520 return id;
521 }
522 // #/string_id_map#
523
524 @Override
525 protected void initPrototypeId(int id) {
526 String s;
527 int arity;
528 switch (id) {
529 case Id_constructor: {
530 IdFunctionObject ctor;
531 if (this instanceof XML) {
532 ctor = new XMLCtor((XML)this, XMLOBJECT_TAG, id, 1);
533 } else {
534 ctor = new IdFunctionObject(this, XMLOBJECT_TAG, id, 1);
535 }
536 initPrototypeConstructor(ctor);
537 return;
538 }
539
540 case Id_addNamespace: arity=1; s="addNamespace"; break;
541 case Id_appendChild: arity=1; s="appendChild"; break;
542 case Id_attribute: arity=1; s="attribute"; break;
543 case Id_attributes: arity=0; s="attributes"; break;
544 case Id_child: arity=1; s="child"; break;
545 case Id_childIndex: arity=0; s="childIndex"; break;
546 case Id_children: arity=0; s="children"; break;
547 case Id_comments: arity=0; s="comments"; break;
548 case Id_contains: arity=1; s="contains"; break;
549 case Id_copy: arity=0; s="copy"; break;
550 case Id_descendants: arity=1; s="descendants"; break;
551 case Id_elements: arity=1; s="elements"; break;
552 case Id_hasComplexContent: arity=0; s="hasComplexContent"; break;
553 case Id_hasOwnProperty: arity=1; s="hasOwnProperty"; break;
554 case Id_hasSimpleContent: arity=0; s="hasSimpleContent"; break;
555 case Id_inScopeNamespaces: arity=0; s="inScopeNamespaces"; break;
556 case Id_insertChildAfter: arity=2; s="insertChildAfter"; break;
557 case Id_insertChildBefore: arity=2; s="insertChildBefore"; break;
558 case Id_length: arity=0; s="length"; break;
559 case Id_localName: arity=0; s="localName"; break;
560 case Id_name: arity=0; s="name"; break;
561 case Id_namespace: arity=1; s="namespace"; break;
562 case Id_namespaceDeclarations:
563 arity=0; s="namespaceDeclarations"; break;
564 case Id_nodeKind: arity=0; s="nodeKind"; break;
565 case Id_normalize: arity=0; s="normalize"; break;
566 case Id_parent: arity=0; s="parent"; break;
567 case Id_prependChild: arity=1; s="prependChild"; break;
568 case Id_processingInstructions:
569 arity=1; s="processingInstructions"; break;
570 case Id_propertyIsEnumerable:
571 arity=1; s="propertyIsEnumerable"; break;
572 case Id_removeNamespace: arity=1; s="removeNamespace"; break;
573 case Id_replace: arity=2; s="replace"; break;
574 case Id_setChildren: arity=1; s="setChildren"; break;
575 case Id_setLocalName: arity=1; s="setLocalName"; break;
576 case Id_setName: arity=1; s="setName"; break;
577 case Id_setNamespace: arity=1; s="setNamespace"; break;
578 case Id_text: arity=0; s="text"; break;
579 case Id_toString: arity=0; s="toString"; break;
580 case Id_toSource: arity=1; s="toSource"; break;
581 case Id_toXMLString: arity=1; s="toXMLString"; break;
582 case Id_valueOf: arity=0; s="valueOf"; break;
583
584 default: throw new IllegalArgumentException(String.valueOf(id));
585 }
586 initPrototypeMethod(XMLOBJECT_TAG, id, s, arity);
587 }
588
589 private Object[] toObjectArray(Object[] typed) {
590 Object[] rv = new Object[typed.length];
591 for (int i=0; i<rv.length; i++) {
592 rv[i] = typed[i];
593 }
594 return rv;
595 }
596
597 private void xmlMethodNotFound(Object object, String name) {
598 throw ScriptRuntime.notFunctionError(object, name);
599 }
600
601 @Override
602 public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope,
603 Scriptable thisObj, Object[] args)
604 {
605 if (!f.hasTag(XMLOBJECT_TAG)) {
606 return super.execIdCall(f, cx, scope, thisObj, args);
607 }
608 int id = f.methodId();
609 if (id == Id_constructor) {
610 return jsConstructor(cx, thisObj == null, args);
611 }
612
613 // All (XML|XMLList).prototype methods require thisObj to be XML
614 if (!(thisObj instanceof XMLObjectImpl))
615 throw incompatibleCallError(f);
616 XMLObjectImpl realThis = (XMLObjectImpl)thisObj;
617
618 XML xml = realThis.getXML();
619 switch (id) {
620 case Id_appendChild: {
621 if (xml == null) xmlMethodNotFound(realThis, "appendChild");
622 return xml.appendChild(arg(args, 0));
623 }
624 case Id_addNamespace: {
625 if (xml == null) xmlMethodNotFound(realThis, "addNamespace");
626 Namespace ns = lib.castToNamespace(cx, arg(args, 0));
627 return xml.addNamespace(ns);
628 }
629 case Id_childIndex: {
630 if (xml == null) xmlMethodNotFound(realThis, "childIndex");
631 return ScriptRuntime.wrapInt(xml.childIndex());
632 }
633 case Id_inScopeNamespaces: {
634 if (xml == null) xmlMethodNotFound(realThis, "inScopeNamespaces");
635 return cx.newArray(scope, toObjectArray(xml.inScopeNamespaces()));
636 }
637 case Id_insertChildAfter: {
638 if (xml == null) xmlMethodNotFound(realThis, "insertChildAfter");
639 Object arg0 = arg(args, 0);
640 if (arg0 == null || arg0 instanceof XML) {
641 return xml.insertChildAfter((XML)arg0, arg(args, 1));
642 }
643 return Undefined.instance;
644 }
645 case Id_insertChildBefore: {
646 if (xml == null) xmlMethodNotFound(realThis, "insertChildBefore");
647 Object arg0 = arg(args, 0);
648 if (arg0 == null || arg0 instanceof XML) {
649 return xml.insertChildBefore((XML)arg0, arg(args, 1));
650 }
651 return Undefined.instance;
652 }
653 case Id_localName: {
654 if (xml == null) xmlMethodNotFound(realThis, "localName");
655 return xml.localName();
656 }
657 case Id_name: {
658 if (xml == null) xmlMethodNotFound(realThis, "name");
659 return xml.name();
660 }
661 case Id_namespace: {
662 if (xml == null) xmlMethodNotFound(realThis, "namespace");
663 String prefix = (args.length > 0) ? ScriptRuntime.toString(args[0]) : null;
664 Namespace rv = xml.namespace(prefix);
665 if (rv == null) {
666 return Undefined.instance;
667 } else {
668 return rv;
669 }
670 }
671 case Id_namespaceDeclarations: {
672 if (xml == null) xmlMethodNotFound(realThis, "namespaceDeclarations");
673 Namespace[] array = xml.namespaceDeclarations();
674 return cx.newArray(scope, toObjectArray(array));
675 }
676 case Id_nodeKind: {
677 if (xml == null) xmlMethodNotFound(realThis, "nodeKind");
678 return xml.nodeKind();
679 }
680 case Id_prependChild: {
681 if (xml == null) xmlMethodNotFound(realThis, "prependChild");
682 return xml.prependChild(arg(args, 0));
683 }
684 case Id_removeNamespace: {
685 if (xml == null) xmlMethodNotFound(realThis, "removeNamespace");
686 Namespace ns = lib.castToNamespace(cx, arg(args, 0));
687 return xml.removeNamespace(ns);
688 }
689 case Id_replace: {
690 if (xml == null) xmlMethodNotFound(realThis, "replace");
691 XMLName xmlName = lib.toXMLNameOrIndex(cx, arg(args, 0));
692 Object arg1 = arg(args, 1);
693 if (xmlName == null) {
694 // I refuse to believe that this number will exceed 2^31
695 int index = (int)ScriptRuntime.lastUint32Result(cx);
696 return xml.replace(index, arg1);
697 } else {
698 return xml.replace(xmlName, arg1);
699 }
700 }
701 case Id_setChildren: {
702 if (xml == null) xmlMethodNotFound(realThis, "setChildren");
703 return xml.setChildren(arg(args, 0));
704 }
705 case Id_setLocalName: {
706 if (xml == null) xmlMethodNotFound(realThis, "setLocalName");
707 String localName;
708 Object arg = arg(args, 0);
709 if (arg instanceof QName) {
710 localName = ((QName)arg).localName();
711 } else {
712 localName = ScriptRuntime.toString(arg);
713 }
714 xml.setLocalName(localName);
715 return Undefined.instance;
716 }
717 case Id_setName: {
718 if (xml == null) xmlMethodNotFound(realThis, "setName");
719 Object arg = (args.length != 0) ? args[0] : Undefined.instance;
720 QName qname = lib.constructQName(cx, arg);
721 xml.setName(qname);
722 return Undefined.instance;
723 }
724 case Id_setNamespace: {
725 if (xml == null) xmlMethodNotFound(realThis, "setNamespace");
726 Namespace ns = lib.castToNamespace(cx, arg(args, 0));
727 xml.setNamespace(ns);
728 return Undefined.instance;
729 }
730
731 case Id_attribute: {
732 XMLName xmlName = XMLName.create( lib.toNodeQName(cx, arg(args, 0), true), true, false );
733 return realThis.getMatches(xmlName);
734 }
735 case Id_attributes:
736 return realThis.getMatches(XMLName.create(XmlNode.QName.create(null, null), true, false));
737 case Id_child: {
738 XMLName xmlName = lib.toXMLNameOrIndex(cx, arg(args, 0));
739 if (xmlName == null) {
740 // Two billion or so is a fine upper limit, so we cast to int
741 int index = (int)ScriptRuntime.lastUint32Result(cx);
742 return realThis.child(index);
743 } else {
744 return realThis.child(xmlName);
745 }
746 }
747 case Id_children:
748 return realThis.children();
749 case Id_comments:
750 return realThis.comments();
751 case Id_contains:
752 return ScriptRuntime.wrapBoolean(
753 realThis.contains(arg(args, 0)));
754 case Id_copy:
755 return realThis.copy();
756 case Id_descendants: {
757 XmlNode.QName qname = (args.length == 0) ? XmlNode.QName.create(null, null) : lib.toNodeQName(cx, args[0], false);
758 return realThis.getMatches( XMLName.create(qname, false, true) );
759 }
760 case Id_elements: {
761 XMLName xmlName = (args.length == 0)
762 ? XMLName.formStar()
763 : lib.toXMLName(cx, args[0]);
764 return realThis.elements(xmlName);
765 }
766 case Id_hasOwnProperty: {
767 XMLName xmlName = lib.toXMLName(cx, arg(args, 0));
768 return ScriptRuntime.wrapBoolean(
769 realThis.hasOwnProperty(xmlName));
770 }
771 case Id_hasComplexContent:
772 return ScriptRuntime.wrapBoolean(realThis.hasComplexContent());
773 case Id_hasSimpleContent:
774 return ScriptRuntime.wrapBoolean(realThis.hasSimpleContent());
775 case Id_length:
776 return ScriptRuntime.wrapInt(realThis.length());
777 case Id_normalize:
778 realThis.normalize();
779 return Undefined.instance;
780 case Id_parent:
781 return realThis.parent();
782 case Id_processingInstructions: {
783 XMLName xmlName = (args.length > 0)
784 ? lib.toXMLName(cx, args[0])
785 : XMLName.formStar();
786 return realThis.processingInstructions(xmlName);
787 }
788 case Id_propertyIsEnumerable: {
789 return ScriptRuntime.wrapBoolean(
790 realThis.propertyIsEnumerable(arg(args, 0)));
791 }
792 case Id_text:
793 return realThis.text();
794 case Id_toString:
795 return realThis.toString();
796 case Id_toSource:
797 int indent = ScriptRuntime.toInt32(args, 0);
798 return realThis.toSource(indent);
799 case Id_toXMLString: {
800 return realThis.toXMLString();
801 }
802 case Id_valueOf:
803 return realThis.valueOf();
804 }
805 throw new IllegalArgumentException(String.valueOf(id));
806 }
807
808 private static Object arg(Object[] args, int i) {
809 return (i < args.length) ? args[i] : Undefined.instance;
810 }
811
812 final XML newTextElementXML(XmlNode reference, XmlNode.QName qname, String value) {
813 return lib.newTextElementXML(reference, qname, value);
814 }
815
816 /* TODO: Hopefully this can be replaced with ecmaToXml below. */
817 final XML newXMLFromJs(Object inputObject) {
818 return lib.newXMLFromJs(inputObject);
819 }
820
821 final XML ecmaToXml(Object object) {
822 return lib.ecmaToXml(object);
823 }
824
825 final String ecmaEscapeAttributeValue(String s) {
826 // TODO Check this
827 String quoted = lib.escapeAttributeValue(s);
828 return quoted.substring(1, quoted.length() - 1);
829 }
830
831 final XML createEmptyXML() {
832 return newXML(XmlNode.createEmpty(getProcessor()));
833 }
834 }