Line wrapping

M

Mark

I need to write some code to wrap lines for printing. The data to
print is XML. I have not found that the classes that I have found
allow you to choose which characters are used as line breaks.
For example org.apache.xml.serialize.OutputFormat does not do this.

What class is good for this?

Right now I use the following code:

OutputFormat format = new OutputFormat(input);
format.setIndenting(true);
format.setIndent(2);
format.setLineWidth(lineLen);
format.setLineSeparator(lineSep);
Writer out = new StringWriter();
XMLSerializer serializer = new XMLSerializer(out, format);
serializer.serialize(input);
--
(\__/) M.
(='.'=) Due to the amount of spam posted via googlegroups and
(")_(") their inaction to the problem. I am blocking some articles
posted from there. If you wish your postings to be seen by
everyone you will need use a different method of posting.
 
S

Stefan Ram

Mark said:
I have not found that the classes that I have found
allow you to choose which characters are used as line breaks.
Right now I use the following code:
format.setLineSeparator(lineSep);

So, what effect does »format.setLineSeparator(lineSep)« have,
then?

If it can't be set beforehand, the string result of the
StringWriter »out« can be modified via search-and-replace
afterwards.
allow you to choose which characters are used as line breaks.

Maybe you meant »used to trigger line breaks«?
¯¯¯¯¯¯¯¯¯¯
 
J

Jeff Higgins

I need to write some code to wrap lines for printing. The data to
print is XML. I have not found that the classes that I have found
allow you to choose which characters are used as line breaks.
For example org.apache.xml.serialize.OutputFormat does not do this.

This may be of interest.
 
M

Mark

So, what effect does »format.setLineSeparator(lineSep)« have,
then?

On the test data, none that I can see. i.e. it produces lines longer
than that specified in setLineWidth()
--
(\__/) M.
(='.'=) Due to the amount of spam posted via googlegroups and
(")_(") their inaction to the problem. I am blocking some articles
posted from there. If you wish your postings to be seen by
everyone you will need use a different method of posting.
 
M

Mark


I've tried this code but it presents the whole XML document all in one
line. I am trying to get some nicely formatted output for sending to
a printer. The code snippet I posted nearly does this, except for the
line wrapping.
--
(\__/) M.
(='.'=) Due to the amount of spam posted via googlegroups and
(")_(") their inaction to the problem. I am blocking some articles
posted from there. If you wish your postings to be seen by
everyone you will need use a different method of posting.
 
J

Jeff Higgins

I've tried this code but it presents the whole XML document all in one
line. I am trying to get some nicely formatted output for sending to
a printer. The code snippet I posted nearly does this, except for the
line wrapping.
George Hawkins in a comment on the second answer in this question
provided the link above. He also suggests:
writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
<http://stackoverflow.com/questions/139076/how-to-pretty-print-xml-from-java>
I guess I should have pointed to this article to start but I don't know
how to point to individual posts or comments.
 
M

Mark

George Hawkins in a comment on the second answer in this question
provided the link above. He also suggests:
writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
<http://stackoverflow.com/questions/139076/how-to-pretty-print-xml-from-java>
I guess I should have pointed to this article to start but I don't know
how to point to individual posts or comments.

I saw that comment but, as I could not find documentation for that
parameter, I assumed that the poster was wrong. I will try it out but
prefer to rely on documented behaviour ;-)
--
(\__/) M.
(='.'=) Due to the amount of spam posted via googlegroups and
(")_(") their inaction to the problem. I am blocking some articles
posted from there. If you wish your postings to be seen by
everyone you will need use a different method of posting.
 
J

Jeff Higgins

I saw that comment but, as I could not find documentation for that
parameter, I assumed that the poster was wrong. I will try it out but
prefer to rely on documented behaviour ;-)
<http://docs.oracle.com/javase/6/docs/api/org/w3c/dom/DOMConfiguration.html>
<http://www.w3.org/TR/2004/REC-DOM-Level-3-LS-20040407/load-save.html#LS-LSSerializer-config>

Here's a working example. However this does not address your request for
a configurable pretty printer.
<http://www.chipkillmar.net/2009/03/25/pretty-print-xml-from-a-dom/>
 
M

Mark


Thanks for this. I had already implemented something similar to that
and I'll need to write my own line-wrapping code.


--
(\__/) M.
(='.'=) Due to the amount of spam posted via googlegroups and
(")_(") their inaction to the problem. I am blocking some articles
posted from there. If you wish your postings to be seen by
everyone you will need use a different method of posting.
 
M

Mark

Is there any known bugs that might affect this (Method 2)? It is
throwing an Exception/Error that the documentation does not mention.
[java.lang.AbstractMethodError]
--
(\__/) M.
(='.'=) Due to the amount of spam posted via googlegroups and
(")_(") their inaction to the problem. I am blocking some articles
posted from there. If you wish your postings to be seen by
everyone you will need use a different method of posting.
 
J

Jeff Higgins

Is there any known bugs that might affect this (Method 2)? It is
throwing an Exception/Error that the documentation does not mention.
[java.lang.AbstractMethodError]

Runs as expected for me using the authors source and example xml,
located here:

<http://www.chipkillmar.net/wp-content/uploads/2009/03/pretty-print-xml-src.jar>

and using this Java:
java version "1.6.0_18"
OpenJDK Runtime Environment (IcedTea6 1.8.10) (6b18-1.8.10-0+squeeze2)
OpenJDK Client VM (build 14.0-b16, mixed mode, sharing)

the docs say "shouldn't happen at runtime, except ..."
<http://docs.oracle.com/javase/6/docs/api/java/lang/AbstractMethodError.html>

there are no Uses listed here:
<http://docs.oracle.com/javase/6/docs/api/java/lang/class-use/AbstractMethodError.html>

stacktrace?
 
L

Lew

Mark said:
Is there any known bugs that might affect this (Method 2)? It is
throwing an Exception/Error that the documentation does not mention.
[java.lang.AbstractMethodError]

Now there's a load of detail, not!

'Exception's and 'Error's are different beasts. It's very sloppy to call them
"Exception/Error". Which?

What is the error message? What is the code? What is the stacktrace? Where's
your simple, self-contained compilable example (SSCCE http://sscce.org/)?
Where is *any* information about your situation that merits anyone giving any
response? "My arm hurts, doctor, what's wrong with me?"

Give data. Otherwise you might as well not bother posting.
 
J

Jeff Higgins

You're pretty far behind on security patches, aren't you?
Maybe that's why it works for me and not him? Shrug. :)
Wouldn't have been the first time functionality has been broken in updates.
 
J

Jeff Higgins

Is there any known bugs that might affect this (Method 2)? It is
throwing an Exception/Error that the documentation does not mention.
[java.lang.AbstractMethodError]
Just out of curiosity, does the author's Method#3 work for you?
Upthread you say:
So, what effect does »format.setLineSeparator(lineSep)« have,
On the test data, none that I can see. i.e. it produces lines longer
than that specified in setLineWidth()

It starts to sound as if you have an incompatible implementation on your
classpath.
 
J

Jeff Higgins

On Thu, 19 Jan 2012 06:26:37 -0500, Jeff Higgins

On 01/19/2012 04:00 AM, Mark wrote:
I saw that comment but, as I could not find documentation for that
parameter, I assumed that the poster was wrong. I will try it out but
prefer to rely on documented behaviour ;-)
<http://docs.oracle.com/javase/6/docs/api/org/w3c/dom/DOMConfiguration.html>

<http://www.w3.org/TR/2004/REC-DOM-Level-3-LS-20040407/load-save.html#LS-LSSerializer-config>


Here's a working example. However this does not address your request
for
a configurable pretty printer.
<http://www.chipkillmar.net/2009/03/25/pretty-print-xml-from-a-dom/>

Is there any known bugs that might affect this (Method 2)? It is
throwing an Exception/Error that the documentation does not mention.
[java.lang.AbstractMethodError]
Just out of curiosity, does the author's Method#3 work for you?
Upthread you say:
So, what effect does »format.setLineSeparator(lineSep)« have,
On the test data, none that I can see. i.e. it produces lines longer
than that specified in setLineWidth()

It starts to sound as if you have an incompatible implementation on your
classpath.
^ non-default
 
J

Jeff Higgins

Mark said:
Mark said:
Jeff Higgins wrote:
Mark wrote:
I saw that comment but, as I could not find documentation for that
parameter, I assumed that the poster was wrong. I will try it out but
prefer to rely on documented behaviour ;-)
<http://docs.oracle.com/javase/6/docs/api/org/w3c/dom/DOMConfiguration.html>

<http://www.w3.org/TR/2004/REC-DOM-Level-3-LS-20040407/load-save.html#LS-LSSerializer-config>


Here's a working example. However this does not address your request
for
a configurable pretty printer.
<http://www.chipkillmar.net/2009/03/25/pretty-print-xml-from-a-dom/>

Is there any known bugs that might affect this (Method 2)? It is
throwing an Exception/Error that the documentation does not mention.
[java.lang.AbstractMethodError]

Now there's a load of detail, not!

'Exception's and 'Error's are different beasts. It's very sloppy to call
them "Exception/Error". Which?
 
L

Lew

Jeff said:

Cute, but a red herring. This is not an 'Error', nor claimed to be in the
Javadocs. I concur that the name could have been better chosen.

So could its constructors. Notice that they don't even allow an 'Error' as a
constructor argument. This must have been a deliberate choice, given that they
could have easily carried forward the policy of the supertype constructor:
<http://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html#Exception(java.lang.String,
java.lang.Throwable)>

So it's crystal clear that the compound name part "Error" and the document
comments about "error" refer to the word in the natural language sense only,
and no confusion between 'java.lang.Error' and 'java.lang.Exception' should be
inferred.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top