📄 xmlwriter.java
字号:
package org.dbunit.util.xml;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org /)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Commons" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Turbine", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org />.
*/
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Stack;
/**
* Makes writing XML much much easier.
* Improved from
* <a href="http://builder.com.com/article.jhtml?id=u00220020318yan01.htm&page=1&vf=tt">article</a>
*
* @author <a href="mailto:bayard@apache.org">Henri Yandell</a>
* @author <a href="mailto:pete@fingertipsoft.com">Peter Cassetta</a>
* @version 1.0
*/
public class XmlWriter
{
/**
* Logger for this class
*/
private static final Logger logger = LoggerFactory.getLogger(XmlWriter.class);
private Writer out; // underlying writer
private String encoding;
private Stack stack = new Stack(); // of xml element names
private StringBuffer attrs; // current attribute string
private boolean empty; // is the current node empty
private boolean closed = true; // is the current node closed...
private boolean pretty = true; // is pretty printing enabled?
private boolean wroteText = false; // was text the last thing output?
private String indent = " "; // output this to indent one level when pretty printing
private String newline = "\n"; // output this to end a line when pretty printing
/**
* Create an XmlWriter on top of an existing java.io.Writer.
*/
public XmlWriter(Writer writer)
{
this(writer, null);
}
/**
* Create an XmlWriter on top of an existing java.io.Writer.
*/
public XmlWriter(Writer writer, String encoding)
{
setWriter(writer, encoding);
}
/**
* Turn pretty printing on or off.
* Pretty printing is enabled by default, but it can be turned off
* to generate more compact XML.
*
* @param enable true to enable, false to disable pretty printing.
*/
public void enablePrettyPrint(boolean enable)
{
logger.debug("enablePrettyPrint(enable=" + enable + ") - start");
this.pretty = enable;
}
/**
* Specify the string to prepend to a line for each level of indent.
* It is 2 spaces (" ") by default. Some may prefer a single tab ("\t")
* or a different number of spaces. Specifying an empty string will turn
* off indentation when pretty printing.
*
* @param indent representing one level of indentation while pretty printing.
*/
public void setIndent(String indent)
{
logger.debug("setIndent(indent=" + indent + ") - start");
this.indent = indent;
}
/**
* Specify the string used to terminate each line when pretty printing.
* It is a single newline ("\n") by default. Users who need to read
* generated XML documents in Windows editors like Notepad may wish to
* set this to a carriage return/newline sequence ("\r\n"). Specifying
* an empty string will turn off generation of line breaks when pretty
* printing.
*
* @param newline representing the newline sequence when pretty printing.
*/
public void setNewline(String newline)
{
logger.debug("setNewline(newline=" + newline + ") - start");
this.newline = newline;
}
/**
* A helper method. It writes out an element which contains only text.
*
* @param name String name of tag
* @param text String of text to go inside the tag
*/
public XmlWriter writeElementWithText(String name, String text) throws IOException
{
logger.debug("writeElementWithText(name=" + name + ", text=" + text + ") - start");
writeElement(name);
writeText(text);
return endElement();
}
/**
* A helper method. It writes out empty entities.
*
* @param name String name of tag
*/
public XmlWriter writeEmptyElement(String name) throws IOException
{
logger.debug("writeEmptyElement(name=" + name + ") - start");
writeElement(name);
return endElement();
}
/**
* Begin to write out an element. Unlike the helper tags, this tag
* will need to be ended with the endElement method.
*
* @param name String name of tag
*/
public XmlWriter writeElement(String name) throws IOException
{
logger.debug("writeElement(name=" + name + ") - start");
return openElement(name);
}
/**
* Begin to output an element.
*
* @param name name of element.
*/
private XmlWriter openElement(String name) throws IOException
{
logger.debug("openElement(name=" + name + ") - start");
boolean wasClosed = this.closed;
closeOpeningTag();
this.closed = false;
if (this.pretty)
{
// ! wasClosed separates adjacent opening tags by a newline.
// this.wroteText makes sure an element embedded within the text of
// its parent element begins on a new line, indented to the proper
// level. This solves only part of the problem of pretty printing
// entities which contain both text and child entities.
if (!wasClosed || this.wroteText)
{
this.out.write(newline);
}
for (int i = 0; i < this.stack.size(); i++)
{
this.out.write(indent); // Indent opening tag to proper level
}
}
this.out.write("<");
this.out.write(name);
stack.add(name);
this.empty = true;
this.wroteText = false;
return this;
}
// close off the opening tag
private void closeOpeningTag() throws IOException
{
logger.debug("closeOpeningTag() - start");
if (!this.closed)
{
writeAttributes();
this.closed = true;
this.out.write(">");
}
}
// write out all current attributes
private void writeAttributes() throws IOException
{
logger.debug("writeAttributes() - start");
if (this.attrs != null)
{
this.out.write(this.attrs.toString());
this.attrs.setLength(0);
this.empty = false;
}
}
/**
* Write an attribute out for the current element.
* Any xml characters in the value are escaped.
* Currently it does not actually throw the exception, but
* the api is set that way for future changes.
*
* @param attr name of attribute.
* @param value value of attribute.
*/
public XmlWriter writeAttribute(String attr, String value) throws IOException
{
logger.debug("writeAttribute(attr=" + attr + ", value=" + value + ") - start");
// maintain api
if (false) throw new IOException();
if (this.attrs == null)
{
this.attrs = new StringBuffer();
}
this.attrs.append(" ");
this.attrs.append(attr);
this.attrs.append("=\"");
this.attrs.append(escapeXml(value));
this.attrs.append("\"");
return this;
}
/**
* End the current element. This will throw an exception
* if it is called when there is not a currently open
* element.
*/
public XmlWriter endElement() throws IOException
{
logger.debug("endElement() - start");
if (this.stack.empty())
{
throw new IOException("Called endElement too many times. ");
}
String name = (String)this.stack.pop();
if (name != null)
{
if (this.empty)
{
writeAttributes();
this.out.write("/>");
}
else
{
if (this.pretty && !this.wroteText)
{
for (int i = 0; i < this.stack.size(); i++)
{
this.out.write(indent); // Indent closing tag to proper level
}
}
this.out.write("</");
this.out.write(name);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -