📄 xmllibimpl.java
字号:
} 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); } 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; } // // // Overriding XMLLib methods // // /** * See E4X 13.1.2.1. */ public boolean isXMLName(Context cx, Object nameObj) { String name; try { name = ScriptRuntime.toString(nameObj); } catch (EcmaError ee) { if ("TypeError".equals(ee.getName())) { return false; } throw ee; } // See http://w3.org/TR/xml-names11/#NT-NCName int length = name.length(); if (length != 0) { if (isNCNameStartChar(name.charAt(0))) { for (int i = 1; i != length; ++i) { if (!isNCNameChar(name.charAt(i))) { return false; } } return true; } } return false; } private static boolean isNCNameStartChar(int c) { if ((c & ~0x7F) == 0) { // Optimize for ASCII and use A..Z < _ < a..z if (c >= 'a') { return c <= 'z'; } else if (c >= 'A') { if (c <= 'Z') { return true; } return c == '_'; } } else if ((c & ~0x1FFF) == 0) { return (0xC0 <= c && c <= 0xD6) || (0xD8 <= c && c <= 0xF6) || (0xF8 <= c && c <= 0x2FF) || (0x370 <= c && c <= 0x37D) || 0x37F <= c; } return (0x200C <= c && c <= 0x200D) || (0x2070 <= c && c <= 0x218F) || (0x2C00 <= c && c <= 0x2FEF) || (0x3001 <= c && c <= 0xD7FF) || (0xF900 <= c && c <= 0xFDCF) || (0xFDF0 <= c && c <= 0xFFFD) || (0x10000 <= c && c <= 0xEFFFF); } private static boolean isNCNameChar(int c) { if ((c & ~0x7F) == 0) { // Optimize for ASCII and use - < . < 0..9 < A..Z < _ < a..z if (c >= 'a') { return c <= 'z'; } else if (c >= 'A') { if (c <= 'Z') { return true; } return c == '_'; } else if (c >= '0') { return c <= '9'; } else { return c == '-' || c == '.'; } } else if ((c & ~0x1FFF) == 0) { return isNCNameStartChar(c) || c == 0xB7 || (0x300 <= c && c <= 0x36F); } return isNCNameStartChar(c) || (0x203F <= c && c <= 0x2040); } XMLName toQualifiedName(Context cx, Object namespaceValue, Object nameValue) { // This is duplication of constructQName(cx, namespaceValue, nameValue) // but for XMLName String uri; String localName; 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; } else { uri = ns.uri(); } return XMLName.formProperty(uri, localName); } 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 = toQualifiedName(cx, namespace, name); if ((memberTypeFlags & Node.ATTRIBUTE_FLAG) != 0) { if (!xmlName.isAttributeName()) { xmlName.setAttributeName(); } } return xmlPrimaryReference(cx, xmlName, scope); } private Ref xmlPrimaryReference(Context cx, XMLName xmlName, Scriptable scope) { XMLObjectImpl xmlObj; XMLObjectImpl firstXmlObject = 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 (firstXmlObject == null) { firstXmlObject = xmlObj; } } scope = scope.getParentScope(); if (scope == null) { xmlObj = firstXmlObject; break; } } // xmlObj == null corresponds to undefined as the target of // the reference if (xmlObj != null) { xmlName.initXMLObject(xmlObj); } return xmlName; } /** * Escapes the reserved characters in a value of an attribute * and surround it by "". * * @param value Unescaped text * @return The escaped text */ public String escapeAttributeValue(Object value) { String text = ScriptRuntime.toString(value); if (text.length() == 0) return "\"\""; 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('"'); int end = elementText.lastIndexOf('"'); return elementText.substring(begin, end + 1); } /** * 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(0); } 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 toDefaultXmlNamespace(Context cx, Object uriValue) { return constructNamespace(cx, uriValue); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -