Dr. Jung,

True that it requires a java (jdk 1.4) server and client, but if you 
know and can accept that (and that's probably most of us)...

Actually, it handles complex graphs quite nicely.  Here's an example 
java file:

import java.beans.XMLEncoder;
import java.io.*;
import java.util.*;
import javax.swing.*;

public class EncTest {

        public static void main(String[] args) throws Exception {
                Map m = new HashMap();
                m.put("jbutton",  new JButton("hello") );
                m.put("string", "foobar");
                List list = new ArrayList();
                list.add("one");
                list.add( new Integer(2) );
                m.put("list", list);
                XMLEncoder enc = new XMLEncoder(new FileOutputStream("output.xml"));
                enc.writeObject(m);
                enc.close();
        }

}

and here is the output it created:

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.0_01" class="java.beans.XMLDecoder">
  <object class="java.util.HashMap">
   <void method="put">
    <string>list</string>
    <object class="java.util.ArrayList">
     <void method="add">
      <string>one</string>
     </void>
     <void method="add">
      <int>2</int>
     </void>
    </object>
   </void>
   <void method="put">
    <string>jbutton</string>
    <object class="javax.swing.JButton">
     <void property="actionCommand">
      <string>hello</string>
     </void>
     <void property="label">
      <string>hello</string>
     </void>
     <void property="model">
      <void property="actionCommand">
       <null/>
      </void>
     </void>
    </object>
   </void>
   <void method="put">
    <string>string</string>
    <string>foobar</string>
   </void>
  </object>
</java>

The only 2 problems that this output created for me are:
1) I needed to strip off the xml declartation (easy).
2) I needed to wrap everything in a CDATA section (easy).

Here is the pertinant code that puts it all together:

// implements Serializer
public void serialize(QName pQName, Attributes pAttributes,
                       Object pValue, SerializationContext pContext)
        throws IOException
{
        pContext.startElement(pQName, pAttributes);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        XMLEncoder enc = new XMLEncoder(baos);
        enc.writeObject(pValue);
        enc.close();
        String data = baos.toString().trim();
        // strip off the xml declaration and encapsulate as CDATA
        data = "<![CDATA[" + data.substring( (data.indexOf('>') + 1), 
data.length() ) + "]]>";
        pContext.writeString(data);
        pContext.endElement();
}

//extends SimpleDeserializer
public Object makeValue(String pSource)
{
        ByteArrayInputStream bais =
                new ByteArrayInputStream( pSource.getBytes() );
        XMLDecoder dec = new XMLDecoder( new BufferedInputStream(bais) );
        Object data = dec.readObject();
        dec.close();
        return data;
}

Baically, I modeled this thing after the CalendarSerializer.java and 
CalendarDeserializer.java that I found in axis cvs.  Premise is that if 
you can encode/decode something to/from a single String value, it's 
easy.  As long as I encapsulate XMLEncoder's output in a CDATA section, 
it doesn't confuse the axis wsdl xml format.

Now what do you think?
David

--

