Debian Codegraph @

rhino/xmlimplsrc/org/mozilla/javascript/xmlimpl/XMLName.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 class XMLName extends Ref {
12 static final long serialVersionUID = 3832176310755686977L;
13
14 private static boolean isNCNameStartChar(int c) {
15 if ((c & ~0x7F) == 0) {
16 // Optimize for ASCII and use A..Z < _ < a..z
17 if (c >= 'a') {
18 return c <= 'z';
19 } else if (c >= 'A') {
20 if (c <= 'Z') {
21 return true;
22 }
23 return c == '_';
24 }
25 } else if ((c & ~0x1FFF) == 0) {
26 return (0xC0 <= c && c <= 0xD6)
27 || (0xD8 <= c && c <= 0xF6)
28 || (0xF8 <= c && c <= 0x2FF)
29 || (0x370 <= c && c <= 0x37D)
30 || 0x37F <= c;
31 }
32 return (0x200C <= c && c <= 0x200D)
33 || (0x2070 <= c && c <= 0x218F)
34 || (0x2C00 <= c && c <= 0x2FEF)
35 || (0x3001 <= c && c <= 0xD7FF)
36 || (0xF900 <= c && c <= 0xFDCF)
37 || (0xFDF0 <= c && c <= 0xFFFD)
38 || (0x10000 <= c && c <= 0xEFFFF);
39 }
40
41 private static boolean isNCNameChar(int c) {
42 if ((c & ~0x7F) == 0) {
43 // Optimize for ASCII and use - < . < 0..9 < A..Z < _ < a..z
44 if (c >= 'a') {
45 return c <= 'z';
46 } else if (c >= 'A') {
47 if (c <= 'Z') {
48 return true;
49 }
50 return c == '_';
51 } else if (c >= '0') {
52 return c <= '9';
53 } else {
54 return c == '-' || c == '.';
55 }
56 } else if ((c & ~0x1FFF) == 0) {
57 return isNCNameStartChar(c) || c == 0xB7
58 || (0x300 <= c && c <= 0x36F);
59 }
60 return isNCNameStartChar(c) || (0x203F <= c && c <= 0x2040);
61 }
62
63 // This means "accept" in the parsing sense
64 // See ECMA357 13.1.2.1
65 static boolean accept(Object nameObj) {
66 String name;
67 try {
68 name = ScriptRuntime.toString(nameObj);
69 } catch (EcmaError ee) {
70 if ("TypeError".equals(ee.getName())) {
71 return false;
72 }
73 throw ee;
74 }
75
76 // See http://w3.org/TR/xml-names11/#NT-NCName
77 int length = name.length();
78 if (length != 0) {
79 if (isNCNameStartChar(name.charAt(0))) {
80 for (int i = 1; i != length; ++i) {
81 if (!isNCNameChar(name.charAt(i))) {
82 return false;
83 }
84 }
85 return true;
86 }
87 }
88
89 return false;
90 }
91
92 private XmlNode.QName qname;
93 private boolean isAttributeName;
94 private boolean isDescendants;
95 private XMLObjectImpl xmlObject;
96
97 private XMLName() {
98 }
99
100 static XMLName formStar() {
101 XMLName rv = new XMLName();
102 rv.qname = XmlNode.QName.create(null, null);
103 return rv;
104 }
105
106 /** @deprecated */
107 @Deprecated
108 static XMLName formProperty(XmlNode.Namespace namespace, String localName) {
109 if (localName != null && localName.equals("*")) localName = null;
110 XMLName rv = new XMLName();
111 rv.qname = XmlNode.QName.create(namespace, localName);
112 return rv;
113 }
114
115 /** TODO: marked deprecated by original author */
116 static XMLName formProperty(String uri, String localName) {
117 return formProperty(XmlNode.Namespace.create(uri), localName);
118 }
119
120 /** TODO: marked deprecated by original implementor */
121 static XMLName create(String defaultNamespaceUri, String name) {
122 if (name == null)
123 throw new IllegalArgumentException();
124
125 int l = name.length();
126 if (l != 0) {
127 char firstChar = name.charAt(0);
128 if (firstChar == '*') {
129 if (l == 1) {
130 return XMLName.formStar();
131 }
132 } else if (firstChar == '@') {
133 XMLName xmlName = XMLName.formProperty("", name.substring(1));
134 xmlName.setAttributeName();
135 return xmlName;
136 }
137 }
138
139 return XMLName.formProperty(defaultNamespaceUri, name);
140 }
141
142 static XMLName create(XmlNode.QName qname, boolean attribute, boolean descendants) {
143 XMLName rv = new XMLName();
144 rv.qname = qname;
145 rv.isAttributeName = attribute;
146 rv.isDescendants = descendants;
147 return rv;
148 }
149
150 /** @deprecated */
151 @Deprecated
152 static XMLName create(XmlNode.QName qname) {
153 return create(qname, false, false);
154 }
155
156 void initXMLObject(XMLObjectImpl xmlObject) {
157 if (xmlObject == null) throw new IllegalArgumentException();
158 if (this.xmlObject != null) throw new IllegalStateException();
159 this.xmlObject = xmlObject;
160 }
161
162 String uri() {
163 if (qname.getNamespace() == null) return null;
164 return qname.getNamespace().getUri();
165 }
166
167 String localName() {
168 if (qname.getLocalName() == null) return "*";
169 return qname.getLocalName();
170 }
171
172 private void addDescendantChildren(XMLList list, XML target) {
173 XMLName xmlName = this;
174 if (target.isElement()) {
175 XML[] children = target.getChildren();
176 for (int i=0; i<children.length; i++) {
177 if (xmlName.matches( children[i] )) {
178 list.addToList( children[i] );
179 }
180 addDescendantChildren(list, children[i]);
181 }
182 }
183 }
184
185 void addMatchingAttributes(XMLList list, XML target) {
186 XMLName name = this;
187 if (target.isElement()) {
188 XML[] attributes = target.getAttributes();
189 for (int i=0; i<attributes.length; i++) {
190 if (name.matches( attributes[i]) ) {
191 list.addToList( attributes[i] );
192 }
193 }
194 }
195 }
196
197 private void addDescendantAttributes(XMLList list, XML target) {
198 if (target.isElement()) {
199 addMatchingAttributes(list, target);
200 XML[] children = target.getChildren();
201 for (int i=0; i<children.length; i++) {
202 addDescendantAttributes(list, children[i]);
203 }
204 }
205 }
206
207 XMLList matchDescendantAttributes(XMLList rv, XML target) {
208 rv.setTargets(target, null);
209 addDescendantAttributes(rv, target);
210 return rv;
211 }
212
213 XMLList matchDescendantChildren(XMLList rv, XML target) {
214 rv.setTargets(target, null);
215 addDescendantChildren(rv, target);
216 return rv;
217 }
218
219 void addDescendants(XMLList rv, XML target) {
220 XMLName xmlName = this;
221 if (xmlName.isAttributeName()) {
222 matchDescendantAttributes(rv, target);
223 } else {
224 matchDescendantChildren(rv, target);
225 }
226 }
227
228 private void addAttributes(XMLList rv, XML target) {
229 addMatchingAttributes(rv, target);
230 }
231
232 void addMatches(XMLList rv, XML target) {
233 if (isDescendants()) {
234 addDescendants(rv, target);
235 } else if (isAttributeName()) {
236 addAttributes(rv, target);
237 } else {
238 XML[] children = target.getChildren();
239 if (children != null) {
240 for (int i=0; i<children.length; i++) {
241 if (this.matches(children[i])) {
242 rv.addToList( children[i] );
243 }
244 }
245 }
246 rv.setTargets(target, this.toQname());
247 }
248 }
249
250 XMLList getMyValueOn(XML target) {
251 XMLList rv = target.newXMLList();
252 addMatches(rv, target);
253 return rv;
254 }
255
256 void setMyValueOn(XML target, Object value) {
257 // Special-case checks for undefined and null
258 if (value == null) {
259 value = "null";
260 } else if (value instanceof Undefined) {
261 value = "undefined";
262 }
263
264 XMLName xmlName = this;
265 // Get the named property
266 if (xmlName.isAttributeName()) {
267 target.setAttribute(xmlName, value);
268 } else if (xmlName.uri() == null && xmlName.localName().equals("*")) {
269 target.setChildren(value);
270 } else {
271 // Convert text into XML if needed.
272 XMLObjectImpl xmlValue = null;
273
274 if (value instanceof XMLObjectImpl) {
275 xmlValue = (XMLObjectImpl)value;
276
277 // Check for attribute type and convert to textNode
278 if (xmlValue instanceof XML) {
279 if (((XML)xmlValue).isAttribute()) {
280 xmlValue = target.makeXmlFromString(xmlName,
281 xmlValue.toString());
282 }
283 }
284
285 if (xmlValue instanceof XMLList) {
286 for (int i = 0; i < xmlValue.length(); i++) {
287 XML xml = ((XMLList) xmlValue).item(i);
288
289 if (xml.isAttribute()) {
290 ((XMLList)xmlValue).replace(i, target.makeXmlFromString(xmlName, xml.toString()));
291 }
292 }
293 }
294 } else {
295 xmlValue = target.makeXmlFromString(xmlName, ScriptRuntime.toString(value));
296 }
297
298 XMLList matches = target.getPropertyList(xmlName);
299
300 if (matches.length() == 0) {
301 target.appendChild(xmlValue);
302 } else {
303 // Remove all other matches
304 for (int i = 1; i < matches.length(); i++) {
305 target.removeChild(matches.item(i).childIndex());
306 }
307
308 // Replace first match with new value.
309 XML firstMatch = matches.item(0);
310 target.replace(firstMatch.childIndex(), xmlValue);
311 }
312 }
313 }
314
315 @Override
316 public boolean has(Context cx) {
317 if (xmlObject == null) {
318 return false;
319 }
320 return xmlObject.hasXMLProperty(this);
321 }
322
323 @Override
324 public Object get(Context cx) {
325 if (xmlObject == null) {
326 throw ScriptRuntime.undefReadError(Undefined.instance,
327 toString());
328 }
329 return xmlObject.getXMLProperty(this);
330 }
331
332 @Override
333 public Object set(Context cx, Object value) {
334 if (xmlObject == null) {
335 throw ScriptRuntime.undefWriteError(Undefined.instance,
336 toString(),
337 value);
338 }
339 // Assignment to descendants causes parse error on bad reference
340 // and this should not be called
341 if (isDescendants) throw Kit.codeBug();
342 xmlObject.putXMLProperty(this, value);
343 return value;
344 }
345
346 @Override
347 public boolean delete(Context cx) {
348 if (xmlObject == null) {
349 return true;
350 }
351 xmlObject.deleteXMLProperty(this);
352 return !xmlObject.hasXMLProperty(this);
353 }
354
355 @Override
356 public String toString() {
357 //return qname.localName();
358 StringBuilder buff = new StringBuilder();
359 if (isDescendants) buff.append("..");
360 if (isAttributeName) buff.append('@');
361 if (uri() == null) {
362 buff.append('*');
363 if(localName().equals("*")) {
364 return buff.toString();
365 }
366 } else {
367 buff.append('"').append(uri()).append('"');
368 }
369 buff.append(':').append(localName());
370 return buff.toString();
371 }
372
373 final XmlNode.QName toQname() {
374 return this.qname;
375 }
376
377 final boolean matchesLocalName(String localName) {
378 return localName().equals("*") || localName().equals(localName);
379 }
380
381 final boolean matchesElement(XmlNode.QName qname) {
382 if (this.uri() == null || this.uri().equals(qname.getNamespace().getUri())) {
383 if (this.localName().equals("*") || this.localName().equals(qname.getLocalName())) {
384 return true;
385 }
386 }
387 return false;
388 }
389
390 final boolean matches(XML node) {
391 XmlNode.QName qname = node.getNodeQname();
392 String nodeUri = null;
393 if (qname.getNamespace() != null) {
394 nodeUri = qname.getNamespace().getUri();
395 }
396 if (isAttributeName) {
397 if (node.isAttribute()) {
398 if (this.uri() == null || this.uri().equals(nodeUri)) {
399 if (this.localName().equals("*") || this.localName().equals(qname.getLocalName())) {
400 return true;
401 }
402 }
403 return false;
404 } else {
405 // TODO Could throw exception maybe, should not call this method on attribute name with arbitrary node type
406 // unless we traverse all attributes and children habitually
407 return false;
408 }
409 } else {
410 if ( this.uri() == null || ((node.isElement()) && this.uri().equals(nodeUri)) ) {
411 if (localName().equals("*")) return true;
412 if (node.isElement()) {
413 if (localName().equals(qname.getLocalName())) return true;
414 }
415 }
416 return false;
417 }
418 }
419
420 /* TODO: marked deprecated by original author */
421 boolean isAttributeName() {
422 return isAttributeName;
423 }
424
425 // TODO Fix whether this is an attribute XMLName at construction?
426 // Marked deprecated by original author
427 void setAttributeName() {
428 // if (isAttributeName) throw new IllegalStateException();
429 isAttributeName = true;
430 }
431
432 /* TODO: was marked deprecated by original author */
433 boolean isDescendants() {
434 return isDescendants;
435 }
436
437 // TODO Fix whether this is an descendant XMLName at construction?
438 /** @deprecated */
439 @Deprecated
440 void setIsDescendants() {
441 // if (isDescendants) throw new IllegalStateException();
442 isDescendants = true;
443 }
444 }