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 Frank Maritato <fr...@overture.com> on 2004/12/14 22:37:19 UTC

deserialize static xml?

Hi,

Is it possible to use my java classes generated from a wsdl and xsd's to 
deserialize static xml (i.e. a file)? I have a sample of what the output 
is and I want to run it through deserialization to make sure it will 
work. I can't just run it against the server because I'm waiting on 
other groups to complete their work, but I'd like to get as much done as 
possible in the meantime.

Thanks!
-- 
Frank Maritato

Re: deserialize static xml?

Posted by Michael Schuerig <mi...@schuerig.de>.
On Thursday 16 December 2004 02:42, Frank Maritato wrote:
> Thanks! If I'm not using my own deserializer, what would I put in
> place of MyClassDeserializerFactory()?

Well, which deserializer are you using? Presumably, BeanDeserializer. 
Then it's BeanDeserializerFactory. The best way to create such a 
factory is like this

BaseDeserializerFactory.createFactory(BeanDeserializerFactory.class, 
javaClass, xmlType));

As far as I can tell, you can pass null for xmlType in this case.

Michael

-- 
Michael Schuerig                  Failures to use one's frontal lobes
mailto:michael@schuerig.de        can result in the loss of them.
http://www.schuerig.de/michael/   --William H. Calvin

Re: deserialize static xml?

Posted by Frank Maritato <fr...@overture.com>.
Thanks! If I'm not using my own deserializer, what would I put in place 
of MyClassDeserializerFactory()?


Michael Schuerig wrote:
> On Tuesday 14 December 2004 22:37, Frank Maritato wrote:
> 
> 
>>Is it possible to use my java classes generated from a wsdl and xsd's
>>to deserialize static xml (i.e. a file)? I have a sample of what the
>>output is and I want to run it through deserialization to make sure
>>it will work. I can't just run it against the server because I'm
>>waiting on other groups to complete their work, but I'd like to get
>>as much done as possible in the meantime.
> 
> 
> I've attached a class that I use as a helper for unit testing 
> deserializers. In the actual test case class, I have a method like the 
> following that I use to get at the deserialized class and test it 
> further.
> 
>     private MyClass deserializeVerzeichnis(String filename)
>       throws Exception {
>         URL xmlFile = getClass().getResource(filename);
>         
>         DeserializationTester tester = new DeserializationTester(
>                 xmlFile, new QName("myclass"),
>                 MyClass.class,
>                 new MyClassDeserializerFactory());
> 
>         Object result = tester.getResult();
>         assertNotNull(result);
>         assertTrue(result instanceof MyClass);
>         return Verzeichnis vz = (MyClass)result;
>     }
> 
> 
> HTH,
> Michael
> 
> 
> 
> ------------------------------------------------------------------------
> 
> /*
>  * Copyright 2004 Michael Schuerig <mi...@schuerig.de>
>  *
>  * Licensed under the Apache License, Version 2.0 (the "License");
>  * you may not use this file except in compliance with the License.
>  * You may obtain a copy of the License at
>  *
>  *     http://www.apache.org/licenses/LICENSE-2.0
>  *
>  * Unless required by applicable law or agreed to in writing, software
>  * distributed under the License is distributed on an "AS IS" BASIS,
>  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>  * See the License for the specific language governing permissions and
>  * limitations under the License.
>  */
> 
> 
> package de.schuerig.util;
> 
> import java.io.ByteArrayInputStream;
> import java.io.IOException;
> import java.io.StringReader;
> import java.net.URL;
> 
> import javax.xml.namespace.QName;
> 
> import org.apache.axis.AxisFault;
> import org.apache.axis.Constants;
> import org.apache.axis.Message;
> import org.apache.axis.MessageContext;
> import org.apache.axis.encoding.DeserializationContext;
> import org.apache.axis.encoding.DeserializerFactory;
> import org.apache.axis.encoding.TypeMapping;
> import org.apache.axis.encoding.TypeMappingRegistry;
> import org.apache.axis.message.RPCElement;
> import org.apache.axis.message.SOAPBodyElement;
> import org.apache.axis.message.SOAPEnvelope;
> import org.apache.axis.server.AxisServer;
> import org.apache.commons.io.FileUtils;
> import org.xml.sax.InputSource;
> 
> 
> 
> public class DeserializationTester {
>     private static final String XML_ENCODING = "UTF-8";
>     private final DeserializationContext _context;
>     private final QName _elementName;
>     private final Class _javaClass;
>     private final DeserializerFactory _factory;
>      
>     
>     public DeserializationTester(
>             String xmlInput,
>             QName elementName,
>             Class javaClass,
>             DeserializerFactory factory) throws Exception {
>         _elementName = elementName;
>         _javaClass = javaClass;
>         _factory = factory;
>         
>         _context = createDeserializationContext(xmlInput, elementName,
>                 javaClass, factory);
>     }
>     
> 
>     public DeserializationTester(
>             URL xmlInput,
>             QName elementName,
>             Class javaClass,
>             DeserializerFactory factory) throws Exception {
>         this(getText(xmlInput), elementName, javaClass, factory);
>     }
>     
> 
>     public Object getResult() throws Exception {
>         _context.parse();
>         return extractResult(_context, _elementName, _javaClass);
>     }
>     
>     
>     private static String getText( URL xmlInput ) throws IOException {
>         return FileUtils.readFileToString(FileUtils.toFile(xmlInput), XML_ENCODING);
>     }
> 
> 
>     public static DeserializationContext createDeserializationContext(
>             String document,
>             QName elementName,
>             Class javaClass,
>             DeserializerFactory factory)
>     		throws Exception {
>         DeserializationContext context;
>         SOAPEnvelope env = new SOAPEnvelope();
>         SOAPBodyElement body = new SOAPBodyElement(
>                 new ByteArrayInputStream(document.getBytes()));
>         env.addBodyElement(body);
>     
>         MessageContext mctx = new MessageContext(new AxisServer());
>         InputSource is = new InputSource(new StringReader(env.toString()));
>         context = new DeserializationContext(is, mctx, Message.RESPONSE);
>         
>         TypeMappingRegistry reg = context.getTypeMappingRegistry();
>         TypeMapping tm = (TypeMapping)reg.getTypeMapping("");
>         if (tm == null) {
>             tm = (TypeMapping) reg.createTypeMapping();
>             reg.register(Constants.URI_DEFAULT_SOAP_ENC, tm);
>         }
>         tm.register(javaClass,
>                 elementName,
>                 null,
>                 factory);
>     
>         return context;
>     }
> 
> 
>     public static Object extractResult(DeserializationContext context,
>             QName elementName, Class javaClass) throws AxisFault, Exception {
>         SOAPEnvelope env = context.getEnvelope();
>         RPCElement body = (RPCElement)env.getFirstBody();
>         return body.getValueAsType(elementName, javaClass);
>     }
> }

-- 
Frank Maritato

Re: deserialize static xml?

Posted by Michael Schuerig <mi...@schuerig.de>.
On Tuesday 14 December 2004 22:37, Frank Maritato wrote:

> Is it possible to use my java classes generated from a wsdl and xsd's
> to deserialize static xml (i.e. a file)? I have a sample of what the
> output is and I want to run it through deserialization to make sure
> it will work. I can't just run it against the server because I'm
> waiting on other groups to complete their work, but I'd like to get
> as much done as possible in the meantime.

I've attached a class that I use as a helper for unit testing 
deserializers. In the actual test case class, I have a method like the 
following that I use to get at the deserialized class and test it 
further.

    private MyClass deserializeVerzeichnis(String filename)
      throws Exception {
        URL xmlFile = getClass().getResource(filename);
        
        DeserializationTester tester = new DeserializationTester(
                xmlFile, new QName("myclass"),
                MyClass.class,
                new MyClassDeserializerFactory());

        Object result = tester.getResult();
        assertNotNull(result);
        assertTrue(result instanceof MyClass);
        return Verzeichnis vz = (MyClass)result;
    }


HTH,
Michael

-- 
Michael Schuerig               Most people would rather die than think.
mailto:michael@schuerig.de                            In fact, they do.
http://www.schuerig.de/michael/                      --Bertrand Russell