/*
* 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
没有合适的资源?快使用搜索试试~ 我知道了~
dom4.jar包,高效xml解析
共2648个文件
html:1926个
java:315个
gif:137个
5星 · 超过95%的资源 需积分: 10 71 下载量 191 浏览量
2008-12-07
13:54:39
上传
评论
收藏 14.37MB RAR 举报
温馨提示
dom4j是现在见到的解析xml效率最高的jar包,并且支持XPath定位,xml schema验证等功能。
资源推荐
资源详情
资源评论
收起资源包目录
dom4.jar包,高效xml解析 (2648个子文件)
maven-classic.css 14KB
maven-classic.css 13KB
tigris.css 8KB
style.css 4KB
maven.css 3KB
javadoc-style.css 3KB
javadoc-style.css 3KB
maven-base.css 2KB
ns4_only.css 2KB
maven-base.css 2KB
maven-theme.css 2KB
main.css 2KB
stylesheet.css 2KB
maven-theme.css 2KB
stylesheet.css 1KB
stylesheet.css 1KB
stylesheet.css 1KB
stylesheet.css 1KB
javadoc.css 1KB
source-viewer.css 1KB
sortabletable.css 930B
tooltip.css 796B
maven_ns4_only.css 450B
help.css 264B
print.css 211B
print.css 168B
nitf.dtd 81KB
xhtml1-strict.dtd 27KB
repository.dtd 876B
personal.dtd 669B
personal.dtd 669B
moreovernews.dtd 556B
sample.dtd 181B
address.dtd 119B
xhtml-symbol.ent 14KB
xhtml-lat1.ent 12KB
xhtml-special.ent 4KB
faq.fml 22KB
help_logo.gif 2KB
help_logo.gif 2KB
icon_members_lrg.gif 2KB
icon_members_lrg.gif 2KB
icon_folder_lrg.gif 2KB
icon_folder_lrg.gif 2KB
icon_error_lrg.gif 1KB
icon_error_lrg.gif 1KB
icon_usergroups_lrg.gif 1KB
icon_usergroups_lrg.gif 1KB
icon_success_lrg.gif 1KB
icon_success_lrg.gif 1KB
icon_warning_lrg.gif 1KB
icon_warning_lrg.gif 1KB
icon_help_lrg.gif 1KB
icon_help_lrg.gif 1KB
icon_info_lrg.gif 1KB
icon_info_lrg.gif 1KB
icon_doc_lrg.gif 1KB
icon_doc_lrg.gif 1KB
icon_alert.gif 1KB
icon_alert.gif 1KB
icon_arrowfolder1_sml.gif 1KB
icon_arrowfolder1_sml.gif 1KB
icon_arrowfolder2_sml.gif 1KB
icon_arrowfolder2_sml.gif 1KB
icon_arrowusergroups2_sml.gif 1KB
logo.gif 1KB
icon_arrowusergroups2_sml.gif 1KB
logo.gif 1KB
icon_arrowmembers1_sml.gif 1KB
icon_arrowmembers1_sml.gif 1KB
icon_arrowmembers2_sml.gif 1KB
icon_arrowusergroups1_sml.gif 1KB
icon_arrowmembers2_sml.gif 1KB
icon_arrowusergroups1_sml.gif 1KB
icon_usergroups_sml.gif 1KB
icon_usergroups_sml.gif 1KB
icon_members_sml.gif 1023B
icon_members_sml.gif 1023B
icon_help_sml.gif 1019B
icon_help_sml.gif 1019B
icon_error_sml.gif 1010B
icon_error_sml.gif 1010B
icon_success_sml.gif 990B
icon_success_sml.gif 990B
pdf.gif 950B
pdf.gif 950B
poweredby_036.gif 808B
poweredby_036.gif 808B
icon_waste_lrg.gif 780B
icon_waste_lrg.gif 780B
jaxen.gif 735B
logo.gif 725B
product_logo.gif 680B
product_logo.gif 680B
icon_arrowfolderopen2_sml.gif 664B
icon_arrowfolderopen2_sml.gif 664B
icon_folder_sml.gif 634B
icon_folder_sml.gif 634B
icon_arrowwaste2_sml.gif 609B
icon_arrowwaste2_sml.gif 609B
共 2648 条
- 1
- 2
- 3
- 4
- 5
- 6
- 27
资源评论
- Neil_Learning_Space2013-09-01可以用,蛮好
- jy101916242012-11-29很好,可以用
- chenrl20052013-11-27很好用,谢谢
aixiang_123
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- DatabaseWatermarkingBasedonTextFormat
- -移动通信-网络课程设计与研究
- 基于Python的PCA人脸识别算法的原理及实现代码+文档详解.zip
- 甘肃移动全业务工程建设项目管理流程优化研究
- 549springboot + vue 民宿管理平台.zip (可运行源码+数据库文件+文档)
- ZArchiver.Pro_0.9.5.apk
- vmware环境配置.mp4
- 548springboot + vue 大学生社团活动平台.zip(可运行源码+数据库文件+文档)
- 微信小程序 辩论倒计时小程序源码 作业设计demo 计算机专业参考
- 深入探究文件IO,嵌入式Linux
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功