📄 xmllibimpl.java
字号:
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0 * * 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 Rhino code, released * May 6, 1999. * * The Initial Developer of the Original Code is * Netscape Communications Corporation. * Portions created by the Initial Developer are Copyright (C) 1997-2000 * the Initial Developer. All Rights Reserved. * * Contributor(s): * Igor Bukanov * David P. Caldwell <inonit@inonit.com> * * Alternatively, the contents of this file may be used under the terms of * the GNU General Public License Version 2 or later (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 * MPL, indicate your decision by deleting the provisions above and replacing * 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 MPL or the GPL. * * ***** END LICENSE BLOCK ***** */package org.mozilla.javascript.xmlimpl;import java.io.Serializable;import org.mozilla.javascript.*;import org.mozilla.javascript.xml.*;public final class XMLLibImpl extends XMLLib implements Serializable { // TODO Document that this only works with JDK 1.5 or backport its // features to earlier versions private static final long serialVersionUID = 1L; // // EXPERIMENTAL Java interface // /** This experimental interface is undocumented. */ public static org.w3c.dom.Node toDomNode(Object xmlObject) { // Could return DocumentFragment for XMLList // Probably a single node for XMLList with one element if (xmlObject instanceof XML) { return ((XML)xmlObject).toDomNode(); } else { throw new IllegalArgumentException("xmlObject is not an XML object in JavaScript."); } } public static void init(Context cx, Scriptable scope, boolean sealed) { XMLLibImpl lib = new XMLLibImpl(scope); XMLLib bound = lib.bindToScope(scope); if (bound == lib) { lib.exportToScope(sealed); } } private Scriptable globalScope; private XML xmlPrototype; private XMLList xmlListPrototype; private Namespace namespacePrototype; private QName qnamePrototype; private XmlProcessor options = new XmlProcessor(); private XMLLibImpl(Scriptable globalScope) { this.globalScope = globalScope; } /** @deprecated */ QName qnamePrototype() { return qnamePrototype; } /** @deprecated */ Scriptable globalScope() { return globalScope; } XmlProcessor getProcessor() { return options; } private void exportToScope(boolean sealed) { xmlPrototype = newXML(XmlNode.createText(options, "")); xmlListPrototype = newXMLList(); namespacePrototype = Namespace.create(this.globalScope, null, XmlNode.Namespace.GLOBAL); qnamePrototype = QName.create(this, this.globalScope, null, XmlNode.QName.create(XmlNode.Namespace.create(""), "")); xmlPrototype.exportAsJSClass(sealed); xmlListPrototype.exportAsJSClass(sealed); namespacePrototype.exportAsJSClass(sealed); qnamePrototype.exportAsJSClass(sealed); } /** @deprecated */ XMLName toAttributeName(Context cx, Object nameValue) { if (nameValue instanceof XMLName) { // TODO Will this always be an XMLName of type attribute name? return (XMLName)nameValue; } else if (nameValue instanceof QName) { return XMLName.create( ((QName)nameValue).getDelegate(), true, false ); } else if (nameValue instanceof Boolean || nameValue instanceof Number || nameValue == Undefined.instance || nameValue == null) { throw badXMLName(nameValue); } else { // TODO Not 100% sure that putting these in global namespace is the right thing to do String localName = null; if (nameValue instanceof String) { localName = (String)nameValue; } else { localName = ScriptRuntime.toString(nameValue); } if (localName != null && localName.equals("*")) localName = null; return XMLName.create(XmlNode.QName.create(XmlNode.Namespace.create(""), localName), true, false); } } private static RuntimeException badXMLName(Object value) { String msg; if (value instanceof Number) { msg = "Can not construct XML name from number: "; } else if (value instanceof Boolean) { msg = "Can not construct XML name from boolean: "; } else if (value == Undefined.instance || value == null) { msg = "Can not construct XML name from "; } else { throw new IllegalArgumentException(value.toString()); } return ScriptRuntime.typeError(msg+ScriptRuntime.toString(value)); } XMLName toXMLNameFromString(Context cx, String name) { return XMLName.create( getDefaultNamespaceURI(cx), name ); } /** @deprecated */ 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 if (nameValue instanceof String) { result = toXMLNameFromString(cx, (String)nameValue); } else if (nameValue instanceof Boolean || nameValue instanceof Number || nameValue == Undefined.instance || nameValue == null) { throw badXMLName(nameValue); } 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 { throw badXMLName(value); } } 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 if (value instanceof Boolean || value == Undefined.instance || value == null) { throw badXMLName(value); } 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; } Object addXMLObjects(Context cx, XMLObject obj1, XMLObject obj2) { XMLList listToAdd = newXMLList(); 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 = newXMLListFrom(obj1); } } else { listToAdd.addToList(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(obj2); } return listToAdd; } private Ref xmlPrimaryReference(Context cx, XMLName xmlName, Scriptable scope) { XMLObjectImpl xmlObj; XMLObjectImpl firstXml = null; 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; } if (firstXml == null) { firstXml = xmlObj; } } scope = scope.getParentScope(); if (scope == null) { xmlObj = firstXml; break; } } // xmlObj == null corresponds to undefined as the target of // the reference if (xmlObj != null) { xmlName.initXMLObject(xmlObj); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -