📄 xml.java
字号:
else if (tt.isStart()) { tt = curs.toPrevToken(); if (tt.isEnd()) { curs.toNextToken(); if (!curs.toPrevSibling()) { break; } index++; } else { // Hit the parent start tag so get out we're down counting children. break; } } else if (tt.isComment() || tt.isProcinst()) { curs.toPrevToken(); } else { break; } tt = curs.currentTokenType(); } index = curs.currentTokenType().isStartdoc() ? -1 : index; curs.dispose(); return index; } /** * * @return */ XMLList children() { return allChildNodes(null); } /** * * @return */ XMLList comments() { return matchChildren(XmlCursor.TokenType.COMMENT); } /** * * @param xml * @return */ boolean contains(Object xml) { boolean result = false; if (xml instanceof XML) { result = equivalentXml(xml); } return result; } /** * * @return */ Object copy() { XmlCursor srcCurs = newCursor(); if (srcCurs.isStartdoc()) { srcCurs.toFirstContentToken(); } XML xml = createEmptyXML(lib); XmlCursor destCurs = xml.newCursor(); destCurs.toFirstContentToken(); srcCurs.copyXml(destCurs); destCurs.dispose(); srcCurs.dispose(); return xml; } /** * * @param name * @return */ XMLList descendants(XMLName xmlName) { XMLList result; if (xmlName.isAttributeName()) { result = matchDescendantAttributes(xmlName); } else { result = matchDescendantChildren(xmlName); } return result; } /** * The inScopeNamespaces method returns an Array of Namespace objects * representing the namespaces in scope for this XML object in the * context of its parent. * * @return Array of all Namespaces in scope for this XML Object. */ Object[] inScopeNamespaces() { XmlCursor cursor = newCursor(); Object[] namespaces = NamespaceHelper.inScopeNamespaces(lib, cursor); cursor.dispose(); return namespaces; } /** * * @param child * @param xml */ XML insertChildAfter(Object child, Object xml) { if (child == null) { // Spec says inserting after nothing is the same as prepending prependChild(xml); } else if (child instanceof XML) { insertChild((XML) child, xml, APPEND_CHILD); } return this; } /** * * @param child * @param xml */ XML insertChildBefore(Object child, Object xml) { if (child == null) { // Spec says inserting before nothing is the same as appending appendChild(xml); } else if (child instanceof XML) { insertChild((XML) child, xml, PREPEND_CHILD); } return this; } /** * * @return */ boolean hasOwnProperty(XMLName xmlName) { boolean hasProperty = false; if (prototypeFlag) { String property = xmlName.localName(); hasProperty = (0 != findPrototypeId(property)); } else { hasProperty = (getPropertyList(xmlName).length() > 0); } return hasProperty; } /** * * @return */ boolean hasComplexContent() { return !hasSimpleContent(); } /** * * @return */ boolean hasSimpleContent() { boolean simpleContent = false; XmlCursor curs = newCursor(); if (curs.isAttr() || curs.isText()) { return true; } if (curs.isStartdoc()) { curs.toFirstContentToken(); } simpleContent = !(curs.toFirstChild()); curs.dispose(); return simpleContent; } /** * Length of an XML object is always 1, it's a list of XML objects of size 1. * * @return */ int length() { return 1; } /** * * @return */ String localName() { XmlCursor cursor = newCursor(); if (cursor.isStartdoc()) cursor.toFirstContentToken(); String name = null; if(cursor.isStart() || cursor.isAttr() || cursor.isProcinst()) { javax.xml.namespace.QName qname = cursor.getName(); name = qname.getLocalPart(); } cursor.dispose(); return name; } /** * The name method returns the qualified name associated with this XML object. * * @return The qualified name associated with this XML object. */ QName name() { XmlCursor cursor = newCursor(); if (cursor.isStartdoc()) cursor.toFirstContentToken(); QName name = null; if(cursor.isStart() || cursor.isAttr() || cursor.isProcinst()) { javax.xml.namespace.QName qname = cursor.getName(); if(cursor.isProcinst()) { name = new QName(lib, "", qname.getLocalPart(), ""); } else { String uri = qname.getNamespaceURI(); String prefix = qname.getPrefix(); name = new QName(lib, uri, qname.getLocalPart(), prefix); } } cursor.dispose(); return name; } /** * * @param prefix * @return */ Object namespace(String prefix) { XmlCursor cursor = newCursor(); if (cursor.isStartdoc()) { cursor.toFirstContentToken(); } Object result = null; if (prefix == null) { if(cursor.isStart() || cursor.isAttr()) { Object[] inScopeNS = NamespaceHelper.inScopeNamespaces(lib, cursor); // XXX Is it reaaly necessary to create the second cursor? XmlCursor cursor2 = newCursor(); if (cursor2.isStartdoc()) cursor2.toFirstContentToken(); result = NamespaceHelper.getNamespace(lib, cursor2, inScopeNS); cursor2.dispose(); } } else { Map prefixToURI = NamespaceHelper.getAllNamespaces(lib, cursor); String uri = (String)prefixToURI.get(prefix); result = (uri == null) ? Undefined.instance : new Namespace(lib, prefix, uri); } cursor.dispose(); return result; } /** * * @return */ Object[] namespaceDeclarations() { XmlCursor cursor = newCursor(); Object[] namespaces = NamespaceHelper.namespaceDeclarations(lib, cursor); cursor.dispose(); return namespaces; } /** * * @return */ Object nodeKind() { String result; XmlCursor.TokenType tt = tokenType(); if (tt == XmlCursor.TokenType.ATTR) { result = "attribute"; } else if (tt == XmlCursor.TokenType.TEXT) { result = "text"; } else if (tt == XmlCursor.TokenType.COMMENT) { result = "comment"; } else if (tt == XmlCursor.TokenType.PROCINST) { result = "processing-instruction"; } else if (tt == XmlCursor.TokenType.START) { result = "element"; } else { // A non-existant node has the nodeKind() of text result = "text"; } return result; } /** * */ void normalize() { XmlCursor curs = newCursor(); TokenType tt = curs.currentTokenType(); // Walk through the tokens removing empty text nodes and merging adjacent text nodes. if (tt.isStartdoc()) { tt = curs.toFirstContentToken(); } if (tt.isContainer()) { int nestLevel = 1; String previousText = null; while (nestLevel > 0) { tt = curs.toNextToken(); if (tt == XmlCursor.TokenType.TEXT) { String currentText = curs.getChars().trim(); if (currentText.trim().length() == 0) { // Empty text node, remove. removeToken(curs); curs.toPrevToken(); } else if (previousText == null) { // No previous text node, reset to trimmed version previousText = currentText; } else { // It appears that this case never happens with XBeans. // Previous text node exists, concatenate String newText = previousText + currentText; curs.toPrevToken(); removeToken(curs); removeToken(curs); curs.insertChars(newText); } } else { previousText = null; } if (tt.isStart()) { nestLevel++; } else if (tt.isEnd()) { nestLevel--; } else if (tt.isEnddoc()) { // Shouldn't get here, but just in case. break; } } } curs.dispose(); } /** * * @return */ Object parent() { Object parent; XmlCursor curs = newCursor(); if (curs.isStartdoc()) { // At doc level - no parent parent = Undefined.instance; } else { if (curs.toParent()) { if (curs.isStartdoc()) { // Was top-level - no parent parent = Undefined.instance; } else { parent = getFromAnnotation(lib, findAnnotation(curs)); } } else { // No parent parent = Undefined.instance; } } curs.dispose(); return parent; } /** * *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -