⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dom4jxmlwriter.java

📁 一个“对象--XML 映射”(Object-Xml Mapping) 的类库。 它的目的是帮助开发者方便、快速的从XML 文件构建出Java 对象
💻 JAVA
字号:
/**
 * @author 沈东良 <a href="mailto:shendl_s@hotmail.com">shendl_s@hotmail.com</a>
 * 2007-1-24 下午07:05:31
 */
package net.sf.oxmled.xml.util.dom4j.override;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.Text;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

/**
 * @author 沈东良 <a href="mailto:shendl_s@hotmail.com">shendl_s@hotmail.com</a>
 *         2007-1-24 下午07:05:31 这个类是为了修正dom4j1.61版本de XMLWriter的一个错误。这个错误导致无法输出
 *         美观格式的 xml!
 */
public class Dom4jXMLWriter extends XMLWriter {
	private static final String PAD_TEXT = " ";

	/** The format used by this writer */
	/**
	 * 和基类饮用一样的对象
	 */
	private OutputFormat format;

	protected static final OutputFormat DEFAULT_FORMAT = new OutputFormat();

	/**
	 * Outputs the content of the given element. If whitespace trimming is
	 * enabled then all adjacent text nodes are appended together before the
	 * whitespace trimming occurs to avoid problems with multiple text nodes
	 * being created due to text content that spans parser buffers in a SAX
	 * parser.
	 * 
	 * @param element
	 *            DOCUMENT ME!
	 * 
	 * @throws IOException
	 *             DOCUMENT ME!
	 */

	//@Override
	protected void writeElementContent(Element element) throws IOException {
		// OutputFormat format;
		boolean trim = format.isTrimText();
		boolean oldPreserve = preserve;
		// 如果去除空格
		if (trim) { // verify we have to before more expensive test
			preserve = isElementSpacePreserved(element);
			trim = !preserve;
		}

		if (trim) {
			// concatenate adjacent text nodes together
			// so that whitespace trimming works properly
			Text lastTextNode = null;
			StringBuffer buff = null;
			boolean textOnly = true;

			for (int i = 0, size = element.nodeCount(); i < size; i++) {
				Node node = element.node(i);

				if (node instanceof Text) {
					if (lastTextNode == null) {
						lastTextNode = (Text) node;
					} else {
						if (buff == null) {
							buff = new StringBuffer(lastTextNode.getText());
						}

						buff.append(((Text) node).getText());
					}
				} else {
					if (!textOnly && format.isPadText()) {
						// only add the PAD_TEXT if the text itself starts with
						// whitespace
						char firstChar = 'a';
						if (buff != null) {
							firstChar = buff.charAt(0);
						} else if (lastTextNode != null) {
							firstChar = lastTextNode.getText().charAt(0);
						}

						if (Character.isWhitespace(firstChar)) {
							writer.write(PAD_TEXT);
						}
					}

					if (lastTextNode != null) {
						if (buff != null) {
							writeString(buff.toString());
							buff = null;
						} else {
							writeString(lastTextNode.getText());
						}
						// 如果去除空格
						if (format.isPadText()) {
							// only add the PAD_TEXT if the text itself ends
							// with whitespace
							char lastTextChar = 'a';
							if (buff != null) {
								lastTextChar = buff.charAt(buff.length() - 1);
							} else if (lastTextNode != null) {
								// 出错的地方
								/**
								 * 
								 * 
								 * 
								 */
								String txt = lastTextNode.getText();
								if (txt.length() != 0) {
									lastTextChar = txt.charAt(txt.length() - 1);
								} else {
									// 如果是空,最后一个字符是 空格
									lastTextChar = ' ';
								}
							}

							if (Character.isWhitespace(lastTextChar)) {
								writer.write(PAD_TEXT);
							}
						}

						lastTextNode = null;
					}

					textOnly = false;
					writeNode(node);
				}
			}

			if (lastTextNode != null) {
				if (!textOnly && format.isPadText()) {
					// only add the PAD_TEXT if the text itself starts with
					// whitespace
					char firstChar = 'a';
					if (buff != null) {
						firstChar = buff.charAt(0);
					} else {
						firstChar = lastTextNode.getText().charAt(0);
					}

					if (Character.isWhitespace(firstChar)) {
						writer.write(PAD_TEXT);
					}
				}

				if (buff != null) {
					writeString(buff.toString());
					buff = null;
				} else {
					writeString(lastTextNode.getText());
				}

				lastTextNode = null;
			}
		} else {
			Node lastTextNode = null;

			for (int i = 0, size = element.nodeCount(); i < size; i++) {
				Node node = element.node(i);

				if (node instanceof Text) {
					writeNode(node);
					lastTextNode = node;
				} else {
					if ((lastTextNode != null) && format.isPadText()) {
						// only add the PAD_TEXT if the text itself ends with
						// whitespace
						String txt = lastTextNode.getText();
						char lastTextChar = txt.charAt(txt.length() - 1);

						if (Character.isWhitespace(lastTextChar)) {
							writer.write(PAD_TEXT);
						}
					}

					writeNode(node);

					// if ((lastTextNode != null) && format.isPadText()) {
					// writer.write(PAD_TEXT);
					// }

					lastTextNode = null;
				}
			}
		}

		preserve = oldPreserve;
	}

	/**
	 * 
	 */
	public Dom4jXMLWriter() {
		/*
		 * 
		 */
	}

	/**
	 * @param writer
	 */
	public Dom4jXMLWriter(Writer writer) {
		super(writer);
		this.setFormat(Dom4jXMLWriter.DEFAULT_FORMAT);
		/*
		 * 
		 */
	}

	/**
	 * @param out
	 * @throws UnsupportedEncodingException
	 */
	public Dom4jXMLWriter(OutputStream out) throws UnsupportedEncodingException {

		super(out);
		this.setFormat(Dom4jXMLWriter.DEFAULT_FORMAT);
		/*
		 * 
		 */
	}

	/**
	 * @param format
	 * @throws UnsupportedEncodingException
	 */
	public Dom4jXMLWriter(OutputFormat format)
			throws UnsupportedEncodingException {

		super(format);
		this.setFormat(format);
		/*
		 * 
		 */
	}

	/**
	 * @param writer
	 * @param format
	 */
	public Dom4jXMLWriter(Writer writer, OutputFormat format) {
		super(writer, format);
		this.setFormat(format);
		/*
		 * 
		 */
	}

	/**
	 * @param out
	 * @param format
	 * @throws UnsupportedEncodingException
	 */
	public Dom4jXMLWriter(OutputStream out, OutputFormat format)
			throws UnsupportedEncodingException {
		super(out, format);
		this.setFormat(format);
		/*
		 * 
		 */
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		/*
		 * 
		 */

	}

	/**
	 * @return format
	 */
	public OutputFormat getFormat() {
		return format;
	}

	/**
	 * @param format
	 *            要设置的 format
	 */
	public void setFormat(OutputFormat format) {
		this.format = format;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -