commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Henning P. Schmiedehausen" <>
Subject [BETWIXT] Array of bytes anyone?
Date Tue, 13 Jul 2004 15:51:07 GMT

for a project, I need Betwixt to convert a special sort of beans into
XML. Beans, which contain a byte [] property (They come from Hibernate
which maps BINARY and TEXT onto these types, which is fine).

Regular Betwixt builds something like


for propery foo, which is not exactly the most efficient way to
express this array of bytes. Especially if it has about 5 MBytes
size. ;-) 

I was wondering whether I could get this done with regular betwixt but
the isPrimitive() in XMLIntrospectorHelp always bite me. In the end I
came up with the attached patch, which works fine for me (it might not
be ideal, because the resulting XML contains the raw sew^Wbinary data
if you write the bean out.

For my application I extend the DefaultObjectStringConverter to do
base64 encoding on the fly and end up with nice looking XML like this:

<foo attr1="1" attr2="2" attr3="3">

which is exactly what I need. 

I was wondering if it would be more clever to allow the user to
explicitly set the primitiveType property of the element descriptor
from the .betwixt file. I found no way to do so, though and I already
had this patch which works for me.

Anyway, here is the patch, discussions welcome. ;-) This is against
CVS HEAD. I would volunteer to write an Unit test for it if has a
chance to get applied.


--- cut ---
Index: src/java/org/apache/commons/betwixt/digester/
RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/digester/,v
retrieving revision 1.31
diff -u -r1.31
--- src/java/org/apache/commons/betwixt/digester/	4 Jul 2004 16:40:49
-0000	1.31
+++ src/java/org/apache/commons/betwixt/digester/	13 Jul 2004 15:39:37
@@ -583,6 +583,8 @@
         } else if ( type.equals( Object.class ) ) {
             return false;
+        } else if (type.equals ( byte [].class) ) {
+            return true;
         return type.getName().startsWith( "java.lang." )
             || Number.class.isAssignableFrom( type ) 
Index: src/java/org/apache/commons/betwixt/strategy/
RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/strategy/,v
retrieving revision 1.10
diff -u -r1.10
--- src/java/org/apache/commons/betwixt/strategy/	4 Jul 2004
16:57:05 -0000	1.10
+++ src/java/org/apache/commons/betwixt/strategy/	13 Jul
2004 15:39:37 -0000
@@ -66,7 +66,8 @@
             if ( object instanceof java.util.Date && isUtilDate( type ) ) {
                 return formatter.format( (java.util.Date) object );
+            } else if ( object instanceof byte[]) {
+                return new String((byte []) object);
             } else {
                 // use ConvertUtils implementation
                 return super.objectToString( object, type, flavour, context );
@@ -98,6 +99,8 @@
                     // but never mind
                     return value;
+            } else if (type.equals(byte [].class)) {
+                return value.getBytes();
             } else {
                 // use ConvertUtils implementation
                 return super.stringToObject( value, type, flavour, context );
--- cut ---

Dipl.-Inf. (Univ.) Henning P. Schmiedehausen          INTERMETA GmbH        +49 9131 50 654 0

RedHat Certified Engineer -- Jakarta Turbine Development  -- hero for hire
   Linux, Java, perl, Solaris -- Consulting, Training, Development

"Fighting for one's political stand is an honorable action, but re-
 fusing to acknowledge that there might be weaknesses in one's
 position - in order to identify them so that they can be remedied -
 is a large enough problem with the Open Source movement that it
 deserves to be on this list of the top five problems."
                       -- Michelle Levesque, "Fundamental Issues with
                                    Open Source Software Development"

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message