Jung , Dr. Christoph wrote:
> Hi David,
> 
> Sounds good, but I fear it�s similar to serializing the class via xsd:binary
> when it comes to attach a client that has no
> JDK1.4 (thinking of m$ .net and others) ... 
> 
> How do you generate the corresponding wsdl? How does it handle polymorphism?
> Is the XMLEncoder well-typed, i.e., does it use the namespace environment of
> the embedding tag? How do you callback from the beans encoder into the Axis
> serialization engine in case you get embedded complex values, attachements
> and all that stuff?
> 
> I see that your solution is very elegant, but it�s a kind of dead-end when
> serializing 
> complex object graphs, isn�t it?
> 
> CGJ
> 
> -----Urspr�ngliche Nachricht-----
> Von: David Ward [mailto:[EMAIL PROTECTED]] 
> Gesendet: Montag, 10. Juni 2002 16:58
> An: [EMAIL PROTECTED]
> Betreff: Re: AW: [JBoss-user] IllegalArgumentException from jboss.net to
> SLSB
> 
> 
> Dr. Jung and others,
> 
> I came up with a way to generically and tolerantly serialize and 
> deserialize any Object.
> 
> I use java.beans.XMLEndoder in a class that implements Serializer, and I 
> use java.beans.XMLDecoder in a class that extends SimpleDeserializer.
> 
> Pros:
> 1) It's reusable for any object, so you don't need to have a serializer 
> and deserializer for each of your custom data types.
> 2) It's tolerant of "gets without sets" and other problems that "strict 
> javabean" encoders have.
> 3) It comes with the JDK.
> 4) It handles the Collection problem I talked about.  (Actually, I 
> changed the type to java.util.List, but in my client-config.xml and 
> web-service.xml, I have to have a typeMapping of the exact impl: in my 
> case, ArrayList.)
> 
> Cons:
> 1) It depends on JDK 1.4.
> 2) It's a bit (can't quantify yet) slower.
> 
> If (the jboss team) wants to include it in their jboss.net codebase, I'd 
> be happy to share it, though I'd have to check with my boss first. 
> Still, with the description I gave I'm sure anyone could implement it 
> (the operative code in each class is less between 6-10 lines!).
> 
> Thanks for all your help!
> David
> 
> --
> 
> Jung , Dr. Christoph wrote:
> 
>>David,
>>
>>IMO, Collection is a particularily bad type to map XML-Schema to and 
>>from in Axix. If you use an array or a vector, the automatic mapping 
>>of Axis will however do something meaningful.
>>
>>I�m currently working on an additional annotation of the Java-methods 
>>that will allow to specify the Xml-mapping used, such that a 
>>Collection argument of return type can be mapped to typed arrays, 
>>datasets and the like.
>>
>>CGJ
>>
>>
>>-----Urspr�ngliche Nachricht-----
>>Von: David Ward [mailto:[EMAIL PROTECTED]]
>>Gesendet: Montag, 10. Juni 2002 09:29
>>An: [EMAIL PROTECTED]
>>Betreff: [JBoss-user] IllegalArgumentException from jboss.net to SLSB
>>
>>
>>(JBoss 3.0.0 final, Sun Linux JDK 1.4.0_01.)
>>
>>I have an axis client hitting jboss.net, which in turn executes a
>>stateless ejb of mine.
>>
>>When I make calls to a method of:
>>
>>public UserData addUser(UserData pOrigData, PersistenceLevel pLevel)
>>
>>I have no problem.  The problem comes when the first argument turns 
>>into
>>a Collection in another method:
>>
>>public Collection addUsers(Collection pOrigDatas, PersistenceLevel 
>>pLevel).
>>
>>Not sure why the Collection is confusing things (causing an "argument
>>type mismatch").  Here's my stack trace -
>>
>>Thanks for any and all help,
>>David
>>
>>--
>>
>>      [java] java.lang.IllegalArgumentException: argument type 
>>mismatch on object "$Proxy74", method name "addUsers", tried argument 
>>types: [Ljava.lang.Object;,
> 
> com.dotech.gizzard.persistence.PersistenceLevel
> 
>>      [java]     at
>>org.apache.axis.message.SOAPFaultBuilder.endElement(SOAPFaultBuilder.j
>>ava:13
>>5)
>>      [java]     at
>>
> 
> org.apache.axis.encoding.DeserializationContextImpl.endElement(Deserializati
> 
>>onContextImpl.java:865)
>>      [java]     at
>>org.apache.crimson.parser.Parser2.maybeElement(Parser2.java:1528)
>>      [java]     at 
>>org.apache.crimson.parser.Parser2.content(Parser2.java:1779)
>>      [java]     at
>>org.apache.crimson.parser.Parser2.maybeElement(Parser2.java:1507)
>>      [java]     at 
>>org.apache.crimson.parser.Parser2.content(Parser2.java:1779)
>>      [java]     at
>>org.apache.crimson.parser.Parser2.maybeElement(Parser2.java:1507)
>>      [java]     at
>>org.apache.crimson.parser.Parser2.parseInternal(Parser2.java:500)
>>      [java]     at 
>>org.apache.crimson.parser.Parser2.parse(Parser2.java:305)
>>      [java]     at
>>org.apache.crimson.parser.XMLReaderImpl.parse(XMLReaderImpl.java:442)
>>      [java]     at javax.xml.parsers.SAXParser.parse(SAXParser.java:345)
>>      [java]     at
>>
> 
> org.apache.axis.encoding.DeserializationContextImpl.parse(DeserializationCon
> 
>>textImpl.java:201)
>>      [java]     at
>>org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:428)     [java] 
>>     at
>>org.apache.axis.client.Call.invoke(Call.java:1793)
>>      [java]     at org.apache.axis.client.Call.invoke(Call.java:1594)
>>      [java]     at org.apache.axis.client.Call.invoke(Call.java:1513)
>>      [java]     at org.apache.axis.client.Call.invoke(Call.java:1093)
>>      [java]     at
>>
> 
> com.dotech.gizzard.persistence.axis.AXISPersistenceManagerFactory$1.invoke(A
> 
>>XISPersistenceManagerFactory.java:46)
>>      [java]     at $Proxy0.addUsers(Unknown Source)
>>      [java]     at
>>com.dotech.gizzard.persistence.test.UserTest.addUsers(UserTest.java:66)
>>      [java]     at
>>com.dotech.gizzard.persistence.test.UserTest.main(UserTest.java:31)
>>
>>
>>_______________________________________________________________
>>
>>Don't miss the 2002 Sprint PCS Application Developer's Conference 
>>August 25-28 in Las Vegas - 
>>http://devcon.sprintpcs.com/adp/index.cfm?source=osdntextlink
>>
>>_______________________________________________
>>JBoss-user mailing list
>>[EMAIL PROTECTED] 
>>https://lists.sourceforge.net/lists/listinfo/jboss-user
>>###########################################
>>
>>This message has been scanned by F-Secure Anti-Virus for Microsoft 
>>Exchange. For more information, connect to http://www.F-Secure.com/
>>
>>_______________________________________________________________
>>
>>Don't miss the 2002 Sprint PCS Application Developer's Conference 
>>August 25-28 in Las Vegas - 
>>http://devcon.sprintpcs.com/adp/index.cfm?source
>>_______________________________________________
>>JBoss-user mailing list
>>[EMAIL PROTECTED]
>>https://lists.sourceforge.net/lists/listinfo/jboss-user
> 
> 
> 
> 



-- 

---------------------
David Ward
[EMAIL PROTECTED]
http://www.dotech.com


_______________________________________________________________

Don't miss the 2002 Sprint PCS Application Developer's Conference
August 25-28 in Las Vegas - 
http://devcon.sprintpcs.com/adp/index.cfm?source=osdntextlink

_______________________________________________
JBoss-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to