📄 xml.java
字号:
} curs.insertAttributeWithValue(qName, strValue); } curs.dispose(); } /** * * @param namespace * @return */ private XMLList allChildNodes(String namespace) { XMLList result = new XMLList(lib); XmlCursor curs = newCursor(); TokenType tt = curs.currentTokenType(); javax.xml.namespace.QName targetProperty = new javax.xml.namespace.QName(namespace, "*"); if (tt.isStartdoc()) { tt = curs.toFirstContentToken(); } if (tt.isContainer()) { tt = curs.toFirstContentToken(); while (!tt.isEnd()) { if (!tt.isStart()) { // Not an element result.addToList(findAnnotation(curs)); // Reset target property to null in this case targetProperty = null; } else { // Match namespace as well if specified if (namespace == null || namespace.length() == 0 || namespace.equals("*") || curs.getName().getNamespaceURI().equals(namespace)) { // Add it to the list result.addToList(findAnnotation(curs)); // Set target property if target name is "*", // Otherwise if target property does not match current, then // set to null if (targetProperty != null) { if (targetProperty.getLocalPart().equals("*")) { targetProperty = curs.getName(); } else if (!targetProperty.getLocalPart().equals(curs.getName().getLocalPart())) { // Not a match, unset target property targetProperty = null; } } } } // Skip over child elements if (tt.isStart()) { tt = curs.toEndToken(); } tt = curs.toNextToken(); } } curs.dispose(); // Set the targets for this XMLList. result.setTargets(this, targetProperty); return result; } /** * * @return */ private XMLList matchDescendantAttributes(XMLName xmlName) { XMLList result = new XMLList(lib); XmlCursor curs = newCursor(); TokenType tt = curs.currentTokenType(); // Set the targets for this XMLList. result.setTargets(this, null); if (tt.isStartdoc()) { tt = curs.toFirstContentToken(); } if (tt.isContainer()) { int nestLevel = 1; while (nestLevel > 0) { tt = curs.toNextToken(); // Only try to match names for attributes if (tt.isAttr()) { if (qnameMatches(xmlName, curs.getName())) { result.addToList(findAnnotation(curs)); } } 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 result; } /** * * @return */ private XMLList matchDescendantChildren(XMLName xmlName) { XMLList result = new XMLList(lib); XmlCursor curs = newCursor(); TokenType tt = curs.currentTokenType(); // Set the targets for this XMLList. result.setTargets(this, null); if (tt.isStartdoc()) { tt = curs.toFirstContentToken(); } if (tt.isContainer()) { int nestLevel = 1; while (nestLevel > 0) { tt = curs.toNextToken(); if (!tt.isAttr() && !tt.isEnd() && !tt.isEnddoc()) { // Only try to match names for elements or processing instructions. if (!tt.isStart() && !tt.isProcinst()) { // Not an element or procinst, only add if qname is all if (xmlName.localName().equals("*")) { result.addToList(findAnnotation(curs)); } } else { if (qnameMatches(xmlName, curs.getName())) { result.addToList(findAnnotation(curs)); } } } 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 result; } /** * * @param tokenType * @return */ private XMLList matchChildren(XmlCursor.TokenType tokenType) { return matchChildren(tokenType, XMLName.formStar()); } /** * * @return */ private XMLList matchChildren(XmlCursor.TokenType tokenType, XMLName name) { XMLList result = new XMLList(lib); XmlCursor curs = newCursor(); TokenType tt = curs.currentTokenType(); javax.xml.namespace.QName qname = new javax.xml.namespace.QName(name.uri(), name.localName()); javax.xml.namespace.QName targetProperty = qname; if (tt.isStartdoc()) { tt = curs.toFirstContentToken(); } if (tt.isContainer()) { tt = curs.toFirstContentToken(); while (!tt.isEnd()) { if (tt == tokenType) { // Only try to match names for elements or processing instructions. if (!tt.isStart() && !tt.isProcinst()) { // Not an element or no name specified. result.addToList(findAnnotation(curs)); // Reset target property to null in this case targetProperty = null; } else { // Match names as well if (qnameMatches(name, curs.getName())) { // Add it to the list result.addToList(findAnnotation(curs)); // Set target property if target name is "*", // Otherwise if target property does not match current, then // set to null if (targetProperty != null) { if (targetProperty.getLocalPart().equals("*")) { targetProperty = curs.getName(); } else if (!targetProperty.getLocalPart().equals(curs.getName().getLocalPart())) { // Not a match, unset target property targetProperty = null; } } } } } // Skip over child elements if (tt.isStart()) { tt = curs.toEndToken(); } tt = curs.toNextToken(); } } curs.dispose(); if (tokenType == XmlCursor.TokenType.START) { // Set the targets for this XMLList. result.setTargets(this, targetProperty); } return result; } /** * * @param template * @param match * @return */ private boolean qnameMatches(XMLName template, javax.xml.namespace.QName match) { boolean matches = false; if (template.uri() == null || template.uri().equals(match.getNamespaceURI())) { // URI OK, test name if (template.localName().equals("*") || template.localName().equals(match.getLocalPart())) { matches = true; } } return matches; } // // // Methods from section 12.4.4 in the spec // // /** * The addNamespace method adds a namespace declaration to the in scope * namespaces for this XML object and returns this XML object. * * @param toAdd */ XML addNamespace(Namespace ns) { // When a namespace is used it will be added automatically // to the inScopeNamespaces set. There is no need to add // Namespaces with undefined prefixes. String nsPrefix = ns.prefix(); if (nsPrefix == null) return this; XmlCursor cursor = newCursor(); try { if(!cursor.isContainer()) return this; javax.xml.namespace.QName qname = cursor.getName(); // Don't add a default namespace declarations to containers // with QNames in no namespace. if(qname.getNamespaceURI().equals("") && nsPrefix.equals("")) return this; // Get all declared namespaces that are in scope Map prefixToURI = NamespaceHelper.getAllNamespaces(lib, cursor); String uri = (String)prefixToURI.get(nsPrefix); if(uri != null) { // Check if the Namespace is not already in scope if(uri.equals(ns.uri())) return this; cursor.push(); // Let's see if we have to delete a namespace declaration while(cursor.toNextToken().isAnyAttr()) { if(cursor.isNamespace()) { qname = cursor.getName(); String prefix = qname.getLocalPart(); if(prefix.equals(nsPrefix)) { // Delete the current Namespace declaration cursor.removeXml(); break; } } } cursor.pop(); } cursor.toNextToken(); cursor.insertNamespace(nsPrefix, ns.uri()); } finally { cursor.dispose(); } return this; } /** * * @param xml * @return */ XML appendChild(Object xml) { XmlCursor curs = newCursor(); if (curs.isStartdoc()) { curs.toFirstContentToken(); } // Move the cursor to the end of this element if (curs.isStart()) { curs.toEndToken(); } insertChild(curs, xml); curs.dispose(); return this; } /** * * @param name * @return */ XMLList attribute(XMLName xmlName) { return matchAttributes(xmlName); } /** * * @return */ XMLList attributes() { XMLName xmlName = XMLName.formStar(); return matchAttributes(xmlName); } XMLList child(long index) { XMLList result = new XMLList(lib); result.setTargets(this, null); result.addToList(getXmlChild(index)); return result; } XMLList child(XMLName xmlName) { if (xmlName == null) return new XMLList(lib); XMLList result; if (xmlName.localName().equals("*")) { result = allChildNodes(xmlName.uri()); } else { result = matchChildren(XmlCursor.TokenType.START, xmlName); } return result; } /** * * @param index * @return */ XML getXmlChild(long index) { XML result = null; XmlCursor curs = newCursor(); if (moveToChild(curs, index, false, true)) { result = createXML(lib, curs); } curs.dispose(); return result; } /** * * @return */ int childIndex() { int index = 0; XmlCursor curs = newCursor(); TokenType tt = curs.currentTokenType(); while (true) { if (tt.isText()) { index++; if (!curs.toPrevSibling()) { break; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -