[jdom-interest] XMLOutputter & StringWriter

Elliotte Rusty Harold elharo at metalab.unc.edu
Wed Jul 5 18:20:51 PDT 2000

>But what about the case where you want to output a document and all
>you're given is a Writer?  Such a situation happens sometimes with
>servlets, where nested includes can force you to use a Writer.  If the
>outside servlet used a Writer and you call getOutputStream() it'll throw
>an exception.

Yuck. Here's the problem I see:

1. You want to write an XML document using a Writer you yourself did 
not create.

2. The Writer uses some encoding, maybe UTF-8, maybe UTF-16, maybe 
something else. You don't know what.

3. If the Writer is using some Unicode variant, then you can just 
write it out, leave off the encoding declaration, and the processor 
that eventually reads the document will probably be able to 
auto-detect the encoding, especially if we always include a byte 
order mark.

4. However, if the Writer is writing anything other than Unicode, we 
MUST include an encoding declaration, but we don't know what encoding 
to declare! And the Writer won't tell us!

I just don't see how we can do the right thing given the existing API 
of the Writer class. I agree that servlets are an important use case. 
Would this use case be satisfied if we had a method that was declared 
to take OutputStreamWriter rather than Writer so that we could find 
out what encoding it  was using?

| Elliotte Rusty Harold | elharo at metalab.unc.edu | Writer/Programmer |
|                  The XML Bible (IDG Books, 1999)                   |
|              http://metalab.unc.edu/xml/books/bible/               |
|   http://www.amazon.com/exec/obidos/ISBN=0764532367/cafeaulaitA/   |
|  Read Cafe au Lait for Java News:  http://metalab.unc.edu/javafaq/ |
|  Read Cafe con Leche for XML News: http://metalab.unc.edu/xml/     |

More information about the jdom-interest mailing list