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

📄 xmlwritersupport.java

📁 水晶 ? ?  报表 ? ? ? 源码
💻 JAVA
字号:
/**
 * ========================================
 * JFreeReport : a free Java report library
 * ========================================
 *
 * Project Info:  http://www.object-refinery.com/jfreereport/index.html
 * Project Lead:  Thomas Morgner (taquera@sherito.org);
 *
 * (C) Copyright 2000-2003, by Simba Management Limited and Contributors.
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * ------------------------------
 * XMLWriterSupport.java
 * ------------------------------
 * (C)opyright 2003, by Thomas Morgner and Contributors.
 *
 * Original Author:  Thomas Morgner;
 * Contributor(s):   David Gilbert (for Simba Management Limited);
 *
 * $Id: XMLWriterSupport.java,v 1.1 2003/07/12 13:29:04 taqua Exp $
 *
 * Changes
 * -------------------------
 * 21.06.2003 : Initial version
 *
 */

package org.jfree.xml.writer;

import java.io.Writer;
import java.io.IOException;
import java.util.Properties;
import java.util.Enumeration;

public class XMLWriterSupport {

    /** A int constant for controling the indent function. */
    protected static final int OPEN_TAG_INCREASE = 1;
    /** A int constant for controling the indent function. */
    protected static final int CLOSE_TAG_DECREASE = 2;
    /** A int constant for controling the indent function. */
    protected static final int INDENT_ONLY = 3;

    /** A constant for close. */
    public static final boolean CLOSE = true;

    /** A constant for open. */
    public static final boolean OPEN = false;

    /** The line separator. */
    private static String lineSeparator;

    /** A list of safe tags. */
    private SafeTagList safeTags;

    /** The indent level for that writer. */
    private int indentLevel;

    private String indentString;

    public XMLWriterSupport(SafeTagList safeTags, int indentLevel) {
        this(safeTags, indentLevel, "    ");
    }

    public XMLWriterSupport(SafeTagList safeTags, int indentLevel, String indentString) {
        if (indentString == null) {
            throw new NullPointerException("IndentString must not be null");
        }

        this.safeTags = safeTags;
        this.indentLevel = indentLevel;
        this.indentString = indentString;
    }


    /**
     * Returns the line separator.
     *
     * @return The line separator.
     */
    public static String getLineSeparator() {
        if (lineSeparator == null) {
            try {
                lineSeparator = System.getProperty("line.separator", "\n");
            }
            catch (SecurityException se) {
                lineSeparator = "\n";
            }
        }
        return lineSeparator;
    }

    /**
     * Writes an opening XML tag that has no attributes.
     *
     * @param w  the writer.
     * @param name  the tag name.
     *
     * @throws java.io.IOException if there is an I/O problem.
     */
    public void writeTag(Writer w, String name) throws IOException {
        indent(w, OPEN_TAG_INCREASE);

        w.write("<");
        w.write(name);
        w.write(">");
        if (getSafeTags().isSafeForOpen(name)) {
            w.write(getLineSeparator());
        }
    }

    /**
     * Writes a closing XML tag.
     *
     * @param w  the writer.
     * @param tag  the tag name.
     *
     * @throws java.io.IOException if there is an I/O problem.
     */
    public void writeCloseTag(Writer w, String tag) throws IOException {
        // check whether the tag contains CData - we ma not indent such tags
        if (getSafeTags().isSafeForOpen(tag)) {
            indent(w, CLOSE_TAG_DECREASE);
        }
        else {
            decreaseIndent();
        }
        w.write("</");
        w.write(tag);
        w.write(">");
        if (getSafeTags().isSafeForClose(tag)) {
            w.write(getLineSeparator());
        }
    }

    /**
     * Writes an opening XML tag with an attribute/value pair.
     *
     * @param w  the writer.
     * @param name  the tag name.
     * @param attributeName  the attribute name.
     * @param attributeValue  the attribute value.
     * @param close  controls whether the tag is closed.
     *
     * @throws java.io.IOException if there is an I/O problem.
     */
    public void writeTag(Writer w, String name, String attributeName, String attributeValue,
                         boolean close) throws IOException {
        Properties attr = new Properties();
        attr.setProperty(attributeName, attributeValue);
        writeTag(w, name, attr, close);
    }

    /**
     * Writes an opening XML tag along with a list of attribute/value pairs.
     *
     * @param w  the writer.
     * @param name  the tag name.
     * @param attributes  the attributes.
     * @param close  controls whether the tag is closed.
     *
     * @throws java.io.IOException if there is an I/O problem.
     */
    public void writeTag(Writer w, String name, Properties attributes, boolean close)
        throws IOException {
        indent(w, OPEN_TAG_INCREASE);

        w.write("<");
        w.write(name);
        Enumeration keys = attributes.keys();
        while (keys.hasMoreElements()) {
            String key = (String) keys.nextElement();
            String value = attributes.getProperty(key);
            w.write(" ");
            w.write(key);
            w.write("=\"");
            w.write(normalize(value));
            w.write("\"");
        }
        if (close) {
            w.write("/>");
            if (getSafeTags().isSafeForClose(name)) {
                w.write(getLineSeparator());
            }
            decreaseIndent();
        }
        else {
            w.write(">");
            if (getSafeTags().isSafeForOpen(name)) {
                w.write(getLineSeparator());
            }
        }
    }

    /**
     * Normalises a string, replacing certain characters with their escape sequences so that
     * the XML text is not corrupted.
     *
     * @param s  the string.
     *
     * @return The normalised string.
     */
    public static String normalize(String s) {
        StringBuffer str = new StringBuffer();
        int len = (s != null) ? s.length() : 0;

        for (int i = 0; i < len; i++) {
            char ch = s.charAt(i);

            switch (ch) {
                case '<':
                    {
                        str.append("&lt;");
                        break;
                    }
                case '>':
                    {
                        str.append("&gt;");
                        break;
                    }
                case '&':
                    {
                        str.append("&amp;");
                        break;
                    }
                case '"':
                    {
                        str.append("&quot;");
                        break;
                    }
                case '\n':
                    {
                        if (i > 0) {
                            char lastChar = str.charAt(str.length() - 1);

                            if (lastChar != '\r') {
                                str.append(getLineSeparator());
                            }
                            else {
                                str.append('\n');
                            }
                        }
                        else {
                            str.append(getLineSeparator());
                        }
                        break;
                    }
                default :
                    {
                        str.append(ch);
                    }
            }
        }

        return (str.toString());
    }

    /**
     * Indent the line. Called for proper indenting in various places.
     *
     * @param writer the writer which should receive the indentention.
     * @param increase the current indent level.
     * @throws java.io.IOException if writing the stream failed.
     */
    protected void indent(Writer writer, int increase) throws IOException {
        if (increase == CLOSE_TAG_DECREASE) {
            decreaseIndent();
        }
        for (int i = 0; i < indentLevel; i++) {
            writer.write(indentString); // 4 spaces, we could also try tab,
            // but I do not know whether this works
            // with our XML edit pane
        }
        if (increase == OPEN_TAG_INCREASE) {
            increaseIndent();
        }
    }

    /**
     * Returns the current indent level.
     *
     * @return the current indent level.
     */
    public int getIndentLevel() {
        return indentLevel;
    }

    /**
     * Increases the indention by one level.
     */
    protected void increaseIndent() {
        indentLevel++;
    }

    /**
     * Decreates the indention by one level.
     */
    protected void decreaseIndent() {
        indentLevel--;
    }

    public SafeTagList getSafeTags() {
        return safeTags;
    }
}

⌨️ 快捷键说明

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