Debian Codegraph @

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