Committing E4X runtime implementation: see enhancement report 242805.

This is based on code contributed to Rhino by AgileDelta, Inc, www.agiledelta.com and in particular by

Ethan Hugg
Terry Lucas
Milen Nankov
John Schneider

Thanks!
This commit is contained in:
igor%mir2.org 2004-07-28 23:43:17 +00:00
Родитель 63a5e306e6
Коммит 88a3913994
13 изменённых файлов: 8200 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,117 @@
<?xml version="1.0" encoding="utf-8"?>
<project name="xmlimplsrc" basedir=".." default="compile">
<property file="build.properties"/>
<path id="xmlimpl.classpath">
<pathelement location="${xbean.jar}"/>
</path>
<target name="compile" unless="without-xmlimpl">
<available property="xbean-present?" file="${xbean.jar}" />
<antcall target="xbean-get" />
<condition property="xmlimpl-compile?">
<and>
<available file="${xbean.jar}" />
<!--
Does not work under JDK 1.3 due to incompatible classes
<available classname="javax.xml.namespace.QName"
classpathref="xmlimpl.classpath"/>
<available classname="org.apache.xmlbeans.XmlCursor"
classpathref="xmlimpl.classpath"/>
<available classname="org.apache.xmlbeans.XmlException"
classpathref="xmlimpl.classpath"/>
<available classname="org.apache.xmlbeans.XmlObject"
classpathref="xmlimpl.classpath"/>
<available classname="org.apache.xmlbeans.XmlOptions"
classpathref="xmlimpl.classpath"/>
-->
</and>
</condition>
<antcall target="do-compile" />
<antcall target="do-not-compile" />
</target>
<target name="do-compile" if="xmlimpl-compile?">
<echo>Compiling E4X implementation using ${xbean.jar}</echo>
<javac srcdir="xmlimplsrc"
destdir="${classes}"
includes="org/**/*.java"
deprecation="on"
debug="${debug}"
target="${target-jvm}"
classpathref="xmlimpl.classpath"
failonerror="${xmlimpl.compile.failonerror}"
>
</javac>
</target>
<target name="do-not-compile" unless="xmlimpl-compile?">
<echo>
Skipping compilation of E4X implementation due to lack of
javax.xml.namespace.*, org.apache.xmlbeans.* classes
</echo>
</target>
<target name="copy-source">
<mkdir dir="${dist.dir}/xmlimplsrc"/>
<copy todir="${dist.dir}/xmlimplsrc">
<fileset dir="xmlimplsrc"
includes="**/*.java,**/*.properties,**/*.xml"
/>
</copy>
</target>
<target name="clean">
<delete includeEmptyDirs="true">
<fileset dir="${classes}"
includes="org/mozilla/javascript/xmlimpl/**"/>
</delete>
</target>
<target name="xbean-get" unless="xbean-present?">
<property
name="xbean.url"
value="http://www.apache.org/dist/xml/xmlbeans/xmlbeans-current.zip"
/>
<property name="xbean.tmp" location="${build.dir}/tmp-xbean" />
<property name="xbean.zip" location="${xbean.tmp}/xbean.zip" />
<mkdir dir="${xbean.tmp}"/>
<get src="${xbean.url}" dest="${xbean.zip}" ignoreerrors="true" />
<available property="xbean-zip-present?" file="${xbean.zip}" />
<antcall target="xbean-unzip" />
</target>
<target name="xbean-unzip" if="xbean-zip-present?">
<unzip src="${xbean.zip}" dest="${xbean.tmp}">
<patternset includes="xmlbeans-*/lib/xbean.jar" />
</unzip>
<mkdir dir="${jarlib}" />
<copy tofile="${xbean.jar}">
<fileset dir="${xbean.tmp}" includes="xmlbeans-*/lib/xbean.jar" />
</copy>
<delete dir="${xbean.tmp}" />
</target>
</project>

Просмотреть файл

@ -0,0 +1,364 @@
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Rhino code, released
* May 6, 1999.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1997-2000 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* Ethan Hugg
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU Public License (the "GPL"), in which case the
* provisions of the GPL are applicable instead of those above.
* If you wish to allow use of your version of this file only
* under the terms of the GPL and not to allow others to use your
* version of this file under the NPL, indicate your decision by
* deleting the provisions above and replace them with the notice
* and other provisions required by the GPL. If you do not delete
* the provisions above, a recipient may use your version of this
* file under either the NPL or the GPL.
*/
package org.mozilla.javascript.xmlimpl;
import org.apache.xmlbeans.XmlCursor;
import java.util.*;
public class LogicalEquality
{
public static boolean nodesEqual(XmlCursor xmlOne, XmlCursor xmlTwo)
{
boolean result = false;
if (xmlOne.isStartdoc())
{
xmlOne.toFirstContentToken();
}
if (xmlTwo.isStartdoc())
{
xmlTwo.toFirstContentToken();
}
if (xmlOne.currentTokenType() == xmlTwo.currentTokenType())
{
if (xmlOne.isEnddoc())
{
// Both empty
result = true;
}
else if (xmlOne.isAttr())
{
result = attributesEqual(xmlOne, xmlTwo);
}
else if (xmlOne.isText())
{
result = textNodesEqual(xmlOne, xmlTwo);
}
else if (xmlOne.isComment())
{
result = commentsEqual(xmlOne, xmlTwo);
}
else if (xmlOne.isProcinst())
{
result = processingInstructionsEqual(xmlOne, xmlTwo);
}
else if (xmlOne.isStart())
{
// Compare root elements
result = elementsEqual(xmlOne, xmlTwo);
}
}
return result;
}
private static boolean elementsEqual(XmlCursor xmlOne, XmlCursor xmlTwo)
{
boolean result = true;
if (!qnamesEqual(xmlOne.getName(), xmlTwo.getName()))
{
result = false;
}
else
{
// These filter out empty text nodes.
nextToken(xmlOne);
nextToken(xmlTwo);
do
{
if (xmlOne.currentTokenType() != xmlTwo.currentTokenType())
{
// Not same token
result = false;
break;
}
else if (xmlOne.isEnd())
{
// Done with this element, step over end
break;
}
else if (xmlOne.isEnddoc())
{
// Shouldn't get here
break;
}
else if (xmlOne.isAttr())
{
// This one will move us to the first non-attr token.
result = attributeListsEqual(xmlOne, xmlTwo);
}
else
{
if (xmlOne.isText())
{
result = textNodesEqual(xmlOne, xmlTwo);
}
else if (xmlOne.isComment())
{
result = commentsEqual(xmlOne, xmlTwo);
}
else if (xmlOne.isProcinst())
{
result = processingInstructionsEqual(xmlOne, xmlTwo);
}
else if (xmlOne.isStart())
{
result = elementsEqual(xmlOne, xmlTwo);
}
else
{
//XML.log("Unknown token type" + xmlOne.currentTokenType());
}
// These filter out empty text nodes.
nextToken(xmlOne);
nextToken(xmlTwo);
}
}
while(result);
}
return result;
}
/**
*
* @param xmlOne
* @param xmlTwo
* @return
*/
private static boolean attributeListsEqual(XmlCursor xmlOne, XmlCursor xmlTwo)
{
boolean result = true;
TreeMap mapOne = loadAttributeMap(xmlOne);
TreeMap mapTwo = loadAttributeMap(xmlTwo);
if (mapOne.size() != mapTwo.size())
{
result = false;
}
else
{
Set keysOne = mapOne.keySet();
Set keysTwo = mapTwo.keySet();
Iterator itOne = keysOne.iterator();
Iterator itTwo = keysTwo.iterator();
while (result && itOne.hasNext())
{
String valueOne = (String) itOne.next();
String valueTwo = (String) itTwo.next();
if (!valueOne.equals(valueTwo))
{
result = false;
}
else
{
javax.xml.namespace.QName qnameOne = (javax.xml.namespace.QName) mapOne.get(valueOne);
javax.xml.namespace.QName qnameTwo = (javax.xml.namespace.QName) mapTwo.get(valueTwo);
if (!qnamesEqual(qnameOne, qnameTwo))
{
result = false;
}
}
}
}
return result;
}
/**
*
* @param xml
* @return
*/
private static TreeMap loadAttributeMap(XmlCursor xml)
{
TreeMap result = new TreeMap();
while (xml.isAttr())
{
result.put(xml.getTextValue(), xml.getName());
nextToken(xml);
}
return result;
}
/**
*
* @param xmlOne
* @param xmlTwo
* @return
*/
private static boolean attributesEqual(XmlCursor xmlOne, XmlCursor xmlTwo)
{
boolean result = false;
if (xmlOne.isAttr() && xmlTwo.isAttr())
{
if (qnamesEqual(xmlOne.getName(), xmlTwo.getName()))
{
if (xmlOne.getTextValue().equals(xmlTwo.getTextValue()))
{
result = true;
}
}
}
return result;
}
/**
*
* @param xmlOne
* @param xmlTwo
* @return
*/
private static boolean textNodesEqual(XmlCursor xmlOne, XmlCursor xmlTwo)
{
boolean result = false;
if (xmlOne.isText() && xmlTwo.isText())
{
if (xmlOne.getChars().equals(xmlTwo.getChars()))
{
result = true;
}
}
return result;
}
/**
*
* @param xmlOne
* @param xmlTwo
* @return
*/
private static boolean commentsEqual(XmlCursor xmlOne, XmlCursor xmlTwo)
{
boolean result = false;
if (xmlOne.isComment() && xmlTwo.isComment())
{
if (xmlOne.getTextValue().equals(xmlTwo.getTextValue()))
{
result = true;
}
}
return result;
}
/**
*
* @param xmlOne
* @param xmlTwo
* @return
*/
private static boolean processingInstructionsEqual(XmlCursor xmlOne, XmlCursor xmlTwo)
{
boolean result = false;
if (xmlOne.isProcinst() && xmlTwo.isProcinst())
{
if (qnamesEqual(xmlOne.getName(), xmlTwo.getName()))
{
if (xmlOne.getTextValue().equals(xmlTwo.getTextValue()))
{
result = true;
}
}
}
return result;
}
/**
*
* @param qnameOne
* @param qnameTwo
* @return
*/
private static boolean qnamesEqual(javax.xml.namespace.QName qnameOne, javax.xml.namespace.QName qnameTwo)
{
boolean result = false;
if (qnameOne.getNamespaceURI().equals(qnameTwo.getNamespaceURI()))
{
if (qnameOne.getLocalPart().equals(qnameTwo.getLocalPart()))
{
return true;
}
}
return result;
}
/**
* filter out empty textNodes here
*
* @param xml
*/
private static void nextToken(XmlCursor xml)
{
do
{
xml.toNextToken();
if (!xml.isText())
{
// Not a text node
break;
}
else if (xml.getChars().trim().length() > 0)
{
// Text node is not empty
break;
}
}
while (true);
}
}

