Re: Fwd: Formatting differences after migrating to JDOM2
Rolf <[email protected]> Sun, 06 Oct 2013 16:05:59 -0400
| Newsgroups | gmane.comp.java.jdom.general |
|---|---|
| Message-ID | <[email protected]> |
This is a multi-part message in MIME format.
--===============1870515316==
Content-Type: multipart/alternative;
boundary="------------030400030902030509070400"
This is a multi-part message in MIME format.
--------------030400030902030509070400
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: quoted-printable
X-MIME-Autoconverted: from 8bit to quoted-printable by servlets.kattare.com id r96K6EXO011202
Hi Robert.
OK. I have spent some time going through things, and, admittedly, this=20
is confusing, and working through the combinations/permutations for=20
formatting is liable to end in a headache.
So, I think I have resolved that there are a number of issues at hand in=20
your case:
1. JDOM2 is doing different things than JDOM1
2. JDOM1 is probably doing the wrong thing in this case
3. JDOM2 is also probably doing the wrong thing, but, in fairness,=20
changing the 'TextMode' of a PrettyPrint format is a 'dangerous' thing=20
.... not by design, but because of the actual implementation and choices=20
the formatter makes with the pretty format.
4. If whitespace is significant for certain members of an XML document=20
then you should not be relying on the whim of JDOM to make things right,=20
but you should be using the xml:space=3D"preserve" mechanism that is=20
designed for this purpose.
So, here are a few 'answers'.
Answer 0:
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
The output you are getting from JDOM 1.x is broken. If you have a=20
'preserve' text mode then there should be no whitespace between any=20
elements (indenting/newlines) because that is not 'preserved' space=20
(it's 'invented' whitespace).
The JDOM output you currently get is relying on a bug in JDOM 1.x
Answer 1:
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
The "right" thing for you to do is to add the xml:space=3D"preserve" to=20
the sub2 elements:
public static void main(String argv[]) throws Exception{
Document document =3D new Document();
*Attribute cloneme =3D new Attribute("space", "preserve",=20
Namespace.XML_NAMESPACE);*
Element root =3D new Element("root");
document.addContent(root);
Element sub1 =3D new Element("sub1");
root.addContent(sub1);
sub1.addContent(new Element("sub2").setText("Some=20
text")*.setAttribute(cloneme.clone())*);
sub1.addContent(new Element("sub2").setText(" text with left=20
and right whitespace ")*.setAttribute(cloneme.clone())*);
Format fmt =3D Format.getPrettyFormat();
XMLOutputter xout =3D new XMLOutputter(fmt);
xout.output(document, System.out);
}
Gives the output:
<root>
<sub1>
<sub2 xml:space=3D"preserve">Some text</sub2>
<sub2 xml:space=3D"preserve"> text with left and right whitespace =20
</sub2>
</sub1>
</root>
Answer 2:
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
The "OK" thing for you to do is to use the TextMode.TRIM_FULL_WHITE=20
instead of TextMode.PRESERVE... the default TextMode for PrettyPrint is=20
TextMode.TRIM, which removes white-space from either-end of the text,=20
but the TRIM_FULL_WHITE will remove whitespace only when there's only=20
whitespace, and will do nothing if there's any non-whitespace=20
characters. I want you to be aware that other tools (JDOM, xmllint) have=20
the right to mess with the whitespace (=20
http://www.w3.org/TR/REC-xml/#sec-white-space ). It is only by=20
convention that the following will work in JDOM (I recommend preserving=20
whitespace correctly with xml:space=3D"preserve") :
public static void main(String argv[]) throws Exception{
Document document =3D new Document();
Element root =3D new Element("root");
document.addContent(root);
Element sub1 =3D new Element("sub1");
root.addContent(sub1);
sub1.addContent(new Element("sub2").setText("Some text"));
sub1.addContent(new Element("sub2").setText(" text with left=20
and right whitespace "));
Format fmt =3D Format.getPrettyFormat();
fmt.setTextMode(Format.TextMode.TRIM_FULL_WHITE);
XMLOutputter xout =3D new XMLOutputter(fmt);
xout.output(document, System.out);
}
Gives the output:
<root>
<sub1>
<sub2>Some text</sub2>
<sub2> text with left and right whitespace </sub2>
</sub1>
</root>
Answer 3:
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
JDOM 2.x uses a different (faster, and more flexible) algorithm for=20
output handling. This algorithm has two major triggers: The TextMode and=20
the Indent. PrettyPrint sets the TextMode to TRIM and the Indent to two=20
spaces " ". The TRIM mode tells JDOM it can mess with whitespace in=20
Text. The INDENT tells JDOM it can mess with the formatting of the XML=20
structure (setting it to null tells JDOM not to mess with any indenting).
You have been changing the TextMode to PRESERVE, and, as I think about=20
that, JDOM should never mess with the indenting when the mode is=20
PRESERVE. JDOM has code to make sure that it manages the INDENT and the=20
TextMode correctly when they need to change internally, but you are=20
basically setting an invalid situation by setting INDENT and PRESERVE at=20
the same time. JDOM should handle that better.
But, the right thing to do, is when you set PRESERVE, JDOM2 should=20
output the following:
<root><sub1><sub2>Some text</sub2><sub2> text with left and right=20
whitespace </sub2></sub1></root>
So, I think there's a bug in JDOM2, and, given the input you have=20
(Format.getPrettyFormat().setTextMode(TextMode.PRESERVE) ) It should be=20
outputting the above (which is not what you want).
Answer 4:
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
You can use the Raw format, and output the spaces yourself by adding=20
your own indenting and newlines.
On 06/10/2013 1:09 PM, Robert Kr=C3=BCger wrote:
> Hi Rolf,
>
> On Sat, Oct 5, 2013 at 2:08 AM, Rolf <[email protected]> wrote:
>> Hi Robert.
>>
>> Just so we are on the same page, when I run the code, I get the follow=
ing
>> output:
>>
>> with the setTextMode(...):
>> new
>> XMLOutputter(Format.getPrettyFormat().setTextMode(Format.TextMode.PRES=
ERVE)).output(document,
>> System.out);
>> <?xml version=3D"1.0" encoding=3D"UTF-8"?>
>> <root>
>> <sub1>
>> <sub2>
>> Some text
>> </sub2><sub2>
>>
>> text with left and right whitespace
>> </sub2>
>> </sub1>
>> </root>
>>
>>
>> without the setTextMode(...)
>> new XMLOutputter(Format.getPrettyFormat()).output(document,
>> System.out);
>> <?xml version=3D"1.0" encoding=3D"UTF-8"?>
>> <root>
>> <sub1>
>> <sub2>Some text</sub2>
>> <sub2>text with left and right whitespace</sub2>
>> </sub1>
>> </root>
>>
>> The plain "Pretty" format is the way I think you want the output, and =
it is
>> right, right?
> Yes, except for whitespace being trimmed. I do not want that but want
> indenting and no whitespace trimming for text-only elements (that was
> the behaviour of JDOM1). The use case is that I use xml to store data
> (e.g. user input of a content management system) and removing
> whitespace modifies the data, which I do not want to happen but I do
> want indenting.
>
>> It is very unusual for someone ysing the PrettyFormat to modify the
>> TextMode.... I wonder why you have the setTextMode() at all...
> see above.
>
>> Rolf
> Robert
>
>>
>>
>> On 30/09/2013 9:43 AM, Robert Kr=C3=BCger wrote:
>>> forgot to reply to the list
>>>
>>>
>>> ---------- Forwarded message ----------
>>> From: Robert Kr=C3=BCger <[email protected]>
>>> Date: Mon, Sep 30, 2013 at 3:42 PM
>>> Subject: Re: [jdom-interest] Formatting differences after migrating t=
o
>>> JDOM2
>>> To: Rolf <[email protected]>
>>>
>>>
>>> This reproduces the behaviour:
>>>
>>> import org.jdom2.Document;
>>> import org.jdom2.Element;
>>> import org.jdom2.output.Format;
>>> import org.jdom2.output.XMLOutputter;
>>>
>>> public class JDOMOutput {
>>>
>>> public static void main(String argv[]) throws Exception{
>>> Document document =3D new Document();
>>> Element root =3D new Element("root");
>>> document.addContent(root);
>>> Element sub1 =3D new Element("sub1");
>>> root.addContent(sub1);
>>> sub1.addContent(new Element("sub2").setText("Some text"));
>>> sub1.addContent(new Element("sub2").setText(" text with le=
ft
>>> and right whitespace "));
>>> new
>>> XMLOutputter(Format.getPrettyFormat().setTextMode(Format.TextMode.PRE=
SERVE)).output(document,
>>> System.out);
>>> }
>>>
>>> }
>>>
>>> Try with and without the setTextMode(Format.TextMode.PRESERVE). None
>>> of them does what I need.
>>>
>>> On Sun, Sep 29, 2013 at 7:10 PM, Robert Kr=C3=BCger <krueger@lesspain=
.de>
>>> wrote:
>>>> Hi,
>>>>
>>>> it is part of a large application. I will try to build a simple test
>>>> program that demonstrates the effect.
>>>>
>>>> Cheers,
>>>>
>>>> Robert
>>>>
>>>> On Sun, Sep 29, 2013 at 5:26 PM, Rolf <[email protected]> wrote:
>>>>> Hi Robert.
>>>>>
>>>>> This is surprising indeed, and I agree it should not be different f=
rom
>>>>> JDOM
>>>>> 1.x
>>>>>
>>>>> Can you get me a copy of the input file and the relevant parts of J=
ava
>>>>> code?
>>>>> You don't need to CC the whole list it is large...
>>>>>
>>>>> Thanks
>>>>>
>>>>> Rolf
>>>>>
>>>>>
>>>>> On 29/09/2013 10:42 AM, Robert Kr=C3=BCger wrote:
>>>>>> Hi,
>>>>>>
>>>>>> I just migrated my code to from JDOM to JDOM2 and noticed some of =
our
>>>>>> unit tests failed. The reason is different formatting. I used
>>>>>> Format.getPrettyFormat().setTextMode(PRESERVE) for the formatting =
and
>>>>>> with jdom this produced output like the following
>>>>>>
>>>>>> <av-container format-version=3D"0.3.4">
>>>>>> <container-format>MP4</container-format>
>>>>>> <bitrate>646448</bitrate>
>>>>>> <duration>2002002</duration>
>>>>>> <start-time>0</start-time>
>>>>>> <acquisition-timestamp>1340887741000</acquisition-timestamp>
>>>>>> <stream>
>>>>>> <type>VIDEO</type>
>>>>>> <codec>H.264</codec>
>>>>>> ...
>>>>>>
>>>>>> after replacing the imports by jdom2 I got
>>>>>>
>>>>>> <av-container format-version=3D"0.3.4">
>>>>>> <container-format>
>>>>>> MP4
>>>>>> </container-format><bitrate>
>>>>>> 646448
>>>>>> </bitrate><duration>
>>>>>> 2002002
>>>>>> </duration><start-time>
>>>>>> 0
>>>>>> </start-time><acquisition-timestamp>
>>>>>> 1340887741000
>>>>>> </acquisition-timestamp><stream>
>>>>>> <type>
>>>>>> VIDEO
>>>>>> </type><codec>
>>>>>> H.264
>>>>>> </codec>...
>>>>>>
>>>>>> This looks rather broken as it does not preserve the original data=
at
>>>>>> all with all those added newlines. Removing the setTextMode(PRESER=
VE)
>>>>>> restored the format to what is shown above but the reason I added
>>>>>> setTextMode(PRESERVE) was that without it, whitespace was trimmed =
and
>>>>>> I do not want that for elements with text content.
>>>>>>
>>>>>> Is this a bug? How can I achieve what I want, i.e. have a "pretty"=
,
>>>>>> i.e. indented format and have text-only elements preserve whitespa=
ce?
>>>>>>
>>>>>> Thanks in advance,
>>>>>>
>>>>>> Robert
>>>>>> _______________________________________________
>>>>>> To control your jdom-interest membership:
>>>>>> http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhos=
t.com
>>>>>>
>>> _______________________________________________
>>> To control your jdom-interest membership:
>>> http://www.jdom.org/mailman/options/jdom-interest/[email protected]=
om
>>
--------------030400030902030509070400
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
X-MIME-Autoconverted: from 8bit to quoted-printable by servlets.kattare.com id r96K6EXO011202
<html>
<head>
<meta content=3D"text/html; charset=3DUTF-8" http-equiv=3D"Content-Ty=
pe">
</head>
<body text=3D"#000000" bgcolor=3D"#FFFFFF">
<div class=3D"moz-cite-prefix">Hi Robert.<br>
<br>
OK. I have spent some time going through things, and, admittedly,
this is confusing, and working through the
combinations/permutations for formatting is liable to end in a
headache.<br>
<br>
So, I think I have resolved that there are a number of issues at
hand in your case:<br>
1. JDOM2 is doing different things than JDOM1<br>
2. JDOM1 is probably doing the wrong thing in this case<br>
3. JDOM2 is also probably doing the wrong thing, but, in fairness,
changing the 'TextMode' of a PrettyPrint format is a 'dangerous'
thing .... not by design, but because of the actual implementation
and choices the formatter makes with the pretty format.<br>
4. If whitespace is significant for certain members of an XML
document then you should not be relying on the whim of JDOM to
make things right, but you should be using the
xml:space=3D"preserve" mechanism that is designed for this purpose.=
<br>
<br>
So, here are a few 'answers'.<br>
<br>
Answer 0:<br>
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D<br>
The output you are getting from JDOM 1.x is broken. If you have a
'preserve' text mode then there should be no whitespace between
any elements (indenting/newlines) because that is not 'preserved'
space (it's 'invented' whitespace).<br>
<br>
The JDOM output you currently get is relying on a bug in JDOM 1.x<b=
r>
<br>
Answer 1:<br>
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D<br>
The "right" thing for you to do is to add the xml:space=3D"preserve=
"
to the sub2 elements:<br>
<br>
=C2=A0=C2=A0=C2=A0 public static void main(String argv[]) throws Ex=
ception{<br>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Document document =3D ne=
w Document();<br>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 <b>Attribute cloneme =3D=
new Attribute("space", "preserve",
Namespace.XML_NAMESPACE);</b><br>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Element root =3D new Ele=
ment("root");<br>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 document.addContent(root=
);<br>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Element sub1 =3D new Ele=
ment("sub1");<br>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 root.addContent(sub1);<b=
r>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 sub1.addContent(new Elem=
ent("sub2").setText("Some text")<b>.setAttribute(cloneme.clone())</b>);<b=
r>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 sub1.addContent(new Elem=
ent("sub2").setText("=C2=A0 text with
left and right whitespace=C2=A0 ")<b>.setAttribute(cloneme.clone())=
</b>);<br>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Format fmt =3D Format.ge=
tPrettyFormat();<br>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 XMLOutputter xout =3D ne=
w XMLOutputter(fmt);<br>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 xout.output(document, Sy=
stem.out);<br>
=C2=A0=C2=A0=C2=A0 }<br>
<br>
Gives the output:<br>
<br>
<root><br>
=C2=A0 <sub1><br>
=C2=A0=C2=A0=C2=A0 <sub2 xml:space=3D"preserve">Some text<=
/sub2><br>
=C2=A0=C2=A0=C2=A0 <sub2 xml:space=3D"preserve">=C2=A0 text w=
ith left and right
whitespace=C2=A0 </sub2><br>
=C2=A0 </sub1><br>
</root><br>
<br>
Answer 2:<br>
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D<br>
The "OK" thing for you to do is to use the
TextMode.TRIM_FULL_WHITE instead of TextMode.PRESERVE... the
default TextMode for PrettyPrint is TextMode.TRIM, which removes
white-space from either-end of the text, but the TRIM_FULL_WHITE
will remove whitespace only when there's only whitespace, and will
do nothing if there's any non-whitespace characters. I want you to
be aware that other tools (JDOM, xmllint) have the right to mess
with the whitespace (
<a class=3D"moz-txt-link-freetext" href=3D"http://www.w3.org/TR/REC=
-xml/#sec-white-space">http://www.w3.org/TR/REC-xml/#sec-white-space</a> =
). It is only by
convention that the following will work in JDOM (I recommend
preserving whitespace correctly with xml:space=3D"preserve") :<br>
<br>
=C2=A0=C2=A0=C2=A0 public static void main(String argv[]) throws Ex=
ception{<br>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Document document =3D ne=
w Document();<br>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Element root =3D new Ele=
ment("root");<br>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 document.addContent(root=
);<br>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Element sub1 =3D new Ele=
ment("sub1");<br>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 root.addContent(sub1);<b=
r>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 sub1.addContent(new Elem=
ent("sub2").setText("Some text"));<br>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 sub1.addContent(new Elem=
ent("sub2").setText("=C2=A0 text with
left and right whitespace=C2=A0 "));<br>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Format fmt =3D Format.ge=
tPrettyFormat();<br>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 fmt.setTextMode(Format.T=
extMode.TRIM_FULL_WHITE);<br>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 XMLOutputter xout =3D ne=
w XMLOutputter(fmt);<br>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 xout.output(document, Sy=
stem.out);<br>
=C2=A0=C2=A0=C2=A0 }<br>
<br>
Gives the output:<br>
<br>
<root><br>
=C2=A0 <sub1><br>
=C2=A0=C2=A0=C2=A0 <sub2>Some text</sub2><br>
=C2=A0=C2=A0=C2=A0 <sub2>=C2=A0 text with left and right whit=
espace=C2=A0
</sub2><br>
=C2=A0 </sub1><br>
</root><br>
<br>
Answer 3:<br>
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D<br>
JDOM 2.x uses a different (faster, and more flexible) algorithm
for output handling. This algorithm has two major triggers: The
TextMode and the Indent. PrettyPrint sets the TextMode to TRIM and
the Indent to two spaces "=C2=A0 ". The TRIM mode tells JDOM it can
mess with whitespace in Text. The INDENT tells JDOM it can mess
with the formatting of the XML structure (setting it to null tells
JDOM not to mess with any indenting).<br>
You have been changing the TextMode to PRESERVE, and, as I think
about that, JDOM should never mess with the indenting when the
mode is PRESERVE. JDOM has code to make sure that it manages the
INDENT and the TextMode correctly when they need to change
internally, but you are basically setting an invalid situation by
setting INDENT and PRESERVE at the same time. JDOM should handle
that better.<br>
<br>
But, the right thing to do, is when you set PRESERVE, JDOM2 should
output the following:<br>
<root><sub1><sub2>Some
text</sub2><sub2>=C2=A0 text with left and right
whitespace=C2=A0 </sub2></sub1></root><br>
<br>
So, I think there's a bug in JDOM2, and, given the input you have
(Format.getPrettyFormat().setTextMode(TextMode.PRESERVE) ) It
should be outputting the above (which is not what you want).<br>
<br>
Answer 4:<br>
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D<br>
You can use the Raw format, and output the spaces yourself by
adding your own indenting and newlines.<br>
<br>
<br>
<br>
<br>
<br>
On 06/10/2013 1:09 PM, Robert Kr=C3=BCger wrote:<br>
</div>
<blockquote
cite=3D"mid:[email protected]=
ail.com"
type=3D"cite">
<pre wrap=3D"">Hi Rolf,
On Sat, Oct 5, 2013 at 2:08 AM, Rolf <a class=3D"moz-txt-link-rfc2396E" h=
ref=3D"mailto:[email protected]"><[email protected]></a> wrote:
</pre>
<blockquote type=3D"cite">
<pre wrap=3D"">Hi Robert.
Just so we are on the same page, when I run the code, I get the following
output:
with the setTextMode(...):
new
XMLOutputter(Format.getPrettyFormat().setTextMode(Format.TextMode.PRESERV=
E)).output(document,
System.out);
<?xml version=3D"1.0" encoding=3D"UTF-8"?>
<root>
<sub1>
<sub2>
Some text
</sub2><sub2>
text with left and right whitespace
</sub2>
</sub1>
</root>
without the setTextMode(...)
new XMLOutputter(Format.getPrettyFormat()).output(document,
System.out);
<?xml version=3D"1.0" encoding=3D"UTF-8"?>
<root>
<sub1>
<sub2>Some text</sub2>
<sub2>text with left and right whitespace</sub2>
</sub1>
</root>
The plain "Pretty" format is the way I think you want the output, and it =
is
right, right?
</pre>
</blockquote>
<pre wrap=3D"">
Yes, except for whitespace being trimmed. I do not want that but want
indenting and no whitespace trimming for text-only elements (that was
the behaviour of JDOM1). The use case is that I use xml to store data
(e.g. user input of a content management system) and removing
whitespace modifies the data, which I do not want to happen but I do
want indenting.
</pre>
<blockquote type=3D"cite">
<pre wrap=3D"">
It is very unusual for someone ysing the PrettyFormat to modify the
TextMode.... I wonder why you have the setTextMode() at all...
</pre>
</blockquote>
<pre wrap=3D"">
see above.
</pre>
<blockquote type=3D"cite">
<pre wrap=3D"">
Rolf
</pre>
</blockquote>
<pre wrap=3D"">
Robert
</pre>
<blockquote type=3D"cite">
<pre wrap=3D"">
On 30/09/2013 9:43 AM, Robert Kr=C3=BCger wrote:
</pre>
<blockquote type=3D"cite">
<pre wrap=3D"">
forgot to reply to the list
---------- Forwarded message ----------
From: Robert Kr=C3=BCger <a class=3D"moz-txt-link-rfc2396E" href=3D"mailt=
o:[email protected]"><[email protected]></a>
Date: Mon, Sep 30, 2013 at 3:42 PM
Subject: Re: [jdom-interest] Formatting differences after migrating to
JDOM2
To: Rolf <a class=3D"moz-txt-link-rfc2396E" href=3D"mailto:[email protected]"=
><[email protected]></a>
This reproduces the behaviour:
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
public class JDOMOutput {
public static void main(String argv[]) throws Exception{
Document document =3D new Document();
Element root =3D new Element("root");
document.addContent(root);
Element sub1 =3D new Element("sub1");
root.addContent(sub1);
sub1.addContent(new Element("sub2").setText("Some text"));
sub1.addContent(new Element("sub2").setText(" text with left
and right whitespace "));
new
XMLOutputter(Format.getPrettyFormat().setTextMode(Format.TextMode.PRESERV=
E)).output(document,
System.out);
}
}
Try with and without the setTextMode(Format.TextMode.PRESERVE). None
of them does what I need.
On Sun, Sep 29, 2013 at 7:10 PM, Robert Kr=C3=BCger <a class=3D"moz-txt-l=
ink-rfc2396E" href=3D"mailto:[email protected]"><[email protected]=
></a>
wrote:
</pre>
<blockquote type=3D"cite">
<pre wrap=3D"">
Hi,
it is part of a large application. I will try to build a simple test
program that demonstrates the effect.
Cheers,
Robert
On Sun, Sep 29, 2013 at 5:26 PM, Rolf <a class=3D"moz-txt-link-rfc2396E" =
href=3D"mailto:[email protected]"><[email protected]></a> wrote:
</pre>
<blockquote type=3D"cite">
<pre wrap=3D"">
Hi Robert.
This is surprising indeed, and I agree it should not be different from
JDOM
1.x
Can you get me a copy of the input file and the relevant parts of Java
code?
You don't need to CC the whole list it is large...
Thanks
Rolf
On 29/09/2013 10:42 AM, Robert Kr=C3=BCger wrote:
</pre>
<blockquote type=3D"cite">
<pre wrap=3D"">
Hi,
I just migrated my code to from JDOM to JDOM2 and noticed some of our
unit tests failed. The reason is different formatting. I used
Format.getPrettyFormat().setTextMode(PRESERVE) for the formatting and
with jdom this produced output like the following
<av-container format-version=3D"0.3.4">
<container-format>MP4</container-format>
<bitrate>646448</bitrate>
<duration>2002002</duration>
<start-time>0</start-time>
<acquisition-timestamp>1340887741000</acquisition-timestamp&=
gt;
<stream>
<type>VIDEO</type>
<codec>H.264</codec>
...
after replacing the imports by jdom2 I got
<av-container format-version=3D"0.3.4">
<container-format>
MP4
</container-format><bitrate>
646448
</bitrate><duration>
2002002
</duration><start-time>
0
</start-time><acquisition-timestamp>
1340887741000
</acquisition-timestamp><stream>
<type>
VIDEO
</type><codec>
H.264
</codec>...
This looks rather broken as it does not preserve the original data at
all with all those added newlines. Removing the setTextMode(PRESERVE)
restored the format to what is shown above but the reason I added
setTextMode(PRESERVE) was that without it, whitespace was trimmed and
I do not want that for elements with text content.
Is this a bug? How can I achieve what I want, i.e. have a "pretty",
i.e. indented format and have text-only elements preserve whitespace?
Thanks in advance,
Robert
_______________________________________________
To control your jdom-interest membership:
<a class=3D"moz-txt-link-freetext" href=3D"http://www.jdom.org/mailman/op=
tions/jdom-interest/[email protected]">http://www.jdom.org/mailman/op=
tions/jdom-interest/[email protected]</a>
</pre>
</blockquote>
</blockquote>
</blockquote>
<pre wrap=3D"">_______________________________________________
To control your jdom-interest membership:
<a class=3D"moz-txt-link-freetext" href=3D"http://www.jdom.org/mailman/op=
tions/jdom-interest/[email protected]">http://www.jdom.org/mailman/op=
tions/jdom-interest/[email protected]</a>
</pre>
</blockquote>
<pre wrap=3D"">
</pre>
</blockquote>
<pre wrap=3D"">
</pre>
</blockquote>
<br>
</body>
</html>
--------------030400030902030509070400--
--===============1870515316==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Disposition: inline
Content-Transfer-Encoding: 7bit
_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/[email protected]
--===============1870515316==--