xmlwriter.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 834 行 · 第 1/2 页
JAVA
834 行
/* * Copyright (c) 1998-2005 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * Free SoftwareFoundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * * @author Sam */package com.caucho.vfs;import java.io.PrintWriter;import java.io.Writer;import java.util.HashMap;// TODO: get rid of set/getContentType, use getStrategyForContentType(...) and setStrategy(...)// TODO: trap all print(...) for xml escaping, indenting// TODO: capture all \n and \r\n and make them do println()// TODO: clean up flags now that patterns are known// TODO: CDATA// TODO: startDocument, endDocument (<?xml with charset and DOCTYPE)// TODO: li review (including test case), because of bug in some browser (dt,dd?)public class XmlWriter extends PrintWriter{ public final static Strategy XML = new Xml(); public final static Strategy XHTML = new Xhtml(); public final static Strategy HTML = new Html(); private boolean _isIndenting = false; private int _indent = 0; private boolean _isElementOpen; private boolean _isElementOpenNeedsNewline; private String _openElementName; private Strategy _strategy = XML; private String _contentType = "text/xml"; private String _characterEncoding; private boolean _isNewLine = true; public XmlWriter(Writer out) { super(out); } public String getContentType() { return _contentType; } /** * Default is "text/xml". */ public void setContentType(String contentType) { _contentType = contentType; if (_contentType.equals("text/xml")) _strategy = XML; if (_contentType.equals("application/xml")) _strategy = XML; else if (_contentType.equals("text/xhtml")) _strategy = XHTML; else if (_contentType.equals("application/xhtml+xml")) _strategy = XHTML; else if (_contentType.equals("text/html")) _strategy = HTML; else _strategy = XML; } public void setStrategy(Strategy strategy) { _strategy = strategy; } public void setIndenting(boolean isIndenting) { _isIndenting = isIndenting; } public boolean isIndenting() { return _isIndenting; } /** * Default is "UTF-8". */ public void setCharacterEncoding(String characterEncoding) { _characterEncoding = characterEncoding; } public String getCharacterEncoding() { return _characterEncoding; } private boolean closeElementIfNeeded(boolean isEnd) { if (_isElementOpen) { _isElementOpen = false; _strategy.closeElement(this, _openElementName, isEnd); if (_isElementOpenNeedsNewline) { _isElementOpenNeedsNewline = false; softPrintln(); } return true; } return false; } private void startElement(String name, boolean isLineBefore, boolean isLineAfter) { closeElementIfNeeded(false); if (isLineBefore) softPrintln(); _openElementName = name; _strategy.openElement(this, name); _isElementOpen = true; _isElementOpenNeedsNewline = isLineAfter; if (_isIndenting) _indent++; } private void endElement(String name, boolean isLineBefore, boolean isLineAfter) { if (_isIndenting) _indent--; if (!closeElementIfNeeded(true)) { if (isLineBefore) softPrintln(); _strategy.endElement(this, name); } if (isLineAfter) softPrintln(); } /** * Start an element. */ public void startElement(String name) { startElement(name, false, false); } /** * End an element. */ public void endElement(String name) { endElement(name, false, false); } /** * Start an element where the opening tag is on it's own line, the content * is on it's own line, and the closing tag is on it's own line. */ public void startBlockElement(String name) { startElement(name, true, true); } /** * End an element where the opening tag is on it's own line, the content * is on it's own line, and the closing tag is on it's own line. */ public void endBlockElement(String name) { endElement(name, true, true); } /** * Start an element where the opening tag, content, and closing tags are on * a single line of their own. */ public void startLineElement(String name) { startElement(name, true, false); } /** * End an element where the opening tag, content, and closing tags are on * a single line of their own. */ public void endLineElement(String name) { endElement(name, false, true); } /** * Convenience method, same as doing a startElement() and then immediately * doing an endElement(). */ public void writeElement(String name) { startElement(name); endElement(name); } /** * Convenience method, same as doing a startLineElement() and then immediately * doing an endLineElement(). */ public void writeLineElement(String name) { startLineElement(name); endLineElement(name); } /** * Convenience method, same as doing a startBlockElement() and then immediately * doing an endBlockElement(). */ public void writeBlockElement(String name) { startBlockElement(name); endBlockElement(name); } /** * Convenience method, same as doing a startElement(), writeText(text), * endElement(). */ public void writeElement(String name, Object text) { startElement(name); writeText(text); endElement(name); } /** * Convenience method, same as doing a startLineElement(), writeText(text), * endLineElement(). */ public void writeLineElement(String name, Object text) { startLineElement(name); writeText(text); endLineElement(name); } /** * Convenience method, same as doing a startBlockElement(), writeText(text), * endBlockElement(). */ public void writeBlockElement(String name, Object text) { startBlockElement(name); writeText(text); endBlockElement(name); } /** * Write an attribute with a value, if value is null nothing is written. * * @throws IllegalStateException if the is no element is open */ public void writeAttribute(String name, Object value) { if (!_isElementOpen) throw new IllegalStateException("no open element"); if (value == null) return; _isElementOpen = false; try { _strategy.writeAttribute(this, name, value); } finally { _isElementOpen = true; } } /** * Write an attribute with multiple values, separated by space, if a value * is null nothing is written. * * @throws IllegalStateException if the is no element is open */ public void writeAttribute(String name, Object ... values) { if (!_isElementOpen) throw new IllegalStateException("no open element"); _isElementOpen = false; try { _strategy.writeAttribute(this, name, values); } finally { _isElementOpen = true; } } /** * Close an open element (if any), then write with escaping as needed. */ public void writeText(char ch) { closeElementIfNeeded(false); writeIndentIfNewLine(); _strategy.writeText(this, ch); } /** * Close an open element (if any), then write with escaping as needed. */ public void writeText(char[] buf) { closeElementIfNeeded(false); writeIndentIfNewLine(); _strategy.writeText(this, buf); } /** * Close an open element (if any), then write with escaping as needed. */ public void writeText(char[] buf, int offset, int length) { closeElementIfNeeded(false); writeIndentIfNewLine(); _strategy.writeText(this, buf, offset, length); } /** * Close an open element (if any), then write object.toString(), with escaping * as needed. */ public void writeText(Object obj) { closeElementIfNeeded(false); writeIndentIfNewLine(); _strategy.writeTextObject(this, obj); } /** * Close an open element (if any), then write with escaping as needed. */ public void writeComment(String comment) { closeElementIfNeeded(false); writeIndentIfNewLine(); _strategy.writeComment(this, comment); } /** * Close an open element (if any), then flush the underlying * writer. */ public void flush() { closeElementIfNeeded(true); super.flush(); } public void println() { closeElementIfNeeded(false); super.println(); _isNewLine = true; } public boolean isNewLine() { return _isNewLine; } public boolean softPrintln() { if (!isNewLine()) { println(); return true; } else return false; } public void write(int ch) { closeElementIfNeeded(false);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?