Просмотреть файл

@ -0,0 +1,309 @@
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Rhino code, released
* May 6, 1999.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1997-2000 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* Ethan Hugg
* Terry Lucas
* Milen Nankov
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU Public License (the "GPL"), in which case the
* provisions of the GPL are applicable instead of those above.
* If you wish to allow use of your version of this file only
* under the terms of the GPL and not to allow others to use your
* version of this file under the NPL, indicate your decision by
* deleting the provisions above and replace them with the notice
* and other provisions required by the GPL. If you do not delete
* the provisions above, a recipient may use your version of this
* file under either the NPL or the GPL.
*/
package org.mozilla.javascript.xmlimpl;
import org.mozilla.javascript.*;
/**
* Class Namespace
*
*/
class Namespace extends IdScriptable
{
private static final Object NAMESPACE_TAG = new Object();
private XMLLibImpl lib;
private String prefix;
private String uri;
public Namespace(XMLLibImpl lib, String uri)
{
if (uri == null)
throw new IllegalArgumentException();
this.lib = lib;
this.prefix = (uri.length() == 0) ? "" : null;
this.uri = uri;
}
public Namespace(XMLLibImpl lib, String prefix, String uri)
{
if (uri == null)
throw new IllegalArgumentException();
if (uri.length() == 0) {
// prefix should be "" for empty uri
if (prefix == null)
throw new IllegalArgumentException();
if (prefix.length() != 0)
throw new IllegalArgumentException();
}
this.lib = lib;
this.prefix = prefix;
this.uri = uri;
}
public void exportAsJSClass(boolean sealed)
{
exportAsJSClass(MAX_PROTOTYPE_ID, lib.globalScope(), sealed);
}
/**
*
* @return
*/
public String uri()
{
return uri;
}
/**
*
* @return
*/
public String prefix()
{
return prefix;
}
/**
*
* @return
*/
public String toString ()
{
return uri();
}
/**
*
* @return
*/
public String toLocaleString ()
{
return toString();
}
public boolean equals(Object obj)
{
if (!(obj instanceof Namespace)) return false;
return equivalentValues(obj).booleanValue();
}
public Boolean equivalentValues(Object value)
{
if (!(value instanceof Namespace)) return null;
Namespace n = (Namespace)value;
return uri().equals(n.uri()) ? Boolean.TRUE : Boolean.FALSE;
}
/**
*
* @return
*/
public String getClassName ()
{
return "Namespace";
}
/**
*
* @param hint
* @return
*/
public Object getDefaultValue (Class hint)
{
return uri();
}
protected Scriptable defaultPrototype()
{
Scriptable result = lib.namespacePrototype;
if (result == this) {
result = null;
}
return result;
}
protected Scriptable defaultParentScope()
{
return lib.globalScope();
}
// #string_id_map#
private static final int
Id_prefix = 1,
Id_uri = 2,
MAX_INSTANCE_ID = 2;
// maxId for superclass
private static int idBase = -1;
{
if (idBase < 0) idBase = getMaxInstanceId();
setMaxInstanceId(idBase, idBase + MAX_INSTANCE_ID);
}
protected int findInstanceIdInfo(String s)
{
int id;
// #generated# Last update: 2004-07-20 19:50:50 CEST
L0: { id = 0; String X = null;
int s_length = s.length();
if (s_length==3) { X="uri";id=Id_uri; }
else if (s_length==6) { X="prefix";id=Id_prefix; }
if (X!=null && X!=s && !X.equals(s)) id = 0;
}
// #/generated#
if (id == 0) return super.findInstanceIdInfo(s);
int attr;
switch (id) {
case Id_prefix:
case Id_uri:
attr = PERMANENT | READONLY;
break;
default: throw new IllegalStateException();
}
return instanceIdInfo(attr, idBase + id);
}
// #/string_id_map#
protected String getInstanceIdName(int id)
{
switch (id - idBase) {
case Id_prefix: return "prefix";
case Id_uri: return "uri";
}
return super.getInstanceIdName(id);
}
protected Object getInstanceIdValue(int id)
{
switch (id - idBase) {
case Id_prefix:
if (prefix == null) return Undefined.instance;
return prefix;
case Id_uri:
return uri;
}
return super.getInstanceIdValue(id);
}
// #string_id_map#
private static final int
Id_constructor = 1,
Id_toString = 2,
MAX_PROTOTYPE_ID = 2;
protected int findPrototypeId(String s)
{
int id;
// #generated# Last update: 2004-07-20 19:45:27 CEST
L0: { id = 0; String X = null;
int s_length = s.length();
if (s_length==8) { X="toString";id=Id_toString; }
else if (s_length==11) { X="constructor";id=Id_constructor; }
if (X!=null && X!=s && !X.equals(s)) id = 0;
}
// #/generated#
return id;
}
// #/string_id_map#
protected void initPrototypeId(int id)
{
String s;
int arity;
switch (id) {
case Id_constructor: arity=2; s="constructor"; break;
case Id_toString: arity=0; s="toString"; break;
default: throw new IllegalArgumentException(String.valueOf(id));
}
initPrototypeMethod(NAMESPACE_TAG, id, s, arity);
}
public Object execMethod(IdFunction f,
Context cx,
Scriptable scope,
Scriptable thisObj,
Object[] args)
throws JavaScriptException
{
if (!f.hasTag(NAMESPACE_TAG)) {
return super.execMethod(f, cx, scope, thisObj, args);
}
int id = f.methodId();
if (id == Id_constructor) {
return jsConstructor(cx, (thisObj == null), args);
} else if(id == Id_toString) {
return realThis(thisObj, f).jsFunction_toString();
}
throw new IllegalArgumentException(String.valueOf(id));
}
private Namespace realThis(Scriptable thisObj, IdFunction f)
{
if(!(thisObj instanceof Namespace))
throw incompatibleCallError(f);
return (Namespace)thisObj;
}
private Object jsConstructor(Context cx, boolean inNewExpr, Object[] args)
{
if (!inNewExpr && args.length == 1) {
return lib.castToNamespace(cx, args[0]);
}
if (args.length == 0) {
return lib.constructNamespace(cx);
} else if (args.length == 1) {
return lib.constructNamespace(cx, args[0]);
} else {
return lib.constructNamespace(cx, args[0], args[1]);
}
}
private String jsFunction_toString()
{
return toString();
}
}

Просмотреть файл

