/*=============================================================================*
* 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.MessageContext;
import org.apache.axis.encoding.AnyContentType;
import org.apache.axis.encoding.SerializationContext;
import org.apache.axis.message.MessageElement;
import org.apache.axis.message.Text;
import org.apache.ws.resource.NamespaceConstants;
import org.apache.ws.util.XmlUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import javax.xml.soap.SOAPElement;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.List;
/**
* The <code>AnyHelper</code> is a utility that provides common functions for working with <code>MessageElement</code>
* and beans with <code>AnyContentType</code> class. <b>Do not used this class for serialization or deserialization of
* objects.</b> Use {@link ObjectSerializer} and {@link ObjectDeserializer} for that purposes instead.
*/
public class AnyHelper
{
/**
* DOCUMENT_ME
*
* @param object DOCUMENT_ME
* @param value DOCUMENT_ME
*/
public static void setAny( AnyContentType object,
SOAPElement value )
{
if ( ( value == null ) || ( object == null ) )
{
return;
}
if ( !( value instanceof MessageElement ) )
{
throw new IllegalArgumentException( );
}
object.set_any( new MessageElement[]
{
(MessageElement) value
} );
}
/**
* DOCUMENT_ME
*
* @param object DOCUMENT_ME
* @param values DOCUMENT_ME
*/
public static void setAny( AnyContentType object,
SOAPElement[] values )
{
if ( ( values == null ) || ( object == null ) )
{
return;
}
MessageElement[] me = null;
if ( values instanceof MessageElement[] )
{
me = (MessageElement[]) values;
}
else
{
me = new MessageElement[values.length];
for ( int i = 0; i < values.length; i++ )
{
if ( values[i] instanceof MessageElement )
{
me[i] = (MessageElement) values[i];
}
else
{
throw new IllegalArgumentException( );
}
}
}
object.set_any( me );
}
/**
* DOCUMENT_ME
*
* @param object DOCUMENT_ME
* @param values DOCUMENT_ME
*/
public static void setAny( AnyContentType object,
List values )
{
if ( values == null )
{
return;
}
Object obj;
MessageElement[] v = new MessageElement[values.size( )];
for ( int i = 0; i < values.size( ); i++ )
{
obj = values.get( i );
if ( obj instanceof MessageElement )
{
v[i] = (MessageElement) obj;
}
else
{
throw new IllegalArgumentException( );
}
}
object.set_any( v );
}
/**
* Converts type containing any element to a single DOM Element.
*
* @see #toElement(MessageElement element)
*/
public static Element getFirstAsElement( AnyContentType any )
throws Exception
{
Element[] values = toElement( any );
return ( ( values != null ) && ( values.length > 0 ) ) ? values[0] : null;
}
/**
* Converts type containing any element to a single DOM Element, representing the parent MessageElement.
*
* @see #toElement(MessageElement element)
*/
public static Element getFirstParentAsElement( AnyContentType any )
throws Exception
{
return toElement( getParent( any ) );
}
/**
* Converts type containing any element to a String, representing the parent MessageElement.
*
* @see #toString(MessageElement element)
*/
public static String getFirstParentAsString( AnyContentType any )
throws Exception
{
return toString( getParent( any ) );
}
// ------------------------
public static MessageElement getParent( MessageElement element )
{
return ( element == null ) ? null : (MessageElement) element.getParentElement( );
}
/**
* DOCUMENT_ME
*
* @param elements DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
public static MessageElement getParent( MessageElement[] elements )
{
return ( ( elements != null ) && ( elements.length > 0 ) ) ? getParent( elements[0] ) : null;
}
/**
* DOCUMENT_ME
*
* @param any DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
public static MessageElement getParent( AnyContentType any )
{
return ( any != null ) ? getParent( any.get_any( ) ) : null;
}
/**
* Populates a SOAP MessageElement with an arbitrary object.
*
* @param obj object to be serialized in the any element.
*
* @return content of any element as a SOAP MessageElement
*
* @see #toAnyTypeElement(Object)
*/
public static MessageElement toAny( Object obj )
{
if ( obj == null )
{
return null;
}
if ( obj instanceof MessageElement )
{
return (MessageElement) obj;
}
else if ( obj instanceof Element )
{
return new MessageElement( (Element) obj );
}
return toAnyTypeElement( obj );
}
// -------------------------------------------
/**
* Populates a SOAP MessageElement array with an array of arbitrary objects.
*
* @param obj array of objects to be serialized in the any element
*
* @return content of any element as a SOAP MessageElement array
*/
public static MessageElement[] toAnyArray( Object[] obj )
{
MessageElement[] result = new MessageElement[obj.length];
for ( int i = 0; i < obj.length; i++ )
{
result[i] = toAny( obj[i] );
}
return result;
}
/**
* Populates a SOAP MessageElement array with a single object.
*
* @param obj object to be serialized in the any element
*
* @return content of any element as a SOAP MessageElement array
*/
public static MessageElement[] toAnyArray( Object obj )
{
return new MessageElement[]
{
toAny( obj )
};
}
/**
* Populates a SOAP MessageElement array with a single DOM element.
*
* @param element element to be inserted in the any element
*
* @return content of any element as a SOAP MessageElement array
*/
public static MessageElement[] toAnyArray( Element element )
{
return new MessageElement[]
{
new MessageElement( element )
};
}
/**
* Populates a SOAP MessageElement with an arbitrary object, and wraps it inside of a value element with an xsi:type
* attribute. This is similar to using the xsd:any in the same way you would use xsd:anyType objects.
*
* @param obj object to be serialized in the any element
*
* @return content of any element as a SOAP MessageElement
*/
public static MessageElement toAnyTypeElement( Object obj )
{
MessageElement messageElement = new MessageElement( NamespaceConstants.NSURI_GLOBUS_CORE, "value", obj );
messageElement.setType( org.apache.axis.Constants.XSD_ANYTYPE );
return messageElement;
}
// ****** toElements ********
/**
* Converts a SOAP MessageElement to a DOM Element representation
*
* @param element SOAP MessageElement to be converted
*
* @return DOM Element representing the input
*
* @throws Exception if the DOM Element could not be created
*/
public static Element toElement( MessageElement element )
throws Exception
{
String str = toString( element );
if ( str == null )
{
return null;
}
StringReader reader = new StringReader( str );
Document doc = XmlUtils.newDocument( new InputSource( reader ) );
return ( doc == null ) ? null : doc.getDocumentElement( );
}
/**
* Array version of {@link #toElement(MessageElement element) toElements}
*/
public static Element[] toElement( MessageElement[] elements )
throws Exception
{
if ( elements == null )
{
return null;
}
Element[] result = new Element[elements.length];
for ( int i = 0; i < elements.length; i++ )
{
result[i] = toElement( elements[i] );
}
return result;
}
/**
* Converts type containing any element to an array of DOM Elements.
*
* @see #toElement(MessageElement element)
*/
public static Element[] toElement( AnyContentType any )
throws Exception
{
if ( any == null )
{
return null;
}
return toElement( any.get_any( ) );
}
/**
*
*/
public static String toSingleString( MessageElement[] elements )
throws Exception
{
if ( elements == null )
{
return null;
}
MessageContext messageContext = ContainerConfig.getContext( );
StringWriter writer = new StringWriter( );
SerializationContext context = new SerializationContext( writer, messageContext );
context.setPretty( true );
for ( int i = 0; i < elements.length; i++ )
{
elements[i].output( context );
}
writer.flush( );
return writer.toString( );
}
/**
*
*/
public static String toSingleString( AnyContentType any )
throws Exception
{
return ( any == null ) ? null : toSingleString( any.get_any( ) );
}
// ********* toString **********
/**
* Converts a SOAP MessageElement to an XML String representation
*
* @param element SOAP MessageElement to be converted
*
* @return String in XML format representing the input
*/
public static String toString( MessageElement element )
throws Exception
{
if ( element == null )
{
return null;
}
StringWriter writer = new StringWriter( );
write( writer, element );
writer.flush( );
return writer.toString( );
}
/**
* Array version of {@link #toString(MessageElement element) toString}
*/
public static String[] toString( MessageElement[] elements )
throws Exception
{
if ( elements == null )
{
return null;
}
String[] result = new String[elements.length];
for ( int i = 0; i < elements.length; i++ )
{
result[i] = toString( elements[i] );
}
return result;
}
/**
* Populates a SOAP MessageElement array with a single object.
*
* @param obj object to be serialized as a text node
*
* @return content of any element as a SOAP MessageElement array
*/
public static MessageElement[] toText( Object obj )
{
return new MessageElement[]
{
new MessageElement( new Text( obj.toString( ) ) )
};
}
/**
* DOCUMENT_ME
*
* @param writer DOCUMENT_ME
* @param element DOCUMENT_ME
*
* @throws Exception DOCUMENT_ME
*/
public static void write( Writer writer,
MessageElement element )
throws Exception
{
MessageContext messageContext = ContainerConfig.getContext( );
SerializationContext context = new SerializationContext( writer, messageContext );
context.setPretty( true );
element.output( context );
}
}