📄 xml.java
字号:
throw ScriptRuntime.typeError(ex.getMessage()); } finally { curs.dispose(); } return result; } /** * Make a text node element with this element name and text value. * * @param name * @param value * @return */ private XML makeXmlFromString(XMLLibImpl lib, XMLName name, String value) { XML result; javax.xml.namespace.QName qname; try { qname = new javax.xml.namespace.QName(name.uri(), name.localName()); } catch(Exception e) { throw ScriptRuntime.typeError(e.getMessage()); } result = createTextElement(lib, qname, value); return result; } /** * * @param name * @return */ private XMLList matchAttributes(XMLName xmlName) { XMLList result = new XMLList(lib); XmlCursor curs = newCursor(); if (curs.currentTokenType().isStartdoc()) { curs.toFirstContentToken(); } if (curs.isStart()) { if (curs.toFirstAttribute()) { do { if (qnameMatches(xmlName, curs.getName())) { result.addToList(createAttributeObject(curs)); } } while (curs.toNextAttribute()); } } curs.dispose(); return result; } /** * * @param attrCurs * @return */ private XML createAttributeObject (XmlCursor attrCurs) { XML result = null; if (attrCurs.currentTokenType().isAttr()) { result = createAttributeXML(lib, attrCurs); } return result; } // // // methods overriding ScriptableObject // // public String getClassName () { return "XML"; } // // // methods overriding IdScriptableObject // // /** * XML[0] should return this, all other indexes are Undefined * * @param index * @param start * @return */ public Object get(int index, Scriptable start) { //Log("get index: " + index); if (index == 0) { return this; } else { return Scriptable.NOT_FOUND; } } /** * Does the named property exist * * @param name * @param start * @return */ boolean hasXMLProperty(XMLName xmlName) { boolean result = false; if (prototypeFlag) { String name = xmlName.localName(); if (getMethod(name) != NOT_FOUND) { result = true; } } else { // Has now should return true if the property would have results > 0 or // if it's a method name String name = xmlName.localName(); if ((getPropertyList(xmlName).length() > 0) || (getMethod(name) != NOT_FOUND)) { result = true; } } return result; } /** * * @param index * @param start * @return */ public boolean has(int index, Scriptable start) { return (index == 0); } /** * * @return */ public Object[] getIds() { Object[] enumObjs; if (prototypeFlag) { enumObjs = new Object[0]; } else { enumObjs = new Object[1]; enumObjs[0] = new Integer(0); } return enumObjs; } /** * * @return */ public Object [] getIdsForDebug() { return getIds(); } /** * * @param name * @param start * @return */ Object getXMLProperty(XMLName xmlName) { Object result = NOT_FOUND; if (prototypeFlag) { String name = xmlName.localName(); result = getMethod(name); } else { result = getPropertyList(xmlName); } return result; } /** * * @param name * @param start * @param value */ void putXMLProperty(XMLName xmlName, Object value) { //Log("put property: " + name + " value: " + value.getClass()); if (prototypeFlag) { } else { // Special-case checks for undefined and null if (value == null) { value = "null"; } else if (value instanceof Undefined) { value = "undefined"; } // Get the named property if (xmlName.isAttributeName()) { setAttribute(xmlName, value); } else if (xmlName.uri() == null && xmlName.localName().equals("*")) { setChildren(value); } else { // Convert text into XML if needed. XMLObjectImpl xmlValue = null; if (value instanceof XMLObjectImpl) { xmlValue = (XMLObjectImpl) value; // Check for attribute type and convert to textNode if (xmlValue instanceof XML) { if (((XML) xmlValue).tokenType() == XmlCursor.TokenType.ATTR) { xmlValue = makeXmlFromString(lib, xmlName, xmlValue.toString()); } } if (xmlValue instanceof XMLList) { for (int i = 0; i < xmlValue.length(); i++) { XML xml = ((XMLList) xmlValue).item(i); if (xml.tokenType() == XmlCursor.TokenType.ATTR) { ((XMLList) xmlValue).replace(i, makeXmlFromString(lib, xmlName, xml.toString())); } } } } else { xmlValue = makeXmlFromString(lib, xmlName, ScriptRuntime.toString(value)); } XMLList matches = getPropertyList(xmlName); if (matches.length() == 0) { appendChild(xmlValue); } else { // Remove all other matches for (int i = 1; i < matches.length(); i++) { removeChild(matches.item(i).childIndex()); } // Replace first match with new value. doPut(xmlName, matches.item(0), xmlValue); } } } } /** * * @param index * @param start * @param value */ public void put(int index, Scriptable start, Object value) { // Spec says assignment to indexed XML object should return type error throw ScriptRuntime.typeError("Assignment to indexed XML is not allowed"); } /** * * @param name */ void deleteXMLProperty(XMLName name) { if (!name.isDescendants() && name.isAttributeName()) { XmlCursor curs = newCursor(); // TODO: Cover the case *::name if (name.localName().equals("*")) { // Delete all attributes. if (curs.toFirstAttribute()) { while (curs.currentTokenType().isAttr()) { curs.removeXml(); } } } else { // Delete an attribute. javax.xml.namespace.QName qname = new javax.xml.namespace.QName( name.uri(), name.localName()); curs.removeAttribute(qname); } curs.dispose(); } else { XMLList matches = getPropertyList(name); matches.remove(); } } /** * * @param index */ public void delete(int index) { if (index == 0) { remove(); } } // // // package utility functions: // // protected XScriptAnnotation getAnnotation () { return _anno; } protected void changeNS (String oldURI, String newURI) { XmlCursor curs = newCursor(); while (curs.toParent()) { /* Goto the top of the document */ } TokenType tt = curs.currentTokenType(); if (tt.isStartdoc()) { tt = curs.toFirstContentToken(); } if (tt.isStart()) { do { if (tt.isStart() || tt.isAttr() || tt.isNamespace()) { javax.xml.namespace.QName currQName = curs.getName(); if (oldURI.equals(currQName.getNamespaceURI())) { curs.setName(new javax.xml.namespace.QName(newURI, currQName.getLocalPart())); } } tt = curs.toNextToken(); } while (!tt.isEnddoc() && !tt.isNone()); } curs.dispose(); } /** * */ void remove () { XmlCursor childCurs = newCursor(); if (childCurs.currentTokenType().isStartdoc()) { // Remove on the document removes all children. TokenType tt = childCurs.toFirstContentToken(); while (!tt.isEnd() && !tt.isEnddoc()) { removeToken(childCurs); tt = childCurs.currentTokenType(); // Now see where we're pointing after the delete -- next token. } } else { removeToken(childCurs); } childCurs.dispose(); } /** * * @param value */ void replaceAll(XML value) { XmlCursor curs = newCursor(); replace(curs, value); _anno = value._anno; curs.dispose(); } /** * * @param attrName * @param value */ void setAttribute(XMLName xmlName, Object value) { if (xmlName.uri() == null && xmlName.localName().equals("*")) { throw ScriptRuntime.typeError("@* assignment not supported."); } XmlCursor curs = newCursor(); String strValue = ScriptRuntime.toString(value); if (curs.currentTokenType().isStartdoc()) { curs.toFirstContentToken(); } javax.xml.namespace.QName qName; try { qName = new javax.xml.namespace.QName(xmlName.uri(), xmlName.localName()); } catch(Exception e) { throw ScriptRuntime.typeError(e.getMessage()); } if (!curs.setAttributeText(qName, strValue)) { if (curs.currentTokenType().isStart()) { // Can only add attributes inside of a start. curs.toNextToken();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -