Skip to Content.
Sympa Menu

xom-interest - Re: [XOM-interest] Serialization API

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: Dmitry Katsubo <dma_k AT mail.ru>
  • To: xom-interest AT lists.ibiblio.org
  • Subject: Re: [XOM-interest] Serialization API
  • Date: Wed, 29 Sep 2010 20:15:42 +0200

Dear Elliotte,

Thanks for great remarks!

On 29.09.2010 15:00, Elliotte Rusty Harold wrote:
> On Wed, Sep 29, 2010 at 7:53 AM, Dmitry Katsubo <dma_k AT mail.ru> wrote:
>
>> I attach a test case, that serializes two XML models via XOM and DOMv3
>> with same result. Please, point me exactly the line I am wrong or which
>> has a pitfall that most of programmers will hit.
>
> Your code uses a StringWriter. Problems arise with something like this:
>
> Writer out = new OutputStreamWriter(new ByteArrayOutputStream(),
> "ISO-8859-1");

This case worked fine for me. If you specify the encoding for output
stream, it works OK. Diff is attached.

> or even just, depending on platform:
>
> Writer out = new OutputStreamWriter(new ByteArrayOutputStream());

I agree. This may trigger problems. I would rather say it is not a
problem of a library, but a misuse of Writer. But why do I need to wrap
OutputStream into Writer, if I can pass OutputStream directly to XOM
API? And from the other side, if I write the code like you suggest, I
know what I am doing, so it not XOM to blame on what will happen to
Writer after the serialization is done (whether the stream goes to byte
array or string or pipe or ...).

> The problem is I have no good way to tell which characters do and do
> not need to be escaped given the Writer. A StringWriter can write
> anything, as can a CharArrayWriter, but most other writers can't.

I think, the same characters should be escaped as for OutputStream (no
difference). Unescaped characters should be left in UTF8.

--
With best regards,
Dmitry
--- a/SerializationTest.java Wed Sep 29 19:59:04 2010
+++ b/SerializationTest.java Wed Sep 29 20:00:01 2010
@@ -1,6 +1,6 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
-import java.io.OutputStreamWriter;
+import java.io.StringWriter;
import java.io.UnsupportedEncodingException;

import javax.xml.parsers.DocumentBuilderFactory;
@@ -133,10 +133,10 @@
(new Serializer(xomOutputStream,
encodings[i])).write(xomDocument);

// DOM:
- ByteArrayOutputStream domOutputStream = new
ByteArrayOutputStream();
+ StringWriter domWriter = new StringWriter();

LSOutput lsOutput = lsImpl.createLSOutput();
- lsOutput.setCharacterStream(new
OutputStreamWriter(domOutputStream, encodings[i]));
+ lsOutput.setCharacterStream(domWriter);
lsOutput.setEncoding(encodings[i]);

lsImpl.createLSSerializer().write(w3cDocument,
lsOutput);
@@ -144,8 +144,8 @@
// byte-by-byte comparison:
Assert.assertTrue(
"Failed char test for
encoding " + encodings[i],
-
compareByteArraysIgnoringNewlinesAndCapitalization(xomOutputStream.toByteArray(),
-
domOutputStream.toByteArray()));
+
compareByteArraysIgnoringNewlinesAndCapitalization(xomOutputStream.toByteArray(),
domWriter
+
.toString().getBytes(encodings[i])));
}
}




Archive powered by MHonArc 2.6.24.

Top of Page