@ -0,0 +1,348 @@
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Rhino code, released
* May 6, 1999.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1997-2000 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* Milen Nankov
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU Public License (the "GPL"), in which case the
* provisions of the GPL are applicable instead of those above.
* If you wish to allow use of your version of this file only
* under the terms of the GPL and not to allow others to use your
* version of this file under the NPL, indicate your decision by
* deleting the provisions above and replace them with the notice
* and other provisions required by the GPL. If you do not delete
* the provisions above, a recipient may use your version of this
* file under either the NPL or the GPL.
*/
package org.mozilla.javascript.xmlimpl;
import java.util.*;
import org.apache.xmlbeans.XmlCursor;
import org.mozilla.javascript.*;
import org.mozilla.javascript.xml.*;
class NamespaceHelper
{
private XMLLibImpl lib;
private final Map prefixToURI = new HashMap();
private final Map uriToPrefix = new HashMap();
// A set of URIs that are used without explicit namespace declaration in scope.
private final Set undeclared = new HashSet();
private NamespaceHelper(XMLLibImpl lib)
{
this.lib = lib;
// Insert the default namespace
prefixToURI.put("", "");
Set prefixes = new HashSet();
prefixes.add("");
uriToPrefix.put("", prefixes);
}
/**
* Declared a new namespace
*
* @param prefix
* @param uri
* @param declarations
*/
private void declareNamespace(String prefix, String uri, ObjArray declarations)
{
Set prefixes = (Set)uriToPrefix.get(uri);
if(prefixes == null)
{
prefixes = new HashSet();
uriToPrefix.put(uri, prefixes);
}
if(!prefixes.contains(prefix))
{
String oldURI = (String)prefixToURI.get(prefix);
// Add the new mapping
prefixes.add(prefix);
prefixToURI.put(prefix, uri);
if(declarations != null)
declarations.add(new Namespace(lib, prefix, uri));
if(oldURI != null)
{
// Update the existing mapping
prefixes = (Set)uriToPrefix.get(oldURI);
prefixes.remove(prefix);
}
}
}
/**
* Updates the internal state of this NamespaceHelper to reflect the
* existance of the XML token pointed to by the cursor.
*/
private void processName(XmlCursor cursor, ObjArray declarations)
{
javax.xml.namespace.QName qname = cursor.getName();
String uri = qname.getNamespaceURI();
Set prefixes = (Set)uriToPrefix.get(uri);
if(prefixes == null || prefixes.size() == 0)
{
undeclared.add(uri);
if(declarations != null)
declarations.add(new Namespace(lib, uri));
}
}
/**
* Updates the internal state of this NamespaceHelper with the
* namespace information of the element pointed to by the cursor.
*/
private void update(XmlCursor cursor, ObjArray declarations)
{
// Process the Namespace declarations
cursor.push();
while(cursor.toNextToken().isAnyAttr())
{
if(cursor.isNamespace())
{
javax.xml.namespace.QName name = cursor.getName();
String prefix = name.getLocalPart();
String uri = name.getNamespaceURI();
declareNamespace(prefix, uri, declarations);
}
}
cursor.pop();
// Process the element
processName(cursor, declarations);
// Process the attributes
cursor.push();
boolean hasNext = cursor.toFirstAttribute();
while(hasNext)
{
processName(cursor, declarations);
hasNext = cursor.toNextAttribute();
}
cursor.pop();
}
/**
* @return Object[] array of Namespace objects in scope at the cursor.
*/
public static Object[] inScopeNamespaces(XMLLibImpl lib, XmlCursor cursor)
{
ObjArray namespaces = new ObjArray();
NamespaceHelper helper = new NamespaceHelper(lib);
cursor.push();
int depth = 0;
while(cursor.hasPrevToken())
{
if(cursor.isContainer())
{
cursor.push();
depth++;
}
cursor.toParent();
}
for(int i = 0; i < depth; i++)
{
cursor.pop();
helper.update(cursor, null);
}
Iterator i = helper.prefixToURI.entrySet().iterator();
while(i.hasNext())
{
Map.Entry entry = (Map.Entry)i.next();
Namespace ns = new Namespace(lib, (String)entry.getKey(),
(String)entry.getValue());
namespaces.add(ns);
}
i = helper.undeclared.iterator();
while(i.hasNext())
{
Namespace ns = new Namespace(lib, (String)i.next());
namespaces.add(ns);
}
cursor.pop();
return namespaces.toArray();
}
static Namespace getNamespace(XMLLibImpl lib, XmlCursor cursor,
Object[] inScopeNamespaces)
{
String uri;
String prefix;
if (cursor.isProcinst()) {
uri = "";
prefix = "";
} else {
javax.xml.namespace.QName qname = cursor.getName();
uri = qname.getNamespaceURI();
prefix = qname.getPrefix();
}
if (inScopeNamespaces == null)
return new Namespace(lib, prefix, uri);
Namespace result = null;
for (int i = 0; i != inScopeNamespaces.length; ++i) {
Namespace ns = (Namespace)inScopeNamespaces[i];
if(ns == null) continue;
String nsURI = ns.uri();
if(nsURI.equals(uri))
{
if(prefix.equals(ns.prefix()))
{
result = ns;
break;
}
if(result == null ||
(result.prefix() == null &&
ns.prefix() != null))
result = ns;
}
}
if(result == null)
result = new Namespace(lib, prefix, uri);
return result;
}
/**
* @return List of Namespace objects that are declared in the container pointed to by the cursor.
*/
public static Object[] namespaceDeclarations(XMLLibImpl lib, XmlCursor cursor)
{
ObjArray declarations = new ObjArray();
NamespaceHelper helper = new NamespaceHelper(lib);
cursor.push();
int depth = 0;
while(cursor.hasPrevToken())
{
if(cursor.isContainer())
{
cursor.push();
depth++;
}
cursor.toParent();
}
for(int i = 0; i < depth - 1; i++)
{
cursor.pop();
helper.update(cursor, null);
}
if(depth > 0)
{
cursor.pop();
helper.update(cursor, declarations);
}
cursor.pop();
return declarations.toArray();
}
/**
* @return Prefix to URI map of all namespaces in scope at the cursor.
*/
public static Map getAllNamespaces(XMLLibImpl lib, XmlCursor cursor)
{
NamespaceHelper helper = new NamespaceHelper(lib);
cursor.push();
int depth = 0;
while(cursor.hasPrevToken())
{
if(cursor.isContainer())
{
cursor.push();
depth++;
}
cursor.toParent();
}
for(int i = 0; i < depth; i++)
{
cursor.pop();
helper.update(cursor, null);
}
cursor.pop();
return helper.prefixToURI;
}
public static void getNamespaces(XmlCursor cursor, Map prefixToURI)
{
cursor.push();
while(cursor.toNextToken().isAnyAttr())
{
if(cursor.isNamespace())
{
javax.xml.namespace.QName name = cursor.getName();
String prefix = name.getLocalPart();
String uri = name.getNamespaceURI();
prefixToURI.put(prefix, uri);
}
}
cursor.pop();
}
public static void removeNamespace(XmlCursor cursor, String prefix)
{
cursor.push();
while(cursor.toNextToken().isAnyAttr())
{
if(cursor.isNamespace())
{
javax.xml.namespace.QName name = cursor.getName();
if(name.getLocalPart().equals(prefix))
{
cursor.removeXml();
break;
}
}
}
cursor.pop();
}
}

Просмотреть файл

@ -0,0 +1,306 @@
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Rhino code, released
* May 6, 1999.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1997-2000 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* Ethan Hugg
* Terry Lucas
* Milen Nankov
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU Public License (the "GPL"), in which case the
* provisions of the GPL are applicable instead of those above.
* If you wish to allow use of your version of this file only
* under the terms of the GPL and not to allow others to use your
* version of this file under the NPL, indicate your decision by
* deleting the provisions above and replace them with the notice
* and other provisions required by the GPL. If you do not delete
* the provisions above, a recipient may use your version of this
* file under either the NPL or the GPL.
*/
package org.mozilla.javascript.xmlimpl;
import org.mozilla.javascript.*;
/**
* Class QName
*
*/
public final class QName extends IdScriptable
{
private static final Object QNAME_TAG = new Object();
XMLLibImpl lib;
private String prefix;
private String localName;
private String uri;
static void init(XMLLibImpl lib, boolean sealed)
{
QName obj = new QName(lib, "", "", "");
lib.qnamePrototype = obj;
obj.exportAsJSClass(MAX_PROTOTYPE_ID, lib.globalScope(), sealed);
}
public QName(XMLLibImpl lib, String uri, String localName, String prefix)
{
if (localName == null) throw new IllegalArgumentException();
this.lib = lib;
this.uri = uri;
this.prefix = prefix;
this.localName = localName;
}
public void exportAsJSClass(boolean sealed)
{
exportAsJSClass(MAX_PROTOTYPE_ID, lib.globalScope(), sealed);
}
/**
*
* @return
*/
public String toString()
{
String result;
if (uri == null)
{
result = "*::".concat(localName);
}
else if(uri.length() == 0)
{
result = localName;
}
else
{
result = uri + "::" + localName;
}
return result;
}
public String localName()
{
return localName;
}
public String prefix()
{
return (prefix == null) ? prefix : "";
}
public String uri()
{
return uri;
}
public boolean equals(Object obj)
{
if(!(obj instanceof QName)) return false;
return equivalentValues((QName)obj).booleanValue();
}
public Boolean equivalentValues(Object value)
{
if(!(value instanceof QName)) return null;
boolean result;
QName q = (QName)value;
if (uri == null) {
result = q.uri == null && localName.equals(q.localName);
} else {
result = uri.equals(q.uri) && localName.equals(q.localName);
}
return result ? Boolean.TRUE : Boolean.FALSE;
}
/**
*
* @return
*/
public String getClassName ()
{
return "QName";
}
/**
*
* @param hint
* @return
*/
public Object getDefaultValue (Class hint)
{
return toString();
}
protected Scriptable defaultPrototype()
{
Scriptable result = lib.qnamePrototype;
if (result == this) {
result = null;
}
return result;
}
protected Scriptable defaultParentScope()
{
return lib.globalScope();
}
// #string_id_map#
private static final int
Id_localName = 1,
Id_uri = 2,
MAX_INSTANCE_ID = 2;
// maxId for superclass
private static int idBase = -1;
{
if (idBase < 0) idBase = getMaxInstanceId();
setMaxInstanceId(idBase, idBase + MAX_INSTANCE_ID);
}
protected int findInstanceIdInfo(String s)
{
int id;
// #generated# Last update: 2004-07-18 12:32:51 CEST
L0: { id = 0; String X = null;
int s_length = s.length();
if (s_length==3) { X="uri";id=Id_uri; }
else if (s_length==9) { X="localName";id=Id_localName; }
if (X!=null && X!=s && !X.equals(s)) id = 0;
}
// #/generated#
if (id == 0) return super.findInstanceIdInfo(s);
int attr;
switch (id) {
case Id_localName:
case Id_uri:
attr = PERMANENT | READONLY;
break;
default: throw new IllegalStateException();
}
return instanceIdInfo(attr, idBase + id);
}
// #/string_id_map#
protected String getInstanceIdName(int id)
{
switch (id - idBase) {
case Id_localName: return "localName";
case Id_uri: return "uri";
}
return super.getInstanceIdName(id);
}
protected Object getInstanceIdValue(int id)
{
switch (id - idBase) {
case Id_localName: return localName;
case Id_uri: return uri;
}
return super.getInstanceIdValue(id);
}
// #string_id_map#
private static final int
Id_constructor = 1,
Id_toString = 2,
MAX_PROTOTYPE_ID = 2;
protected int findPrototypeId(String s)
{
int id;
// #generated# Last update: 2004-07-18 12:32:51 CEST
L0: { id = 0; String X = null;
int s_length = s.length();
if (s_length==8) { X="toString";id=Id_toString; }
else if (s_length==11) { X="constructor";id=Id_constructor; }
if (X!=null && X!=s && !X.equals(s)) id = 0;
}
// #/generated#
return id;
}
// #/string_id_map#
protected void initPrototypeId(int id)
{
String s;
int arity;
switch (id) {
case Id_constructor: arity=2; s="constructor"; break;
case Id_toString: arity=0; s="toString"; break;
default: throw new IllegalArgumentException(String.valueOf(id));
}
initPrototypeMethod(QNAME_TAG, id, s, arity);
}
public Object execMethod(IdFunction f,
Context cx,
Scriptable scope,
Scriptable thisObj,
Object[] args)
throws JavaScriptException
{
if (!f.hasTag(QNAME_TAG)) {
return super.execMethod(f, cx, scope, thisObj, args);
}
int id = f.methodId();
if (id == Id_constructor) {
return jsConstructor(cx, (thisObj == null), args);
} else if(id == Id_toString) {
return realThis(thisObj, f).jsFunction_toString();
}
throw new IllegalArgumentException(String.valueOf(id));
}
private QName realThis(Scriptable thisObj, IdFunction f)
{
if(!(thisObj instanceof QName))
throw incompatibleCallError(f);
return (QName)thisObj;
}
private Object jsConstructor(Context cx, boolean inNewExpr, Object[] args)
{
if (!inNewExpr && args.length == 1) {
return lib.castToQName(cx, args[0]);
}
if (args.length == 0) {
return lib.constructQName(cx, Undefined.instance);
} else if (args.length == 1) {
return lib.constructQName(cx, args[0]);
} else {
return lib.constructQName(cx, args[0], args[1]);
}
}
private String jsFunction_toString()
{
return toString();
}
}

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -0,0 +1,270 @@
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Rhino code, released
* May 6, 1999.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1997-2000 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* Igor Bukanov
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU Public License (the "GPL"), in which case the
* provisions of the GPL are applicable instead of those above.
* If you wish to allow use of your version of this file only
* under the terms of the GPL and not to allow others to use your
* version of this file under the NPL, indicate your decision by
* deleting the provisions above and replace them with the notice
* and other provisions required by the GPL. If you do not delete
* the provisions above, a recipient may use your version of this
* file under either the NPL or the GPL.
*/
package org.mozilla.javascript.xmlimpl;
import org.mozilla.javascript.*;
class XMLCtor extends IdFunction
{
private static final Object XMLCTOR_TAG = new Object();
private XMLLibImpl lib;
XMLCtor(XML xml, Object tag, int id, int arity)
{
super(xml, tag, id, arity);
this.lib = xml.lib;
activatePrototypeMap(MAX_FUNCTION_ID);
}
private void writeSetting(Scriptable target)
{
for (int i = 1; i <= MAX_INSTANCE_ID; ++i) {
int id = idBase + i;
String name = getInstanceIdName(id);
Object value = getInstanceIdValue(id);
ScriptableObject.putProperty(target, name, value);
}
}
private void readSettings(Scriptable source)
{
for (int i = 1; i <= MAX_INSTANCE_ID; ++i) {
int id = idBase + i;
String name = getInstanceIdName(id);
Object value = ScriptableObject.getProperty(source, name);
if (value == ScriptableObject.NOT_FOUND) {
continue;
}
switch (i) {
case Id_ignoreComments:
case Id_ignoreProcessingInstructions:
case Id_ignoreWhitespace:
case Id_prettyPrinting:
if (!(value instanceof Boolean)) {
continue;
}
break;
case Id_prettyIndent:
if (!(value instanceof Number)) {
continue;
}
break;
default:
throw new IllegalStateException();
}
setInstanceIdValue(id, value);
}
}
// #string_id_map#
private static final int
Id_ignoreComments = 1,
Id_ignoreProcessingInstructions = 2,
Id_ignoreWhitespace = 3,
Id_prettyIndent = 4,
Id_prettyPrinting = 5,
MAX_INSTANCE_ID = 5;
// maxId for superclass
private static int idBase = -1;
{
if (idBase < 0) {
idBase = getMaxInstanceId();
}
setMaxInstanceId(idBase, idBase + MAX_INSTANCE_ID);
}
protected int findInstanceIdInfo(String s) {
int id;
// #generated# Last update: 2004-07-19 13:03:52 CEST
L0: { id = 0; String X = null; int c;
L: switch (s.length()) {
case 12: X="prettyIndent";id=Id_prettyIndent; break L;
case 14: c=s.charAt(0);
if (c=='i') { X="ignoreComments";id=Id_ignoreComments; }
else if (c=='p') { X="prettyPrinting";id=Id_prettyPrinting; }
break L;
case 16: X="ignoreWhitespace";id=Id_ignoreWhitespace; break L;
case 28: X="ignoreProcessingInstructions";id=Id_ignoreProcessingInstructions; break L;
}
if (X!=null && X!=s && !X.equals(s)) id = 0;
}
// #/generated#
if (id == 0) return super.findInstanceIdInfo(s);
int attr;
switch (id) {
case Id_ignoreComments:
case Id_ignoreProcessingInstructions:
case Id_ignoreWhitespace:
case Id_prettyIndent:
case Id_prettyPrinting:
attr = PERMANENT | DONTENUM;
break;
default: throw new IllegalStateException();
}
return instanceIdInfo(attr, idBase + id);
}
// #/string_id_map#
protected String getInstanceIdName(int id)
{
switch (id - idBase) {
case Id_ignoreComments: return "ignoreComments";
case Id_ignoreProcessingInstructions: return "ignoreProcessingInstructions";
case Id_ignoreWhitespace: return "ignoreWhitespace";
case Id_prettyIndent: return "prettyIndent";
case Id_prettyPrinting: return "prettyPrinting";
}
return super.getInstanceIdName(id);
}
protected Object getInstanceIdValue(int id)
{
switch (id - idBase) {
case Id_ignoreComments:
return ScriptRuntime.wrapBoolean(lib.ignoreComments);
case Id_ignoreProcessingInstructions:
return ScriptRuntime.wrapBoolean(lib.ignoreProcessingInstructions);
case Id_ignoreWhitespace:
return ScriptRuntime.wrapBoolean(lib.ignoreWhitespace);
case Id_prettyIndent:
return ScriptRuntime.wrapInt(lib.prettyIndent);
case Id_prettyPrinting:
return ScriptRuntime.wrapBoolean(lib.prettyPrinting);
}
return super.getInstanceIdValue(id);
}
protected void setInstanceIdValue(int id, Object value)
{
switch (id - idBase) {
case Id_ignoreComments:
lib.ignoreComments = ScriptRuntime.toBoolean(value);
return;
case Id_ignoreProcessingInstructions:
lib.ignoreProcessingInstructions = ScriptRuntime.toBoolean(value);
return;
case Id_ignoreWhitespace:
lib.ignoreWhitespace = ScriptRuntime.toBoolean(value);
return;
case Id_prettyIndent:
lib.prettyIndent = ScriptRuntime.toInt32(value);
return;
case Id_prettyPrinting:
lib.prettyPrinting = ScriptRuntime.toBoolean(value);
return;
}
super.setInstanceIdValue(id, value);
}
// #string_id_map#
private static final int
Id_defaultSettings = 1,
Id_settings = 2,
Id_setSettings = 3,
MAX_FUNCTION_ID = 3;
protected int findPrototypeId(String s)
{
int id;
// #generated# Last update: 2004-07-19 13:03:52 CEST
L0: { id = 0; String X = null;
int s_length = s.length();
if (s_length==8) { X="settings";id=Id_settings; }
else if (s_length==11) { X="setSettings";id=Id_setSettings; }
else if (s_length==15) { X="defaultSettings";id=Id_defaultSettings; }
if (X!=null && X!=s && !X.equals(s)) id = 0;
}
// #/generated#
return id;
}
// #/string_id_map#
protected void initPrototypeId(int id)
{
String s;
int arity;
switch (id) {
case Id_defaultSettings: arity=0; s="defaultSettings"; break;
case Id_settings: arity=0; s="settings"; break;
case Id_setSettings: arity=1; s="setSettings"; break;
default: throw new IllegalArgumentException(String.valueOf(id));
}
initPrototypeMethod(XMLCTOR_TAG, id, s, arity);
}
public Object execMethod(IdFunction f, Context cx, Scriptable scope,
Scriptable thisObj, Object[] args)
{
if (!f.hasTag(XMLCTOR_TAG)) {
return super.execMethod(f, cx, scope, thisObj, args);
}
int id = f.methodId();
switch (id) {
case Id_defaultSettings: {
lib.defaultSettings();
Scriptable obj = cx.newObject(scope);
writeSetting(obj);
return obj;
}
case Id_settings: {
Scriptable obj = cx.newObject(scope);
writeSetting(obj);
return obj;
}
case Id_setSettings: {
Scriptable obj = null;
if (args.length == 0
|| args[0] == null
|| args[0] == Undefined.instance)
{
lib.defaultSettings();
} else if (args[0] instanceof Scriptable) {
readSettings((Scriptable)args[0]);
}
return Undefined.instance;
}
}
throw new IllegalArgumentException(String.valueOf(id));
}
}

Просмотреть файл

