You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@axis.apache.org by Scott Wilson <s....@bangor.ac.uk> on 2005/03/17 02:17:00 UTC

Deserializing an XSL-transformed response

I have a bit of an odd problem:

My application is a search adapter for a federated search system; it 
calls a remote search service, which returns some results in its native 
schema format. This is deserialized into my RecordData bean that AXIS 
has helpfully generated, with the results lurking in the '_any' value 
(the other value being Srw_DcType dc).

I use XSL to take the contents of '_any' and transform them into Dublin 
Core, then insert this back into the RecordData bean instance.

However, I now need to deserialize the resulting data so I can do some 
further work on it (such as vocab mapping) before I return it for 
output to the federated search engine that called my adapter. I'd like 
to make RecordData.dc actually have the deserialized value of the 
transformation output.

The problem is, automated serialization only happens in Axis at the end 
of a SOAP call; I can't figure out a way of programmatically invoking a 
BeanDeserializer on my transformed XML, even though I have everything 
Axis needs already in place (a generated type, registered, and working 
- when a call is invoked).

Some source code below will, I hope, show you what I'm after:

/*
  * Created on Mar 16, 2005
  *
  */
package au.edu.mq.melcoe.mams.adapter.search.handler;

import gov.loc.www.zing.srw.RecordsType;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.dom.DOMResult;
import org.apache.axis.message.MessageElement;
import java.io.StringReader;
import org.w3c.dom.*;
/**
  * Performs an XSL transformation on a results set, and places the 
results into
  * the RecordsType passed in.
  *
  * @author Scott Wilson
  */
public class XslMetadataProcessor implements IMetadataProcessor {
	
	private String stylesheet;
	
	public XslMetadataProcessor(String stylesheet){
		this.stylesheet = stylesheet;
	}
	
	public RecordsType processRecords(RecordsType records) throws 
Exception{
		//return records;
		if(stylesheet != null && records != null){
			
			for (int i=0;i<records.getRecord().length;i++){
				
				MessageElement[] msg = 
records.getRecord(i).getRecordData().get_any();
				DOMResult out = new DOMResult();
				StreamSource src = new StreamSource(new 
StringReader(msg[0].getAsString()));
				
				try{
					javax.xml.transform.TransformerFactory tFactory =
						javax.xml.transform.TransformerFactory.newInstance();
					javax.xml.transform.Transformer transformer = 
tFactory.newTransformer
					(new StreamSource("META-INF/stylesheets/"+this.stylesheet));
					transformer.transform (src, out);
					Document outDoc = (Document)out.getNode();
					msg[0] = new 
MessageElement((Element)outDoc.getElementsByTagName("*").item(0));
				} catch (Exception ex){
					ex.printStackTrace();
					throw new Exception("Transformation could not be completed");
				}
				// OK, we now have the record transformed, and the output is inside
				// the "_any" part of the record. time to deserialize it - but how?
				// I'd like to write :
				// Srw_dcType dc = new Srw_dcType(SOME MAGIC HERE);
				// records.getRecord(i).getRecordData().setDc(dc);
			}

		} else {
			throw new Exception("Stylesheet not defined");
		}
		return records;
	}
}

Re: Deserializing an XSL-transformed response

Posted by Yves Langisch <li...@langisch.ch>.
Scott,

Are you looking for this?

    /**
     * Deserializes a SOAP Reader (i.e. a XML file) to the appropriate
Java Class
     * generated by Axis.
     *
     * @param reader content for deserializing (_a complete SOAP
envelope_)
     * @param clazz Java Type representation of the XML stream is
     * @return Object which has to be casted to the specified Java Class
(clazz)
     */
    public static Object deserializeFromSOAPReader(Reader reader, Class
clazz)
            throws DocumentStructureException, AxisFault {

        MessageContext msgContext = new MessageContext(new
AxisServer());
        msgContext.setEncodingStyle(null);

        TypeMappingRegistry tmr = new TypeMappingRegistryImpl();
        msgContext.setTypeMappingRegistry(tmr);

        DeserializationContext dser = new DeserializationContext(new
InputSource(reader),
                msgContext,
                org.apache.axis.Message.REQUEST);
        // parse the InputSource
        try {
            dser.parse();
        }
        catch(SAXException e) {
            throw new DocumentStructureException(e.getMessage(), e);
        }

        RPCElement rpcElem = (RPCElement)
dser.getEnvelope().getFirstBody();

        try {
            return rpcElem.getValueAsType(null, clazz);
        }
        catch(Exception e) {
            throw new DocumentStructureException(e.getMessage(), e);
        }
    }

