XML 1.0 serialization [Re: Re: kxml / junit]
Aleksander Slominski <[email protected]> Mon, 09 Feb 2004 15:17:17 -0500
| Newsgroups | gmane.text.xml.xmlpull.devel |
|---|---|
| Message-ID | <[email protected]> |
Stefan Haustein wrote:
>Aleksander Slominski wrote:
>
>
>>i think baseline shoul dbe XML 1.0?
>>
>>
>
>OK
>
>
so lets get serializer to write correct XML 1.0 output as much as
possible without requiring writing too much code in case of J2ME.
> > in XML 1.0
>
>
>>\r -> (to respect EOL as otherwise it may get normalized!)
>>
>>
>
>I disagree: If you want to avoid normalization, use a call to entityRef().
>Otherwise, it is impossible to write text documents that match the line end
>encoding of the windows platform. Editors expecting \r\n will open the whole
>document in a single line if \r is escaped.
>
>
so you would do no escaping for \r\n\t for text()? this seems what
Xerces 2 is doing (see below) so i think it is reasonable think to do.
however for attributes i think we have to do escaping otherwise all
\r\n\t is normalized to spaces by XML parser. and that is exactly what
Xerces2 is doing (see below) so it looks reasonable for me to do this as
well.
>Ok, lets stick with 1.0 for the default behaviour....
>
>
please verify that unit tests in TestSerialize... i have just updated
them to checking correctly that serializer does nto escape \r ...
thanks,
alek
ps. here is how it is done in Xerces2 serializer - it seems thaey print
\r and \n unescaped ...
org.apache.xml.serialize.BaseMarkupSerializer
...
protected Printer _printer;
...
protected void printText( char[] chars, int start, int length,
boolean preserveSpace, boolean
unescaped )
throws IOException
{
int index;
char ch;
if ( preserveSpace ) {
// Preserving spaces: the text must print exactly as it is,
// without breaking when spaces appear in the text and without
// consolidating spaces. If a line terminator is used, a line
// break will occur.
while ( length-- > 0 ) {
ch = chars[ start ];
++start;
if ( ch == '\n' || ch == '\r' || unescaped )
_printer.printText( ch );
else
printEscaped( ch );
}
...
protected void printEscaped( int ch )
throws IOException
{
String charRef;
// If there is a suitable entity reference for this
// character, print it. The list of available entity
// references is almost but not identical between
// XML and HTML.
charRef = getEntityRef( ch );
if ( charRef != null ) {
_printer.printText( '&' );
_printer.printText( charRef );
_printer.printText( ';' );
} else if ( ( ch >= ' ' && _encodingInfo.isPrintable((char)ch)
&& ch != 0xF7 ) ||
ch == '\n' || ch == '\r' || ch == '\t' ) {
// Non printables are below ASCII space but not tab or line
// terminator, ASCII delete, or above a certain Unicode
threshold.
if (ch < 0x10000) {
_printer.printText((char)ch );
} else {
_printer.printText((char)(((ch-0x10000)>>10)+0xd800));
_printer.printText((char)(((ch-0x10000)&0x3ff)+0xdc00));
}
} else {
printHex(ch);
}
}
and attributes:
org.apache.xml.serializeXMLSerializer extends BaseMarkupSerializer;
...
value = attrs.getValue( i );
if (value == null)
value = "";
_printer.printText( name );
_printer.printText( "=\"" );
printEscaped( value );
_printer.printText( '"' );
...
//
// Printing attribute value
//
protected void printEscaped(String source) throws IOException {
int length = source.length();
for (int i = 0; i < length; ++i) {
int ch = source.charAt(i);
if (!XMLChar.isValid(ch)) {
if (++i < length) {
surrogates(ch, source.charAt(i));
} else {
fatalError("The character '" + (char) ch + "' is an
invalid XML character");
}
continue;
}
// escape NL, CR, TAB
if (ch == '\n' || ch == '\r' || ch == '\t') {
printHex(ch);
} else if (ch == '<') {
_printer.printText("<");
} else if (ch == '&') {
_printer.printText("&");
} else if (ch == '"') {
_printer.printText(""");
} else if ((ch >= ' ' && _encodingInfo.isPrintable((char)
ch))) {
_printer.printText((char) ch);
} else {
printHex(ch);
}
}
}
--
The best way to predict the future is to invent it - Alan Kay
------------------------ Yahoo! Groups Sponsor ---------------------~-->
Buy Ink Cartridges or Refill Kits for your HP, Epson, Canon or Lexmark
Printer at MyInks.com. Free s/h on orders $50 or more to the US & Canada.
http://www.c1tracking.com/l.asp?cid=5511
http://us.click.yahoo.com/mOAaAA/3exGAA/qnsNAA/2U_rlB/TM
---------------------------------------------------------------------~->
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/xmlpull-dev/
<*> To unsubscribe from this group, send an email to:
[email protected]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/