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

📄 xmlwriter.java

📁 openlogic-jdom-1.1-all-src-1.zip 可以用于操作xml文件
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    /*     * Filter a end entity event.     *     * Pass the event on down the filter chain for further processing.     *     * @param name The name of the entity that is ending.     * @exception org.xml.sax.SAXException If a filter     *            further down the chain raises an exception.     * @see org.xml.sax.ext.LexicalHandler#endEntity     */    public void endEntity(String name)    throws SAXException {        super.endEntity(name);    }    /*     * Write start of CDATA.     *     * Pass the event on down the filter chain for further processing.     *     * @exception org.xml.sax.SAXException If a filter     *            further down the chain raises an exception.     * @see org.xml.sax.ext.LexicalHandler#startCDATA     */    public void startCDATA()    throws SAXException {        closeElement();        write("<![CDATA[");        super.startCDATA();    }        /*     * Write end of CDATA.     *     * Pass the event on down the filter chain for further processing.     *     * @exception org.xml.sax.SAXException If a filter     *            further down the chain raises an exception.     * @see org.xml.sax.ext.LexicalHandler#endCDATA     */    public void endCDATA()    throws SAXException {        write("]]>");        super.endCDATA();    }    /*     * Write a comment.     *     * Pass the event on down the filter chain for further processing.     *     * @param ch An array holding the characters in the comment.     * @param start The starting position in the array.     * @param length The number of characters to use from the array.     * @exception org.xml.sax.SAXException If a filter     *            further down the chain raises an exception.     * @see org.xml.sax.ext.LexicalHandler#comment     */    public void comment(char[] ch, int start, int length)    throws SAXException {        if (!inDTD) {            closeElement();            write("<!--");            write(ch, start, length);            write("-->");            if (elementLevel < 1) {                write('\n');            }        }        super.comment(ch, start, length);    }    ////////////////////////////////////////////////////////////////////    // Internal methods.    ////////////////////////////////////////////////////////////////////    /**     * Force all Namespaces to be declared.     *     * This method is used on the root element to ensure that     * the predeclared Namespaces all appear.     */    private void forceNSDecls ()    {        Iterator prefixes = forcedDeclTable.keySet().iterator();        while (prefixes.hasNext()) {            String prefix = (String)prefixes.next();            doPrefix(prefix, null, true);        }    }    /**     * Determine the prefix for an element or attribute name.     *     * TODO: this method probably needs some cleanup.     *     * @param uri The Namespace URI.     * @param qName The qualified name (optional); this will be used     *        to indicate the preferred prefix if none is currently     *        bound.     * @param isElement true if this is an element name, false     *        if it is an attribute name (which cannot use the     *        default Namespace).     */    private String doPrefix (String uri, String qName, boolean isElement)    {        String defaultNS = nsSupport.getURI("");        if ("".equals(uri)) {            if (isElement && defaultNS != null)            nsSupport.declarePrefix("", "");            return null;        }        String prefix;        if (isElement && defaultNS != null && uri.equals(defaultNS)) {            prefix = "";        } else {            prefix = nsSupport.getPrefix(uri);        }        if (prefix != null) {            return prefix;        }        prefix = (String) doneDeclTable.get(uri);        if (prefix != null &&        ((!isElement || defaultNS != null) &&        "".equals(prefix) || nsSupport.getURI(prefix) != null)) {            prefix = null;        }        if (prefix == null) {            prefix = (String) prefixTable.get(uri);            if (prefix != null &&            ((!isElement || defaultNS != null) &&            "".equals(prefix) || nsSupport.getURI(prefix) != null)) {                prefix = null;            }        }        if (prefix == null && qName != null && !"".equals(qName)) {            int i = qName.indexOf(':');            if (i == -1) {                if (isElement && defaultNS == null) {                    prefix = "";                }            } else {                prefix = qName.substring(0, i);            }        }        for (;        prefix == null || nsSupport.getURI(prefix) != null;        prefix = "__NS" + ++prefixCounter)        ;        nsSupport.declarePrefix(prefix, uri);        doneDeclTable.put(uri, prefix);        return prefix;    }    /**     * Write a raw character.     *     * @param c The character to write.     * @exception org.xml.sax.SAXException If there is an error writing     *            the character, this method will throw an IOException     *            wrapped in a SAXException.     */    private void write (char c)    throws SAXException    {        try {            output.write(c);        } catch (IOException e) {            throw new SAXException(e);        }    }    /**     * Write a portion of an array of characters.     *     * @param cbuf Array of characters.     * @param off Offset from which to start writing characters.     * @param len Number of characters to write.     * @exception org.xml.sax.SAXException If there is an error writing     *            the character, this method will throw an IOException     *            wrapped in a SAXException.     */    private void write (char[] cbuf, int off, int len)    throws SAXException    {        try {            output.write(cbuf, off, len);        } catch (IOException e) {            throw new SAXException(e);        }    }    /**     * Write a raw string.     *     * @param s     * @exception org.xml.sax.SAXException If there is an error writing     *            the string, this method will throw an IOException     *            wrapped in a SAXException     */    private void write (String s)    throws SAXException    {        try {            output.write(s);        } catch (IOException e) {            throw new SAXException(e);        }    }    /**     * Write out an attribute list, escaping values.     *     * The names will have prefixes added to them.     *     * @param atts The attribute list to write.     * @exception org.xml.SAXException If there is an error writing     *            the attribute list, this method will throw an     *            IOException wrapped in a SAXException.     */    private void writeAttributes (Attributes atts)    throws SAXException    {        int len = atts.getLength();        for (int i = 0; i < len; i++) {            char ch[] = atts.getValue(i).toCharArray();            write(' ');            writeName(atts.getURI(i), atts.getLocalName(i),            atts.getQName(i), false);            write("=\"");            writeEsc(ch, 0, ch.length, true);            write('"');        }    }    /**     * Write an array of data characters with escaping.     *     * @param ch The array of characters.     * @param start The starting position.     * @param length The number of characters to use.     * @param isAttVal true if this is an attribute value literal.     * @exception org.xml.SAXException If there is an error writing     *            the characters, this method will throw an     *            IOException wrapped in a SAXException.     */    private void writeEsc (char ch[], int start,                           int length, boolean isAttVal)    throws SAXException    {        for (int i = start; i < start + length; i++) {            switch (ch[i]) {                case '&':                write("&amp;");                break;                case '<':                write("&lt;");                break;                case '>':                write("&gt;");                break;                case '\"':                if (isAttVal) {                    write("&quot;");                } else {                    write('\"');                }                break;                default:                if (ch[i] > '\u007f') {                    write("&#");                    write(Integer.toString(ch[i]));                    write(';');                } else {                    write(ch[i]);                }            }        }    }    /**     * Write out the list of Namespace declarations.     *     * @exception org.xml.sax.SAXException This method will throw     *            an IOException wrapped in a SAXException if     *            there is an error writing the Namespace     *            declarations.     */    private void writeNSDecls ()    throws SAXException    {        Enumeration prefixes = nsSupport.getDeclaredPrefixes();        while (prefixes.hasMoreElements()) {            String prefix = (String) prefixes.nextElement();            String uri = nsSupport.getURI(prefix);            if (uri == null) {                uri = "";            }            char ch[] = uri.toCharArray();            write(' ');            if ("".equals(prefix)) {                write("xmlns=\"");            } else {                write("xmlns:");                write(prefix);                write("=\"");            }            writeEsc(ch, 0, ch.length, true);            write('\"');        }    }    /**     * Write an element or attribute name.     *     * @param uri The Namespace URI.     * @param localName The local name.     * @param qName The prefixed name, if available, or the empty string.     * @param isElement true if this is an element name, false if it     *        is an attribute name.     * @exception org.xml.sax.SAXException This method will throw an     *            IOException wrapped in a SAXException if there is     *            an error writing the name.     */    private void writeName (String uri, String localName,                            String qName, boolean isElement)    throws SAXException    {        String prefix = doPrefix(uri, qName, isElement);        if (prefix != null && !"".equals(prefix)) {            write(prefix);            write(':');        }        write(localName);    }    /**     * If start element tag is still open, write closing bracket.      */    private void closeElement()    throws SAXException    {        if (openElement) {            write('>');            openElement = false;        }    }    ////////////////////////////////////////////////////////////////////    // Internal state.    ////////////////////////////////////////////////////////////////////    private Map prefixTable;    private Map forcedDeclTable;    private Map doneDeclTable;    private boolean openElement = false;    private int elementLevel = 0;    private Writer output;    private NamespaceSupport nsSupport;    private int prefixCounter = 0;    private boolean inDTD = false;}// end of XMLWriter.java

⌨️ 快捷键说明

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