@ -0,0 +1,770 @@
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Rhino code, released
* May 6, 1999.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1997-2000 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* Igor Bukanov
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU Public License (the "GPL"), in which case the
* provisions of the GPL are applicable instead of those above.
* If you wish to allow use of your version of this file only
* under the terms of the GPL and not to allow others to use your
* version of this file under the NPL, indicate your decision by
* deleting the provisions above and replace them with the notice
* and other provisions required by the GPL. If you do not delete
* the provisions above, a recipient may use your version of this
* file under either the NPL or the GPL.
*/
package org.mozilla.javascript.xmlimpl;
import org.mozilla.javascript.*;
import org.mozilla.javascript.xml.*;
import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlObject;
public final class XMLLibImpl extends XMLLib
{
private Scriptable globalScope;
XML xmlPrototype;
XMLList xmlListPrototype;
Namespace namespacePrototype;
QName qnamePrototype;
// Environment settings...
boolean ignoreComments;
boolean ignoreProcessingInstructions;
boolean ignoreWhitespace;
boolean prettyPrinting;
int prettyIndent;
Scriptable globalScope()
{
return globalScope;
}
private XMLLibImpl(Scriptable globalScope)
{
this.globalScope = globalScope;
defaultSettings();
}
public static void init(Context cx, Scriptable scope, boolean sealed)
{
XMLLibImpl lib = new XMLLibImpl(scope);
lib.exportToScope(sealed);
}
private void exportToScope(boolean sealed)
{
xmlPrototype = new XML(this);
xmlListPrototype = new XMLList(this);
namespacePrototype = new Namespace(this, "", "");
qnamePrototype = new QName(this, "", "", "");
xmlPrototype.exportAsJSClass(sealed);
xmlListPrototype.exportAsJSClass(sealed);
namespacePrototype.exportAsJSClass(sealed);
qnamePrototype.exportAsJSClass(sealed);
}
void defaultSettings()
{
ignoreComments = true;
ignoreProcessingInstructions = true;
ignoreWhitespace = true;
prettyPrinting = true;
prettyIndent = 2;
}
boolean ignoreComments()
{
return ignoreComments;
}
boolean ignoreProcessingInstructions()
{
return ignoreProcessingInstructions;
}
boolean ignoreWhitespace()
{
return ignoreWhitespace;
}
boolean prettyPrinting()
{
return prettyPrinting;
}
int prettyIndent()
{
return prettyIndent;
}
private XMLName resolveName(Context cx, Object id)
{
if(id instanceof XMLName) return (XMLName)id;
if (id instanceof QName) {
QName qname = (QName)id;
return XMLName.formProperty(qname.uri(), qname.localName());
}
String name = ScriptRuntime.toString(id);
boolean isAttributeName = false;
if (name.length() != 0 && name.charAt(0) == '@') {
name = name.substring(1);
isAttributeName = true;
}
String uri = null;
if(!name.equals("*")) {
if (isAttributeName) {
uri = "";
} else {
uri = "";
if (cx == null) {
cx = Context.getCurrentContext();
}
if (cx != null) {
Object defaultNS = ScriptRuntime.searchDefaultNamespace(cx);
if (defaultNS != null) {
if (defaultNS instanceof Namespace) {
uri = ((Namespace)defaultNS).uri();
} else {
// Should not happen but for now it could
// due to bad searchDefaultNamespace implementation.
}
}
}
}
}
XMLName xmlName = XMLName.formProperty(uri, name);
if (isAttributeName) {
xmlName.setAttributeName();
}
return xmlName;
}
private Namespace resolveNamespace(String prefix, Scriptable scope)
{
Namespace ns = null;
Scriptable nsScope = scope;
while (nsScope != null) {
Object obj = ScriptableObject.getProperty(nsScope, prefix);
if (obj instanceof Namespace) {
ns = (Namespace)obj;
break;
}
nsScope = nsScope.getParentScope();
}
if (ns == null) {
// Only namespace type allowed to left of :: operator.
throw Context.reportRuntimeError(
ScriptRuntime.getMessage1("msg.namespace.expected", prefix));
}
return ns;
}
XMLName toAttributeNameImpl(Context cx, Object nameValue)
{
String uri;
String localName;
if (nameValue instanceof String) {
uri = "";
localName = (String)nameValue;
} else if (nameValue instanceof XMLName) {
XMLName xmlName = (XMLName)nameValue;
if (!xmlName.isAttributeName()) {
xmlName.setAttributeName();
}
return xmlName;
} else if (nameValue instanceof QName) {
QName qname = (QName)nameValue;
uri = qname.uri();
localName = qname.localName();
} else if (nameValue instanceof Scriptable) {
uri = "";
localName = ScriptRuntime.toString(nameValue);
} else {
throw ScriptRuntime.typeError("Bad attribute name: "+nameValue);
}
XMLName xmlName = XMLName.formProperty(uri, localName);
xmlName.setAttributeName();
return xmlName;
}
XMLName toXMLName(Context cx, Object nameValue)
{
XMLName result;
if (nameValue instanceof XMLName) {
result = (XMLName)nameValue;
} else if (nameValue instanceof QName) {
QName qname = (QName)nameValue;
result = XMLName.formProperty(qname.uri(), qname.localName());
} else {
String name = ScriptRuntime.toString(nameValue);
result = toXMLNameFromString(cx, name);
}
return result;
}
/**
* If value represents Uint32 index, make it available through
* ScriptRuntime.lastUint32Result(cx) and return null.
* Otherwise return the same value as toXMLName(cx, value).
*/
XMLName toXMLNameOrIndex(Context cx, Object value)
{
XMLName result;
if (value instanceof XMLName) {
result = (XMLName)value;
} else if (value instanceof String) {
String str = (String)value;
long test = ScriptRuntime.testUint32String(str);
if (test >= 0) {
ScriptRuntime.storeUint32Result(cx, test);
result = null;
} else {
result = toXMLNameFromString(cx, str);
}
} else if (value instanceof Number) {
double d = ((Number)value).doubleValue();
long l = (long)d;
if (l == d && 0 <= l && l <= 0xFFFFFFFFL) {
ScriptRuntime.storeUint32Result(cx, l);
result = null;
} else {
String str = ScriptRuntime.toString(d);
result = toXMLNameFromString(cx, str);
}
} else if (value instanceof QName) {
QName qname = (QName)value;
String uri = qname.uri();
boolean number = false;
result = null;
if (uri != null && uri.length() == 0) {
// Only in this case qname.toString() can resemble uint32
long test = ScriptRuntime.testUint32String(uri);
if (test >= 0) {
ScriptRuntime.storeUint32Result(cx, test);
number = true;
}
}
if (!number) {
result = XMLName.formProperty(uri, qname.localName());
}
} else {
String str = ScriptRuntime.toString(value);
long test = ScriptRuntime.testUint32String(str);
if (test >= 0) {
ScriptRuntime.storeUint32Result(cx, test);
result = null;
} else {
result = toXMLNameFromString(cx, str);
}
}
return result;
}
XMLName toXMLNameFromString(Context cx, String name)
{
if (name == null)
throw new IllegalArgumentException();
int l = name.length();
if (l != 0) {
char firstChar = name.charAt(0);
if (firstChar == '*') {
if (l == 1) {
return XMLName.formStar();
}
} else if (firstChar == '@') {
XMLName xmlName = XMLName.formProperty("", name.substring(1));
xmlName.setAttributeName();
return xmlName;
}
}
String uri = getDefaultNamespaceURI(cx);
return XMLName.formProperty(uri, name);
}
Namespace constructNamespace(Context cx, Object uriValue)
{
String prefix;
String uri;
if (uriValue instanceof Namespace) {
Namespace ns = (Namespace)uriValue;
prefix = ns.prefix();
uri = ns.uri();
} else if (uriValue instanceof QName) {
QName qname = (QName)uriValue;
uri = qname.uri();
if (uri != null) {
prefix = qname.prefix();
} else {
uri = qname.toString();
prefix = null;
}
} else {
uri = ScriptRuntime.toString(uriValue);
prefix = (uri.length() == 0) ? "" : null;
}
return new Namespace(this, prefix, uri);
}
Namespace castToNamespace(Context cx, Object namescapeObj)
{
if (namescapeObj instanceof Namespace) {
return (Namespace)namescapeObj;
}
return constructNamespace(cx, namescapeObj);
}
Namespace constructNamespace(Context cx)
{
return new Namespace(this, "", "");
}
public Namespace constructNamespace(Context cx, Object prefixValue,
Object uriValue)
{
String prefix;
String uri;
if (uriValue instanceof QName) {
QName qname = (QName)uriValue;
uri = qname.uri();
if (uri == null) {
uri = qname.toString();
}
} else {
uri = ScriptRuntime.toString(uriValue);
}
if (uri.length() == 0) {
if (prefixValue == Undefined.instance) {
prefix = "";
} else {
prefix = ScriptRuntime.toString(prefixValue);
if (prefix.length() != 0) {
throw ScriptRuntime.constructError("TypeError",
"Illegal prefix '"+prefix+"' for 'no namespace'.");
}
}
} else if (prefixValue == Undefined.instance) {
prefix = "";
} else if (!isXMLName(cx, prefixValue)) {
prefix = "";
} else {
prefix = ScriptRuntime.toString(prefixValue);
}
return new Namespace(this, prefix, uri);
}
String getDefaultNamespaceURI(Context cx)
{
String uri = "";
if (cx == null) {
cx = Context.getCurrentContext();
}
if (cx != null) {
Object ns = ScriptRuntime.searchDefaultNamespace(cx);
if (ns != null) {
if (ns instanceof Namespace) {
uri = ((Namespace)ns).uri();
} else {
// Should not happen but for now it could
// due to bad searchDefaultNamespace implementation.
}
}
}
return uri;
}
Namespace getDefaultNamespace(Context cx)
{
if (cx == null) {
cx = Context.getCurrentContext();
if (cx == null) {
return namespacePrototype;
}
}
Namespace result;
Object ns = ScriptRuntime.searchDefaultNamespace(cx);
if (ns == null) {
result = namespacePrototype;
} else {
if (ns instanceof Namespace) {
result = (Namespace)ns;
} else {
// Should not happen but for now it could
// due to bad searchDefaultNamespace implementation.
result = namespacePrototype;
}
}
return result;
}
QName castToQName(Context cx, Object qnameValue)
{
if (qnameValue instanceof QName) {
return (QName)qnameValue;
}
return constructQName(cx, qnameValue);
}
QName constructQName(Context cx, Object nameValue)
{
QName result;
if (nameValue instanceof QName) {
QName qname = (QName)nameValue;
result = new QName(this, qname.uri(), qname.localName(),
qname.prefix());
} else {
String localName = ScriptRuntime.toString(nameValue);
result = constructQNameFromString(cx, localName);
}
return result;
}
/**
* Optimized version of constructQName for String type
*/
QName constructQNameFromString(Context cx, String localName)
{
if (localName == null)
throw new IllegalArgumentException();
String uri;
String prefix;
if ("*".equals(localName)) {
uri = null;
prefix = null;
} else {
Namespace ns = getDefaultNamespace(cx);
uri = ns.uri();
prefix = ns.prefix();
}
return new QName(this, uri, localName, prefix);
}
QName constructQName(Context cx, Object namespaceValue, Object nameValue)
{
String uri;
String localName;
String prefix;
if (nameValue instanceof QName) {
QName qname = (QName)nameValue;
localName = qname.localName();
} else {
localName = ScriptRuntime.toString(nameValue);
}
Namespace ns;
if (namespaceValue == Undefined.instance) {
if ("*".equals(localName)) {
ns = null;
} else {
ns = getDefaultNamespace(cx);
}
} else if (namespaceValue == null) {
ns = null;
} else if (namespaceValue instanceof Namespace) {
ns = (Namespace)namespaceValue;
} else {
ns = constructNamespace(cx, namespaceValue);
}
if (ns == null) {
uri = null;
prefix = null;
} else {
uri = ns.uri();
prefix = ns.prefix();
}
return new QName(this, uri, localName, prefix);
}
//
//
// Overriding XMLLib methods
//
//
/**
* TODO: Implement this method!
*/
public boolean isXMLName(Context cx, Object name)
{
// TODO: Check if qname.localName() matches NCName
return true;
}
public Object toQualifiedName(String namespace,
Object nameValue,
Scriptable scope)
{
String uri;
String localName;
if (nameValue instanceof QName) {
QName qname = (QName)nameValue;
localName = qname.localName();
} else {
localName = ScriptRuntime.toString(nameValue);
}
if ("*".equals(namespace)) {
uri = null;
} else {
uri = resolveNamespace(namespace, scope).uri();
}
return XMLName.formProperty(uri, localName);
}
public Object toAttributeName(Context cx, Object nameValue)
{
return toAttributeNameImpl(cx, nameValue);
}
public Object toDescendantsName(Context cx, Object name)
{
XMLName xmlName = resolveName(cx, name);
xmlName.setDescendants();
return xmlName;
}
/**
* See E4X 11.1 PrimaryExpression : PropertyIdentifier production
*/
public Reference xmlPrimaryReference(Object nameObject, Scriptable scope)
{
if (!(nameObject instanceof XMLName))
throw new IllegalArgumentException();
XMLName xmlName = (XMLName)nameObject;
XMLObjectImpl xmlObj;
for (;;) {
// XML object can only present on scope chain as a wrapper
// of XMLWithScope
if (scope instanceof XMLWithScope) {
xmlObj = (XMLObjectImpl)scope.getPrototype();
if (xmlObj.hasXMLProperty(xmlName)) {
break;
}
}
scope = scope.getParentScope();
if (scope == null) {
xmlObj = null;
break;
}
}
// xmlName == null corresponds to undefined
return new XMLReference(xmlObj, xmlName);
}
public Scriptable enterXMLWith(XMLObject object, Scriptable scope)
{
return new XMLWithScope(this, scope, object);
}
public Scriptable enterDotQuery(XMLObject object, Scriptable scope)
{
XMLWithScope xws = new XMLWithScope(this, scope, object);
// XMLWithScope also handles the .(xxx) DotQuery for XML
// basically DotQuery is a for/in/with statement and in
// the following 3 statements we setup to signal it's
// DotQuery,
// the index and the object being looped over. The
// xws.setPrototype is the scope of the object which is
// is a element of the lhs (XMLList).
xws.setCurrIndex(0);
xws.setDQPrototype(object);
if (object instanceof XMLList) {
XMLList xl = (XMLList)object;
if (xl.length() > 0) {
xws.setPrototype((Scriptable)(xl.get(0, null)));
}
}
// Always return the outer-most type of XML lValue of
// XML to left of dotQuery.
xws.setXMLList(new XMLList(this));
return xws;
}
/**
* Escapes the reserved characters in a value of an attribute
*
* @param value Unescaped text
* @return The escaped text
*/
public String escapeAttributeValue(Object value)
{
String text = ScriptRuntime.toString(value);
if (text.length() == 0) return text;
XmlObject xo = XmlObject.Factory.newInstance();
XmlCursor cursor = xo.newCursor();
cursor.toNextToken();
cursor.beginElement("a");
cursor.insertAttributeWithValue("a", text);
cursor.dispose();
String elementText = xo.toString();
int begin = elementText.indexOf('"') + 1;
int end = elementText.lastIndexOf('"');
return (begin < end) ? elementText.substring(begin, end) : "";
}
/**
* Escapes the reserved characters in a value of a text node
*
* @param value Unescaped text
* @return The escaped text
*/
public String escapeTextValue(Object value)
{
if (value instanceof XMLObjectImpl) {
return ((XMLObjectImpl)value).toXMLString();
}
String text = ScriptRuntime.toString(value);
if (text.length() == 0) return text;
XmlObject xo = XmlObject.Factory.newInstance();
XmlCursor cursor = xo.newCursor();
cursor.toNextToken();
cursor.beginElement("a");
cursor.insertChars(text);
cursor.dispose();
String elementText = xo.toString();
int begin = elementText.indexOf('>') + 1;
int end = elementText.lastIndexOf('<');
return (begin < end) ? elementText.substring(begin, end) : "";
}
public Object addXMLObjects(Context cx, XMLObject obj1, XMLObject obj2)
{
XMLList listToAdd = new XMLList(this);
if (obj1 instanceof XMLList) {
XMLList list1 = (XMLList)obj1;
if (list1.length() == 1) {
listToAdd.addToList(list1.item(0));
} else {
// Might be xmlFragment + xmlFragment + xmlFragment + ...;
// then the result will be an XMLList which we want to be an
// rValue and allow it to be assigned to an lvalue.
listToAdd = new XMLList(this, obj1);
}
} else {
listToAdd.addToList(((XML)obj1));
}
if (obj2 instanceof XMLList) {
XMLList list2 = (XMLList)obj2;
for (int i = 0; i < list2.length(); i++) {
listToAdd.addToList(list2.item(i));
}
} else if (obj2 instanceof XML) {
listToAdd.addToList(((XML)obj2));
}
return listToAdd;
}
public Object toDefaultXmlNamespace(Context cx, Object uriValue)
{
return constructNamespace(cx, uriValue);
}
/**
* This method exists in order to handle the difference between a XML element property
* and a method on an XML Object that might have the same name.
*
* @param obj
* @param id
* @param scope
* @param thisObj
* @return
*/
static Object getXmlMethod(Object obj, String id, Scriptable scope,
Scriptable thisObj)
{
Scriptable start;
if (obj instanceof Scriptable) {
start = (Scriptable) obj;
} else {
start = ScriptRuntime.toObject(scope, obj);
}
Scriptable m = start;
do {
if (m instanceof XMLObjectImpl) {
XMLObjectImpl xmlObject = (XMLObjectImpl) m;
Object result = xmlObject.getMethod(id);
if (result != Scriptable.NOT_FOUND) {
return result;
}
}
m = m.getPrototype();
} while (m != null);
return Undefined.instance;
}
}

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -0,0 +1,116 @@
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Rhino code, released
* May 6, 1999.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1997-2000 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* Igor Bukanov
* Milen Nankov
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU Public License (the "GPL"), in which case the
* provisions of the GPL are applicable instead of those above.
* If you wish to allow use of your version of this file only
* under the terms of the GPL and not to allow others to use your
* version of this file under the NPL, indicate your decision by
* deleting the provisions above and replace them with the notice
* and other provisions required by the GPL. If you do not delete
* the provisions above, a recipient may use your version of this
* file under either the NPL or the GPL.
*/
package org.mozilla.javascript.xmlimpl;
class XMLName
{
private String uri;
private String localName;
private boolean isAttributeName;
private boolean isDescendants;
private XMLName(String uri, String localName)
{
this.uri = uri;
this.localName = localName;
}
String uri()
{
return uri;
}
String localName()
{
return localName;
}
boolean isAttributeName()
{
return isAttributeName;
}
void setAttributeName()
{
if (isAttributeName)
throw new IllegalStateException();
isAttributeName = true;
}
boolean isDescendants()
{
return isDescendants;
}
void setDescendants()
{
if (isDescendants)
throw new IllegalStateException();
isDescendants = true;
}
public String toString()
{
//return qname.localName();
StringBuffer buff = new StringBuffer();
if(isDescendants()) buff.append("..");
if(isAttributeName()) buff.append('@');
if(uri() == null)
{
buff.append('*');
if(localName().equals("*"))
return buff.toString();
}
else
{
buff.append('"').append(uri()).append('"');
}
buff.append(':').append(localName());
return buff.toString();
}
static XMLName formStar()
{
return new XMLName(null, "*");
}
static XMLName formProperty(String uri, String localName)
{
return new XMLName(uri, localName);
}
}

