org.attoparser.dom.DOMWriter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of attoparser Show documentation
Show all versions of attoparser Show documentation
Powerful, fast and easy to use HTML and XML parser for Java
/*
* =============================================================================
*
* Copyright (c) 2012-2014, The ATTOPARSER team (http://www.attoparser.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* =============================================================================
*/
package org.attoparser.dom;
import java.io.IOException;
import java.io.Writer;
import java.util.Map;
/**
*
* Static utility class able to write a DOM tree (or a fragment of it) as markup.
*
*
* @author Daniel Fernández
*
* @since 2.0.0
*
*/
public final class DOMWriter {
public static void write(final INode node, final Writer writer) throws IOException {
if (node == null) {
return;
}
if (node instanceof Text) {
writeText((Text)node, writer);
return;
}
if (node instanceof Element) {
writeElement((Element)node, writer);
return;
}
if (node instanceof Comment) {
writeComment((Comment)node, writer);
return;
}
if (node instanceof CDATASection) {
writeCDATASection((CDATASection)node, writer);
return;
}
if (node instanceof DocType) {
writeDocType((DocType)node, writer);
return;
}
if (node instanceof Document) {
writeDocument((Document)node, writer);
return;
}
if (node instanceof XmlDeclaration) {
writeXmlDeclaration((XmlDeclaration)node, writer);
return;
}
if (node instanceof ProcessingInstruction) {
writeProcessingInstruction((ProcessingInstruction)node, writer);
return;
}
}
public static void writeCDATASection(final CDATASection cdataSection, final Writer writer)
throws IOException{
writer.write("");
}
public static void writeComment(final Comment comment, final Writer writer) throws IOException {
writer.write("");
}
public static void writeDocType(final DocType docType, final Writer writer) throws IOException {
writer.write("');
}
public static void writeDocument(final Document document, final Writer writer) throws IOException {
if (!document.hasChildren()) {
return;
}
for (final INode child : document.getChildren()) {
write(child, writer);
}
}
public static void writeElement(final Element element, final Writer writer) throws IOException {
writer.write('<');
writer.write(element.getElementName());
if (element.hasAttributes()) {
final Map attributes = element.getAttributeMap();
for (final Map.Entry attributeEntry : attributes.entrySet()) {
writer.write(' ');
writer.write(attributeEntry.getKey());
writer.write('=');
writer.write('"');
writer.write(attributeEntry.getValue());
writer.write('"');
}
}
if (!element.hasChildren()) {
writer.write('/');
writer.write('>');
return;
}
writer.write('>');
for (final INode child : element.getChildren()) {
write(child, writer);
}
writer.write('<');
writer.write('/');
writer.write(element.getElementName());
writer.write('>');
}
public static void writeProcessingInstruction(
final ProcessingInstruction processingInstruction, final Writer writer)
throws IOException {
writer.write('<');
writer.write('?');
writer.write(processingInstruction.getTarget());
final String content = processingInstruction.getContent();
if (content != null) {
writer.write(' ');
writer.write(content);
}
writer.write('?');
writer.write('>');
}
public static void writeText(final Text text, final Writer writer) throws IOException {
validateNotNull(text, "Text node cannot be null");
validateNotNull(writer, "Writer cannot be null");
writer.write(text.getContent());
}
public static void writeXmlDeclaration(final XmlDeclaration xmlDeclaration, final Writer writer) throws IOException {
validateNotNull(xmlDeclaration, "XML declaration cannot be null");
validateNotNull(writer, "Writer cannot be null");
writer.write("');
}
private static void validateNotNull(final Object obj, final String message) {
if (obj == null) {
throw new IllegalArgumentException(message);
}
}
private DOMWriter() {
super();
}
}