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 Michael Shillinger <ms...@lucent.com> on 2004/10/18 19:08:42 UTC

RE: Implementation of "struct" data type in =XmlWriter.writeObject(Object)

Oded,

I needed more flexibility too.

I just created my own XmlWriter implementation and modified XmlRpc to use a class specified at runtime.

Modify XmlRpc.java - Add the following 3 methods and a static member (xmlWriterClass)


//++++++ START CODE SNIPPET +++++

/** ********************************************************************
 * Set the XML Writer Class used for converting to/from XMLRPC data
 * format.  Use XmlRpc.getWriter(OutputStream os, String enc) to get an instance.
 * (ADDED: MJS 6/30/2004)
 ******************************************************************** */
public static void setXmlWriter(Class writer){
  xmlWriterClass = writer;
}

/** ********************************************************************
 * Set the XML Writer Class using it's fully qualified class name.
 * (used for converting to/from XMLRPC data format)
 *  Use XmlRpc.getWriter(OutputStream os, String enc) to get an instance.
 * (ADDED: MJS 6/30/2004)
 ******************************************************************** */
public static void setXmlWriter(String writerClassName) throws ClassNotFoundException {
  try {
    xmlWriterClass = Class.forName(writerClassName);
  }
  catch (ClassNotFoundException x){
    throw new ClassNotFoundException ("XMLWriter class not found: "
                                      + writerClassName);
  }
}

/** ********************************************************************
 * Get an instance of the XmlWriter. (ADDED: MJS 6/30/2004)
 ******************************************************************** */
public static XmlWriter getWriter(java.io.OutputStream os, String enc)
    throws UnsupportedEncodingException, IOException{

  XmlWriter writer = null;
  if (xmlWriterClass == null){
    return new XmlWriter(os, enc);
  }
  try {
    Class[] classes = new Class[2];
    classes[0] = Class.forName("java.io.OutputStream");
    classes[1] = Class.forName("java.lang.String");

    Constructor c = xmlWriterClass.getConstructor(classes);
    writer = (XmlWriter)c.newInstance(new Object[]{os,enc});
  }
  catch (Exception ex){
    log.error("Cannot get Custom XmlWriter.", ex);
    //Add failure message
    writer = new XmlWriter(os, enc);
  }
  return writer;
}
//-------END CODE SNIPPET --------

Next, Go through the code an replace all instances of

XmlWriter writer = new XmlWriter(buffer, encoding);

with

XmlWriter writer = XmlRpc.getWriter(buffer, encoding);

Finally, you create a class that extends XmlWriter and overrides
"writeObject(Object obj)" to handle any type of objects you want.

All you have to do, is feed XmlRpc a fully qualified class name of your own writer at
startup ( via XmlRcp.setWriter("org.mshillin.xmlrpc.MjsXmlWriter") ).




My XmlWriter class implementation currently supports Maps, Lists,
the Trove Collections, and objects of my own design.

Hope this helps.


Mike Shillinger
mshillinger At lucent Dot com




>Hi list
>
>I have an issue with the way writeObject() does "struct" data type: in
>version 1.2b1 (which is the one one can download from the binary
>distribution servers), writeObject only recognizes java.util.Hashtable
>as a "struct" type, while it would have been much much better to
>recgonize any class implementing java.util.Map as a "struct" type.
>
>Same with "array" - one would expect any java.util.Collection to be
>supported as an "array" type while only java.util.Vector is supported
>(And there are some issues with using Vector to store collection data
>which is why one shouldn't pick use Vector at all unless synchronized
>data access is needed).
>
>Are there any plans to fix that problem ?
>
>--
>Oded Arbel
>m-Wise mobile solutions
>oded@m-wise.com
>
>+972-9-9611212 (204)
>+972-54-7340014
>
>::..
>Class schedules are designed so that every student will waste maximum
>time between classes.
>Corollary: When you are occasionally able to schedule two classes in a
>row, they will be held in classrooms at opposite ends of the campus.
>	-- Laws of Class Scheduling n°2