/*
* Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
*
* This software is open source.
* See the bottom of this file for the licence.
*/
package org.dom4j.io;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import org.dom4j.Attribute;
import org.dom4j.CDATA;
import org.dom4j.Comment;
import org.dom4j.Document;
import org.dom4j.DocumentType;
import org.dom4j.Element;
import org.dom4j.Entity;
import org.dom4j.Namespace;
import org.dom4j.Node;
import org.dom4j.ProcessingInstruction;
import org.dom4j.Text;
import org.dom4j.tree.NamespaceStack;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader;
import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.helpers.XMLFilterImpl;
/**
* <p>
* <code>XMLWriter</code> takes a DOM4J tree and formats it to a stream as
* XML. It can also take SAX events too so can be used by SAX clients as this
* object implements the {@link org.xml.sax.ContentHandler}and {@link
* LexicalHandler} interfaces. as well. This formatter performs typical document
* formatting. The XML declaration and processing instructions are always on
* their own lines. An {@link OutputFormat}object can be used to define how
* whitespace is handled when printing and allows various configuration options,
* such as to allow suppression of the XML declaration, the encoding declaration
* or whether empty documents are collapsed.
* </p>
*
* <p>
* There are <code>write(...)</code> methods to print any of the standard
* DOM4J classes, including <code>Document</code> and <code>Element</code>,
* to either a <code>Writer</code> or an <code>OutputStream</code>.
* Warning: using your own <code>Writer</code> may cause the writer's
* preferred character encoding to be ignored. If you use encodings other than
* UTF8, we recommend using the method that takes an OutputStream instead.
* </p>
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
* @author Joseph Bowbeer
* @version $Revision: 1.83.2.2 $
*/
public class XMLWriter extends XMLFilterImpl implements LexicalHandler {
private static final String PAD_TEXT = " ";
protected static final String[] LEXICAL_HANDLER_NAMES = {
"http://xml.org/sax/properties/lexical-handler",
"http://xml.org/sax/handlers/LexicalHandler"};
protected static final OutputFormat DEFAULT_FORMAT = new OutputFormat();
/** Should entityRefs by resolved when writing ? */
private boolean resolveEntityRefs = true;
/**
* Stores the last type of node written so algorithms can refer to the
* previous node type
*/
protected int lastOutputNodeType;
/**
* Stores if the last written element node was a closing tag or an opening
* tag.
*/
private boolean lastElementClosed = false;
/** Stores the xml:space attribute value of preserve for whitespace flag */
protected boolean preserve = false;
/** The Writer used to output to */
protected Writer writer;
/** The Stack of namespaceStack written so far */
private NamespaceStack namespaceStack = new NamespaceStack();
/** The format used by this writer */
private OutputFormat format;
/** whether we should escape text */
private boolean escapeText = true;
/**
* The initial number of indentations (so you can print a whole document
* indented, if you like)
*/
private int indentLevel = 0;
/** buffer used when escaping strings */
private StringBuffer buffer = new StringBuffer();
/**
* whether we have added characters before from the same chunk of characters
*/
private boolean charsAdded = false;
private char lastChar;
/** Whether a flush should occur after writing a document */
private boolean autoFlush;
/** Lexical handler we should delegate to */
private LexicalHandler lexicalHandler;
/**
* Whether comments should appear inside DTD declarations - defaults to
* false
*/
private boolean showCommentsInDTDs;
/** Is the writer curerntly inside a DTD definition? */
private boolean inDTD;
/** The namespaces used for the current element when consuming SAX events */
private Map namespacesMap;
/**
* what is the maximum allowed character code such as 127 in US-ASCII (7
* bit) or 255 in ISO- (8 bit) or -1 to not escape any characters (other
* than the special XML characters like < > &)
*/
private int maximumAllowedCharacter;
public XMLWriter(Writer writer) {
this(writer, DEFAULT_FORMAT);
}
public XMLWriter(Writer writer, OutputFormat format) {
this.writer = writer;
this.format = format;
namespaceStack.push(Namespace.NO_NAMESPACE);
}
public XMLWriter() {
this.format = DEFAULT_FORMAT;
this.writer = new BufferedWriter(new OutputStreamWriter(System.out));
this.autoFlush = true;
namespaceStack.push(Namespace.NO_NAMESPACE);
}
public XMLWriter(OutputStream out) throws UnsupportedEncodingException {
this.format = DEFAULT_FORMAT;
this.writer = createWriter(out, format.getEncoding());
this.autoFlush = true;
namespaceStack.push(Namespace.NO_NAMESPACE);
}
public XMLWriter(OutputStream out, OutputFormat format)
throws UnsupportedEncodingException {
this.format = format;
this.writer = createWriter(out, format.getEncoding());
this.autoFlush = true;
namespaceStack.push(Namespace.NO_NAMESPACE);
}
public XMLWriter(OutputFormat format) throws UnsupportedEncodingException {
this.format = format;
this.writer = createWriter(System.out, format.getEncoding());
this.autoFlush = true;
namespaceStack.push(Namespace.NO_NAMESPACE);
}
public void setWriter(Writer writer) {
this.writer = writer;
this.autoFlush = false;
}
public void setOutputStream(OutputStream out)
throws UnsupportedEncodingException {
this.writer = createWriter(out, format.getEncoding());
this.autoFlush = true;
}
/**
* DOCUMENT ME!
*
* @return true if text thats output should be escaped. This is enabled by
* default. It could be disabled if the output format is textual,
* like in XSLT where we can have xml, html or text output.
*/
public boolean isEscapeText() {
return escapeText;
}
/**
* Sets whether text output should be escaped or not. This is enabled by
* default. It could be disabled if the output format is textual, like in
* XSLT where we can have xml, html or text output.
*
* @param escapeText
* DOCUMENT ME!
*/
public void setEscapeText(boolean escapeText) {
this.escapeText = escapeText;
}
/**
* Set the initial indentation level. This can be used to output a document
* (or, more likely, an element) starting at a given indent level, so it's
* not always flush against the left margin. Default: 0
*
* @param indentLevel
* the number of indents to start with
*/
public void setIndentLevel(int indentLevel) {
this.indentLevel = indentLevel;
}
/**
* Returns
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
提供dom4j的源代码 (179个子文件)
package.html 395B
package.html 261B
package.html 259B
package.html 219B
overview.html 216B
package.html 202B
package.html 195B
package.html 182B
package.html 177B
package.html 173B
package.html 165B
package.html 155B
package.html 125B
XMLWriter.java 57KB
AbstractElement.java 46KB
ConcurrentReaderHashMap.java 43KB
SAXReader.java 33KB
SAXContentHandler.java 32KB
DefaultElement.java 30KB
SAXWriter.java 29KB
HTMLWriter.java 29KB
Element.java 28KB
STAXEventReader.java 24KB
STAXEventWriter.java 22KB
SchemaParser.java 21KB
SAXEventRecorder.java 20KB
Node.java 20KB
OutputFormat.java 19KB
DOMNodeHelper.java 18KB
XPP3Reader.java 18KB
JAXBModifier.java 18KB
SAXModifier.java 15KB
XPPReader.java 15KB
DOMWriter.java 15KB
DocumentFactory.java 15KB
NamespaceStack.java 15KB
AbstractBranch.java 14KB
JAXBReader.java 14KB
DocumentHelper.java 14KB
DOMElement.java 14KB
Branch.java 12KB
DefaultXPath.java 12KB
XPath.java 12KB
DOMDocument.java 11KB
JAXBWriter.java 11KB
NodeComparator.java 11KB
DefaultDocument.java 11KB
Stylesheet.java 11KB
DOMReader.java 11KB
XMLTableDefinition.java 10KB
ProxyXmlStartTag.java 10KB
IndexedElement.java 10KB
Mode.java 10KB
Rule.java 9KB
QName.java 9KB
SAXModifyContentHandler.java 9KB
QNameCache.java 9KB
DispatchHandler.java 9KB
DOMCDATA.java 9KB
DOMText.java 9KB
AbstractDocument.java 9KB
DatatypeDocumentFactory.java 8KB
AbstractNode.java 8KB
Namespace.java 8KB
RuleManager.java 7KB
DOMDocumentFactory.java 7KB
DOMComment.java 7KB
DatatypeAttribute.java 7KB
AbstractProcessingInstruction.java 7KB
NamespaceCache.java 7KB
DOMProcessingInstruction.java 7KB
DOMAttribute.java 7KB
ProxyDocumentFactory.java 7KB
DOMEntityReference.java 7KB
DOMDocumentType.java 7KB
DatatypeElement.java 7KB
XMLTableColumnDefinition.java 7KB
SAXHelper.java 7KB
BackedList.java 7KB
BeanMetaData.java 7KB
SAXValidator.java 7KB
ElementStack.java 6KB
BeanElement.java 6KB
ContentListFacade.java 6KB
XMLTableModel.java 6KB
Document.java 6KB
AbstractDocumentType.java 6KB
DocumentSource.java 6KB
BeanAttributeList.java 6KB
DOMNamespace.java 6KB
BranchTreeNode.java 6KB
AbstractAttribute.java 6KB
AttributeDecl.java 6KB
DatatypeElementFactory.java 6KB
BaseElement.java 6KB
XMLErrorHandler.java 6KB
Attribute.java 6KB
SAXModifyElementHandler.java 6KB
PruningElementStack.java 5KB
XPathPattern.java 5KB
共 179 条
- 1
- 2
jim1232323
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 课高分程设计-基于C++实现的民航飞行与地图简易管理系统-南京航空航天大学
- 航天器遥测数据故障检测系统python源码+文档说明+数据库(课程设计)
- 北京航空航天大学操作系统课设+ppt+实验报告
- 基于Vue+Echarts实现风力发电机中传感器的数据展示监控可视化系统+源代码+文档说明(高分课程设计)
- 基于单片机的风力发电机转速控制源码
- 基于C++实现的风力发电气动平衡监测系统+源代码+测量数据(高分课程设计)
- 毕业设计- 基于STM32F103C8T6 单片机,物联网技术的太阳能发电装置+源代码+文档说明+架构图+界面截图
- 基于 LSTM(长短期记忆)(即改进的循环神经网络)预测风力发电厂中风力涡轮机产生的功率+源代码+文档说明
- 基于stm32f103+空心杯电机+oled按键+运动算法
- 《CKA/CKAD应试指南/从docker到kubernetes 完全攻略》学习笔记 第1章docker基础(1.1-1.4)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页