/*=============================================================================*
* Copyright 2004 The Apache Software Foundation
*
* 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 org.apache.ws.resource.handler.axis;
import org.apache.axis.Constants;
import org.apache.axis.encoding.DeserializationContext;
import org.apache.axis.encoding.Deserializer;
import org.apache.axis.message.EnvelopeHandler;
import org.apache.axis.message.MessageElement;
import org.apache.axis.message.SOAPHandler;
import org.apache.axis.utils.XMLUtils;
import org.apache.commons.lang.SerializationException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import javax.xml.namespace.QName;
import java.io.StringReader;
/**
* DOCUMENT_ME
*
* @author $author$
* @version $Revision: 1.4 $
*/
public class ObjectDeserializationContext
extends DeserializationContext
{
/**
* DOCUMENT_ME
*/
static Log LOG = LogFactory.getLog( ObjectDeserializationContext.class.getName( ) );
private Deserializer topDeserializer = null;
/**
* Creates a new {@link ObjectDeserializationContext} object.
*
* @param element DOCUMENT_ME
*
* @throws SerializationException DOCUMENT_ME
*/
public ObjectDeserializationContext( MessageElement element )
throws SerializationException
{
this( element, null );
}
/**
* Creates a new {@link ObjectDeserializationContext} object.
*
* @param element DOCUMENT_ME
* @param javaClass DOCUMENT_ME
*
* @throws SerializationException DOCUMENT_ME
*/
public ObjectDeserializationContext( MessageElement element,
Class javaClass )
throws SerializationException
{
super( ContainerConfig.getContext( ), new SOAPHandler( ) );
init( element.getType( ),
javaClass );
String inputString = element.toString( );
LOG.debug( inputString );
inputSource = new InputSource( new StringReader( inputString ) );
}
/**
* Creates a new {@link ObjectDeserializationContext} object.
*
* @param element DOCUMENT_ME
*
* @throws SerializationException DOCUMENT_ME
*/
public ObjectDeserializationContext( Element element )
throws SerializationException
{
this( element, null );
}
/**
* Creates a new {@link ObjectDeserializationContext} object.
*
* @param element DOCUMENT_ME
* @param javaClass DOCUMENT_ME
*
* @throws SerializationException DOCUMENT_ME
*/
public ObjectDeserializationContext( Element element,
Class javaClass )
throws SerializationException
{
super( ContainerConfig.getContext( ), new SOAPHandler( ) );
String typeAttr = element.getAttributeNS( Constants.URI_DEFAULT_SCHEMA_XSI, "type" );
QName type = null;
if ( ( typeAttr != null ) && ( typeAttr.length( ) > 0 ) )
{
type = XMLUtils.getQNameFromString( typeAttr, element );
}
init( type, javaClass );
String inputString = XMLUtils.ElementToString( element );
LOG.debug( inputString );
inputSource = new InputSource( new StringReader( inputString ) );
}
/**
* Creates a new {@link ObjectDeserializationContext} object.
*
* @param input DOCUMENT_ME
* @param javaClass DOCUMENT_ME
*
* @throws SerializationException DOCUMENT_ME
*/
public ObjectDeserializationContext( InputSource input,
Class javaClass )
throws SerializationException
{
super( ContainerConfig.getContext( ), new SOAPHandler( ) );
init( null, javaClass );
this.inputSource = input;
}
/**
* DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
public MessageElement getMessageElement( )
{
if ( ( this.topDeserializer == null ) || !( this.topDeserializer instanceof SOAPHandler ) )
{
return null;
}
return ( (SOAPHandler) this.topDeserializer ).myElement;
}
/**
* DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
public QName getQName( )
{
MessageElement element = getMessageElement( );
return ( element == null ) ? null : element.getQName( );
}
/**
* DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
public Object getValue( )
{
return ( this.topDeserializer == null ) ? null : this.topDeserializer.getValue( );
}
private void setDeserializer( QName type,
Class javaClass )
throws SerializationException
{
if ( ( type == null ) && ( javaClass == null ) )
{
throw new SerializationException( "typeOrClassRequired" );
}
if ( type != null )
{
// Use the xmlType to get the deserializer.
this.topDeserializer = getDeserializerForType( type );
}
else
{
QName defaultXMLType = getTypeMapping( ).getTypeQName( javaClass );
this.topDeserializer = getDeserializer( javaClass, defaultXMLType );
}
if ( this.topDeserializer == null )
{
this.topDeserializer = getDeserializerForClass( javaClass );
}
}
private void init( QName type,
Class javaClass )
throws SerializationException
{
msgContext.setEncodingStyle( "" );
popElementHandler( );
setDeserializer( type, javaClass );
if ( topDeserializer == null )
{
String arg = ( type == null ) ? javaClass.getName( ) : type.toString( );
throw new SerializationException( "noDeserializer" + arg );
}
pushElementHandler( new EnvelopeHandler( (SOAPHandler) this.topDeserializer ) );
}
}