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

📄 outputformat.java

📁 解决如何把XML应用到JAVA里问题
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
 *
 * This software is open source.
 * See the bottom of this file for the licence.
 */

package org.dom4j.io;

/**
 * <p>
 * <code>OutputFormat</code> represents the format configuration used by
 * {@linkXMLWriter}and its base classes to format the XML output
 * </p>
 * 
 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
 * @version $Revision: 1.17 $
 */
public class OutputFormat implements Cloneable {
    /** standard value to indent by, if we are indenting */
    protected static final String STANDARD_INDENT = "  ";

    /**
     * Whether or not to suppress the XML declaration - default is
     * <code>false</code>
     */
    private boolean suppressDeclaration = false;

    /**
     * Whether or not to print new line after the XML declaration - default is
     * <code>true</code>
     */
    private boolean newLineAfterDeclaration = true;

    /** The encoding format */
    private String encoding = "UTF-8";

    /**
     * Whether or not to output the encoding in the XML declaration - default is
     * <code>false</code>
     */
    private boolean omitEncoding = false;

    /** The default indent is no spaces (as original document) */
    private String indent = null;

    /**
     * Whether or not to expand empty elements to
     * &lt;tagName&gt;&lt;/tagName&gt; - default is <code>false</code>
     */
    private boolean expandEmptyElements = false;

    /**
     * The default new line flag, set to do new lines only as in original
     * document
     */
    private boolean newlines = false;

    /** New line separator */
    private String lineSeparator = "\n";

    /** should we preserve whitespace or not in text nodes? */
    private boolean trimText = false;

    /** pad string-element boundaries with whitespace */
    private boolean padText = false;

    /** Whether or not to use XHTML standard. */
    private boolean doXHTML = false;

    /**
     * Controls when to output a line.separtor every so many tags in case of no
     * lines and total text trimming.
     */
    private int newLineAfterNTags = 0; // zero means don't bother.

    /** Quote character to use when writing attributes. */
    private char attributeQuoteChar = '\"';

    /**
     * Creates an <code>OutputFormat</code> with no additional whitespace
     * (indent or new lines) added. The whitespace from the element text content
     * is fully preserved.
     */
    public OutputFormat() {
    }

    /**
     * Creates an <code>OutputFormat</code> with the given indent added but no
     * new lines added. All whitespace from element text will be included.
     * 
     * @param indent
     *            is the indent string to be used for indentation (usually a
     *            number of spaces).
     */
    public OutputFormat(String indent) {
        this.indent = indent;
    }

    /**
     * Creates an <code>OutputFormat</code> with the given indent added with
     * optional newlines between the Elements. All whitespace from element text
     * will be included.
     * 
     * @param indent
     *            is the indent string to be used for indentation (usually a
     *            number of spaces).
     * @param newlines
     *            whether new lines are added to layout the
     */
    public OutputFormat(String indent, boolean newlines) {
        this.indent = indent;
        this.newlines = newlines;
    }

    /**
     * Creates an <code>OutputFormat</code> with the given indent added with
     * optional newlines between the Elements and the given encoding format.
     * 
     * @param indent
     *            is the indent string to be used for indentation (usually a
     *            number of spaces).
     * @param newlines
     *            whether new lines are added to layout the
     * @param encoding
     *            is the text encoding to use for writing the XML
     */
    public OutputFormat(String indent, boolean newlines, String encoding) {
        this.indent = indent;
        this.newlines = newlines;
        this.encoding = encoding;
    }

    public String getLineSeparator() {
        return lineSeparator;
    }

    /**
     * <p>
     * This will set the new-line separator. The default is <code>\n</code>.
     * Note that if the "newlines" property is false, this value is irrelevant.
     * To make it output the system default line ending string, call
     * <code>setLineSeparator(System.getProperty("line.separator"))</code>
     * </p>
     * 
     * @param separator
     *            <code>String</code> line separator to use.
     * 
     * @see #setNewlines(boolean)
     */
    public void setLineSeparator(String separator) {
        lineSeparator = separator;
    }