Просмотреть файл

@ -0,0 +1,649 @@
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Rhino code, released
* May 6, 1999.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1997-2000 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* Igor Bukanov
* Ethan Hugg
* Terry Lucas
* Milen Nankov
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU Public License (the "GPL"), in which case the
* provisions of the GPL are applicable instead of those above.
* If you wish to allow use of your version of this file only
* under the terms of the GPL and not to allow others to use your
* version of this file under the NPL, indicate your decision by
* deleting the provisions above and replace them with the notice
* and other provisions required by the GPL. If you do not delete
* the provisions above, a recipient may use your version of this
* file under either the NPL or the GPL.
*/
package org.mozilla.javascript.xmlimpl;
import org.mozilla.javascript.*;
import org.mozilla.javascript.xml.*;
/**
* This abstract class describes what all XML objects (XML, XMLList) should have in common.
*
* @see XML
*/
public abstract class XMLObjectImpl extends XMLObject
{
private static final Object XMLOBJECT_TAG = new Object();
protected final XMLLibImpl lib;
protected boolean prototypeFlag;
protected XMLObjectImpl(XMLLibImpl lib)
{
this.lib = lib;
}
/**
* ecmaHas(cx, id) calls this after resolving when id to XMLName
* and checking it is not Uint32 index.
*/
public abstract boolean hasXMLProperty(XMLName name);
/**
* ecmaGet(cx, id) calls this after resolving when id to XMLName
* and checking it is not Uint32 index.
*/
public abstract Object getXMLProperty(XMLName name);
/**
* ecmaPut(cx, id, value) calls this after resolving when id to XMLName
* and checking it is not Uint32 index.
*/
public abstract void putXMLProperty(XMLName name, Object value);
/**
* ecmaDelete(cx, id) calls this after resolving when id to XMLName
* and checking it is not Uint32 index.
*/
public abstract void deleteXMLProperty(XMLName name);
/**
* Test XML equality with target the target.
*/
public abstract boolean equivalentXml(Object target);
// Methods from section 12.4.4 in the spec
public abstract XML addNamespace(Namespace ns);
public abstract XML appendChild(Object xml);
public abstract XMLList attribute(XMLName xmlName);
public abstract XMLList attributes();
public abstract XMLList child(long index);
public abstract XMLList child(XMLName xmlName);
public abstract int childIndex();
public abstract XMLList children();
public abstract XMLList comments();
public abstract boolean contains(Object xml);
public abstract Object copy();
public abstract XMLList descendants(XMLName xmlName);
public abstract Object[] inScopeNamespaces();
public abstract XML insertChildAfter(Object child, Object xml);
public abstract XML insertChildBefore(Object child, Object xml);
public abstract boolean hasOwnProperty(XMLName xmlName);
public abstract boolean hasComplexContent();
public abstract boolean hasSimpleContent();
public abstract int length();
public abstract String localName();
public abstract QName name();
public abstract Object namespace(String prefix);
public abstract Object[] namespaceDeclarations();
public abstract Object nodeKind();
public abstract void normalize();
public abstract Object parent();
public abstract XML prependChild(Object xml);
public abstract Object processingInstructions(XMLName xmlName);
public abstract boolean propertyIsEnumerable(XMLName xmlName);
public abstract XML removeNamespace(Namespace ns);
public abstract XML replace(long index, Object xml);
public abstract XML replace(XMLName name, Object xml);
public abstract XML setChildren(Object xml);
public abstract void setLocalName(String name);
public abstract void setName(QName xmlName);
public abstract void setNamespace(Namespace ns);
public abstract XMLList text();
public abstract String toString();
public abstract String toXMLString();
public abstract Object valueOf();
protected abstract Object jsConstructor(Context cx, boolean inNewExpr,
Object[] args);
public final Object getMethod(String id)
{
return super.get(id, this);
}
//
//
// Methods overriding ScriptableObject
//
//
public final Object getDefaultValue(Class hint)
{
return toString();
}
protected final Scriptable defaultParentScope()
{
return lib.globalScope();
}
public void delete(String name)
{
throw new IllegalArgumentException("String: [" + name + "]");
}
/**
* XMLObject always compare with any value and equivalentValues
* never returns null for them but rather calls equivalentXml(value)
* and box the result as Boolean.
*/
public final Boolean equivalentValues(Object value)
{
boolean result = equivalentXml(value);
return result ? Boolean.TRUE : Boolean.FALSE;
}
//
//
// Methods overriding XMLObject
//
//
public final XMLLib lib()
{
return lib;
}
/**
* Implementation of ECMAScript [[Has]]
*/
public final boolean ecmaHas(Context cx, Object id)
{
if (cx == null) cx = Context.getCurrentContext();
XMLName xmlName = lib.toXMLNameOrIndex(cx, id);
if (xmlName == null) {
long index = ScriptRuntime.lastUint32Result(cx);
// XXX Fix this cast
return has((int)index, this);
}
return hasXMLProperty(xmlName);
}
/**
* Implementation of ECMAScript [[Get]]
*/
public final Object ecmaGet(Context cx, Object id)
{
if (cx == null) cx = Context.getCurrentContext();
XMLName xmlName = lib.toXMLNameOrIndex(cx, id);
if (xmlName == null) {
long index = ScriptRuntime.lastUint32Result(cx);
// XXX Fix this cast
Object result = get((int)index, this);
if (result == Scriptable.NOT_FOUND) {
result = Undefined.instance;
}
return result;
}
return getXMLProperty(xmlName);
}
/**
* Implementation of ECMAScript [[Put]]
*/
public final void ecmaPut(Context cx, Object id, Object value)
{
if (cx == null) cx = Context.getCurrentContext();
XMLName xmlName = lib.toXMLNameOrIndex(cx, id);
if (xmlName == null) {
long index = ScriptRuntime.lastUint32Result(cx);
// XXX Fix this cast
put((int)index, this, value);
return;
}
putXMLProperty(xmlName, value);
}
/**
* Implementation of ECMAScript [[Delete]]
*/
public final boolean ecmaDelete(Context cx, Object id)
{
if (cx == null) cx = Context.getCurrentContext();
XMLName xmlName = lib.toXMLNameOrIndex(cx, id);
if (xmlName == null) {
long index = ScriptRuntime.lastUint32Result(cx);
// XXX Fix this
delete((int)index);
return true;
}
deleteXMLProperty(xmlName);
return true;
}
//
//
// IdScriptable machinery
//
//
final void exportAsJSClass(boolean sealed)
{
prototypeFlag = true;
exportAsJSClass(MAX_PROTOTYPE_ID, lib.globalScope(), sealed);
}
// #string_id_map#
private final static int
Id_constructor = 1,
Id_addNamespace = 2,
Id_appendChild = 3,
Id_attribute = 4,
Id_attributes = 5,
Id_child = 6,
Id_childIndex = 7,
Id_children = 8,
Id_comments = 9,
Id_contains = 10,
Id_copy = 11,
Id_descendants = 12,
Id_inScopeNamespaces = 13,
Id_insertChildAfter = 14,
Id_insertChildBefore = 15,
Id_hasOwnProperty = 16,
Id_hasComplexContent = 17,
Id_hasSimpleContent = 18,
Id_length = 19,
Id_localName = 20,
Id_name = 21,
Id_namespace = 22,
Id_namespaceDeclarations = 23,
Id_nodeKind = 24,
Id_normalize = 25,
Id_parent = 26,
Id_prependChild = 27,
Id_processingInstructions = 28,
Id_propertyIsEnumerable = 29,
Id_removeNamespace = 30,
Id_replace = 31,
Id_setChildren = 32,
Id_setLocalName = 33,
Id_setName = 34,
Id_setNamespace = 35,
Id_text = 36,
Id_toString = 37,
Id_toXMLString = 38,
Id_valueOf = 39,
MAX_PROTOTYPE_ID = 39;
protected int findPrototypeId(String s)
{
int id;
// #generated# Last update: 2004-07-19 13:13:35 CEST
L0: { id = 0; String X = null; int c;
L: switch (s.length()) {
case 4: c=s.charAt(0);
if (c=='c') { X="copy";id=Id_copy; }
else if (c=='n') { X="name";id=Id_name; }
else if (c=='t') { X="text";id=Id_text; }
break L;
case 5: X="child";id=Id_child; break L;
case 6: c=s.charAt(0);
if (c=='l') { X="length";id=Id_length; }
else if (c=='p') { X="parent";id=Id_parent; }
break L;
case 7: c=s.charAt(0);
if (c=='r') { X="replace";id=Id_replace; }
else if (c=='s') { X="setName";id=Id_setName; }
else if (c=='v') { X="valueOf";id=Id_valueOf; }
break L;
case 8: switch (s.charAt(2)) {
case 'S': X="toString";id=Id_toString; break L;
case 'd': X="nodeKind";id=Id_nodeKind; break L;
case 'i': X="children";id=Id_children; break L;
case 'm': X="comments";id=Id_comments; break L;
case 'n': X="contains";id=Id_contains; break L;
} break L;
case 9: switch (s.charAt(2)) {
case 'c': X="localName";id=Id_localName; break L;
case 'm': X="namespace";id=Id_namespace; break L;
case 'r': X="normalize";id=Id_normalize; break L;
case 't': X="attribute";id=Id_attribute; break L;
} break L;
case 10: c=s.charAt(0);
if (c=='a') { X="attributes";id=Id_attributes; }
else if (c=='c') { X="childIndex";id=Id_childIndex; }
break L;
case 11: switch (s.charAt(0)) {
case 'a': X="appendChild";id=Id_appendChild; break L;
case 'c': X="constructor";id=Id_constructor; break L;
case 'd': X="descendants";id=Id_descendants; break L;
case 's': X="setChildren";id=Id_setChildren; break L;
case 't': X="toXMLString";id=Id_toXMLString; break L;
} break L;
case 12: c=s.charAt(0);
if (c=='a') { X="addNamespace";id=Id_addNamespace; }
else if (c=='p') { X="prependChild";id=Id_prependChild; }
else if (c=='s') {
c=s.charAt(3);
if (c=='L') { X="setLocalName";id=Id_setLocalName; }
else if (c=='N') { X="setNamespace";id=Id_setNamespace; }
}
break L;
case 14: X="hasOwnProperty";id=Id_hasOwnProperty; break L;
case 15: X="removeNamespace";id=Id_removeNamespace; break L;
case 16: c=s.charAt(0);
if (c=='h') { X="hasSimpleContent";id=Id_hasSimpleContent; }
else if (c=='i') { X="insertChildAfter";id=Id_insertChildAfter; }
break L;
case 17: c=s.charAt(3);
if (c=='C') { X="hasComplexContent";id=Id_hasComplexContent; }
else if (c=='c') { X="inScopeNamespaces";id=Id_inScopeNamespaces; }
else if (c=='e') { X="insertChildBefore";id=Id_insertChildBefore; }
break L;
case 20: X="propertyIsEnumerable";id=Id_propertyIsEnumerable; break L;
case 21: X="namespaceDeclarations";id=Id_namespaceDeclarations; break L;
case 22: X="processingInstructions";id=Id_processingInstructions; break L;
}
if (X!=null && X!=s && !X.equals(s)) id = 0;
}
// #/generated#
return id;
}
// #/string_id_map#
protected void initPrototypeId(int id)
{
String s;
int arity;
switch (id) {
case Id_constructor: {
IdFunction ctor;
if (this instanceof XML) {
ctor = new XMLCtor((XML)this, XMLOBJECT_TAG, id, 1);
} else {
ctor = new IdFunction(this, XMLOBJECT_TAG, id, 1);
}
initPrototypeConstructor(ctor);
return;
}
case Id_addNamespace: arity=1; s="addNamespace"; break;
case Id_appendChild: arity=1; s="appendChild"; break;
case Id_attribute: arity=1; s="attribute"; break;
case Id_attributes: arity=0; s="attributes"; break;
case Id_child: arity=1; s="child"; break;
case Id_childIndex: arity=0; s="childIndex"; break;
case Id_children: arity=0; s="children"; break;
case Id_comments: arity=0; s="comments"; break;
case Id_contains: arity=1; s="contains"; break;
case Id_copy: arity=0; s="copy"; break;
case Id_descendants: arity=1; s="descendants"; break;
case Id_hasComplexContent: arity=0; s="hasComplexContent"; break;
case Id_hasOwnProperty: arity=1; s="hasOwnProperty"; break;
case Id_hasSimpleContent: arity=0; s="hasSimpleContent"; break;
case Id_inScopeNamespaces: arity=0; s="inScopeNamespaces"; break;
case Id_insertChildAfter: arity=2; s="insertChildAfter"; break;
case Id_insertChildBefore: arity=2; s="insertChildBefore"; break;
case Id_length: arity=0; s="length"; break;
case Id_localName: arity=0; s="localName"; break;
case Id_name: arity=0; s="name"; break;
case Id_namespace: arity=1; s="namespace"; break;
case Id_namespaceDeclarations:
arity=0; s="namespaceDeclarations"; break;
case Id_nodeKind: arity=0; s="nodeKind"; break;
case Id_normalize: arity=0; s="normalize"; break;
case Id_parent: arity=0; s="parent"; break;
case Id_prependChild: arity=1; s="prependChild"; break;
case Id_processingInstructions:
arity=1; s="processingInstructions"; break;
case Id_propertyIsEnumerable:
arity=1; s="propertyIsEnumerable"; break;
case Id_removeNamespace: arity=1; s="removeNamespace"; break;
case Id_replace: arity=2; s="replace"; break;
case Id_setChildren: arity=1; s="setChildren"; break;
case Id_setLocalName: arity=1; s="setLocalName"; break;
case Id_setName: arity=1; s="setName"; break;
case Id_setNamespace: arity=1; s="setNamespace"; break;
case Id_text: arity=0; s="text"; break;
case Id_toString: arity=0; s="toString"; break;
case Id_toXMLString: arity=0; s="toXMLString"; break;
case Id_valueOf: arity=0; s="valueOf"; break;
default: throw new IllegalArgumentException(String.valueOf(id));
}
initPrototypeMethod(XMLOBJECT_TAG, id, s, arity);
}
/**
*
* @param f
* @param cx
* @param scope
* @param thisObj
* @param args
* @return
* @throws JavaScriptException
*/
public Object execMethod(IdFunction f, Context cx, Scriptable scope,
Scriptable thisObj, Object[] args)
throws JavaScriptException
{
if (!f.hasTag(XMLOBJECT_TAG)) {
return super.execMethod(f, cx, scope, thisObj, args);
}
int id = f.methodId();
switch (id) {
case Id_constructor: {
return jsConstructor(cx, thisObj == null, args);
}
case Id_addNamespace: {
Namespace ns = lib.castToNamespace(cx, arg(args, 0));
return realThis(thisObj, f).addNamespace(ns);
}
case Id_appendChild:
return realThis(thisObj, f).appendChild(arg(args, 0));
case Id_attribute: {
Object arg0 = arg(args, 0);
if (arg0 == null || arg0 == Undefined.instance) {
// XXX: E4X requires to throw the exception in this case
// (whoch toAttributeName will) but the test suite assumes
// it should be OK. Trust test suite for now.
arg0 = ScriptRuntime.toString(arg0);
}
XMLName xmlName = lib.toAttributeNameImpl(cx, arg0);
return realThis(thisObj, f).attribute(xmlName);
}
case Id_attributes:
return realThis(thisObj, f).attributes();
case Id_child: {
XMLName xmlName = getXmlNameOrIndex(cx, args, 0);
if (xmlName == null) {
long index = ScriptRuntime.lastUint32Result(cx);
return realThis(thisObj, f).child(index);
} else {
return realThis(thisObj, f).child(xmlName);
}
}
case Id_childIndex:
return new Integer(realThis(thisObj, f).childIndex());
case Id_children:
return realThis(thisObj, f).children();
case Id_comments:
return realThis(thisObj, f).comments();
case Id_contains:
return wrap_boolean(realThis(thisObj, f).contains(arg(args, 0)));
case Id_copy:
return realThis(thisObj, f).copy();
case Id_descendants: {
XMLName xmlName = (args.length == 0)
? XMLName.formStar()
: lib.toXMLName(cx, args[0]);
return realThis(thisObj, f).descendants(xmlName);
}
case Id_inScopeNamespaces: {
Object[] array = realThis(thisObj, f).inScopeNamespaces();
return cx.newArray(scope, array);
}
case Id_insertChildAfter:
return realThis(thisObj, f).insertChildAfter(arg(args, 0), arg(args, 1));
case Id_insertChildBefore:
return realThis(thisObj, f).insertChildBefore(arg(args, 0), arg(args, 1));
case Id_hasOwnProperty: {
XMLName xmlName = lib.toXMLName(cx, arg(args, 0));
return wrap_boolean(
realThis(thisObj, f).hasOwnProperty(xmlName));
}
case Id_hasComplexContent:
return wrap_boolean(realThis(thisObj, f).hasComplexContent());
case Id_hasSimpleContent:
return wrap_boolean(realThis(thisObj, f).hasSimpleContent());
case Id_length:
return new Integer(realThis(thisObj, f).length());
case Id_localName:
return realThis(thisObj, f).localName();
case Id_name:
return realThis(thisObj, f).name();
case Id_namespace: {
String prefix = (args.length > 0)
? ScriptRuntime.toString(args[0]) : null;
return realThis(thisObj, f).namespace(prefix);
}
case Id_namespaceDeclarations: {
Object[] array = realThis(thisObj, f).namespaceDeclarations();
return cx.newArray(scope, array);
}
case Id_nodeKind:
return realThis(thisObj, f).nodeKind();
case Id_normalize:
realThis(thisObj, f).normalize();
return Undefined.instance;
case Id_parent:
return realThis(thisObj, f).parent();
case Id_prependChild:
return realThis(thisObj, f).prependChild(arg(args, 0));
case Id_processingInstructions: {
XMLName xmlName = (args.length > 0)
? lib.toXMLName(cx, args[0])
: XMLName.formStar();
return realThis(thisObj, f).processingInstructions(xmlName);
}
case Id_propertyIsEnumerable: {
XMLName xmlName = lib.toXMLName(cx, arg(args, 0));
return wrap_boolean(
realThis(thisObj, f).propertyIsEnumerable(xmlName));
}
case Id_removeNamespace: {
Namespace ns = lib.castToNamespace(cx, arg(args, 0));
return realThis(thisObj, f).removeNamespace(ns);
}
case Id_replace: {
XMLName xmlName = getXmlNameOrIndex(cx, args, 0);
Object arg1 = arg(args, 1);
if (xmlName == null) {
long index = ScriptRuntime.lastUint32Result(cx);
return realThis(thisObj, f).replace(index, arg1);
} else {
return realThis(thisObj, f).replace(xmlName, arg1);
}
}
case Id_setChildren:
return realThis(thisObj, f).setChildren(arg(args, 0));
case Id_setLocalName: {
String localName;
Object arg = arg(args, 0);
if (arg instanceof QName) {
localName = ((QName)arg).localName();
} else {
localName = ScriptRuntime.toString(arg);
}
realThis(thisObj, f).setLocalName(localName);
return Undefined.instance;
}
case Id_setName: {
Object arg = (args.length != 0) ? args[0] : Undefined.instance;
QName qname;
if (arg instanceof QName) {
qname = (QName)arg;
if (qname.uri() == null) {
qname = lib.constructQNameFromString(cx, qname.localName());
} else {
// E4X 13.4.4.35 requires to always construct QName
qname = lib.constructQName(cx, qname);
}
} else {
qname = lib.constructQName(cx, arg);
}
realThis(thisObj, f).setName(qname);
return Undefined.instance;
}
case Id_setNamespace: {
Namespace ns = lib.castToNamespace(cx, arg(args, 0));
realThis(thisObj, f).setNamespace(ns);
return Undefined.instance;
}
case Id_text:
return realThis(thisObj, f).text();
case Id_toString:
return realThis(thisObj, f).toString();
case Id_toXMLString:
return realThis(thisObj, f).toXMLString();
case Id_valueOf:
return realThis(thisObj, f).valueOf();
}
throw new IllegalArgumentException(String.valueOf(id));
}
/**
*
* @param thisObj
* @param f
* @return
*/
private XMLObjectImpl realThis(Scriptable thisObj, IdFunction f)
{
if(!(thisObj instanceof XMLObjectImpl))
throw incompatibleCallError(f);
return (XMLObjectImpl)thisObj;
}
private static Object arg(Object[] args, int i)
{
return (i < args.length) ? args[i] : Undefined.instance;
}
private XMLName getXmlNameOrIndex(Context cx, Object[] args, int i)
{
return lib.toXMLNameOrIndex(cx, arg(args, i));
}
}

