[jdom-interest] PATCH: XMLOutputter: padText, outputElementContentSubject

Jason Hunter jhunter at collab.net
Wed Oct 18 16:45:34 PDT 2000


Just integrated.

-jh-

Alex Chaffee wrote:
> 
> Oops again.  The original patch bounced (I misspelled ".org" :-) so I
> didn't need to correct it.  Below is a perfectly good patch.
> 
> ---
> 
> Another patch for XMLOutputter.
> 
> Changes (please add these to the CVS log):
> 
> - renamed public printElementContent to outputElementContent (for consistency)
> 
> - added "padText" property, to avoid trimText changing
>         The quick <b>brown</b> fox
>         into
>         The quick<b>brown</b>fox
>   which allows you to output browser-friendly HTML even when using trimText
> 
> - misc doc fix
> 
> --
> Alex Chaffee                       mailto:alex at jguru.com
> jGuru - Java News and FAQs         http://www.jguru.com/alex/
> Creator of Gamelan                 http://www.gamelan.com/
> Founder of Purple Technology       http://www.purpletech.com/
> Curator of Stinky Art Collective   http://www.stinky.com/
> 
> cvs diff -u XMLOutputter.java
> Index: XMLOutputter.java
> ===================================================================
> RCS file: /home/cvspublic/jdom/src/java/org/jdom/output/XMLOutputter.java,v
> retrieving revision 1.25
> diff -u -r1.25 XMLOutputter.java
> --- XMLOutputter.java   2000/10/13 23:33:10     1.25
> +++ XMLOutputter.java   2000/10/14 18:28:43
> @@ -155,6 +155,11 @@
>      /** should we preserve whitespace or not in text nodes? */
>      private boolean trimText = false;
> 
> +    /** pad string-element boundaries with whitespace **/
> +    private boolean padText = false;
> +
> +    protected String padTextString = " ";
> +
>      /**
>       * <p>
>       * This will create an <code>XMLOutputter</code> with
> @@ -232,13 +237,17 @@
>          this.newlines = that.newlines;
>          this.encoding = that.encoding;
>          this.lineSeparator = that.lineSeparator;
> -        this.trimText = that.trimText;
> +        this.trimText = that.trimText;
> +       this.padText = that.padText;
>      }
> 
>      /**
>       * <p>This will set the new-line separator. The default is
>       * <code>\r\n</code>. Note that if the "newlines" property is
> -     * false, this value is irrelevant.  </p>
> +     * false, this value is irrelevant.  To make it output the system
> +     * default line ending string, call
> +     * <code>setLineSeparator(System.getProperty("line.separator"))</code>
> +     * </p>
>       *
>       * <blockquote>
>       *  We could change this to the System default,
> @@ -329,6 +338,26 @@
>      }
> 
>      /**
> +     * <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) {
> +        this.padText = padText;
> +    }
> +
> +    /**
>       * <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
> @@ -541,6 +570,25 @@
>          writer.flush();         // Flush the output to the underlying stream
>      }
> 
> +    /**
> +     * <p> This will handle printing out an <code>{@link
> +     * Element}</code>'s content only, not including its tag, and
> +     * attributes.  This can be useful for printing the content of an
> +     * element that contains HTML, like "&lt;description&gt;JDOM is
> +     * &lt;b&gt;fun&gt;!&lt;/description&gt;".  </p>
> +     *
> +     * @param element <code>Element</code> to output.
> +     * @param out <code>Writer</code> to write to.
> +     * @param indent <code>int</code> level of indention.  */
> +    public void outputElementContent(Element element, Writer out)
> +        throws IOException
> +    {
> +        List mixedContent = element.getMixedContent();
> +        printElementContent(element, out, indentLevel,
> +                            new NamespaceStack(),
> +                            mixedContent);
> +    }
> +
>      // output cdata
> 
>      /**
> @@ -972,23 +1020,6 @@
>       * @param element <code>Element</code> to output.
>       * @param out <code>Writer</code> to write to.
>       * @param indent <code>int</code> level of indention.  */
> -    public void printElementContent(Element element, Writer out)
> -        throws IOException
> -    {
> -        List mixedContent = element.getMixedContent();
> -        printElementContent(element, out, indentLevel,
> -                            new NamespaceStack(),
> -                            mixedContent);
> -    }
> -
> -    /**
> -     * <p> This will handle printing out an <code>{@link
> -     * Element}</code>'s content only, not including its tag,
> -     * attributes, and namespace info.  </p>
> -     *
> -     * @param element <code>Element</code> to output.
> -     * @param out <code>Writer</code> to write to.
> -     * @param indent <code>int</code> level of indention.  */
>      protected void printElementContent(Element element, Writer out,
>                                         int indentLevel,
>                                         NamespaceStack namespaces,
> @@ -1021,21 +1052,32 @@
>               */
>              // Iterate through children
>              Object content = null;
> +           Class justOutput = null;
>              for (int i=0, size=mixedContent.size(); i<size; i++) {
>                  content = mixedContent.get(i);
>                  // See if text, an element, a processing instruction or a comment
>                  if (content instanceof Comment) {
>                      printComment((Comment) content, out, indentLevel + 1);
> +                   justOutput = Comment.class;
>                  } else if (content instanceof String) {
> -                    printString((String)content, out);
> +                   if (padText && (justOutput == Element.class))
> +                       out.write(padTextString);
> +                   printString((String)content, out);
> +                   justOutput = String.class;
>                  } else if (content instanceof Element) {
> +                   if (padText && (justOutput == String.class))
> +                       out.write(padTextString);
>                      printElement((Element) content, out, indentLevel + 1, namespaces);
> +                   justOutput = Element.class;
>                  } else if (content instanceof Entity) {
>                      printEntity((Entity) content, out);
> +                   justOutput = Entity.class;
>                  } else if (content instanceof ProcessingInstruction) {
>                      printProcessingInstruction((ProcessingInstruction) content, out, indentLevel + 1);
> +                   justOutput = ProcessingInstruction.class;
>                  } else if (content instanceof CDATA) {
>                      printCDATASection((CDATA)content, out, indentLevel + 1);
> +                   justOutput = CDATA.class;
>                  }
>                  // Unsupported types are *not* printed
>              }
> @@ -1275,6 +1317,9 @@
>              else if (args[i].equals("-trimText")) {
>                  setTrimText(true);
>              }
> +            else if (args[i].equals("-padText")) {
> +                setPadText(true);
> +            }
>              else {
>                  return i;
>              }
> @@ -1319,4 +1364,4 @@
>          }
>      }
>      */
> -}
> \ No newline at end of file
> +}
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com



More information about the jdom-interest mailing list