public void startElement( String namespaceURI, String localName, String qName, Attributes attributes) throws SAXException { try { charsAdded = false; writePrintln(); indent(); writer.write("<"); writer.write(qName); writeNamespaces(); writeAttributes(attributes); writer.write(">"); ++indentLevel; lastOutputNodeType = Node.ELEMENT_NODE; lastElementClosed = false; super.startElement(namespaceURI, localName, qName, attributes); } catch (IOException e) { handleException(e); } }
// Implementation methods // ------------------------------------------------------------------------- protected void writeElement(Element element) throws IOException { int size = element.nodeCount(); String qualifiedName = element.getQualifiedName(); 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 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; }
/** * Writes the opening tag of an {@link Element}, including its {@link Attribute}s but without its * content. * * @param element <code>Element</code> to output. * @throws IOException DOCUMENT ME! */ public void writeOpen(Element element) throws IOException { writer.write("<"); writer.write(element.getQualifiedName()); writeAttributes(element); writer.write(">"); }