You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xmlrpc-dev@ws.apache.org by "Daniel L. Rall" <dl...@finemaltcoding.com> on 2004/01/20 04:32:07 UTC

[PATCH] Improve encoding support for XmlWriter

I'd like to be able to send multibyte characters over XML-RPC. In CVS
revision 1.4, we basically removed this ability with the following log
message:

----------------------------
revision 1.4
date: 2002/08/20 16:48:49;  author: dlr;  state: Exp;  lines: +15 -6
writeObject(): Noted new exception thrown.

chardata(): Characters out of range for the XML spec won't be written
as XML to avoid parse errors on the client side.  Instead, and
exception will be thrown.  This was originally noted by John Wilson
<tu...@wilson.co.uk>, with this follow up by Adam Megacz
<ad...@megacz.com>:

> Ah, the joys of the self-contradictory XML-RPC spec ;)
>
>   "<string> [is an] ASCII string"
>
>   "Any characters are allowed in a string except < and &... A string can
>    be used to encode binary data."
>
> Dave absolutely refuses to fix or clarify this.
----------------------------


Here's a patch to
add that ability back in for Unicode encodings, encodings which I
believe XML parsers are required to support by the XML specification.
-- 

Daniel Rall



Index: XmlWriter.java
===================================================================
RCS file: /home/cvs/ws-xmlrpc/src/java/org/apache/xmlrpc/XmlWriter.java,v
retrieving revision 1.6
diff -u -u -r1.6 XmlWriter.java
--- XmlWriter.java	21 Nov 2002 21:57:39 -0000	1.6
+++ XmlWriter.java	20 Jan 2004 03:24:07 -0000
@@ -312,6 +312,11 @@
         throws XmlRpcException, IOException
     {
         int l = text.length ();
+        String enc = super.getEncoding();
+        boolean isUnicode = UTF8.equals(enc) || "UTF-16".equals(enc);
+        // ### TODO: Use a buffer rather than going character by
+        // ### character to scale better for large text sizes.
+        //char[] buf = new char[32];
         for (int i = 0; i < l; i++)
         {
             char c = text.charAt (i);
@@ -332,16 +337,38 @@
                 write(AMPERSAND_ENTITY);
                 break;
             default:
-                if (c < 0x20 || c > 0xff)
+                if (c < 0x20 || c > 0x7f)
                 {
                     // Though the XML-RPC spec allows any ASCII
                     // characters except '<' and '&', the XML spec
                     // does not allow this range of characters,
                     // resulting in a parse error from most XML
-                    // parsers.
-                    throw new XmlRpcException(0, "Invalid character data " +
-                                              "corresponding to XML entity &#" +
-                                              String.valueOf((int) c) + ';');
+                    // parsers.  However, the XML spec does require
+                    // XML parsers to support UTF-8 and UTF-16.
+                    if (isUnicode)
+                    {
+                        if (c < 0x20)
+                        {
+                            // Entity escape the character.
+                            write("&#");
+                            // ### Do we really need the String conversion?
+                            write(String.valueOf((int) c));
+                            write(';');
+                        }
+                        else // c > 0x7f
+                        {
+                            // Write the character in our encoding.
+                            write(new String(String.valueOf(c).getBytes(enc)));
+                        }
+                    }
+                    else
+                    {
+                        throw new XmlRpcException(0, "Invalid character data "
+                                                  + "corresponding to XML "
+                                                  + "entity &#"
+                                                  + String.valueOf((int) c)
+                                                  + ';');
+                    }
                 }
                 else
                 {

Re: [PATCH] Improve encoding support for XmlWriter

Posted by John Wilson <tu...@wilson.co.uk>.
On 20 Jan 2004, at 03:32, Daniel L. Rall wrote:

> I'd like to be able to send multibyte characters over XML-RPC. In CVS
> revision 1.4, we basically removed this ability with the following log
> message:
>
> ----------------------------
> revision 1.4
> date: 2002/08/20 16:48:49;  author: dlr;  state: Exp;  lines: +15 -6
> writeObject(): Noted new exception thrown.
>
> chardata(): Characters out of range for the XML spec won't be written
> as XML to avoid parse errors on the client side.  Instead, and
> exception will be thrown.  This was originally noted by John Wilson
> <tu...@wilson.co.uk>, with this follow up by Adam Megacz
> <ad...@megacz.com>:
>
>> Ah, the joys of the self-contradictory XML-RPC spec ;)
>>
>>   "<string> [is an] ASCII string"
>>
>>   "Any characters are allowed in a string except < and &... A string 
>> can
>>    be used to encode binary data."
>>
>> Dave absolutely refuses to fix or clarify this.
> ----------------------------
>
>
> Here's a patch to
> add that ability back in for Unicode encodings, encodings which I
> believe XML parsers are required to support by the XML specification.
>

Daniel,

	all character encodings can represent all Unicode characters.  The 
valid characters allowed by the XML spec (production 2 section 2.2)  is 
#x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] 
The last range is academic as Java only supports 16 bit Unicode 
characters.

i believe strongly that the XML-RPC implementation should refuse to 
emit non well formed XML. Therefore attempts to emit characters below 
0X20 other than 0X09, 0X0A and 0X0D, within the range 0XD800 to 0XDFFF 
and above 0XFFFD should cause an exception to be thrown.

If the encoding is ISO 8859-1 then characters > 0XFF should be 
represented as numeric character references.

For UTF-8 and UTF-16 the checks for illegal characters should be 
performed but no numeric character reference encoding is needed.

I also think that it would be useful to support a conservative encoding 
where the XML declaration is omitted and all characters > 0X7F is 
encoded using numeric character references. This would mean that we 
would interoperate with all XML-RPC implementations as long as we are 
only sending US-ASCII characters. We can still send characters  > 0X7F  
but not all implementations will cope with them.

I don't seem to be able to apply your patch to the CVS HEAD code (using 
Eclipse). So I  can't provide you with more constructive comments on 
your modification. If you send me the actual file off list I'll take a 
detailed look at it.

	Cheers



John Wilson
The Wilson Partnership
http://www.wilson.co.uk


Re: [PATCH] Improve encoding support for XmlWriter

Posted by John Wilson <tu...@wilson.co.uk>.
On 20 Jan 2004, at 18:40, Ken Gengler wrote:

>
> On Jan 19, 2004, at 10:49 PM, Adam Megacz wrote:
>> I still take issue with
>>
>>    What characters are allowed in strings? Non-printable characters?
>>    Null characters? Can a "string" be used to hold an arbitrary chunk 
>> of
>>    binary data?
>>
>>    Any characters are allowed in a string except < and &, which are
>>    encoded as &lt; and &amp;. A string can be used to encode binary
>>    data.
>
> Can I ask a likely stupid question here? Why aren't the strings sent 
> in CDATA sections? I have to admit that I hacked this implementation 
> to use CDATA for any string over 100 byte (arbitrary). The results I 
> return to the caller usually include an XML document that's often 
> sizable. The cost of encoding the <  and & characters was fairly high 
> and I achieved quite a performance improvement by just using CDATA. I 
> didn't offer it to the community since I figure there was a very good 
> reason why it wasn't used. But, I'm curious as to that reason.
>

If you are sending XML documents then using a CDATA section is a good 
idea (I presume that you handle the case where the document you are 
sending has a CDATA section in it). It might make sense to look at the 
numbers of <, > and & characters in a string and to switch to CDATA 
encoding if there are more than three or four of them.

CDATA sections don't work well if you have characters which have to be 
replaced by numeric character references because they can't be directly 
represented in the encoding as you need to end the CDATA section emit 
the numeric character reference and start the CDATA section again.

Its quite an interesting challenge to chose the optimal mixture of 
CDATA sections and character entities to represent an arbitrary 
document.


John Wilson
The Wilson Partnership
http://www.wilson.co.uk


Re: [PATCH] Improve encoding support for XmlWriter

Posted by Ken Gengler <kg...@techtracker.com>.
On Jan 19, 2004, at 10:49 PM, Adam Megacz wrote:
> I still take issue with
>
>    What characters are allowed in strings? Non-printable characters?
>    Null characters? Can a "string" be used to hold an arbitrary chunk 
> of
>    binary data?
>
>    Any characters are allowed in a string except < and &, which are
>    encoded as &lt; and &amp;. A string can be used to encode binary
>    data.

Can I ask a likely stupid question here? Why aren't the strings sent in 
CDATA sections? I have to admit that I hacked this implementation to 
use CDATA for any string over 100 byte (arbitrary). The results I 
return to the caller usually include an XML document that's often 
sizable. The cost of encoding the <  and & characters was fairly high 
and I achieved quite a performance improvement by just using CDATA. I 
didn't offer it to the community since I figure there was a very good 
reason why it wasn't used. But, I'm curious as to that reason.

ken


Re: [PATCH] Improve encoding support for XmlWriter

Posted by Adam Megacz <ad...@megacz.com>.
Ed Korthof <ed...@collab.net> writes:
> Look for the update at the bottom (2003/06/30).

Oh my god, I can't believe he actually FINALLY did it.  I must've
begged Dave to do this at least thirty times back in 2001/2002.  Ed,
thanks for bringing this to my attention.

I still take issue with

   What characters are allowed in strings? Non-printable characters?
   Null characters? Can a "string" be used to hold an arbitrary chunk of
   binary data?

   Any characters are allowed in a string except < and &, which are
   encoded as &lt; and &amp;. A string can be used to encode binary
   data.

Obviously this is in conflict with "XML-RPC requests are valid XML".
If we could just get this one fixed too, we'd finally have a spec that
doesn't contradict itself, and I would do a little dance of
celebration.

  - a




Re: [PATCH] Improve encoding support for XmlWriter

Posted by Ed Korthof <ed...@collab.net>.
On Mon, Jan 19, 2004 at 09:19:25PM -0800, Adam Megacz wrote:
> 
> "Daniel L. Rall" <dl...@finemaltcoding.com> writes:
> > Here's a patch to
> > add that ability back in for Unicode encodings, encodings which I
> > believe XML parsers are required to support by the XML specification.
> 
> There are quite a few XML-RPC endpoints that don't use "real" XML
> parsers.

Then they're not proper XML-RPC endpoints -- the spec indicates the
data is transmitted as  XML, and this is a basic part of the spec.  Dave
finally got around to taking out the claim that strings should only be
ASCII (which would be pretty absurd in the face of the XML RFCs):

http://www.xmlrpc.com/spec

Look for the update at the bottom (2003/06/30).

Anyway, the patch was designed to allow it to maintain the existing
behavior, if the encoding is set to ISO-8859-1 (well, actually it'll
just transform the multibyte characters to question marks, but I fail to
see how that's worse than failing outright).  The current behavior
allows for bytes with the high bit set, which isn't correct XML, but I
imagine it'd generally work despite that; this allows either that
approach or one where the encoding for the strings is something like
UTF-8.

We use XML-RPC for data which includes characters from other languages;
and I don't see the point in base64 encoding and decoding all our
textual data when XML provides a convenient way to transmit it.

cheers --

Ed
-- 
   +=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=
   |   Ed Korthof   |  edk@collab.net  |   650-228-2527   |
   +=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=

Re: [PATCH] Improve encoding support for XmlWriter

Posted by Adam Megacz <ad...@megacz.com>.
"Daniel L. Rall" <dl...@finemaltcoding.com> writes:
> Here's a patch to
> add that ability back in for Unicode encodings, encodings which I
> believe XML parsers are required to support by the XML specification.

There are quite a few XML-RPC endpoints that don't use "real" XML
parsers.

  - a