📄 xmllibimpl.java
字号:
return xmlName; } Namespace castToNamespace(Context cx, Object namespaceObj) { return this.namespacePrototype.castToNamespace(namespaceObj); } private String getDefaultNamespaceURI(Context cx) { return getDefaultNamespace(cx).uri(); } Namespace newNamespace(String uri) { return this.namespacePrototype.newNamespace(uri); } Namespace getDefaultNamespace(Context cx) { if (cx == null) { cx = Context.getCurrentContext(); if (cx == null) { return namespacePrototype; } } Object ns = ScriptRuntime.searchDefaultNamespace(cx); if (ns == null) { return namespacePrototype; } else { if (ns instanceof Namespace) { return (Namespace)ns; } else { // TODO Clarify or remove the following comment // Should not happen but for now it could // due to bad searchDefaultNamespace implementation. return namespacePrototype; } } } Namespace[] createNamespaces(XmlNode.Namespace[] declarations) { Namespace[] rv = new Namespace[declarations.length]; for (int i=0; i<declarations.length; i++) { rv[i] = this.namespacePrototype.newNamespace(declarations[i].getPrefix(), declarations[i].getUri()); } return rv; } // See ECMA357 13.3.2 QName constructQName(Context cx, Object namespace, Object name) { return this.qnamePrototype.constructQName(this, cx, namespace, name); } QName newQName(String uri, String localName, String prefix) { return this.qnamePrototype.newQName(this, uri, localName, prefix); } QName constructQName(Context cx, Object nameValue) {// return constructQName(cx, Undefined.instance, nameValue); return this.qnamePrototype.constructQName(this, cx, nameValue); } QName castToQName(Context cx, Object qnameValue) { return this.qnamePrototype.castToQName(this, cx, qnameValue); } QName newQName(XmlNode.QName qname) { return QName.create(this, this.globalScope, this.qnamePrototype, qname); } XML newXML(XmlNode node) { return new XML(this, this.globalScope, this.xmlPrototype, node); } /** @deprecated I believe this can be replaced by ecmaToXml below. */ final XML newXMLFromJs(Object inputObject) { String frag; if (inputObject == null || inputObject == Undefined.instance) { frag = ""; } else if (inputObject instanceof XMLObjectImpl) { // todo: faster way for XMLObjects? frag = ((XMLObjectImpl) inputObject).toXMLString(); } else { frag = ScriptRuntime.toString(inputObject); } if (frag.trim().startsWith("<>")) { throw ScriptRuntime.typeError("Invalid use of XML object anonymous tags <></>."); } if (frag.indexOf("<") == -1) { // Solo text node return newXML(XmlNode.createText(options, frag)); } return parse(frag); } private XML parse(String frag) { try { return newXML(XmlNode.createElement(options, getDefaultNamespaceURI(Context.getCurrentContext()), frag)); } catch (org.xml.sax.SAXException e) { throw ScriptRuntime.typeError("Cannot parse XML: " + e.getMessage()); } } final XML ecmaToXml(Object object) { // See ECMA357 10.3 if (object == null || object == Undefined.instance) throw ScriptRuntime.typeError("Cannot convert " + object + " to XML"); if (object instanceof XML) return (XML)object; if (object instanceof XMLList) { XMLList list = (XMLList)object; if (list.getXML() != null) { return list.getXML(); } else { throw ScriptRuntime.typeError("Cannot convert list of >1 element to XML"); } } // TODO Technically we should fail on anything except a String, Number or Boolean // See ECMA357 10.3 // Extension: if object is a DOM node, use that to construct the XML // object. if (object instanceof Wrapper) { object = ((Wrapper) object).unwrap(); } if (object instanceof org.w3c.dom.Node) { org.w3c.dom.Node node = (org.w3c.dom.Node) object; return newXML(XmlNode.createElementFromNode(node)); } // Instead we just blindly cast to a String and let them convert anything. String s = ScriptRuntime.toString(object); // TODO Could this get any uglier? if (s.length() > 0 && s.charAt(0) == '<') { return parse(s); } else { return newXML(XmlNode.createText(options, s)); } } final XML newTextElementXML(XmlNode reference, XmlNode.QName qname, String value) { return newXML(XmlNode.newElementWithText(options, reference, qname, value)); } XMLList newXMLList() { return new XMLList(this, this.globalScope, this.xmlListPrototype); } final XMLList newXMLListFrom(Object inputObject) { XMLList rv = newXMLList(); if (inputObject == null || inputObject instanceof Undefined) { return rv; } else if (inputObject instanceof XML) { XML xml = (XML) inputObject; rv.getNodeList().add(xml); return rv; } else if (inputObject instanceof XMLList) { XMLList xmll = (XMLList) inputObject; rv.getNodeList().add(xmll.getNodeList()); return rv; } else { String frag = ScriptRuntime.toString(inputObject).trim(); if (!frag.startsWith("<>")) { frag = "<>" + frag + "</>"; } frag = "<fragment>" + frag.substring(2); if (!frag.endsWith("</>")) { throw ScriptRuntime.typeError("XML with anonymous tag missing end anonymous tag"); } frag = frag.substring(0, frag.length() - 3) + "</fragment>"; XML orgXML = newXMLFromJs(frag); // Now orphan the children and add them to our XMLList. XMLList children = orgXML.children(); for (int i = 0; i < children.getNodeList().length(); i++) { // Copy here is so that they'll be orphaned (parent() will be undefined) rv.getNodeList().add(((XML) children.item(i).copy())); } return rv; } } XmlNode.QName toNodeQName(Context cx, Object namespaceValue, Object nameValue) { // This is duplication of constructQName(cx, namespaceValue, nameValue) // but for XMLName String localName; if (nameValue instanceof QName) { QName qname = (QName)nameValue; localName = qname.localName(); } else { localName = ScriptRuntime.toString(nameValue); } XmlNode.Namespace ns; if (namespaceValue == Undefined.instance) { if ("*".equals(localName)) { ns = null; } else { ns = getDefaultNamespace(cx).getDelegate(); } } else if (namespaceValue == null) { ns = null; } else if (namespaceValue instanceof Namespace) { ns = ((Namespace)namespaceValue).getDelegate(); } else { ns = this.namespacePrototype.constructNamespace(namespaceValue).getDelegate(); } if (localName != null && localName.equals("*")) localName = null; return XmlNode.QName.create(ns, localName); } XmlNode.QName toNodeQName(Context cx, String name, boolean attribute) { XmlNode.Namespace defaultNamespace = getDefaultNamespace(cx).getDelegate(); if (name != null && name.equals("*")) { return XmlNode.QName.create(null, null); } else { if (attribute) { return XmlNode.QName.create(XmlNode.Namespace.GLOBAL, name); } else { return XmlNode.QName.create(defaultNamespace, name); } } } /** @deprecated Too general; this should be split into overloaded methods. Is that possible? */ XmlNode.QName toNodeQName(Context cx, Object nameValue, boolean attribute) { if (nameValue instanceof XMLName) { return ((XMLName)nameValue).toQname(); } else if (nameValue instanceof QName) { QName qname = (QName)nameValue; return qname.getDelegate(); } else if ( nameValue instanceof Boolean || nameValue instanceof Number || nameValue == Undefined.instance || nameValue == null ) { throw badXMLName(nameValue); } else { String local = null; if (nameValue instanceof String) { local = (String)nameValue; } else { local = ScriptRuntime.toString(nameValue); } return toNodeQName(cx, local, attribute); } } // // Override methods from XMLLib // public boolean isXMLName(Context _cx, Object nameObj) { return XMLName.accept(nameObj); } public Object toDefaultXmlNamespace(Context cx, Object uriValue) { return this.namespacePrototype.constructNamespace(uriValue); } public String escapeTextValue(Object o) { return options.escapeTextValue(o); } public String escapeAttributeValue(Object o) { return options.escapeAttributeValue(o); } public Ref nameRef(Context cx, Object name, Scriptable scope, int memberTypeFlags) { if ((memberTypeFlags & Node.ATTRIBUTE_FLAG) == 0) { // should only be called foir cases like @name or @[expr] throw Kit.codeBug(); } XMLName xmlName = toAttributeName(cx, name); return xmlPrimaryReference(cx, xmlName, scope); } public Ref nameRef(Context cx, Object namespace, Object name, Scriptable scope, int memberTypeFlags) { XMLName xmlName = XMLName.create(toNodeQName(cx, namespace, name), false, false); // No idea what is coming in from the parser in this case; is it detecting the "@"? if ((memberTypeFlags & Node.ATTRIBUTE_FLAG) != 0) { if (!xmlName.isAttributeName()) { xmlName.setAttributeName(); } } return xmlPrimaryReference(cx, xmlName, scope); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -