For those of us that are developing for existing interfaces, the ability to
work with what we are given, without changing it is a necessity. Therefore,
in a lot of instances, adding a new method to a class to return an array
instead of a List or a Collection is not an option.
I am submitting this serialization class for those that might have this need;
i.e. to interface with frozen implementations.
To the SOAP developers:
You're free to include this in the main SOAP library, if you wish; of course,
you'll need to change the package name if it'll become part of the standard
mapping implementation.
Daniel Lynes
/*
* ListSerializer.java
*
* Created on August 14, 2002, 10:41 AM
*/
package com.essc.xml.rpc.soap.encoding.soapenc;
import java.beans.*;
import java.io.*;
import java.util.*;
import java.lang.reflect.*;
import org.w3c.dom.*;
import org.apache.soap.util.*;
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.rpc.*;
import org.apache.soap.encoding.soapenc.* ;
/**
* A <code>ListSerializer</code> can be used to serialize and
* deserialize Lists using the <code>SOAP-ENC</code>
* encoding style.<p>
*
* @author Daniel Lynes ([EMAIL PROTECTED])
*/
public class ListSerializer implements Serializer, Deserializer {
private static final String STR_ITEM = "item";
private static final String STR_VALUE = "value";
/** Creates a new instance of ListSerializer */
public ListSerializer()
{
}
public void marshall( String inScopeEncStyle, Class javaType, Object src, Object context, Writer sink,
NSStack nsStack, XMLJavaMappingRegistry xjmr, SOAPContext ctx )
throws IllegalArgumentException, IOException
{
if( src==null ) {
SoapEncUtils.generateNullStructure(inScopeEncStyle,
javaType,
context,
sink,
nsStack,
xjmr);
} else if( src instanceof List ) {
SoapEncUtils.generateStructureHeader(inScopeEncStyle,
javaType,
context,
sink,
nsStack,
xjmr);
sink.write(StringUtils.lineSeparator);
List list=(List)src ;
for( int i=0; i<list.size(); i++ ) {
Object value=list.get(i) ;
sink.write("<"+STR_ITEM+">") ;
sink.write(StringUtils.lineSeparator);
if( value==null ) {
SoapEncUtils.generateNullStructure(Constants.NS_URI_SOAP_ENC,
Object.class, STR_VALUE, sink,
nsStack, xjmr);
} else {
Class actualComponentType = value.getClass();
xjmr.marshall(Constants.NS_URI_SOAP_ENC, actualComponentType, value,
STR_VALUE, sink, nsStack, ctx);
}
sink.write(StringUtils.lineSeparator);
sink.write("</" + STR_ITEM + ">");
sink.write(StringUtils.lineSeparator);
}
sink.write("</" + context + '>');
}
else
{
throw new IllegalArgumentException("Tried to pass a '" +
src.getClass().toString() +
"' to HashtableSerializer");
}
}
public Bean unmarshall( String inScopeEncStyle, QName elementType, Node src,
XMLJavaMappingRegistry xjmr, SOAPContext ctx )
throws IllegalArgumentException
{
Element root = (Element)src;
String name = root.getTagName();
if( SoapEncUtils.isNull(root) ) {
return new Bean(Hashtable.class, null);
}
List list=new Vector() ;
Element tempEl = DOMUtils.getFirstChildElement(root);
while (tempEl != null) {
// got an item
Element valEl = DOMUtils.getFirstChildElement(tempEl);
String tagName = valEl.getTagName();
if (!tagName.equalsIgnoreCase("value"))
{
throw new IllegalArgumentException("Got <" + tagName +
"> tag when expecting <" +
STR_VALUE + ">");
}
Bean valBean = unmarshallEl(inScopeEncStyle, xjmr, valEl, ctx);
list.add(valBean.value) ;
tempEl = DOMUtils.getNextSiblingElement(tempEl);
}
return new Bean(List.class, list);
}
private Bean unmarshallEl( String inScopeEncStyle, XMLJavaMappingRegistry xjmr,
Element targetEl, SOAPContext ctx )
{
String declEncStyle = DOMUtils.getAttributeNS(targetEl,
Constants.NS_URI_SOAP_ENV, Constants.ATTR_ENCODING_STYLE);
String actualEncStyle = (declEncStyle != null)
? declEncStyle
: inScopeEncStyle;
QName declItemType = SoapEncUtils.getTypeQName(targetEl);
return xjmr.unmarshall(actualEncStyle, declItemType, targetEl, ctx);
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>