Project dom4j 1.5.2 [5/2/05 10:13 PM]
 
Coverage - org/dom4j/io/OutputFormat.java
  /*
   * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
   *
   * This software is open source.
   * See the bottom of this file for the licence.
   *
   * $Id: OutputFormat.java,v 1.13 2004/06/25 08:03:37 maartenc Exp $
   */
 
  package org.dom4j.io;
 
  /** <p><code>OutputFormat</code> represents the format configuration
    * used by {@link XMLWriter} and its base classes to format the XML output
    *
    * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
    * @version $Revision: 1.13 $
    */
  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> */
58x       private boolean suppressDeclaration = false;
      
      /** Whether or not to print new line after the XML declaration - default is <code>true</code> */
58x       private boolean newLineAfterDeclaration = true;
 
      /** The encoding format */
58x       private String encoding = "UTF-8";
 
      /** Whether or not to output the encoding in the XML declaration - default is <code>false</code> */
58x       private boolean omitEncoding = false;
 
      /** The default indent is no spaces (as original document) */
58x       private String indent = null;
 
      /** Whether or not to expand empty elements to &lt;tagName&gt;&lt;/tagName&gt; - default is <code>false</code> */
58x       private boolean expandEmptyElements = false;
 
      /** The default new line flag, set to do new lines only as in original document */
58x       private boolean newlines = false;
 
      /** New line separator */
58x       private String lineSeparator = "\n";
 
      /** should we preserve whitespace or not in text nodes? */
58x       private boolean trimText = false;
 
      /** pad string-element boundaries with whitespace **/
58x       private boolean padText = false;
 
      /** Whether or not to use XHTML standard. */
58x       private boolean doXHTML = false;
 
      /** Controls when to output a line.separtor every so many tags in case of no lines and total text trimming.*/
58x       private int newLineAfterNTags = 0;  //zero means don't bother.
      
      /** Quote character to use when writing attributes. */
58x       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.
        */
58x       public OutputFormat() {
58x       }
 
      /** 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).
        */
0x       public OutputFormat(String indent) {
0x           this.indent = indent;
0x       }
 
      /** 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
        */
4x       public OutputFormat(String indent, boolean newlines) {
4x           this.indent = indent;
4x           this.newlines = newlines;
4x       }
 
      /** 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
        */