    public boolean isNewlines() {
        return newlines;
    }

    /**
     * DOCUMENT ME!
     * 
     * @param newlines
     *            <code>true</code> indicates new lines should be printed,
     *            else new lines are ignored (compacted).
     * 
     * @see #setLineSeparator(String)
     */
    public void setNewlines(boolean newlines) {
        this.newlines = newlines;
    }

    public String getEncoding() {
        return encoding;
    }

    /**
     * DOCUMENT ME!
     * 
     * @param encoding
     *            encoding format
     */
    public void setEncoding(String encoding) {
        if (encoding != null) {
            this.encoding = encoding;
        }
    }

    public boolean isOmitEncoding() {
        return omitEncoding;
    }

    /**
     * <p>
     * This will set whether the XML declaration (<code>&lt;?xml version="1.0"
     * encoding="UTF-8"?&gt;</code>)
     * includes the encoding of the document. It is common to suppress this in
     * protocols such as WML and SOAP.
     * </p>
     * 
     * @param omitEncoding
     *            <code>boolean</code> indicating whether or not the XML
     *            declaration should indicate the document encoding.
     */
    public void setOmitEncoding(boolean omitEncoding) {
        this.omitEncoding = omitEncoding;
    }

    /**
     * <p>
     * This will set whether the XML declaration (<code>&lt;?xml version="1.0"
     * encoding="UTF-8"?&gt;</code>)
     * is included or not. It is common to suppress this in protocols such as
     * WML and SOAP.
     * </p>
     * 
     * @param suppressDeclaration
     *            <code>boolean</code> indicating whether or not the XML
     *            declaration should be suppressed.
     */
    public void setSuppressDeclaration(boolean suppressDeclaration) {
        this.suppressDeclaration = suppressDeclaration;
    }

    /**
     * DOCUMENT ME!
     * 
     * @return true if the output of the XML declaration (<code>&lt;?xml
     *         version="1.0"?&gt;</code>)
     *         should be suppressed else false.
     */
    public boolean isSuppressDeclaration() {
        return suppressDeclaration;
    }

    /**
     * <p>
     * This will set whether a new line is printed after the XML declaration
     * (assuming it is not supressed.)
     * </p>
     * 
     * @param newLineAfterDeclaration
     *            <code>boolean</code> indicating whether or not to print new
     *            line following the XML declaration. The default is true.
     */
    public void setNewLineAfterDeclaration(boolean newLineAfterDeclaration) {
        this.newLineAfterDeclaration = newLineAfterDeclaration;
    }

    /**
     * DOCUMENT ME!
     * 
     * @return true if a new line should be printed following XML declaration
     */
    public boolean isNewLineAfterDeclaration() {
        return newLineAfterDeclaration;
    }

    public boolean isExpandEmptyElements() {
        return expandEmptyElements;
    }

    /**
     * <p>
     * This will set whether empty elements are expanded from
     * <code>&lt;tagName&gt;</code> to
     * <code>&lt;tagName&gt;&lt;/tagName&gt;</code>.
     * </p>
     * 
     * @param expandEmptyElements
     *            <code>boolean</code> indicating whether or not empty
     *            elements should be expanded.
     */
    public void setExpandEmptyElements(boolean expandEmptyElements) {
        this.expandEmptyElements = expandEmptyElements;
    }

    public boolean isTrimText() {
        return trimText;
    }

    /**
     * <p>
     * This will set whether the text is output verbatim (false) or with
     * whitespace stripped as per <code>{@link
     * org.dom4j.Element#getTextTrim()}</code>.
     * </p>
     * 
     * <p>
     * </p>
     * 
     * <p>
     * Default: false
     * </p>
     * 
     * @param trimText
     *            <code>boolean</code> true=>trim the whitespace, false=>use

⌨️ 快捷键说明

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