Yves

On Thu, 2005-03-17 at 12:17 +1100, Scott Wilson wrote:
> 
> 
> ______________________________________________________________________
> 
> I have a bit of an odd problem:
> 
> My application is a search adapter for a federated search system; it
> calls a remote search service, which returns some results in its
> native schema format. This is deserialized into my RecordData bean
> that AXIS has helpfully generated, with the results lurking in the
> '_any' value (the other value being Srw_DcType dc).
> 
> I use XSL to take the contents of '_any' and transform them into
> Dublin Core, then insert this back into the RecordData bean
> instance.  
> 
> However, I now need to deserialize the resulting data so I can do some
> further work on it (such as vocab mapping) before I return it for
> output to the federated search engine that called my adapter. I'd like
> to make RecordData.dc actually have the deserialized value of the
> transformation output.
> 
> The problem is, automated serialization only happens in Axis at the
> end of a SOAP call; I can't figure out a way of programmatically
> invoking a BeanDeserializer on my transformed XML, even though I have
> everything Axis needs already in place (a generated type, registered,
> and working - when a call is invoked).
> 
> Some source code below will, I hope, show you what I'm after:
> 
> /*
> * Created on Mar 16, 2005
> *
> */
> package au.edu.mq.melcoe.mams.adapter.search.handler;
> 
> import gov.loc.www.zing.srw.RecordsType;
> import javax.xml.transform.stream.StreamSource;
> import javax.xml.transform.dom.DOMResult;
> import org.apache.axis.message.MessageElement;
> import java.io.StringReader;
> import org.w3c.dom.*;
> /**
> * Performs an XSL transformation on a results set, and places the
> results into
> * the RecordsType passed in.
> * 
> * @author Scott Wilson
> */
> public class XslMetadataProcessor implements IMetadataProcessor {
> 
> private String stylesheet;
> 
> public XslMetadataProcessor(String stylesheet){
> this.stylesheet = stylesheet;
> }
> 
> public RecordsType processRecords(RecordsType records) throws
> Exception{
> //return records;
> if(stylesheet != null && records != null){
> 
> for (int i=0;i<records.getRecord().length;i++){
> 
> MessageElement[] msg = records.getRecord(i).getRecordData().get_any();
> DOMResult out = new DOMResult();
> StreamSource src = new StreamSource(new
> StringReader(msg[0].getAsString()));
> 
> try{
> javax.xml.transform.TransformerFactory tFactory = 
> javax.xml.transform.TransformerFactory.newInstance();
> javax.xml.transform.Transformer transformer = tFactory.newTransformer
> (new StreamSource("META-INF/stylesheets/"+this.stylesheet));
> transformer.transform (src, out);
> Document outDoc = (Document)out.getNode();
> msg[0] = new
> MessageElement((Element)outDoc.getElementsByTagName("*").item(0));
> } catch (Exception ex){
> ex.printStackTrace();
> throw new Exception("Transformation could not be completed");
> }
> // OK, we now have the record transformed, and the output is inside 
> // the "_any" part of the record. time to deserialize it - but how?
> // I'd like to write :
> // Srw_dcType dc = new Srw_dcType(SOME MAGIC HERE);
> // records.getRecord(i).getRecordData().setDc(dc);
> }
> 
> } else {
> throw new Exception("Stylesheet not defined");
> }
> return records;
> }
> }