|
Groovy example source code file (XmlNodePrinter.java)
This example Groovy source code file (XmlNodePrinter.java) is included in the DevDaily.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.
The Groovy XmlNodePrinter.java source code
/*
* Copyright 2003-2010 the original author or authors.
*
* 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 groovy.util;
import groovy.xml.QName;
import org.codehaus.groovy.runtime.InvokerHelper;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Prints a <code>groovy.util.Node (as used with XmlParser ) including all children in XML format.
* Typical usage:
* <pre>
* def xml = '<html><head><title>Title</title></head><body><h1>Header</h1></body></html>'
* def root = new XmlParser().parseText(xml)
* new XmlNodePrinter(preserveWhitespace:true).print(root.body[0])
* </pre>
* which when run produces this on stdout (or use your own <code>PrintWriter to direct elsewhere):
* <pre>
* <body>
* <h1>Header</h1>
* </body>
* </pre>
*
* @author Christian Stein
* @see groovy.util.NodePrinter
* @see groovy.xml.XmlUtil#serialize(Node)
*/
public class XmlNodePrinter {
protected final IndentPrinter out;
private String quote;
private boolean namespaceAware = true;
private boolean preserveWhitespace = false;
public XmlNodePrinter(PrintWriter out) {
this(out, " ");
}
public XmlNodePrinter(PrintWriter out, String indent) {
this(out, indent, "\"");
}
public XmlNodePrinter(PrintWriter out, String indent, String quote) {
this(new IndentPrinter(out, indent), quote);
}
public XmlNodePrinter(IndentPrinter out) {
this(out, "\"");
}
public XmlNodePrinter(IndentPrinter out, String quote) {
if (out == null) {
throw new IllegalArgumentException("Argument 'IndentPrinter out' must not be null!");
}
this.out = out;
this.quote = quote;
}
public XmlNodePrinter() {
this(new PrintWriter(new OutputStreamWriter(System.out)));
}
public void print(Node node) {
print(node, new NamespaceContext());
}
/**
* Check if namespace handling is enabled.
* Defaults to <code>true.
*
* @return true if namespace handling is enabled
*/
public boolean isNamespaceAware() {
return namespaceAware;
}
/**
* Enable and/or disable namespace handling.
*
* @param namespaceAware the new desired value
*/
public void setNamespaceAware(boolean namespaceAware) {
this.namespaceAware = namespaceAware;
}
/**
* Check if whitespace preservation is enabled.
* Defaults to <code>false.
*
* @return true if whitespaces are honoured when printing simple text nodes
*/
public boolean isPreserveWhitespace() {
return preserveWhitespace;
}
/**
* Enable and/or disable preservation of whitespace.
*
* @param preserveWhitespace the new desired value
*/
public void setPreserveWhitespace(boolean preserveWhitespace) {
this.preserveWhitespace = preserveWhitespace;
}
/**
* Get Quote to use when printing attributes.
*
* @return the quote character
*/
public String getQuote() {
return quote;
}
/**
* Set Quote to use when printing attributes.
*
* @param quote the quote character
*/
public void setQuote(String quote) {
this.quote = quote;
}
protected void print(Node node, NamespaceContext ctx) {
/*
* Handle empty elements like '<br/>', ' or ' .
*/
if (isEmptyElement(node)) {
printLineBegin();
out.print("<");
out.print(getName(node));
if (ctx != null) {
printNamespace(node, ctx);
}
printNameAttributes(node.attributes(), ctx);
out.print("/>");
printLineEnd();
out.flush();
return;
}
/*
* Hook for extra processing, e.g. GSP tag element!
*/
if (printSpecialNode(node)) {
out.flush();
return;
}
/*
* Handle normal element like <html> ... |