Просмотреть файл

@ -0,0 +1,99 @@
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is XMLReference class for E4X extension of Rhino.
*
* The Initial Developer of the Original Code is
* RUnit Software AS.
* Portions created by the Initial Developer are Copyright (C) 2004
* the Initial Developer. All Rights Reserved.
*
* Contributor(s): Igor Bukanov, igor@fastmail.fm
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
package org.mozilla.javascript.xmlimpl;
import org.mozilla.javascript.*;
import org.mozilla.javascript.xml.*;
class XMLReference extends Reference
{
private XMLObjectImpl xmlObject;
private XMLName xmlName;
/**
* When xmlObject == null, it corresponds to undefined in JS, not null
*/
XMLReference(XMLObjectImpl xmlObject, XMLName xmlName)
{
if (xmlName == null)
throw new IllegalArgumentException();
this.xmlObject = xmlObject;
this.xmlName = xmlName;
}
public boolean has()
{
if (xmlObject == null) {
return false;
}
return xmlObject.hasXMLProperty(xmlName);
}
/**
* See E4X 11.1 PrimaryExpression : PropertyIdentifier production
*/
public Object get()
{
if (xmlObject == null) {
throw ScriptRuntime.undefReadError(Undefined.instance,
xmlName.toString());
}
return xmlObject.getXMLProperty(xmlName);
}
public Object set(Object value)
{
if (xmlObject == null) {
throw ScriptRuntime.undefWriteError(Undefined.instance,
xmlName.toString(),
value);
}
xmlObject.putXMLProperty(xmlName, value);
return value;
}
public void delete()
{
if (xmlObject == null) {
return;
}
xmlObject.deleteXMLProperty(xmlName);
}
}

