Debian Codegraph @

rhino/xmlimplsrc/org/mozilla/javascript/xmlimpl/XMLWithScope.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 final class XMLWithScope extends NativeWith
13 {
14 private static final long serialVersionUID = -696429282095170887L;
15
16 private XMLLibImpl lib;
17 private int _currIndex;
18 private XMLList _xmlList;
19 private XMLObject _dqPrototype;
20
21 XMLWithScope(XMLLibImpl lib, Scriptable parent, XMLObject prototype)
22 {
23 super(parent, prototype);
24 this.lib = lib;
25 }
26
27 void initAsDotQuery()
28 {
29 XMLObject prototype = (XMLObject)getPrototype();
30 // XMLWithScope also handles the .(xxx) DotQuery for XML
31 // basically DotQuery is a for/in/with statement and in
32 // the following 3 statements we setup to signal it's
33 // DotQuery,
34 // the index and the object being looped over. The
35 // xws.setPrototype is the scope of the object which is
36 // is a element of the lhs (XMLList).
37 _currIndex = 0;
38 _dqPrototype = prototype;
39 if (prototype instanceof XMLList) {
40 XMLList xl = (XMLList)prototype;
41 if (xl.length() > 0) {
42 setPrototype((Scriptable)(xl.get(0, null)));
43 }
44 }
45 // Always return the outer-most type of XML lValue of
46 // XML to left of dotQuery.
47 _xmlList = lib.newXMLList();
48 }
49
50 @Override
51 protected Object updateDotQuery(boolean value)
52 {
53 // Return null to continue looping
54
55 XMLObject seed = _dqPrototype;
56 XMLList xmlL = _xmlList;
57
58 if (seed instanceof XMLList) {
59 // We're a list so keep testing each element of the list if the
60 // result on the top of stack is true then that element is added
61 // to our result list. If false, we try the next element.
62 XMLList orgXmlL = (XMLList)seed;
63
64 int idx = _currIndex;
65
66 if (value) {
67 xmlL.addToList(orgXmlL.get(idx, null));
68 }
69
70 // More elements to test?
71 if (++idx < orgXmlL.length()) {
72 // Yes, set our new index, get the next element and
73 // reset the expression to run with this object as
74 // the WITH selector.
75 _currIndex = idx;
76 setPrototype((Scriptable)(orgXmlL.get(idx, null)));
77
78 // continue looping
79 return null;
80 }
81 } else {
82 // If we're not a XMLList then there's no looping
83 // just return DQPrototype if the result is true.
84 if (value) {
85 xmlL.addToList(seed);
86 }
87 }
88
89 return xmlL;
90 }
91 }