0

I am upgrading my code jdom to jdom2.0.5. My previous code was,

JDOM 1.0

XMLOutputter outputter = new XMLOutputter("  ", true);
outputter.setIndent(true);

Now I am using Format class like the following,

JDOM 2.0.5

Format format = Format.getRawFormat();
format.setIndent("  ");
format.setTextMode(Format.TextMode.TRIM);
XMLOutputter outputter = new XMLOutputter(format);

or I can use Format.getPrettyFormat().

If I removed the "format.setTextMode(Format.TextMode.TRIM)" line from my new code it not compatible with the old behavior. If I use TRIM then it gives output like my old behavior. But I didn't use TRIM part in my previous code.

My previous code and If I included TRIM in my new code, it gives output like the following,

<Config>
  <Description>Basic 01</Description>
  <CartViews>BasicAndDetailed</CartViews>
  <CartView>Basic</CartView>
  <DetailsInReview>true</DetailsInReview>
  <HeaderInReview>true</HeaderInReview>
  <AddressVisibility>Hide</AddressVisibility>
  <Visibility>Hide</Visibility>
</Config>

If I removed the TRIM part in my new code it gives the output like the following,

<Config>
  <Description>
    Basic 01
  </Description><CartViews>
    BasicAndDetailed
  </CartViews><CartView>
    Basic
  </CartView><DetailsInReview>
    true
  </DetailsInReview><HeaderInReview>
    true
  </HeaderInReview><AddressVisibility>
    Hide
  </AddressVisibility><Visibility>
    Hide
  </Visibility>
</Config>

Which noted as wrong behavior.

I couldn't find the reason why TRIM is needed.

Can you please help me for this?

user1942241
  • 73
  • 1
  • 5

1 Answers1

0

This is the second time I have heard (I maintain JDOM) of someone using Format this way... and getting differences between JDOM 1.x and 2.x.

BUT: You were not using JDOM 1.x in a way that is familiar.... the constructor new XMLOutputter(" ", true); does not exist.... where did you get that from?

Anyway, the TextMode.RAW mechanism (the default) has a very different output process than the other TextMode options. The reality is that there is a potential bug with TextMode.RAW which should always ignore the setIndent() value because indents should always be ignored.

So, I believe Format.getPrettyFormat() is what you want, so you should just use it.

If you want to discuss this more feel free to mail the jdom-interest mailing list and we can correspond directly.

rolfl
  • 17,539
  • 7
  • 42
  • 76
  • Hi, Thanks for you quick reply. When using Format.getPrettyFormat(), the method it self the setIndent(STANDARD_INDENT) and setTextMode(TextMode.TRIM) methods are get executed. so no need to set these explicitly. My doubt is why we need to set TextMode.TRIM. – user1942241 Oct 31 '13 at 13:09
  • If we set the setTextMode(TextMode.TRIM) then only it behaves correctly. Please correct me if I am doing any wrong. – user1942241 Oct 31 '13 at 13:15
  • My previous version of JDOM is Beta 8, dated 2002 or 2003. The latest release is 1.1.1 dated 7/26/2009. – user1942241 Oct 31 '13 at 13:17
  • Try TextMode.TRIM_FULL_WHITE if you do not want to trim everything. – rolfl Oct 31 '13 at 13:27
  • Ya this also work properly. But in previous version there is no need to set TextMode. Why we need to set in JDOM2 and what is use of it? – user1942241 Oct 31 '13 at 13:34
  • This is going back more than 10 years, to a beta-version of JDOM. All things considered I don't think the statement "But in previous version ..." is really fair. The current version is compatible with the last 8 years worth of versions, so I can't change the current version to break. What do you think the right solution would be to your concerns? – rolfl Oct 31 '13 at 14:02