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

📄 saxwriterfilter.java

📁 一个自然语言处理的Java开源工具包。LingPipe目前已有很丰富的功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * LingPipe v. 3.5 * Copyright (C) 2003-2008 Alias-i * * This program is licensed under the Alias-i Royalty Free License * Version 1 WITHOUT ANY WARRANTY, without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the Alias-i * Royalty Free License Version 1 for more details. * * You should have received a copy of the Alias-i Royalty Free License * Version 1 along with this program; if not, visit * http://alias-i.com/lingpipe/licenses/lingpipe-license-1.txt or contact * Alias-i, Inc. at 181 North 11th Street, Suite 401, Brooklyn, NY 11211, * +1 (718) 290-9170. */package com.aliasi.xml;import java.io.BufferedWriter;import java.io.IOException;import java.io.PrintWriter;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.io.UnsupportedEncodingException;import java.nio.charset.Charset;import java.nio.charset.CharsetEncoder;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.TreeSet;import org.xml.sax.Attributes;import org.xml.sax.SAXException;import org.xml.sax.helpers.DefaultHandler;/** * A <code>SAXWriterFilter</code> handles SAX events and writes a * character-based representation to a specified output stream in the * specified character encoding and also passes them to a contained * handler.  The methods provided are exactly the same as those for * a <code>SAXWriter</code>. * * <P>All events are passed to the contained handler, and this * contained handler may be specified at construction time or through * the filter method {@link #setHandler(DefaultHandler)}. * * <P>Note that a DTD specified by {@link #setDTDString(String)} is * not passed to the contained handler, but is only written to the * output stream. * * @author  Bob Carpenter * @version 2.0 * @since   LingPipe2.0 */public class SAXWriterFilter extends SAXFilterHandler {    /**     * Encoder for the current charset.  Used to test if     * a character is encodable or needs to be escaped.     */    private CharsetEncoder mCharsetEncoder;    /**     * Printer to which characters are written.     */    private PrintWriter mPrinter;    /**     * Buffered writer to which printer writers characters.     */    private BufferedWriter mBufWriter;    /**     * Output stream writer to which buffered writer writers     * characters for conversion to bytes.  Wrapping by buffer as per     * recommendation in {@link java.io.OutputStreamWriter} class     * documentation.     */    private OutputStreamWriter mWriter;    /**     * Character set in which characters are encoded.     */    private String mCharsetName;    /**     * The string to write for a DTD declaration in the XML file, or     * <code>null</code> if none.     */    private String mDtdString = null;    /**     * Set to true if an element has been started, but     * not yet closed with a final right angle bracket.     */    private boolean mStartedElement;    private final HashMap mPrefixMap = new HashMap();    /**     * Construct a SAX writer that writes to the specified output     * stream using the specified character set.  See {@link     * #setOutputStream(OutputStream,String)} for details on the     * management of the output stream and character set.     *     * @param out Output stream to which bytes are written.     * @param charsetName Name of character encoding used to write output.     * @param handler Contained handler for this filter.     * @throws UnsupportedEncodingException If the character set is not supported.     */    public SAXWriterFilter(OutputStream out, String charsetName,                            DefaultHandler handler)        throws UnsupportedEncodingException {        super(handler);        setOutputStream(out,charsetName);    }    /**     * Construct a SAX writer that does not have an output stream,     * character set, or contained handler specified.  These must be     * set through {@link #setOutputStream(OutputStream,String)} or an     * illegal state exception will be thrown.  The contained handler     * will default to a no-op handler, but may be set by {@link     * #setHandler(DefaultHandler)}.     */    public SAXWriterFilter() {         /* do nothing */    }    /**     * Sets the DTD to be written by this writer to the specified     * value.  There is no error checking on its well-formedness, and     * it is not wrapped in any way other than being printed on its     * own line; this allows arbitrary DTDs to be written.     *     * @param dtdString String to write after the XML declaration as     * the DTD declaration.     */    public void setDTDString(String dtdString) {        mDtdString = dtdString;    }    /**     * Sets the output stream to which the XML is written, and the     * character set which is used to encode characters.  Before     * writing a document, the output stream and character set must be     * set by the constructor or by this method.  The output stream is     * not closed after an XML document is written, but all output to     * the stream will be produced and does not need to be otherwise     * flushed.     *     * @param out Output stream to which encoded characters are written.     * @param charsetName Character set to use for encoding characters.     * @throws UnsupportedEncodingException If the character set is     * not supported by the Java runtime.     */    public final void setOutputStream(OutputStream out, String charsetName)        throws UnsupportedEncodingException {        Charset charset = Charset.forName(charsetName);        mCharsetEncoder = charset.newEncoder();        mWriter = new OutputStreamWriter(out,mCharsetEncoder);        mBufWriter = new BufferedWriter(mWriter);        mPrinter = new PrintWriter(mBufWriter); // no auto-flush        mCharsetName = charsetName;    }    // ContentHandler    /**     * Prints the XML declaration, and DTD declaration if any.     */    public void startDocument() throws SAXException {        super.startDocument();        printXMLDeclaration();        mStartedElement = false;    }    /**     * Handles the declaration of a namespace mapping from a specified     * URI to its identifying prefix.  The mapping is buffered and     * then flushed and printed as an attribute during the next     * start-element call.     *     * @param prefix The namespace prefix being declared..     * @param uri The namespace URI mapped to prefix.     * @throws SAXException If the contained handler throws an     * exception on the specified prefix mapping.     */    public void startPrefixMapping(String prefix, String uri)         throws SAXException {        mPrefixMap.put(prefix,uri);        super.startPrefixMapping(prefix,uri);    }    /**     * Handles the declaration of a namespace mapping from a specified     * URI to its identifying prefix.  The mapping is buffered and     * then flushed and printed as an attribute during the next     * start-element call.     *     * @param prefix The namespace prefix being declared..     * @throws SAXException If the contained handler throws an exception     * on the specified prefix.     */    public void endPrefixMapping(String prefix) throws SAXException {        super.endPrefixMapping(prefix);    }    /**     * Flushes the underlying character writers output to the     * output stream, trapping all exceptions.     */    public void endDocument() throws SAXException {        super.endDocument();        if (mStartedElement) {            mPrinter.print("/>");            mStartedElement = false;        }        mPrinter.flush();        try {             mBufWriter.flush();         } catch (IOException e) {             // do nothing        }        try {

⌨️ 快捷键说明

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