[jdom-interest] Re: XMLOutputter testing

Bradley S. Huffman hip at a.cs.okstate.edu
Wed Feb 6 21:08:38 PST 2002


Here's a patch, indent() was using the settings of the defaultFormat
instead of the currentFormat settings.

Also fixed a potential problem with the constructor XMLOutputter(XMLOutputter).

Brad

Jason Hunter writes:

> OK, I did a little more advanced testing and found another little bug in
> the xml:space behavior if trim all white is turned on.
> 
> This file:
> 
> <body xml:space="preserve">
> Here goes  the <i xml:space="default"> body</i> and a <a
> xml:space="preserve" href="foo.html">link</a>.
> </body>
> 
> becomes
> 
> <?xml version="1.0" encoding="ISO-8859-1"?>
> <body xml:space="preserve">
> Here goes  the <i xml:space="default"> body</i> and a <a
> xml:space="preserve" href="foo.html">link</a>.
> </body>
> 
> Which is nearly correct except I think " body" should be "body" because
> the <i> tag set the space flag back to default which is trimming all. 
> Correct?

====================== Cut Here ======================
*** XMLOutputter.java	Wed Feb  6 22:53:52 2002
--- XMLOutputter.new	Wed Feb  6 22:58:52 2002
***************
*** 214,220 ****
        * - default is <code>false</code> */
      private boolean omitEncoding = false;
  
!     class Format {
          /** standard value to indent by, if we are indenting */
          static final String STANDARD_INDENT = "  ";
  
--- 214,220 ----
        * - default is <code>false</code> */
      private boolean omitEncoding = false;
  
!     class Format implements Cloneable {
          /** standard value to indent by, if we are indenting */
          static final String STANDARD_INDENT = "  ";
  
***************
*** 245,250 ****
--- 245,260 ----
          boolean newlines = false;
  
          Format() {}
+ 
+         protected Object clone() {
+             Format format = null;
+ 
+             try {
+                 format = (Format) super.clone();
+             } catch (CloneNotSupportedException ce) { }
+ 
+             return format;
+         }
      }
  
      Format noFormatting = new Format();
***************
*** 274,280 ****
       * @param indent  the indent string, usually some number of spaces
       */
      public XMLOutputter(String indent) {
!        defaultFormat.indent = indent;
      }
  
      /**
--- 284,290 ----
       * @param indent  the indent string, usually some number of spaces
       */
      public XMLOutputter(String indent) {
!        setIndent( indent);
      }
  
      /**
***************
*** 291,297 ****
       *                 printed, else new lines are ignored (compacted).
       */
      public XMLOutputter(String indent, boolean newlines) {
!        defaultFormat.indent = indent;
         setNewlines( newlines);
      }
  
--- 301,307 ----
       *                 printed, else new lines are ignored (compacted).
       */
      public XMLOutputter(String indent, boolean newlines) {
!        setIndent( indent);
         setNewlines( newlines);
      }
  
***************
*** 311,318 ****
       *                 "UTF-8" or "ISO-8859-1" or "US-ASCII"
       */
      public XMLOutputter(String indent, boolean newlines, String encoding) {
!        this.encoding = encoding;
!        defaultFormat.indent = indent;
         setNewlines( newlines);
      }
  
--- 321,328 ----
       *                 "UTF-8" or "ISO-8859-1" or "US-ASCII"
       */
      public XMLOutputter(String indent, boolean newlines, String encoding) {
!        setEncoding( encoding);
!        setIndent( indent);
         setNewlines( newlines);
      }
  
***************
*** 330,336 ****
          this.encoding = that.encoding;
          this.omitDeclaration = that.omitDeclaration;
          this.omitEncoding = that.omitEncoding;
!         this.defaultFormat = that.defaultFormat;
      }
  
      // * * * * * * * * * * Set parameters methods * * * * * * * * * *
--- 340,346 ----
          this.encoding = that.encoding;
          this.omitDeclaration = that.omitDeclaration;
          this.omitEncoding = that.omitEncoding;
!         this.defaultFormat = (Format) that.defaultFormat.clone();
      }
  
      // * * * * * * * * * * Set parameters methods * * * * * * * * * *
***************
*** 804,810 ****
  
          // Output final line separator
          // We output this no matter what the newline flags say
!         out.write(defaultFormat.lineSeparator);
  
          out.flush();
      }
--- 814,820 ----
  
          // Output final line separator
          // We output this no matter what the newline flags say
!         out.write(currentFormat.lineSeparator);
  
          out.flush();
      }
***************
*** 1694,1700 ****
       */
      protected void newline(Writer out) throws IOException {
          if (currentFormat.newlines)
!             out.write(defaultFormat.lineSeparator);
      }
  
      /**
--- 1704,1710 ----
       */
      protected void newline(Writer out) throws IOException {
          if (currentFormat.newlines)
!             out.write(currentFormat.lineSeparator);
      }
  
      /**
***************
*** 1723,1734 ****
       */
      protected void indent(Writer out, int level) throws IOException {
          if (currentFormat.newlines) {
!             if (defaultFormat.indent == null ||
!                 defaultFormat.indent.equals(""))
                      return;
  
              for (int i = 0; i < level; i++) {
!                 out.write(defaultFormat.indent);
              }
          }
      }
--- 1733,1744 ----
       */
      protected void indent(Writer out, int level) throws IOException {
          if (currentFormat.newlines) {
!             if (currentFormat.indent == null ||
!                 currentFormat.indent.equals(""))
                      return;
  
              for (int i = 0; i < level; i++) {
!                 out.write(currentFormat.indent);
              }
          }
      }



More information about the jdom-interest mailing list