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: * Ethan Hugg
026: * Terry Lucas
027: * Milen Nankov
028: *
029: * Alternatively, the contents of this file may be used under the terms of
030: * the GNU General Public License Version 2 or later (the "GPL"), in which
031: * case the provisions of the GPL are applicable instead of those above. If
032: * you wish to allow use of your version of this file only under the terms of
033: * the GPL and not to allow others to use your version of this file under the
034: * MPL, indicate your decision by deleting the provisions above and replacing
035: * them with the notice and other provisions required by the GPL. If you do
036: * not delete the provisions above, a recipient may use your version of this
037: * file under either the MPL or the GPL.
038: *
039: * ***** END LICENSE BLOCK ***** */
040:
041: package org.mozilla.javascript.xml.impl.xmlbeans;
042:
043: import org.mozilla.javascript.*;
044:
045: /**
046: * Class Namespace
047: *
048: */
049: class Namespace extends IdScriptableObject {
050: static final long serialVersionUID = -5765755238131301744L;
051:
052: private static final Object NAMESPACE_TAG = new Object();
053:
054: private XMLLibImpl lib;
055: private String prefix;
056: private String uri;
057:
058: public Namespace(XMLLibImpl lib, String uri) {
059: super (lib.globalScope(), lib.namespacePrototype);
060:
061: if (uri == null)
062: throw new IllegalArgumentException();
063:
064: this .lib = lib;
065: this .prefix = (uri.length() == 0) ? "" : null;
066: this .uri = uri;
067: }
068:
069: public Namespace(XMLLibImpl lib, String prefix, String uri) {
070: super (lib.globalScope(), lib.namespacePrototype);
071:
072: if (uri == null)
073: throw new IllegalArgumentException();
074: if (uri.length() == 0) {
075: // prefix should be "" for empty uri
076: if (prefix == null)
077: throw new IllegalArgumentException();
078: if (prefix.length() != 0)
079: throw new IllegalArgumentException();
080: }
081:
082: this .lib = lib;
083: this .prefix = prefix;
084: this .uri = uri;
085: }
086:
087: public void exportAsJSClass(boolean sealed) {
088: exportAsJSClass(MAX_PROTOTYPE_ID, lib.globalScope(), sealed);
089: }
090:
091: /**
092: *
093: * @return
094: */
095: public String uri() {
096: return uri;
097: }
098:
099: /**
100: *
101: * @return
102: */
103: public String prefix() {
104: return prefix;
105: }
106:
107: /**
108: *
109: * @return
110: */
111: public String toString() {
112: return uri();
113: }
114:
115: /**
116: *
117: * @return
118: */
119: public String toLocaleString() {
120: return toString();
121: }
122:
123: public boolean equals(Object obj) {
124: if (!(obj instanceof Namespace))
125: return false;
126: return equals((Namespace) obj);
127: }
128:
129: protected Object equivalentValues(Object value) {
130: if (!(value instanceof Namespace))
131: return Scriptable.NOT_FOUND;
132: boolean result = equals((Namespace) value);
133: return result ? Boolean.TRUE : Boolean.FALSE;
134: }
135:
136: private boolean equals(Namespace n) {
137: return uri().equals(n.uri());
138: }
139:
140: /**
141: *
142: * @return
143: */
144: public String getClassName() {
145: return "Namespace";
146: }
147:
148: /**
149: *
150: * @param hint
151: * @return
152: */
153: public Object getDefaultValue(Class hint) {
154: return uri();
155: }
156:
157: // #string_id_map#
158: private static final int Id_prefix = 1, Id_uri = 2,
159: MAX_INSTANCE_ID = 2;
160:
161: protected int getMaxInstanceId() {
162: return super .getMaxInstanceId() + MAX_INSTANCE_ID;
163: }
164:
165: protected int findInstanceIdInfo(String s) {
166: int id;
167: // #generated# Last update: 2004-07-20 19:50:50 CEST
168: L0: {
169: id = 0;
170: String X = null;
171: int s_length = s.length();
172: if (s_length == 3) {
173: X = "uri";
174: id = Id_uri;
175: } else if (s_length == 6) {
176: X = "prefix";
177: id = Id_prefix;
178: }
179: if (X != null && X != s && !X.equals(s))
180: id = 0;
181: }
182: // #/generated#
183:
184: if (id == 0)
185: return super .findInstanceIdInfo(s);
186:
187: int attr;
188: switch (id) {
189: case Id_prefix:
190: case Id_uri:
191: attr = PERMANENT | READONLY;
192: break;
193: default:
194: throw new IllegalStateException();
195: }
196: return instanceIdInfo(attr, super .getMaxInstanceId() + id);
197: }
198:
199: // #/string_id_map#
200:
201: protected String getInstanceIdName(int id) {
202: switch (id - super .getMaxInstanceId()) {
203: case Id_prefix:
204: return "prefix";
205: case Id_uri:
206: return "uri";
207: }
208: return super .getInstanceIdName(id);
209: }
210:
211: protected Object getInstanceIdValue(int id) {
212: switch (id - super .getMaxInstanceId()) {
213: case Id_prefix:
214: if (prefix == null)
215: return Undefined.instance;
216: return prefix;
217: case Id_uri:
218: return uri;
219: }
220: return super .getInstanceIdValue(id);
221: }
222:
223: // #string_id_map#
224: private static final int Id_constructor = 1, Id_toString = 2,
225: Id_toSource = 3, MAX_PROTOTYPE_ID = 3;
226:
227: protected int findPrototypeId(String s) {
228: int id;
229: // #generated# Last update: 2004-08-21 12:07:01 CEST
230: L0: {
231: id = 0;
232: String X = null;
233: int c;
234: int s_length = s.length();
235: if (s_length == 8) {
236: c = s.charAt(3);
237: if (c == 'o') {
238: X = "toSource";
239: id = Id_toSource;
240: } else if (c == 't') {
241: X = "toString";
242: id = Id_toString;
243: }
244: } else if (s_length == 11) {
245: X = "constructor";
246: id = Id_constructor;
247: }
248: if (X != null && X != s && !X.equals(s))
249: id = 0;
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_constructor:
262: arity = 2;
263: s = "constructor";
264: break;
265: case Id_toString:
266: arity = 0;
267: s = "toString";
268: break;
269: case Id_toSource:
270: arity = 0;
271: s = "toSource";
272: break;
273: default:
274: throw new IllegalArgumentException(String.valueOf(id));
275: }
276: initPrototypeMethod(NAMESPACE_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(NAMESPACE_TAG)) {
282: return super .execIdCall(f, cx, scope, this Obj, args);
283: }
284: int id = f.methodId();
285: switch (id) {
286: case Id_constructor:
287: return jsConstructor(cx, (this Obj == null), args);
288: case Id_toString:
289: return realThis(this Obj, f).toString();
290: case Id_toSource:
291: return realThis(this Obj, f).js_toSource();
292: }
293: throw new IllegalArgumentException(String.valueOf(id));
294: }
295:
296: private Namespace realThis(Scriptable this Obj, IdFunctionObject f) {
297: if (!(this Obj instanceof Namespace))
298: throw incompatibleCallError(f);
299: return (Namespace) this Obj;
300: }
301:
302: private Object jsConstructor(Context cx, boolean inNewExpr,
303: Object[] args) {
304: if (!inNewExpr && args.length == 1) {
305: return lib.castToNamespace(cx, args[0]);
306: }
307:
308: if (args.length == 0) {
309: return lib.constructNamespace(cx);
310: } else if (args.length == 1) {
311: return lib.constructNamespace(cx, args[0]);
312: } else {
313: return lib.constructNamespace(cx, args[0], args[1]);
314: }
315: }
316:
317: private String js_toSource() {
318: StringBuffer sb = new StringBuffer();
319: sb.append('(');
320: toSourceImpl(prefix, uri, sb);
321: sb.append(')');
322: return sb.toString();
323: }
324:
325: static void toSourceImpl(String prefix, String uri, StringBuffer sb) {
326: sb.append("new Namespace(");
327: if (uri.length() == 0) {
328: if (!"".equals(prefix))
329: throw new IllegalArgumentException(prefix);
330: } else {
331: sb.append('\'');
332: if (prefix != null) {
333: sb.append(ScriptRuntime.escapeString(prefix, '\''));
334: sb.append("', '");
335: }
336: sb.append(ScriptRuntime.escapeString(uri, '\''));
337: sb.append('\'');
338: }
339: sb.append(')');
340: }
341: }
|