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 QName
047: *
048: */
049: final class QName extends IdScriptableObject {
050: static final long serialVersionUID = 416745167693026750L;
051:
052: private static final Object QNAME_TAG = new Object();
053:
054: XMLLibImpl lib;
055: private String prefix;
056: private String localName;
057: private String uri;
058:
059: QName(XMLLibImpl lib, String uri, String localName, String prefix) {
060: super (lib.globalScope(), lib.qnamePrototype);
061: if (localName == null)
062: throw new IllegalArgumentException();
063: this .lib = lib;
064: this .uri = uri;
065: this .prefix = prefix;
066: this .localName = localName;
067: }
068:
069: void exportAsJSClass(boolean sealed) {
070: exportAsJSClass(MAX_PROTOTYPE_ID, lib.globalScope(), sealed);
071: }
072:
073: /**
074: *
075: * @return
076: */
077: public String toString() {
078: String result;
079:
080: if (uri == null) {
081: result = "*::".concat(localName);
082: } else if (uri.length() == 0) {
083: result = localName;
084: } else {
085: result = uri + "::" + localName;
086: }
087:
088: return result;
089: }
090:
091: public String localName() {
092: return localName;
093: }
094:
095: String prefix() {
096: return (prefix == null) ? prefix : "";
097: }
098:
099: String uri() {
100: return uri;
101: }
102:
103: public boolean equals(Object obj) {
104: if (!(obj instanceof QName))
105: return false;
106: return equals((QName) obj);
107: }
108:
109: protected Object equivalentValues(Object value) {
110: if (!(value instanceof QName))
111: return Scriptable.NOT_FOUND;
112: boolean result = equals((QName) value);
113: return result ? Boolean.TRUE : Boolean.FALSE;
114: }
115:
116: private boolean equals(QName q) {
117: boolean result;
118:
119: if (uri == null) {
120: result = q.uri == null && localName.equals(q.localName);
121: } else {
122: result = uri.equals(q.uri) && localName.equals(q.localName);
123: }
124:
125: return result;
126: }
127:
128: /**
129: *
130: * @return
131: */
132: public String getClassName() {
133: return "QName";
134: }
135:
136: /**
137: *
138: * @param hint
139: * @return
140: */
141: public Object getDefaultValue(Class hint) {
142: return toString();
143: }
144:
145: // #string_id_map#
146: private static final int Id_localName = 1, Id_uri = 2,
147: MAX_INSTANCE_ID = 2;
148:
149: protected int getMaxInstanceId() {
150: return super .getMaxInstanceId() + MAX_INSTANCE_ID;
151: }
152:
153: protected int findInstanceIdInfo(String s) {
154: int id;
155: // #generated# Last update: 2004-07-18 12:32:51 CEST
156: L0: {
157: id = 0;
158: String X = null;
159: int s_length = s.length();
160: if (s_length == 3) {
161: X = "uri";
162: id = Id_uri;
163: } else if (s_length == 9) {
164: X = "localName";
165: id = Id_localName;
166: }
167: if (X != null && X != s && !X.equals(s))
168: id = 0;
169: }
170: // #/generated#
171:
172: if (id == 0)
173: return super .findInstanceIdInfo(s);
174:
175: int attr;
176: switch (id) {
177: case Id_localName:
178: case Id_uri:
179: attr = PERMANENT | READONLY;
180: break;
181: default:
182: throw new IllegalStateException();
183: }
184: return instanceIdInfo(attr, super .getMaxInstanceId() + id);
185: }
186:
187: // #/string_id_map#
188:
189: protected String getInstanceIdName(int id) {
190: switch (id - super .getMaxInstanceId()) {
191: case Id_localName:
192: return "localName";
193: case Id_uri:
194: return "uri";
195: }
196: return super .getInstanceIdName(id);
197: }
198:
199: protected Object getInstanceIdValue(int id) {
200: switch (id - super .getMaxInstanceId()) {
201: case Id_localName:
202: return localName;
203: case Id_uri:
204: return uri;
205: }
206: return super .getInstanceIdValue(id);
207: }
208:
209: // #string_id_map#
210: private static final int Id_constructor = 1, Id_toString = 2,
211: Id_toSource = 3, MAX_PROTOTYPE_ID = 3;
212:
213: protected int findPrototypeId(String s) {
214: int id;
215: // #generated# Last update: 2004-08-21 12:45:13 CEST
216: L0: {
217: id = 0;
218: String X = null;
219: int c;
220: int s_length = s.length();
221: if (s_length == 8) {
222: c = s.charAt(3);
223: if (c == 'o') {
224: X = "toSource";
225: id = Id_toSource;
226: } else if (c == 't') {
227: X = "toString";
228: id = Id_toString;
229: }
230: } else if (s_length == 11) {
231: X = "constructor";
232: id = Id_constructor;
233: }
234: if (X != null && X != s && !X.equals(s))
235: id = 0;
236: }
237: // #/generated#
238: return id;
239: }
240:
241: // #/string_id_map#
242:
243: protected void initPrototypeId(int id) {
244: String s;
245: int arity;
246: switch (id) {
247: case Id_constructor:
248: arity = 2;
249: s = "constructor";
250: break;
251: case Id_toString:
252: arity = 0;
253: s = "toString";
254: break;
255: case Id_toSource:
256: arity = 0;
257: s = "toSource";
258: break;
259: default:
260: throw new IllegalArgumentException(String.valueOf(id));
261: }
262: initPrototypeMethod(QNAME_TAG, id, s, arity);
263: }
264:
265: public Object execIdCall(IdFunctionObject f, Context cx,
266: Scriptable scope, Scriptable this Obj, Object[] args) {
267: if (!f.hasTag(QNAME_TAG)) {
268: return super .execIdCall(f, cx, scope, this Obj, args);
269: }
270: int id = f.methodId();
271: switch (id) {
272: case Id_constructor:
273: return jsConstructor(cx, (this Obj == null), args);
274: case Id_toString:
275: return realThis(this Obj, f).toString();
276: case Id_toSource:
277: return realThis(this Obj, f).js_toSource();
278: }
279: throw new IllegalArgumentException(String.valueOf(id));
280: }
281:
282: private QName realThis(Scriptable this Obj, IdFunctionObject f) {
283: if (!(this Obj instanceof QName))
284: throw incompatibleCallError(f);
285: return (QName) this Obj;
286: }
287:
288: private Object jsConstructor(Context cx, boolean inNewExpr,
289: Object[] args) {
290: if (!inNewExpr && args.length == 1) {
291: return lib.castToQName(cx, args[0]);
292: }
293: if (args.length == 0) {
294: return lib.constructQName(cx, Undefined.instance);
295: } else if (args.length == 1) {
296: return lib.constructQName(cx, args[0]);
297: } else {
298: return lib.constructQName(cx, args[0], args[1]);
299: }
300: }
301:
302: private String js_toSource() {
303: StringBuffer sb = new StringBuffer();
304: sb.append('(');
305: toSourceImpl(uri, localName, prefix, sb);
306: sb.append(')');
307: return sb.toString();
308: }
309:
310: private static void toSourceImpl(String uri, String localName,
311: String prefix, StringBuffer sb) {
312: sb.append("new QName(");
313: if (uri == null && prefix == null) {
314: if (!"*".equals(localName)) {
315: sb.append("null, ");
316: }
317: } else {
318: Namespace.toSourceImpl(prefix, uri, sb);
319: sb.append(", ");
320: }
321: sb.append('\'');
322: sb.append(ScriptRuntime.escapeString(localName, '\''));
323: sb.append("')");
324: }
325:
326: }
|