001: /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
002: *
003: * ***** BEGIN LICENSE BLOCK *****
004: * Version: MPL 1.1/GPL 2.0
005: *
006: * The contents of this file are subject to the Mozilla Public License Version
007: * 1.1 (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: * http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the
014: * License.
015: *
016: * The Original Code is Rhino code, released
017: * May 6, 1999.
018: *
019: * The Initial Developer of the Original Code is
020: * Netscape Communications Corporation.
021: * Portions created by the Initial Developer are Copyright (C) 1997-2000
022: * the Initial Developer. All Rights Reserved.
023: *
024: * Contributor(s):
025: * Igor Bukanov
026: * David P. Caldwell <inonit@inonit.com>
027: *
028: * Alternatively, the contents of this file may be used under the terms of
029: * the GNU General Public License Version 2 or later (the "GPL"), in which
030: * case the provisions of the GPL are applicable instead of those above. If
031: * you wish to allow use of your version of this file only under the terms of
032: * the GPL and not to allow others to use your version of this file under the
033: * MPL, indicate your decision by deleting the provisions above and replacing
034: * them with the notice and other provisions required by the GPL. If you do
035: * not delete the provisions above, a recipient may use your version of this
036: * file under either the MPL or the GPL.
037: *
038: * ***** END LICENSE BLOCK ***** */
039:
040: package org.mozilla.javascript.xmlimpl;
041:
042: import org.mozilla.javascript.*;
043:
044: class XMLCtor extends IdFunctionObject {
045: static final long serialVersionUID = -8708195078359817341L;
046:
047: private static final Object XMLCTOR_TAG = new Object();
048:
049: private XmlProcessor options;
050:
051: // private XMLLibImpl lib;
052:
053: XMLCtor(XML xml, Object tag, int id, int arity) {
054: super (xml, tag, id, arity);
055: // this.lib = xml.lib;
056: this .options = xml.getProcessor();
057: activatePrototypeMap(MAX_FUNCTION_ID);
058: }
059:
060: private void writeSetting(Scriptable target) {
061: for (int i = 1; i <= MAX_INSTANCE_ID; ++i) {
062: int id = super .getMaxInstanceId() + i;
063: String name = getInstanceIdName(id);
064: Object value = getInstanceIdValue(id);
065: ScriptableObject.putProperty(target, name, value);
066: }
067: }
068:
069: private void readSettings(Scriptable source) {
070: for (int i = 1; i <= MAX_INSTANCE_ID; ++i) {
071: int id = super .getMaxInstanceId() + i;
072: String name = getInstanceIdName(id);
073: Object value = ScriptableObject.getProperty(source, name);
074: if (value == Scriptable.NOT_FOUND) {
075: continue;
076: }
077: switch (i) {
078: case Id_ignoreComments:
079: case Id_ignoreProcessingInstructions:
080: case Id_ignoreWhitespace:
081: case Id_prettyPrinting:
082: if (!(value instanceof Boolean)) {
083: continue;
084: }
085: break;
086: case Id_prettyIndent:
087: if (!(value instanceof Number)) {
088: continue;
089: }
090: break;
091: default:
092: throw new IllegalStateException();
093: }
094: setInstanceIdValue(id, value);
095: }
096: }
097:
098: // #string_id_map#
099:
100: private static final int Id_ignoreComments = 1,
101: Id_ignoreProcessingInstructions = 2,
102: Id_ignoreWhitespace = 3, Id_prettyIndent = 4,
103: Id_prettyPrinting = 5,
104:
105: MAX_INSTANCE_ID = 5;
106:
107: protected int getMaxInstanceId() {
108: return super .getMaxInstanceId() + MAX_INSTANCE_ID;
109: }
110:
111: protected int findInstanceIdInfo(String s) {
112: int id;
113: // #generated# Last update: 2007-08-20 09:01:10 EDT
114: L0: {
115: id = 0;
116: String X = null;
117: int c;
118: L: switch (s.length()) {
119: case 12:
120: X = "prettyIndent";
121: id = Id_prettyIndent;
122: break L;
123: case 14:
124: c = s.charAt(0);
125: if (c == 'i') {
126: X = "ignoreComments";
127: id = Id_ignoreComments;
128: } else if (c == 'p') {
129: X = "prettyPrinting";
130: id = Id_prettyPrinting;
131: }
132: break L;
133: case 16:
134: X = "ignoreWhitespace";
135: id = Id_ignoreWhitespace;
136: break L;
137: case 28:
138: X = "ignoreProcessingInstructions";
139: id = Id_ignoreProcessingInstructions;
140: break L;
141: }
142: if (X != null && X != s && !X.equals(s))
143: id = 0;
144: break L0;
145: }
146: // #/generated#
147:
148: if (id == 0)
149: return super .findInstanceIdInfo(s);
150:
151: int attr;
152: switch (id) {
153: case Id_ignoreComments:
154: case Id_ignoreProcessingInstructions:
155: case Id_ignoreWhitespace:
156: case Id_prettyIndent:
157: case Id_prettyPrinting:
158: attr = PERMANENT | DONTENUM;
159: break;
160: default:
161: throw new IllegalStateException();
162: }
163: return instanceIdInfo(attr, super .getMaxInstanceId() + id);
164: }
165:
166: // #/string_id_map#
167:
168: protected String getInstanceIdName(int id) {
169: switch (id - super .getMaxInstanceId()) {
170: case Id_ignoreComments:
171: return "ignoreComments";
172: case Id_ignoreProcessingInstructions:
173: return "ignoreProcessingInstructions";
174: case Id_ignoreWhitespace:
175: return "ignoreWhitespace";
176: case Id_prettyIndent:
177: return "prettyIndent";
178: case Id_prettyPrinting:
179: return "prettyPrinting";
180: }
181: return super .getInstanceIdName(id);
182: }
183:
184: protected Object getInstanceIdValue(int id) {
185: switch (id - super .getMaxInstanceId()) {
186: case Id_ignoreComments:
187: return ScriptRuntime
188: .wrapBoolean(options.isIgnoreComments());
189: case Id_ignoreProcessingInstructions:
190: return ScriptRuntime.wrapBoolean(options
191: .isIgnoreProcessingInstructions());
192: case Id_ignoreWhitespace:
193: return ScriptRuntime.wrapBoolean(options
194: .isIgnoreWhitespace());
195: case Id_prettyIndent:
196: return ScriptRuntime.wrapInt(options.getPrettyIndent());
197: case Id_prettyPrinting:
198: return ScriptRuntime
199: .wrapBoolean(options.isPrettyPrinting());
200: }
201: return super .getInstanceIdValue(id);
202: }
203:
204: protected void setInstanceIdValue(int id, Object value) {
205: switch (id - super .getMaxInstanceId()) {
206: case Id_ignoreComments:
207: options.setIgnoreComments(ScriptRuntime.toBoolean(value));
208: return;
209: case Id_ignoreProcessingInstructions:
210: options.setIgnoreProcessingInstructions(ScriptRuntime
211: .toBoolean(value));
212: return;
213: case Id_ignoreWhitespace:
214: options.setIgnoreWhitespace(ScriptRuntime.toBoolean(value));
215: return;
216: case Id_prettyIndent:
217: options.setPrettyIndent(ScriptRuntime.toInt32(value));
218: return;
219: case Id_prettyPrinting:
220: options.setPrettyPrinting(ScriptRuntime.toBoolean(value));
221: return;
222: }
223: super .setInstanceIdValue(id, value);
224: }
225:
226: // #string_id_map#
227: private static final int Id_defaultSettings = 1, Id_settings = 2,
228: Id_setSettings = 3, MAX_FUNCTION_ID = 3;
229:
230: protected int findPrototypeId(String s) {
231: int id;
232: // #generated# Last update: 2007-08-20 09:01:10 EDT
233: L0: {
234: id = 0;
235: String X = null;
236: int s_length = s.length();
237: if (s_length == 8) {
238: X = "settings";
239: id = Id_settings;
240: } else if (s_length == 11) {
241: X = "setSettings";
242: id = Id_setSettings;
243: } else if (s_length == 15) {
244: X = "defaultSettings";
245: id = Id_defaultSettings;
246: }
247: if (X != null && X != s && !X.equals(s))
248: id = 0;
249: break L0;
250: }
251: // #/generated#
252: return id;
253: }
254:
255: // #/string_id_map#
256:
257: protected void initPrototypeId(int id) {
258: String s;
259: int arity;
260: switch (id) {
261: case Id_defaultSettings:
262: arity = 0;
263: s = "defaultSettings";
264: break;
265: case Id_settings:
266: arity = 0;
267: s = "settings";
268: break;
269: case Id_setSettings:
270: arity = 1;
271: s = "setSettings";
272: break;
273: default:
274: throw new IllegalArgumentException(String.valueOf(id));
275: }
276: initPrototypeMethod(XMLCTOR_TAG, id, s, arity);
277: }
278:
279: public Object execIdCall(IdFunctionObject f, Context cx,
280: Scriptable scope, Scriptable this Obj, Object[] args) {
281: if (!f.hasTag(XMLCTOR_TAG)) {
282: return super .execIdCall(f, cx, scope, this Obj, args);
283: }
284: int id = f.methodId();
285: switch (id) {
286: case Id_defaultSettings: {
287: options.setDefault();
288: Scriptable obj = cx.newObject(scope);
289: writeSetting(obj);
290: return obj;
291: }
292: case Id_settings: {
293: Scriptable obj = cx.newObject(scope);
294: writeSetting(obj);
295: return obj;
296: }
297: case Id_setSettings: {
298: if (args.length == 0 || args[0] == null
299: || args[0] == Undefined.instance) {
300: options.setDefault();
301: } else if (args[0] instanceof Scriptable) {
302: readSettings((Scriptable) args[0]);
303: }
304: return Undefined.instance;
305: }
306: }
307: throw new IllegalArgumentException(String.valueOf(id));
308: }
309:
310: /**
311: hasInstance for XML objects works differently than other objects; see ECMA357 13.4.3.10.
312: */
313: public boolean hasInstance(Scriptable instance) {
314: return (instance instanceof XML || instance instanceof XMLList);
315: }
316: }
|