Re: Re: kxml / junit

Aleksander Slominski <[email protected]> Mon, 09 Feb 2004 14:56:56 -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 -> &#13; (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 updated yesterday 
are checking correctly for this.

and here is how i serialize text() in XPP3:

               } else if(ch < 32) {
                    //in XML 1.0 only legal character are #x9 | #xA | #xD
                    if( ch == 9 || ch == 10 || ch == 13) {
                        // pass through
                    } else {
                        throw new IllegalStateException(
                            "character "+Integer.toString(ch)+" is not 
allowed in output"+getLocation());


and attribute() values:

            } else if(ch < 32) {
                //in XML 1.0 only legal character are #x9 | #xA | #xD
                // and they must be escaped otherwise in attribute value 
they are normalized to spaces
                if(ch == 13 || ch == 10 || ch == 9) {
                    if(i > pos) out.write(value.substring(pos, i));
                    out.write("&#");
                    out.write(Integer.toString(ch));
                    out.write(';');
                    pos = i + 1;
                } else {
                    throw new IllegalStateException(
                        "character "+Integer.toString(ch)+" is not 
allowed in output"+getLocation());



thanks,

alek

ps. here is how it is done in Xerces2 serializer - it seems they print 
\r and \n unescaped but code is too complex for me to grok and 
documentation has all but useful info  ...


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("&lt;");
            } else if (ch == '&') {
                _printer.printText("&amp;");
            } else if (ch == '"') {
                _printer.printText("&quot;");
            } 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 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/