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

📄 saxwriter.java

📁 一个自然语言处理的Java开源工具包。LingPipe目前已有很丰富的功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    // ContentHandler    /**     * Prints the XML declaration, and DTD declaration if any.     */    public void startDocument() {        printXMLDeclaration();        mStartedElement = false;    }    /**     * Flushes the underlying character writers output to the     * output stream, trapping all exceptions.     */    public void endDocument() {        if (mStartedElement) {            endElement(mStartedNamespaceURI,                       mStartedLocalName,                       mStartedQName);        }        mPrinter.flush();        try {             mBufWriter.flush();         } catch (IOException e) {             // ignore exception         }        try {             mWriter.flush();         } catch (IOException e) {             // ignore exception         }    }    /**     * 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.     */    public void startPrefixMapping(String prefix, String uri) {        mPrefixMap.put(prefix,uri);    }    /**     * Prints the start element, using the qualified name, and sorting     * the attributes using the underlying string ordering.  Namespace     * URI and local names are ignored, and qualified name must not be     * <code>null</code>.     *     * @param namespaceURI The URI of the namespace for this element.     * @param localName The local name (without prefix) for this     * element.     * @param qName The qualified name (with prefix, if any) for this     * element.     * @param atts The attributes for this element.     */    public void startElement(String namespaceURI, String localName,                             String qName, Attributes atts) {        if (mStartedElement) {            mPrinter.print(">");            mStartedElement=false;        }        mPrinter.print('<');        mPrinter.print(qName);        printAttributes(atts);        mStartedElement = true;        mStartedNamespaceURI = namespaceURI;        mStartedLocalName = localName;        mStartedQName = qName;        mStartedHasAtts = atts.getLength() > 0;        // close is picked up later in implicit continuation        // mPrinter.print('>');    }    /**     * Prints the end element, using the qualified name.  Namespace     * URI and local name parameters are ignored, and the qualified     * name must not be <code>null</code>     *     * @param namespaceURI The URI of the namespace for this element.     * @param localName The local name (without prefix) for this     * element.     * @param qName The qualified name (with prefix, if any) for this     * element.     */    public void endElement(String namespaceURI, String localName,                           String qName) {        if (mStartedElement && !mXhtmlMode) {            mStartedElement = false;            mPrinter.print("/>");        } else if (mStartedElement && !mStartedHasAtts) {            mStartedElement = false;            mPrinter.print(" />");        } else {            if (mStartedElement) {                mPrinter.print(">");                mStartedElement = false;            }            mPrinter.print('<');            mPrinter.print('/');            mPrinter.print(qName);            mPrinter.print('>');        }    }    /**     * Prints the characters in the specified range.     *     * @param ch Character array from which to draw characters.     * @param start Index of first character to print.     * @param length Number of characters to print.     */    public void characters(char[] ch, int start, int length) {        if (mStartedElement) {            mPrinter.print('>');            mStartedElement = false;        }        printCharacters(ch,start,length);    }    /**     * Does not print ignorable whitespace.     *     * @param ch Character array from which to draw characters.     * @param start Index of first character to print.     * @param length Number of characters to print.     */    public void ignorableWhitespace(char[] ch, int start, int length) {        /* ignore ignorable whitespace */    }    /**     * Print a representation of the proecssing instruction.  This     * will be <code>&langle;?<i>Target</i>&rangle;</code> if there is     * no data, or * <code>&lt;?<i>Target</i>     * <i>Data</i>&gt;</code> if there is data.     *     * @param target Target of the instruction.     * @param data Value of the instruction, or <code>null</code>.     */    public void processingInstruction(String target, String data) {        if (mStartedElement) {            mPrinter.print('>');            mStartedElement = false;        }        mPrinter.print("<?");        mPrinter.print(target);        if (data != null && data.length() > 0) {            mPrinter.print(' ');            mPrinter.print(data);        }        mPrinter.print("?>");    }    /**     * Convenience method to write a slice of character data as a     * comment.  This method delegates a new string created from the     * specified slice to {@link #comment(String)}; see that method's     * documentation for more information.     *     * <p>Exceptions match those thrown by {@link     * String#String(char[],int,int)}.     *     * @param cs Underlying characters.     * @param start First character in sequence.     * @param length Number of characters in sequence.     * @throws IndexOutOfBoundsException if <code>start</code> and     * <code>length</code> are out of bounds.     */    public void comment(char[] cs, int start, int length) {        comment(new String(cs,start,length));    }    /**     * Write the specified string as a comment.  The string is first     * sanitized by breaking any double hyphens     * (<code>&quot;--&quot;</code>) with a space (producing     * <code>&quot;-&nbsp;-&quot;</code>).  If the comment starts with     * a hyphen (<code>-</code>), a space is inserted before the     * comment (causing it to start with <code>&nbsp;-</code>).  If     * the comment ends with a hyphen (<code>-</code>), a space is     * appended (causing it to end <code>-&nbsp;</code>).     *     * <p>Comments are written between comment delimeters for the     * begin (<code>&lt;--</code>) and end (<code>--&gt;</code>) of a     * comment.  No extra space is inserted after the opening hyphen     * or before the closing hyphen, and no extra line-breaks, etc.     * are inserted.  The method {@link #characters(char[],int,int)}     * may be used for inserting additional formatting, but beware     * that this adds whitespace to the current element's content     * which is only ignored if there is a DTD specifiying that no     * text content is allowed in the current element.     *     * @param comment Comment to write.     */    public void comment(String comment) {        mPrinter.print(START_COMMENT);        String noDoubleHyphenComment = comment.replaceAll("--","- -");        if (noDoubleHyphenComment.startsWith("-"))            mPrinter.print(" ");        mPrinter.print(noDoubleHyphenComment);        if (noDoubleHyphenComment.endsWith("-"))            mPrinter.print(" ");        mPrinter.print(END_COMMENT);    }    /**     * Returns the name of the character set being used by     * this writer.     *     * @return The character set for this writer.     */    public String charsetName() {        return mCharsetName;    }    // prints atts and outstanding namespace decls    private void printAttributes(Attributes atts) {        if (mPrefixMap.size() > 0) {            Iterator it = mPrefixMap.entrySet().iterator();            while (it.hasNext()) {                Map.Entry entry = (Map.Entry) it.next();                String key = entry.getKey().toString();                String value = entry.getValue().toString();                printAttribute(key.length() == 0                               ? "xmlns"                                : "xmlns:" + key,                               value);            }            mPrefixMap.clear();        }        TreeSet orderedAtts = new TreeSet();        for (int i = 0; i < atts.getLength(); ++i)            orderedAtts.add(atts.getQName(i));        Iterator attsIterator = orderedAtts.iterator();        while (attsIterator.hasNext()) {            String attQName = attsIterator.next().toString();            printAttribute(attQName,atts.getValue(attQName));        }    }    /**     * Prints an attribute and value, with value properly     * escaped. Prints leading space before attribute and value pair.     *     * @param att Attribute name.     * @param val Attribute value.     */    private void printAttribute(String att, String val) {        mPrinter.print(' ');        mPrinter.print(att);        mPrinter.print('=');        mPrinter.print('"');        printCharacters(val);        mPrinter.print('"');    }    /**     * Print the specified string, with appropriate escapes.     *     * @param s Print the characters in the specified string.     */    private void printCharacters(String s) {        printCharacters(s.toCharArray(),0,s.length());    }    /**     * Print the specified range of characters, with escapes.     *     * @param ch Array of characters from which to draw.     * @param start Index of first character to print.     * @param length Number of characters to print.     */    private void printCharacters(char[] ch, int start, int length) {        for (int i = start; i < start+length; ++i)            printCharacter(ch[i]);    }    /**     * Print the specified character, rendering it as an entity if     * necessary (see class doc).     *     * @param c Character to print.     */    private void printCharacter(char c) {        // note: does not catch illegal conjugate pairs        if (!mCharsetEncoder.canEncode(c)) {            printEntity("#x" + Integer.toHexString((int)c));            return;        }        switch (c) {        case '<':  { printEntity("lt"); break; }        case '>':  { printEntity("gt"); break; }        case '&':  { printEntity("amp"); break; }        case '"':  { printEntity("quot"); break; }        default:   { mPrinter.print(c); }        }    }    /**     * Print the specified entity.     *     * @param entity Name of entity to print.     */    private void printEntity(String entity) {        mPrinter.print('&');        mPrinter.print(entity);        mPrinter.print(';');    }    /**     * Prints the XML declaration, including the character set     * declaration and the DTD if any is defined.  The declaration     * printed is:     * <blockquote>     *   <code>&lt;?xml version="1.0" encoding="<i>CharSet</i>"?&gt;</code>.     * </blockquote>     * where <code><i>CharSet</i></code> is the string representation     * of the character set being used.  No spaces are included after     *  the XML declaration or the DTD declaration, if any.     */    private void printXMLDeclaration() {        mPrinter.print("<?xml");        printAttribute("version","1.0");        printAttribute("encoding",mCharsetName);        mPrinter.print("?>");        if (mDtdString != null) {            mPrinter.print(mDtdString);        }    }    private static String START_COMMENT = "<!--";    private static String END_COMMENT = "-->";}

⌨️ 快捷键说明

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