List of usage examples for org.dom4j Element node
Node node(int index) throws IndexOutOfBoundsException;
Node
at the specified index position. From source file:architecture.common.xml.XmlWriter.java
License:Apache License
protected void writeElement(Element element) throws IOException { int size = element.nodeCount(); String qualifiedName = element.getQualifiedName(); writePrintln();/*from w ww . j a v a2 s .com*/ indent(); writer.write("<"); writer.write(qualifiedName); int previouslyDeclaredNamespaces = namespaceStack.size(); Namespace ns = element.getNamespace(); if (isNamespaceDeclaration(ns)) { namespaceStack.push(ns); writeNamespace(ns); } // Print out additional namespace declarations boolean textOnly = true; for (int i = 0; i < size; i++) { Node node = element.node(i); if (node instanceof Namespace) { Namespace additional = (Namespace) node; if (isNamespaceDeclaration(additional)) { namespaceStack.push(additional); writeNamespace(additional); } } else if (node instanceof Element) { textOnly = false; } else if (node instanceof Comment) { textOnly = false; } } writeAttributes(element); lastOutputNodeType = Node.ELEMENT_NODE; if (size <= 0) { writeEmptyElementClose(qualifiedName); } else { writer.write(">"); if (textOnly) { // we have at least one text node so lets assume // that its non-empty writeElementContent(element); } else { // we know it's not null or empty from above ++indentLevel; writeElementContent(element); --indentLevel; writePrintln(); indent(); } writer.write("</"); writer.write(qualifiedName); writer.write(">"); } // remove declared namespaceStack from stack while (namespaceStack.size() > previouslyDeclaredNamespaces) { namespaceStack.pop(); } lastOutputNodeType = Node.ELEMENT_NODE; }
From source file:architecture.common.xml.XmlWriter.java
License:Apache License
/** * Outputs the content of the given element. If whitespace trimming is * enabled then all adjacent text nodes are appended together before the * whitespace trimming occurs to avoid problems with multiple text nodes * being created due to text content that spans parser buffers in a SAX * parser.//w w w . j a va2s .c om */ protected void writeElementContent(Element element) throws IOException { boolean trim = format.isTrimText(); boolean oldPreserve = preserve; if (trim) { // verify we have to before more expensive test preserve = isElementSpacePreserved(element); trim = !preserve; } if (trim) { // concatenate adjacent text nodes together // so that whitespace trimming works properly Text lastTextNode = null; StringBuilder buffer = null; boolean textOnly = true; for (int i = 0, size = element.nodeCount(); i < size; i++) { Node node = element.node(i); if (node instanceof Text) { if (lastTextNode == null) { lastTextNode = (Text) node; } else { if (buffer == null) { buffer = new StringBuilder(lastTextNode.getText()); } buffer.append(((Text) node).getText()); } } else { if (!textOnly && format.isPadText()) { writer.write(PAD_TEXT); } textOnly = false; if (lastTextNode != null) { if (buffer != null) { writeString(buffer.toString()); buffer = null; } else { writeString(lastTextNode.getText()); } lastTextNode = null; if (format.isPadText()) { writer.write(PAD_TEXT); } } writeNode(node); } } if (lastTextNode != null) { if (!textOnly && format.isPadText()) { writer.write(PAD_TEXT); } if (buffer != null) { writeString(buffer.toString()); buffer = null; } else { writeString(lastTextNode.getText()); } lastTextNode = null; } } else { Node lastTextNode = null; for (int i = 0, size = element.nodeCount(); i < size; i++) { Node node = element.node(i); if (node instanceof Text) { writeNode(node); lastTextNode = node; } else { if ((lastTextNode != null) && format.isPadText()) { writer.write(PAD_TEXT); } writeNode(node); if ((lastTextNode != null) && format.isPadText()) { writer.write(PAD_TEXT); } lastTextNode = null; } } } preserve = oldPreserve; }
From source file:architecture.ee.util.xml.XmlWriter.java
License:Apache License
/** Outputs the content of the given element. If whitespace trimming is * enabled then all adjacent text nodes are appended together before * the whitespace trimming occurs to avoid problems with multiple * text nodes being created due to text content that spans parser buffers * in a SAX parser./*from ww w. j av a 2s . c o m*/ */ protected void writeElementContent(Element element) throws IOException { boolean trim = format.isTrimText(); boolean oldPreserve = preserve; if (trim) { //verify we have to before more expensive test preserve = isElementSpacePreserved(element); trim = !preserve; } if (trim) { // concatenate adjacent text nodes together // so that whitespace trimming works properly Text lastTextNode = null; StringBuilder buffer = null; boolean textOnly = true; for (int i = 0, size = element.nodeCount(); i < size; i++) { Node node = element.node(i); if (node instanceof Text) { if (lastTextNode == null) { lastTextNode = (Text) node; } else { if (buffer == null) { buffer = new StringBuilder(lastTextNode.getText()); } buffer.append(node.getText()); } } else { if (!textOnly && format.isPadText()) { writer.write(PAD_TEXT); } textOnly = false; if (lastTextNode != null) { if (buffer != null) { writeString(buffer.toString()); buffer = null; } else { writeString(lastTextNode.getText()); } lastTextNode = null; if (format.isPadText()) { writer.write(PAD_TEXT); } } writeNode(node); } } if (lastTextNode != null) { if (!textOnly && format.isPadText()) { writer.write(PAD_TEXT); } if (buffer != null) { writeString(buffer.toString()); buffer = null; } else { writeString(lastTextNode.getText()); } lastTextNode = null; } } else { Node lastTextNode = null; for (int i = 0, size = element.nodeCount(); i < size; i++) { Node node = element.node(i); if (node instanceof Text) { writeNode(node); lastTextNode = node; } else { if ((lastTextNode != null) && format.isPadText()) { writer.write(PAD_TEXT); } writeNode(node); if ((lastTextNode != null) && format.isPadText()) { writer.write(PAD_TEXT); } lastTextNode = null; } } } preserve = oldPreserve; }
From source file:com.amalto.workbench.utils.XmlUtil.java
License:Open Source License
public static void treeWalk(Element element, NodeProcess nodeProcess) { for (int i = 0, size = element.nodeCount(); i < size; i++) { Node node = element.node(i); if (node instanceof Element) { treeWalk((Element) node, nodeProcess); } else {/* w w w . j a v a2 s.c o m*/ nodeProcess.process(node); } } }
From source file:com.chinarewards.license.util.XmlUtil_dom4j.java
public static void searchElement(Element paramElement) { int i = 0;/*from ww w. ja v a 2 s. c om*/ int j = paramElement.nodeCount(); while (i < j) { Node localNode = paramElement.node(i); if (localNode instanceof Element) { Element localElement = (Element) localNode; searchAttribute(localElement); searchElement((Element) localNode); } ++i; } }
From source file:com.cladonia.xml.ExchangerXMLWriter.java
License:Open Source License
/** Outputs the content of the given element. If whitespace trimming is * enabled then all adjacent text nodes are appended together before * the whitespace trimming occurs to avoid problems with multiple * text nodes being created due to text content that spans parser buffers * in a SAX parser./*from www . ja v a 2s .c o m*/ */ protected void writeElementContent(Element element) throws IOException { CDATA lastCDATANode = null; StringBuffer cdataBuffer = null; if (format.isTrimText()) { // concatenate adjacent text nodes together // so that whitespace trimming works properly Text lastTextNode = null; StringBuffer textBuffer = null; for (int i = 0, size = element.nodeCount(); i < size; i++) { Node node = element.node(i); if (node instanceof Text) { if (lastCDATANode != null) { if (cdataBuffer != null) { writeCDATA(cdataBuffer.toString()); cdataBuffer = null; } else { writeCDATA(lastCDATANode.getText()); } lastCDATANode = null; } if (lastTextNode == null) { lastTextNode = (Text) node; } else { if (textBuffer == null) { textBuffer = new StringBuffer(lastTextNode.getText()); } textBuffer.append(((Text) node).getText()); } } else if (node instanceof CDATA) { if (lastTextNode != null) { if (textBuffer != null) { writeString(textBuffer.toString()); textBuffer = null; } else { writeString(lastTextNode.getText()); } lastTextNode = null; } if (lastCDATANode == null) { lastCDATANode = (CDATA) node; } else { if (cdataBuffer == null) { cdataBuffer = new StringBuffer(lastCDATANode.getText()); } cdataBuffer.append(((CDATA) node).getText()); } } else { if (lastCDATANode != null) { if (cdataBuffer != null) { writeCDATA(cdataBuffer.toString()); cdataBuffer = null; } else { writeCDATA(lastCDATANode.getText()); } lastCDATANode = null; } if (lastTextNode != null) { if (textBuffer != null) { writeString(textBuffer.toString()); textBuffer = null; } else { writeString(lastTextNode.getText()); } lastTextNode = null; } writeNode(node); } } if (lastCDATANode != null) { if (cdataBuffer != null) { writeCDATA(cdataBuffer.toString()); cdataBuffer = null; } else { writeCDATA(lastCDATANode.getText()); } lastCDATANode = null; } if (lastTextNode != null) { if (textBuffer != null) { writeString(textBuffer.toString()); textBuffer = null; } else { writeString(lastTextNode.getText()); } lastTextNode = null; } } else { for (int i = 0, size = element.nodeCount(); i < size; i++) { Node node = element.node(i); if (node instanceof CDATA) { if (lastCDATANode == null) { lastCDATANode = (CDATA) node; } else { if (cdataBuffer == null) { cdataBuffer = new StringBuffer(lastCDATANode.getText()); } cdataBuffer.append(((CDATA) node).getText()); } } else { if (lastCDATANode != null) { if (cdataBuffer != null) { writeCDATA(cdataBuffer.toString()); cdataBuffer = null; } else { writeCDATA(lastCDATANode.getText()); } lastCDATANode = null; } writeNode(node); } } if (lastCDATANode != null) { if (cdataBuffer != null) { writeCDATA(cdataBuffer.toString()); cdataBuffer = null; } else { writeCDATA(lastCDATANode.getText()); } lastCDATANode = null; } } }
From source file:com.cladonia.xml.XMLFormatter.java
License:Mozilla Public License
protected void writeElement(Element element) throws IOException { if (DEBUG)/* w w w. j av a 2 s . c om*/ System.out.println("XMLFormatter.writeElement( " + element + ")"); // if ( indentMixed) { // super.writeElement( element); // } else { int size = element.nodeCount(); String qualifiedName = element.getQualifiedName(); boolean hasElement = false; boolean hasText = false; // first test to see if this element has mixed content, // if whitespace is significant ... for (int i = 0; i < size; i++) { Node node = element.node(i); if (node instanceof Element) { hasElement = true; } else if (node instanceof Text) { String text = node.getText(); if (text != null && text.trim().length() > 0) { hasText = true; } } } Attribute space = element.attribute("space"); boolean preserveSpace = false; if (space != null) { String prefix = space.getNamespacePrefix(); String value = space.getValue(); // System.out.println( "prefix = "+prefix+" value = "+value); if (prefix != null && "xml".equals(prefix) && "preserve".equals(value)) { preserveSpace = true; } } writePrintln(); indent(); writer.write("<"); writer.write(qualifiedName); int previouslyDeclaredNamespaces = namespaceStack.size(); Namespace ns = element.getNamespace(); if (isNamespaceDeclaration(ns)) { namespaceStack.push(ns); writeNamespace(ns); } // Print out additional namespace declarations for (int i = 0; i < size; i++) { Node node = element.node(i); if (node instanceof Namespace) { Namespace additional = (Namespace) node; if (isNamespaceDeclaration(additional)) { namespaceStack.push(additional); writeNamespace(additional); } } } writeAttributes(element); lastOutputNodeType = Node.ELEMENT_NODE; if (size <= 0) { writeEmptyElementClose(qualifiedName); } else { writer.write(">"); if (!hasElement && !preserveSpace) { // text only // we have at least one text node so lets assume // that its non-empty // System.out.println( "writeElementContent (Text) ..."); boolean previousWrapText = wrapText; wrapText = true; writeElementContent(element); wrapText = previousWrapText; } else if (preserveMixedContent && hasElement && hasText) { // preserve space // Mixed content // System.out.println( "writeMixedElementContent ..."); Node lastNode = writeMixedElementContent(element); } else if (preserveSpace) { // preserve space // Mixed content // System.out.println( "writePreserveElementContent ..."); Node lastNode = writePreservedElementContent(element); } else { // hasElement && !hasText // System.out.println( "writeElementContent (Element) ..."); boolean previousWrapText = wrapText; wrapText = true; ++indentLevel; writeElementContent(element); --indentLevel; wrapText = previousWrapText; writePrintln(); indent(); } writer.write("</"); writer.write(qualifiedName); writer.write(">"); // writePrintln(); } // remove declared namespaceStack from stack while (namespaceStack.size() > previouslyDeclaredNamespaces) { namespaceStack.pop(); } lastOutputNodeType = Node.ELEMENT_NODE; // } }
From source file:com.cladonia.xml.XMLFormatter.java
License:Mozilla Public License
protected void writeMixedElement(Element element) throws IOException { if (DEBUG)/*from ww w.j a va 2 s. com*/ System.out.println("XMLFormatter.writeMixedElement( " + element + ")"); int size = element.nodeCount(); String qualifiedName = element.getQualifiedName(); boolean previousTrim = format.isTrimText(); boolean previousWrapText = wrapText; if (isPreserveMixedContent()) { format.setTrimText(false); wrapText = false; } else { wrapText = true; } writer.write("<"); writer.write(qualifiedName); int previouslyDeclaredNamespaces = namespaceStack.size(); Namespace ns = element.getNamespace(); if (isNamespaceDeclaration(ns)) { namespaceStack.push(ns); writeNamespace(ns); } // Print out additional namespace declarations boolean textOnly = true; for (int i = 0; i < size; i++) { Node node = element.node(i); if (node instanceof Namespace) { Namespace additional = (Namespace) node; if (isNamespaceDeclaration(additional)) { namespaceStack.push(additional); writeNamespace(additional); } } else if (node instanceof Element) { textOnly = false; } } writeAttributes(element); lastOutputNodeType = Node.ELEMENT_NODE; if (size <= 0) { writeEmptyElementClose(qualifiedName); // writePrintln(); } else { writer.write(">"); writeMixedElementContent(element); writer.write("</"); writer.write(qualifiedName); writer.write(">"); } // remove declared namespaceStack from stack while (namespaceStack.size() > previouslyDeclaredNamespaces) { namespaceStack.pop(); } lastOutputNodeType = Node.ELEMENT_NODE; format.setTrimText(previousTrim); wrapText = previousWrapText; }
From source file:com.cladonia.xml.XMLFormatter.java
License:Mozilla Public License
protected void writePreservedElement(Element element) throws IOException { if (DEBUG)//from w ww .ja v a 2 s. c o m System.out.println("XMLFormatter.writePreservedElement( " + element + ")"); int size = element.nodeCount(); String qualifiedName = element.getQualifiedName(); boolean previousTrim = format.isTrimText(); boolean previousWrapText = wrapText; format.setTrimText(false); wrapText = false; boolean hasElement = false; boolean hasText = false; // first test to see if this element has mixed content, // if whitespace is significant ... for (int i = 0; i < size; i++) { Node node = element.node(i); if (node instanceof Element) { hasElement = true; } else if (node instanceof Text) { String text = node.getText(); if (text != null && text.trim().length() > 0) { hasText = true; } } } Attribute space = element.attribute("space"); boolean defaultSpace = false; if (space != null) { String prefix = space.getNamespacePrefix(); String value = space.getValue(); // System.out.println( "prefix = "+prefix+" value = "+value); if (prefix != null && "xml".equals(prefix) && "default".equals(value)) { defaultSpace = true; } } writer.write("<"); writer.write(qualifiedName); int previouslyDeclaredNamespaces = namespaceStack.size(); Namespace ns = element.getNamespace(); if (isNamespaceDeclaration(ns)) { namespaceStack.push(ns); writeNamespace(ns); } // Print out additional namespace declarations for (int i = 0; i < size; i++) { Node node = element.node(i); if (node instanceof Namespace) { Namespace additional = (Namespace) node; if (isNamespaceDeclaration(additional)) { namespaceStack.push(additional); writeNamespace(additional); } } } writeAttributes(element); lastOutputNodeType = Node.ELEMENT_NODE; if (size <= 0) { writeEmptyElementClose(qualifiedName); } else { writer.write(">"); if (preserveMixedContent && hasElement && hasText) { // mixed content // System.out.println( "writeMixedElementContent ..."); Node lastNode = writeMixedElementContent(element); } else if (!defaultSpace) { // preserve space // System.out.println( "writePreservedElementContent ..."); Node lastNode = writePreservedElementContent(element); } else { // System.out.println( "writeElementContent ..."); format.setTrimText(isTrimText()); boolean prevWrapText = wrapText; wrapText = true; writeElementContent(element); wrapText = prevWrapText; format.setTrimText(false); } writer.write("</"); writer.write(qualifiedName); writer.write(">"); } // remove declared namespaceStack from stack while (namespaceStack.size() > previouslyDeclaredNamespaces) { namespaceStack.pop(); } lastOutputNodeType = Node.ELEMENT_NODE; format.setTrimText(previousTrim); wrapText = previousWrapText; }
From source file:com.cladonia.xml.XMLFormatter.java
License:Mozilla Public License
/** Outputs the content of the given element. If whitespace trimming is * enabled then all adjacent text nodes are appended together before * the whitespace trimming occurs to avoid problems with multiple * text nodes being created due to text content that spans parser buffers * in a SAX parser./* w ww .j ava 2 s . co m*/ */ protected Node writeMixedElementContent(Element element) throws IOException { if (DEBUG) System.out.println("XMLFormatter.writeMixedElementContent( " + element + ")"); Node previousNode = null; boolean previousTrim = format.isTrimText(); boolean previousWrapText = wrapText; if (isPreserveMixedContent()) { format.setTrimText(false); wrapText = false; } else { wrapText = true; } for (int i = 0, size = element.nodeCount(); i < size; i++) { Node node = element.node(i); Node nextNode = null; if (i + 1 < size) { nextNode = element.node(i + 1); } if (node instanceof Text) { writeString(node.getText()); } else { writeMixedNode(node); } previousNode = node; } format.setTrimText(previousTrim); wrapText = previousWrapText; return previousNode; }