Debian Codegraph @

rhino/xmlimplsrc/org/mozilla/javascript/xmlimpl/QName.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
11 /**
12 * Class QName
13 *
14 */
15 final class QName extends IdScriptableObject
16 {
17 static final long serialVersionUID = 416745167693026750L;
18
19 private static final Object QNAME_TAG = "QName";
20
21 private XMLLibImpl lib;
22
23 private QName prototype;
24
25 private XmlNode.QName delegate;
26
27 private QName() {
28 }
29
30 static QName create(XMLLibImpl lib, Scriptable scope, QName prototype, XmlNode.QName delegate) {
31 QName rv = new QName();
32 rv.lib = lib;
33 rv.setParentScope(scope);
34 rv.prototype = prototype;
35 rv.setPrototype(prototype);
36 rv.delegate = delegate;
37 return rv;
38 }
39
40 // /** @deprecated */
41 // static QName create(XMLLibImpl lib, XmlNode.QName nodeQname) {
42 // return create(lib, lib.globalScope(), lib.qnamePrototype(), nodeQname);
43 // }
44
45 void exportAsJSClass(boolean sealed) {
46 exportAsJSClass(MAX_PROTOTYPE_ID, getParentScope(), sealed);
47 }
48
49 @Override
50 public String toString() {
51 // ECMA357 13.3.4.2
52 if (delegate.getNamespace() == null) {
53 return "*::" + localName();
54 } else if (delegate.getNamespace().isGlobal()) {
55 // leave as empty
56 return localName();
57 } else {
58 return uri() + "::" + localName();
59 }
60 }
61
62 public String localName() {
63 if (delegate.getLocalName() == null) return "*";
64 return delegate.getLocalName();
65 }
66
67 /*
68 * TODO This property is supposed to be invisible and I think we can
69 * make it private at some point, though Namespace might need it
70 */
71 String prefix() {
72 if (delegate.getNamespace() == null) return null;
73 return delegate.getNamespace().getPrefix();
74 }
75
76 String uri() {
77 if (delegate.getNamespace() == null) return null;
78 return delegate.getNamespace().getUri();
79 }
80
81 /** @deprecated */
82 @Deprecated
83 final XmlNode.QName toNodeQname() {
84 return delegate;
85 }
86
87 final XmlNode.QName getDelegate() {
88 return delegate;
89 }
90
91 @Override
92 public boolean equals(Object obj) {
93 if(!(obj instanceof QName)) return false;
94 return equals((QName)obj);
95 }
96
97 @Override
98 public int hashCode() {
99 return delegate.hashCode();
100 }
101
102 @Override
103 protected Object equivalentValues(Object value)
104 {
105 if(!(value instanceof QName)) return Scriptable.NOT_FOUND;
106 boolean result = equals((QName)value);
107 return result ? Boolean.TRUE : Boolean.FALSE;
108 }
109
110 private boolean equals(QName q) {
111 return this.delegate.equals(q.delegate);
112 }
113
114 @Override
115 public String getClassName() {
116 return "QName";
117 }
118
119 @Override
120 public Object getDefaultValue(Class<?> hint) {
121 return toString();
122 }
123
124 // #string_id_map#
125 private static final int
126 Id_localName = 1,
127 Id_uri = 2,
128 MAX_INSTANCE_ID = 2;
129
130 @Override
131 protected int getMaxInstanceId()
132 {
133 return super.getMaxInstanceId() + MAX_INSTANCE_ID;
134 }
135
136 @Override
137 protected int findInstanceIdInfo(String s)
138 {
139 int id;
140 // #generated# Last update: 2007-08-20 08:21:41 EDT
141 L0: { id = 0; String X = null;
142 int s_length = s.length();
143 if (s_length==3) { X="uri";id=Id_uri; }
144 else if (s_length==9) { X="localName";id=Id_localName; }
145 if (X!=null && X!=s && !X.equals(s)) id = 0;
146 break L0;
147 }
148 // #/generated#
149
150 if (id == 0) return super.findInstanceIdInfo(s);
151
152 int attr;
153 switch (id) {
154 case Id_localName:
155 case Id_uri:
156 attr = PERMANENT | READONLY;
157 break;
158 default: throw new IllegalStateException();
159 }
160 return instanceIdInfo(attr, super.getMaxInstanceId() + id);
161 }
162 // #/string_id_map#
163
164 @Override
165 protected String getInstanceIdName(int id)
166 {
167 switch (id - super.getMaxInstanceId()) {
168 case Id_localName: return "localName";
169 case Id_uri: return "uri";
170 }
171 return super.getInstanceIdName(id);
172 }
173
174 @Override
175 protected Object getInstanceIdValue(int id)
176 {
177 switch (id - super.getMaxInstanceId()) {
178 case Id_localName: return localName();
179 case Id_uri: return uri();
180 }
181 return super.getInstanceIdValue(id);
182 }
183
184 // #string_id_map#
185 private static final int
186 Id_constructor = 1,
187 Id_toString = 2,
188 Id_toSource = 3,
189 MAX_PROTOTYPE_ID = 3;
190
191 @Override
192 protected int findPrototypeId(String s)
193 {
194 int id;
195 // #generated# Last update: 2007-08-20 08:21:41 EDT
196 L0: { id = 0; String X = null; int c;
197 int s_length = s.length();
198 if (s_length==8) {
199 c=s.charAt(3);
200 if (c=='o') { X="toSource";id=Id_toSource; }
201 else if (c=='t') { X="toString";id=Id_toString; }
202 }
203 else if (s_length==11) { X="constructor";id=Id_constructor; }
204 if (X!=null && X!=s && !X.equals(s)) id = 0;
205 break L0;
206 }
207 // #/generated#
208 return id;
209 }
210 // #/string_id_map#
211
212 @Override
213 protected void initPrototypeId(int id)
214 {
215 String s;
216 int arity;
217 switch (id) {
218 case Id_constructor: arity=2; s="constructor"; break;
219 case Id_toString: arity=0; s="toString"; break;
220 case Id_toSource: arity=0; s="toSource"; break;
221 default: throw new IllegalArgumentException(String.valueOf(id));
222 }
223 initPrototypeMethod(QNAME_TAG, id, s, arity);
224 }
225
226 @Override
227 public Object execIdCall(IdFunctionObject f,
228 Context cx,
229 Scriptable scope,
230 Scriptable thisObj,
231 Object[] args)
232 {
233 if (!f.hasTag(QNAME_TAG)) {
234 return super.execIdCall(f, cx, scope, thisObj, args);
235 }
236 int id = f.methodId();
237 switch (id) {
238 case Id_constructor:
239 return jsConstructor(cx, (thisObj == null), args);
240 case Id_toString:
241 return realThis(thisObj, f).toString();
242 case Id_toSource:
243 return realThis(thisObj, f).js_toSource();
244 }
245 throw new IllegalArgumentException(String.valueOf(id));
246 }
247
248 private QName realThis(Scriptable thisObj, IdFunctionObject f)
249 {
250 if(!(thisObj instanceof QName))
251 throw incompatibleCallError(f);
252 return (QName)thisObj;
253 }
254
255 QName newQName(XMLLibImpl lib, String q_uri, String q_localName, String q_prefix) {
256 QName prototype = this.prototype;
257 if (prototype == null) {
258 prototype = this;
259 }
260 XmlNode.Namespace ns = null;
261 if (q_prefix != null) {
262 ns = XmlNode.Namespace.create(q_prefix, q_uri);
263 } else if (q_uri != null) {
264 ns = XmlNode.Namespace.create(q_uri);
265 } else {
266 ns = null;
267 }
268 if (q_localName != null && q_localName.equals("*")) q_localName = null;
269 return create(lib, this.getParentScope(), prototype, XmlNode.QName.create(ns, q_localName));
270 }
271
272 // See ECMA357 13.3.2
273 QName constructQName(XMLLibImpl lib, Context cx, Object namespace, Object name) {
274 String nameString = null;
275 if (name instanceof QName) {
276 if (namespace == Undefined.instance) {
277 return (QName)name;
278 } else {
279 nameString = ((QName)name).localName();
280 }
281 }
282 if (name == Undefined.instance) {
283 nameString = "";
284 } else {
285 nameString = ScriptRuntime.toString(name);
286 }
287
288 if (namespace == Undefined.instance) {
289 if ("*".equals(nameString)) {
290 namespace = null;
291 } else {
292 namespace = lib.getDefaultNamespace(cx);
293 }
294 }
295 Namespace namespaceNamespace = null;
296 if (namespace == null) {
297 // leave as null
298 } else if (namespace instanceof Namespace) {
299 namespaceNamespace = (Namespace)namespace;
300 } else {
301 namespaceNamespace = lib.newNamespace(ScriptRuntime.toString(namespace));
302 }
303 String q_localName = nameString;
304 String q_uri;
305 String q_prefix;
306 if (namespace == null) {
307 q_uri = null;
308 q_prefix = null; // corresponds to undefined; see QName class
309 } else {
310 q_uri = namespaceNamespace.uri();
311 q_prefix = namespaceNamespace.prefix();
312 }
313 return newQName(lib, q_uri, q_localName, q_prefix);
314 }
315
316 QName constructQName(XMLLibImpl lib, Context cx, Object nameValue) {
317 return constructQName(lib, cx, Undefined.instance, nameValue);
318 }
319
320 QName castToQName(XMLLibImpl lib, Context cx, Object qnameValue) {
321 if (qnameValue instanceof QName) {
322 return (QName)qnameValue;
323 }
324 return constructQName(lib, cx, qnameValue);
325 }
326
327 private Object jsConstructor(Context cx, boolean inNewExpr, Object[] args) {
328 // See ECMA357 13.3.2
329 if (!inNewExpr && args.length == 1) {
330 return castToQName(lib, cx, args[0]);
331 }
332 if (args.length == 0) {
333 return constructQName(lib, cx, Undefined.instance);
334 } else if (args.length == 1) {
335 return constructQName(lib, cx, args[0]);
336 } else {
337 return constructQName(lib, cx, args[0], args[1]);
338 }
339 }
340
341 private String js_toSource() {
342 StringBuilder sb = new StringBuilder();
343 sb.append('(');
344 toSourceImpl(uri(), localName(), prefix(), sb);
345 sb.append(')');
346 return sb.toString();
347 }
348
349 private static void toSourceImpl(String uri, String localName, String prefix, StringBuilder sb) {
350 sb.append("new QName(");
351 if (uri == null && prefix == null) {
352 if (!"*".equals(localName)) {
353 sb.append("null, ");
354 }
355 } else {
356 Namespace.toSourceImpl(prefix, uri, sb);
357 sb.append(", ");
358 }
359 sb.append('\'');
360 sb.append(ScriptRuntime.escapeString(localName, '\''));
361 sb.append("')");
362 }
363 }