Просмотреть файл

@ -0,0 +1,126 @@
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Rhino code, released
* May 6, 1999.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1997-1999 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* Norris Boyd
* Ethan Hugg
* Terry Lucas
* Milen Nankov
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU Public License (the "GPL"), in which case the
* provisions of the GPL are applicable instead of those above.
* If you wish to allow use of your version of this file only
* under the terms of the GPL and not to allow others to use your
* version of this file under the NPL, indicate your decision by
* deleting the provisions above and replace them with the notice
* and other provisions required by the GPL. If you do not delete
* the provisions above, a recipient may use your version of this
* file under either the NPL or the GPL.
*/
package org.mozilla.javascript.xmlimpl;
import org.mozilla.javascript.*;
import org.mozilla.javascript.xml.*;
final class XMLWithScope extends NativeWith
{
private XMLLib lib;
private int _currIndex;
private XMLList _xmlList = null;
private Scriptable _dqPrototype;
XMLWithScope(XMLLib lib, Scriptable parent, XMLObject prototype)
{
super(parent, prototype);
this.lib = lib;
}
public XMLLib lib()
{
return lib;
}
public void setCurrIndex (int idx)
{
_currIndex = idx;
}
public int getCurrIndex ()
{
return _currIndex;
}
public void setXMLList (XMLList l)
{
_xmlList = l;
}
public Scriptable getDQPrototype() {
return _dqPrototype;
}
public void setDQPrototype(Scriptable dqPrototype) {
_dqPrototype = dqPrototype;
}
public Object updateDotQuery(boolean value)
{
// Return null to continue looping
Scriptable seed = getDQPrototype();
XMLList xmlL = _xmlList;
Object result;
if (seed instanceof XMLList) {
// We're a list so keep testing each element of the list if the
// result on the top of stack is true then that element is added
// to our result list. If false, we try the next element.
XMLList orgXmlL = (XMLList)seed;
int idx = getCurrIndex();
if (value) {
xmlL.addToList(orgXmlL.get(idx, null));
}
// More elements to test?
if (++idx < orgXmlL.length()) {
// Yes, set our new index, get the next element and
// reset the expression to run with this object as
// the WITH selector.
setCurrIndex(idx);
setPrototype((Scriptable)(orgXmlL.get(idx, null)));
// continue looping
return null;
}
} else {
// If we're not a XMLList then there's no looping
// just return DQPrototype if the result is true.
if (value) {
xmlL.addToList(seed);
}
}
return xmlL;
}
}