📄 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 * * 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.xml.impl.xmlbeans;import java.io.Serializable;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 implements Serializable{ private static final long serialVersionUID = 1L; 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) { // To force LinkageError if XmlObject is not available XmlObject.class.getName(); XMLLibImpl lib = new XMLLibImpl(scope); XMLLib bound = lib.bindToScope(scope); if (bound == lib) { lib.exportToScope(sealed); } } private void exportToScope(boolean sealed) { xmlPrototype = XML.createEmptyXML(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; } XMLName toAttributeName(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 Boolean || nameValue instanceof Number || nameValue == Undefined.instance || nameValue == null) { throw badXMLName(nameValue); } else { uri = ""; localName = ScriptRuntime.toString(nameValue); } XMLName xmlName = XMLName.formProperty(uri, localName); xmlName.setAttributeName(); return xmlName; } 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 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; } 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.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; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -