XML Writer not pretty printing

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

XML Writer not pretty printing

Peter Uhnak
Hi,

I am trying to output a pretty printed document, but it seems that the configuration is being ignored

doc := '<root>
<tab/>
  <twoSpaces/>
    <fourSpaces/>
</root>' parseXML.

String << [ :stream |
doc
printOn: stream
beforeWritingDo: [ :writer | 
writer
enablePrettyPrinting;
enablePlatformSpecificLineBreak.
writer formatter indentString: '  '. ] ]


produces

 "'<root>
<tab/>
  <twoSpaces/>
    <fourSpaces/>
</root>'"

I thought that maybe #preservesIgnorableWhitespace: has something to do with it, but whether it is true or false, the output is not pretty printed (not to mention, that pretty printing should ignore any preserved whitespaces anyway).

Is this a bug? Should I print it in a different way?

Thanks,
Peter
Reply | Threaded
Open this post in threaded view
|

Re: XML Writer not pretty printing

Carlo-2
Hi Peter

It seems that #preservesIgnorableWhitespace: is only adhered to "...When validation is enabled and a DTD with element declarations is present..." (See comment in SAX2ContentHandler>>ignorableWhitespace:) The actual parsing code is in SAXParserDriver>>handleWhitespace:

What you could do is use #removeAllFormattingNodes but your-mileage-may-vary...

parser := XMLDOMParser
        on:'<root>
        <tab/>
  <twoSpaces/>
    <fourSpaces/>
</root>'.

(parser parseDocument)
        removeAllFormattingNodes;
        prettyPrinted.

Regards
Carlo

On 14 Aug 2018, at 13:08, Peter Uhnak <[hidden email]> wrote:

Hi,

I am trying to output a pretty printed document, but it seems that the configuration is being ignored

doc := '<root>
        <tab/>
  <twoSpaces/>
    <fourSpaces/>
</root>' parseXML.

String << [ :stream |
        doc
                printOn: stream
                beforeWritingDo: [ :writer |
                        writer
                                enablePrettyPrinting;
                                enablePlatformSpecificLineBreak.
                        writer formatter indentString: '  '. ] ]


produces

 "'<root>
        <tab/>
  <twoSpaces/>
    <fourSpaces/>
</root>'"

I thought that maybe #preservesIgnorableWhitespace: has something to do with it, but whether it is true or false, the output is not pretty printed (not to mention, that pretty printing should ignore any preserved whitespaces anyway).

Is this a bug? Should I print it in a different way?

Thanks,
Peter


Reply | Threaded
Open this post in threaded view
|

Re: XML Writer not pretty printing

Peter Uhnak
Thanks! #removeAllFormattingNodes did the trick.

Perhaps #prettyPrinted / #enablePrettyPrinting should call it by default?

WIth regards,
Peter

On Tue, Aug 14, 2018 at 10:27 PM Carlo <[hidden email]> wrote:
Hi Peter

It seems that #preservesIgnorableWhitespace: is only adhered to "...When validation is enabled and a DTD with element declarations is present..." (See comment in SAX2ContentHandler>>ignorableWhitespace:) The actual parsing code is in SAXParserDriver>>handleWhitespace:

What you could do is use #removeAllFormattingNodes but your-mileage-may-vary...

parser := XMLDOMParser
        on:'<root>
        <tab/>
  <twoSpaces/>
    <fourSpaces/>
</root>'.

(parser parseDocument)
        removeAllFormattingNodes;
        prettyPrinted.

Regards
Carlo

On 14 Aug 2018, at 13:08, Peter Uhnak <[hidden email]> wrote:

Hi,

I am trying to output a pretty printed document, but it seems that the configuration is being ignored

doc := '<root>
        <tab/>
  <twoSpaces/>
    <fourSpaces/>
</root>' parseXML.

String << [ :stream |
        doc     
                printOn: stream
                beforeWritingDo: [ :writer |
                        writer
                                enablePrettyPrinting;
                                enablePlatformSpecificLineBreak.
                        writer formatter indentString: '  '. ] ]


produces

 "'<root>
        <tab/>
  <twoSpaces/>
    <fourSpaces/>
</root>'"

I thought that maybe #preservesIgnorableWhitespace: has something to do with it, but whether it is true or false, the output is not pretty printed (not to mention, that pretty printing should ignore any preserved whitespaces anyway).

Is this a bug? Should I print it in a different way?

Thanks,
Peter