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: *
027: * Alternatively, the contents of this file may be used under the terms of
028: * the GNU General Public License Version 2 or later (the "GPL"), in which
029: * case the provisions of the GPL are applicable instead of those above. If
030: * you wish to allow use of your version of this file only under the terms of
031: * the GPL and not to allow others to use your version of this file under the
032: * MPL, indicate your decision by deleting the provisions above and replacing
033: * them with the notice and other provisions required by the GPL. If you do
034: * not delete the provisions above, a recipient may use your version of this
035: * file under either the MPL or the GPL.
036: *
037: * ***** END LICENSE BLOCK ***** */
038:
039: package org.mozilla.javascript.xml.impl.xmlbeans;
040:
041: import org.mozilla.javascript.*;
042:
043: class XMLCtor extends IdFunctionObject {
044: static final long serialVersionUID = -8708195078359817341L;
045:
046: private static final Object XMLCTOR_TAG = new Object();
047:
048: private XMLLibImpl lib;
049:
050: XMLCtor(XML xml, Object tag, int id, int arity) {
051: super (xml, tag, id, arity);
052: this .lib = xml.lib;
053: activatePrototypeMap(MAX_FUNCTION_ID);
054: }
055:
056: private void writeSetting(Scriptable target) {
057: for (int i = 1; i <= MAX_INSTANCE_ID; ++i) {
058: int id = super .getMaxInstanceId() + i;
059: String name = getInstanceIdName(id);
060: Object value = getInstanceIdValue(id);
061: ScriptableObject.putProperty(target, name, value);
062: }
063: }
064:
065: private void readSettings(Scriptable source) {
066: for (int i = 1; i <= MAX_INSTANCE_ID; ++i) {
067: int id = super .getMaxInstanceId() + i;
068: String name = getInstanceIdName(id);
069: Object value = ScriptableObject.getProperty(source, name);
070: if (value == Scriptable.NOT_FOUND) {
071: continue;
072: }
073: switch (i) {
074: case Id_ignoreComments:
075: case Id_ignoreProcessingInstructions:
076: case Id_ignoreWhitespace:
077: case Id_prettyPrinting:
078: if (!(value instanceof Boolean)) {
079: continue;
080: }
081: break;
082: case Id_prettyIndent:
083: if (!(value instanceof Number)) {
084: continue;
085: }
086: break;
087: default:
088: throw new IllegalStateException();
089: }
090: setInstanceIdValue(id, value);
091: }
092: }
093:
094: // #string_id_map#
095:
096: private static final int Id_ignoreComments = 1,
097: Id_ignoreProcessingInstructions = 2,
098: Id_ignoreWhitespace = 3, Id_prettyIndent = 4,
099: Id_prettyPrinting = 5,
100:
101: MAX_INSTANCE_ID = 5;
102:
103: protected int getMaxInstanceId() {
104: return super .getMaxInstanceId() + MAX_INSTANCE_ID;
105: }
106:
107: protected int findInstanceIdInfo(String s) {
108: int id;
109: // #generated# Last update: 2004-07-19 13:03:52 CEST
110: L0: {
111: id = 0;
112: String X = null;
113: int c;
114: L: switch (s.length()) {
115: case 12:
116: X = "prettyIndent";
117: id = Id_prettyIndent;
118: break L;
119: case 14:
120: c = s.charAt(0);
121: if (c == 'i') {
122: X = "ignoreComments";
123: id = Id_ignoreComments;
124: } else if (c == 'p') {
125: X = "prettyPrinting";
126: id = Id_prettyPrinting;
127: }
128: break L;
129: case 16:
130: X = "ignoreWhitespace";
131: id = Id_ignoreWhitespace;
132: break L;
133: case 28:
134: X = "ignoreProcessingInstructions";
135: id = Id_ignoreProcessingInstructions;
136: break L;
137: }
138: if (X != null && X != s && !X.equals(s))
139: id = 0;
140: }
141: // #/generated#
142:
143: if (id == 0)
144: return super .findInstanceIdInfo(s);
145:
146: int attr;
147: switch (id) {
148: case Id_ignoreComments:
149: case Id_ignoreProcessingInstructions:
150: case Id_ignoreWhitespace:
151: case Id_prettyIndent:
152: case Id_prettyPrinting:
153: attr = PERMANENT | DONTENUM;
154: break;
155: default:
156: throw new IllegalStateException();
157: }
158: return instanceIdInfo(attr, super .getMaxInstanceId() + id);
159: }
160:
161: // #/string_id_map#
162:
163: protected String getInstanceIdName(int id) {
164: switch (id - super .getMaxInstanceId()) {
165: case Id_ignoreComments:
166: return "ignoreComments";
167: case Id_ignoreProcessingInstructions:
168: return "ignoreProcessingInstructions";
169: case Id_ignoreWhitespace:
170: return "ignoreWhitespace";
171: case Id_prettyIndent:
172: return "prettyIndent";
173: case Id_prettyPrinting:
174: return "prettyPrinting";
175: }
176: return super .getInstanceIdName(id);
177: }
178:
179: protected Object getInstanceIdValue(int id) {
180: switch (id - super .getMaxInstanceId()) {
181: case Id_ignoreComments:
182: return ScriptRuntime.wrapBoolean(lib.ignoreComments);
183: case Id_ignoreProcessingInstructions:
184: return ScriptRuntime
185: .wrapBoolean(lib.ignoreProcessingInstructions);
186: case Id_ignoreWhitespace:
187: return ScriptRuntime.wrapBoolean(lib.ignoreWhitespace);
188: case Id_prettyIndent:
189: return ScriptRuntime.wrapInt(lib.prettyIndent);
190: case Id_prettyPrinting:
191: return ScriptRuntime.wrapBoolean(lib.prettyPrinting);
192: }
193: return super .getInstanceIdValue(id);
194: }
195:
196: protected void setInstanceIdValue(int id, Object value) {
197: switch (id - super .getMaxInstanceId()) {
198: case Id_ignoreComments:
199: lib.ignoreComments = ScriptRuntime.toBoolean(value);
200: return;
201: case Id_ignoreProcessingInstructions:
202: lib.ignoreProcessingInstructions = ScriptRuntime
203: .toBoolean(value);
204: return;
205: case Id_ignoreWhitespace:
206: lib.ignoreWhitespace = ScriptRuntime.toBoolean(value);
207: return;
208: case Id_prettyIndent:
209: lib.prettyIndent = ScriptRuntime.toInt32(value);
210: return;
211: case Id_prettyPrinting:
212: lib.prettyPrinting = ScriptRuntime.toBoolean(value);
213: return;
214: }
215: super .setInstanceIdValue(id, value);
216: }
217:
218: // #string_id_map#
219: private static final int Id_defaultSettings = 1, Id_settings = 2,
220: Id_setSettings = 3, MAX_FUNCTION_ID = 3;
221:
222: protected int findPrototypeId(String s) {
223: int id;
224: // #generated# Last update: 2004-07-19 13:03:52 CEST
225: L0: {
226: id = 0;
227: String X = null;
228: int s_length = s.length();
229: if (s_length == 8) {
230: X = "settings";
231: id = Id_settings;
232: } else if (s_length == 11) {
233: X = "setSettings";
234: id = Id_setSettings;
235: } else if (s_length == 15) {
236: X = "defaultSettings";
237: id = Id_defaultSettings;
238: }
239: if (X != null && X != s && !X.equals(s))
240: id = 0;
241: }
242: // #/generated#
243: return id;
244: }
245:
246: // #/string_id_map#
247:
248: protected void initPrototypeId(int id) {
249: String s;
250: int arity;
251: switch (id) {
252: case Id_defaultSettings:
253: arity = 0;
254: s = "defaultSettings";
255: break;
256: case Id_settings:
257: arity = 0;
258: s = "settings";
259: break;
260: case Id_setSettings:
261: arity = 1;
262: s = "setSettings";
263: break;
264: default:
265: throw new IllegalArgumentException(String.valueOf(id));
266: }
267: initPrototypeMethod(XMLCTOR_TAG, id, s, arity);
268: }
269:
270: public Object execIdCall(IdFunctionObject f, Context cx,
271: Scriptable scope, Scriptable this Obj, Object[] args) {
272: if (!f.hasTag(XMLCTOR_TAG)) {
273: return super .execIdCall(f, cx, scope, this Obj, args);
274: }
275: int id = f.methodId();
276: switch (id) {
277: case Id_defaultSettings: {
278: lib.defaultSettings();
279: Scriptable obj = cx.newObject(scope);
280: writeSetting(obj);
281: return obj;
282: }
283: case Id_settings: {
284: Scriptable obj = cx.newObject(scope);
285: writeSetting(obj);
286: return obj;
287: }
288: case Id_setSettings: {
289: if (args.length == 0 || args[0] == null
290: || args[0] == Undefined.instance) {
291: lib.defaultSettings();
292: } else if (args[0] instanceof Scriptable) {
293: readSettings((Scriptable) args[0]);
294: }
295: return Undefined.instance;
296: }
297: }
298: throw new IllegalArgumentException(String.valueOf(id));
299: }
300: }
|