[jdom-interest] setIndent("") doesn't work as expected

wkrick at eio-online.com wkrick at eio-online.com
Thu Sep 7 13:26:32 PDT 2006


In digging into this further, I found some more things that don't make  
any sense.

In Format.java setLineSeparator(), the docs say...

      * This will set the newline separator (<code>lineSeparator</code>).
      * The default is <code>\r\n</code>. Note that if the "newlines"
      * property is false, this value is irrelevant.

As far as I can see, there is no "newlines" property.


Then in XMLOutputter.java...

     /**
      * This will print a new line only if the newlines flag was set to
      * true.
      *
      * @param out <code>Writer</code> to use
      */
     private void newline(Writer out) throws IOException {
         if (currentFormat.indent != null) {
             out.write(currentFormat.lineSeparator);
         }
     }

     /**
      * This will print indents (only if the newlines flag was
      * set to <code>true</code>, and indent is non-null).
      *
      * @param out <code>Writer</code> to use
      * @param level current indent level (number of tabs)
      */
     private void indent(Writer out, int level) throws IOException {
         if (currentFormat.indent == null ||
             currentFormat.indent.equals("")) {
             return;
         }

         for (int i = 0; i < level; i++) {
             out.write(currentFormat.indent);
         }
     }


...notice that neither method actually tests a "newlines" flag for  
true, they only test "indent" for null.


In order to really be able to control the output, you need to be able  
to have these options...

1) newlines and indents (1 or more spaces)
2) newlines and no indents (0 spaces)
3) neither

It doesn't make sense to have indents with no newlines.

Judging from the documentation, I believe this was what was originally  
intended but someone tried to simplify the logic somewhere and  
inadvertently removed the ability to have option #2.  I think they  
were trying to overload the meaning of indents=null to mean  
newlines=false.  Probably not a good idea.


I'd like to make a patch to put that functionality back in but I don't  
want to break anything if I can avoid it.  Do you have a set of test  
scripts/programs that you run against it to verify that things aren't  
broken after changes are made?







Quoting Jason Hunter <jhunter at servlets.com>:

> Well, for the time being you can just change the Format class code itself.
>
> Is it a bug?  I'd call it an RFE more than a bug.  It's behaving as
> expected, we just never anticipated your use case.  I don't see a
> problem with enhancing JDOM to differentiate between null and empty.
> Why don't you try changing Format internally, do a build, and let us
> know if it fixes your problem.  Then send in the change as a patch file.
>
> -jh-
>
> wkrick at eio-online.com wrote:
>> Hmmm...  I can't subclass Format because the constructor is private.
>> Any other ideas?
>>
>>
>> Quoting wkrick at eio-online.com:
>>
>>> So is this a bug or is it by design?
>>>
>>> If it's a bug, how do I make sure it gets fixed in the JDOM 1.1 release?
>>>
>>>
>>>
>>> Quoting Laurent Bihanic <laurent.bihanic at atosorigin.com>:
>>>
>>>> The problem comes from XMLOutputter itself:
>>>>   private void newline(Writer out) throws IOException {
>>>>       if (currentFormat.indent != null) {
>>>>           out.write(currentFormat.lineSeparator);
>>>>       }
>>>>   }
>>>> No indent, no newlines!
>>>>
>>>> The easiest way to fix this is to create your own subclass of Format
>>>> and override setIndent() to distinguish between "" and null.
>>>> Here's the current code:
>>>>   public Format setIndent(String indent) {
>>>>       // if passed the empty string, change it to null, for marginal
>>>>       // performance gains later (can compare to null first instead
>>>>       // of calling equals())
>>>>       if ("".equals(indent)) {
>>>>           indent = null;
>>>>       }
>>>>       this.indent = indent;
>>>>       return this;
>>>>   }
>>>> Remove the test on "" and XMLOutputter will behave as you expect.
>>>>
>>>> Laurent
>>>>
>>>> wkrick at eio-online.com a écrit :
>>>>> I'm currently outputting my document with the PrettyFormat...
>>>>>
>>>>> Format fmt = Format.getPrettyFormat();
>>>>> fmt.setTextMode(Format.TextMode.PRESERVE);
>>>>> XMLOutputter outputter = new XMLOutputter(fmt);
>>>>>
>>>>> ...and it works as expected, outputting one start/end tag per line.
>>>>> However, when I try to remove the indents on each line like this...
>>>>>
>>>>> Format fmt = Format.getPrettyFormat();
>>>>> fmt.setTextMode(Format.TextMode.PRESERVE);
>>>>> fmt.setIndent("");
>>>>> XMLOutputter outputter = new XMLOutputter(fmt);
>>>>>
>>>>> ...it removes all indents _AND_ newlines, compressing my whole    
>>>>>   document into a single line of output.
>>>>>
>>>>> I'm confused...
>>>>>
>>>>> fmt.setIndent("  "); adds a two space indent
>>>>> fmt.setIndent(" "); adds a one space indent
>>>>> fmt.setIndent(""); no indent and removes newline on previous line?
>>>>>
>>>>> I'm suspecting that the problem has something to do with...
>>>>>
>>>>> fmt.setTextMode(Format.TextMode.PRESERVE);
>>>>>
>>>>> ...but I need that mode to keep padding that's present in my document.
>>>>>
>>>>>
>>>>> Basically, given a document that looks like this...
>>>>>
>>>>> <FOO><BAR>    12345</BAR><BAR>    67890</BAR></FOO>
>>>>>
>>>>> ...I need the output to look like this...
>>>>>
>>>>> <FOO>
>>>>> <BAR>    12345</BAR>
>>>>> <BAR>    67890</BAR>
>>>>> </FOO>
>>>>>
>>>>> ...instead of this...
>>>>>
>>>>> <FOO>
>>>>> <BAR>    12345</BAR>
>>>>> <BAR>    67890</BAR>
>>>>> </FOO>
>>>>>
>>>>> ...but I can't seem to find the magic incantation that works.
>>>>>
>>>>> Is this possible?
>>>> _______________________________________________
>>>> To control your jdom-interest membership:
>>>> http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com
>>
>>
>>
>> _______________________________________________
>> To control your jdom-interest membership:
>> http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com
>>





More information about the jdom-interest mailing list