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

Jason Hunter jhunter at servlets.com
Thu Sep 7 13:59:50 PDT 2006


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