4x       public OutputFormat(String indent, boolean newlines, String encoding) {
4x           this.indent = indent;
4x           this.newlines = newlines;
4x           this.encoding = encoding;
4x       }
 
      public String getLineSeparator() {
444x           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>
        * @see #setNewlines(boolean)
        * @param separator <code>String</code> line separator to use.
        */
      public void setLineSeparator(String separator) {
0x           lineSeparator = separator;
0x       }
 
      public boolean isNewlines() {
8066x           return newlines;
      }
 
      /** @see #setLineSeparator(String)
        * @param newlines <code>true</code> indicates new lines should be
        *                 printed, else new lines are ignored (compacted).
        */
      public void setNewlines(boolean newlines) {
64x           this.newlines = newlines;
64x       }
 
      public String getEncoding() {
4416x           return encoding;
      }
 
      /** @param encoding encoding format */
      public void setEncoding(String encoding) {
128x           this.encoding = encoding;
128x       }
 
      public boolean isOmitEncoding() {
110x           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) {
12x           this.omitEncoding = omitEncoding;
12x       }
 
      /** <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) {
16x           this.suppressDeclaration = suppressDeclaration;
16x       }
 
      /** @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() {
114x           return suppressDeclaration;
      }
      
      /** <p> This will set whether a new line is printed after the XML
        *  declaration (assuming it is not supressed.)
        * 
        *  @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) {
0x           this.newLineAfterDeclaration = newLineAfterDeclaration;
0x       }
 
      /** @return true if a new line should be printed following XML declaration
        */
      public boolean isNewLineAfterDeclaration() {
110x           return newLineAfterDeclaration;
      }
 
      public boolean isExpandEmptyElements() {
40x           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) {
20x           this.expandEmptyElements = expandEmptyElements;
20x       }
 
      public boolean isTrimText() {
6688x           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>Default: false </p>
        *
        * @param trimText <code>boolean</code> true=>trim the whitespace, false=>use text verbatim
        */
      public void setTrimText(boolean trimText) {
66x           this.trimText = trimText;
66x       }
 
      public boolean isPadText() {
336x           return padText;
      }
 
      /** <p> Ensure that text immediately preceded by or followed by an
        * element will be "padded" with a single space.  This is used to
        * allow make browser-friendly HTML, avoiding trimText's
        * transformation of, e.g.,
        * <code>The quick &lt;b&gt;brown&lt;/b&gt; fox</code> into
        * <code>The quick&lt;b&gt;brown&lt;/b&gt;fox</code> (the latter
        * will run the three separate words together into a single word).
        *
        * This setting is not too useful if you haven't also called
        * {@link #setTrimText}.</p>
        *
        * <p>Default: false </p>
        *
        * @param padText <code>boolean</code> if true, pad string-element boundaries
        */
      public void setPadText(boolean padText) {
2x           this.padText = padText;
2x       }
 
      public String getIndent() {
7948x           return indent;
      }
 
      /** <p> This will set the indent <code>String</code> to use; this
        * is usually a <code>String</code> of empty spaces. If you pass
        * null, or the empty string (""), then no indentation will
        * happen. </p>
        * Default: none (null)
        *
        * @param indent <code>String</code> to use for indentation.
        */
      public void setIndent(String indent) {
          // nullify empty string to void unnecessary indentation code
0/4 0x           if ( indent != null && indent.length() <= 0 ) {
0x               indent = null;
          }
0x           this.indent = indent;
0x       }
 
      /** Set the indent on or off.  If setting on, will use the value of
        * STANDARD_INDENT, which is usually two spaces.
        *
        * @param doIndent if true, set indenting on; if false, set indenting off
        */
      public void setIndent(boolean doIndent) {
2/2 28x           if (doIndent) {
10x               this.indent = STANDARD_INDENT;
          }
          else {
18x               this.indent = null;
          }
28x       }
 
      /** <p>This will set the indent <code>String</code>'s size; an indentSize
        * of 4 would result in the indention being equivalent to the <code>String</code>
        * "&nbsp;&nbsp;&nbsp;&nbsp;" (four space characters).</p>
        *
        * @param indentSize <code>int</code> number of spaces in indentation.
        */
      public void setIndentSize(int indentSize) {
36x           StringBuffer indentBuffer = new StringBuffer();
2/2 124x           for ( int i = 0; i < indentSize; i++ ) {
88x               indentBuffer.append(" ");
          }
36x           this.indent = indentBuffer.toString();
36x       }
 
      /** <p> Whether or not to use the XHTML standard: like HTML but passes an XML parser with real,
       *  closed tags.  Also, XHTML CDATA sections  will be output with the CDATA delimiters:
       *  ( &quot;<b>&lt;![CDATA[</b>&quot; and &quot;<b>]]&gt;</b>&quot; )
       *  otherwise, the class HTMLWriter will output the CDATA text, but not the delimiters.</p>
       *
       *  <p> Default is <code>false</code></p>
       */
4x       public boolean isXHTML(){return doXHTML;}
 
      /** <p> This will set whether or not to use the XHTML standard: like HTML but passes an XML parser with real,
       *  closed tags.  Also, XHTML CDATA sections
       *  will be output with the CDATA delimiters: ( &quot;<b>&lt;[CDATA[</b>&quot; and &quot;<b>]]&lt;</b> )
       *  otherwise, the class HTMLWriter will output the CDATA text, but not the delimiters.</p>
       *
        * <p>Default: false </p>
        *
        * @param xhtml <code>boolean</code> true=>conform to XHTML, false=>conform to HTML, can have unclosed tags, etc.
        */
      public void setXHTML(boolean xhtml){
0x           doXHTML = xhtml;
0x       }
 
      public int getNewLineAfterNTags(){
0x           return newLineAfterNTags;
      }
 
      /** Controls output of a line.separator every tagCount tags when isNewlines is false.
       *  If tagCount equals zero, it means don't do anything special.  If greater than zero, then a line.separator
       *  will be output after tagCount tags have been output.  Used when you would like to squeeze the html as
       *  much as possible, but some browsers don't like really long lines. A tag count of 10 would
       *  produce a line.separator in the output after 10 close tags (including single tags).*/
      public void setNewLineAfterNTags(int tagCount){
0x           newLineAfterNTags = tagCount;
0x       }
 
      public char getAttributeQuoteCharacter() {
608x           return attributeQuoteChar;
      }
 
      /**
       * Sets the character used to quote attribute values. The specified
       * character must be a valid XML attribute quote character, otherwise an
       * <code>IllegalArgumentException</code> will be thrown.
       * 
       * @param quoteChar The character to use when quoting attribute values.
       * @throws IllegalArgumentException If the specified character is not a
       *         valid XML attribute quote character.
       */
      public void setAttributeQuoteCharacter(char quoteChar) {
0/4 0x           if (quoteChar == '\'' || quoteChar == '"') {
0x               attributeQuoteChar = quoteChar;
          } else {
0x               throw new IllegalArgumentException(
                      "Invalid attribute quote character (" + quoteChar + ")");
          }
0x       }
 
      /** Parses command line arguments of the form
        * <code>-omitEncoding -indentSize 3 -newlines -trimText</code>
        *
        * @param args is the array of command line arguments
        * @param i is the index in args to start parsing options
        * @return the index of first parameter that we didn't understand
        */
      public int parseOptions(String[] args, int i) {
0/2 0x           for ( int size = args.length; i < size; i++ ) {
0/2 0x               if (args[i].equals("-suppressDeclaration")) {
0x                   setSuppressDeclaration(true);
              }
0/2 0x               else if (args[i].equals("-omitEncoding")) {
0x                   setOmitEncoding(true);
              }
0/2 0x               else if (args[i].equals("-indent")) {
0x                   setIndent(args[++i]);
              }
0/2 0x               else if (args[i].equals("-indentSize")) {
0x                   setIndentSize(Integer.parseInt(args[++i]));
              }
0/2 0x               else if (args[i].startsWith("-expandEmpty")) {
0x                   setExpandEmptyElements(true);
              }
0/2 0x               else if (args[i].equals("-encoding")) {
0x                   setEncoding(args[++i]);
              }
0/2 0x               else if (args[i].equals("-newlines")) {
0x                   setNewlines(true);
              }
0/2 0x               else if (args[i].equals("-lineSeparator")) {
0x                   setLineSeparator(args[++i]);
              }
0/2 0x               else if (args[i].equals("-trimText")) {
0x                   setTrimText(true);
              }
0/2 0x               else if (args[i].equals("-padText")) {
0x                   setPadText(true);
              }
0/2 0x               else if (args[i].startsWith("-xhtml")) {
0x                   setXHTML(true);
              }
              else {
0x                   return i;
              }
          }
0x           return i;
      }
 
 
      /** A static helper method to create the default pretty printing format.
        * This format consists of an indent of 2 spaces, newlines after each
        * element and all other whitespace trimmed, and XMTML is false.
        */
      public static OutputFormat createPrettyPrint() {
28x           OutputFormat format = new OutputFormat();
28x           format.setIndentSize( 2 );
28x           format.setNewlines(true);
28x           format.setTrimText(true);
28x           return format;
      }
 
      /** A static helper method to create the default compact format.
        * This format does not have any indentation or newlines after an alement
        * and all other whitespace trimmed
        */
      public static OutputFormat createCompactFormat() {
18x           OutputFormat format = new OutputFormat();
18x           format.setIndent(false);
18x           format.setNewlines(false);
18x           format.setTrimText(true);
18x           return format;
      }
 
  }
 
 
 
 
  /*
   * Redistribution and use of this software and associated documentation
   * ("Software"), with or without modification, are permitted provided
   * that the following conditions are met:
   *
   * 1. Redistributions of source code must retain copyright
   *    statements and notices.  Redistributions must also contain a
   *    copy of this document.
   *
   * 2. Redistributions in binary form must reproduce the
   *    above copyright notice, this list of conditions and the
   *    following disclaimer in the documentation and/or other
   *    materials provided with the distribution.
   *
   * 3. The name "DOM4J" must not be used to endorse or promote
   *    products derived from this Software without prior written
   *    permission of MetaStuff, Ltd.  For written permission,
   *    please contact dom4j-info@metastuff.com.
   *
   * 4. Products derived from this Software may not be called "DOM4J"
   *    nor may "DOM4J" appear in their names without prior written
   *    permission of MetaStuff, Ltd. DOM4J is a registered
   *    trademark of MetaStuff, Ltd.
   *
   * 5. Due credit should be given to the DOM4J Project - 
   *    http://www.dom4j.org
   *
   * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   * FI