/*=============================================================================*
* 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.util;
import org.apache.ws.util.helper.Dom2SaajConverter;
import org.apache.ws.util.i18n.Keys;
import org.apache.ws.util.i18n.MessagesImpl;
import org.apache.ws.util.i18n.Messages;
import org.w3c.dom.Element;
import javax.xml.namespace.QName;
import javax.xml.soap.Node;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.Text;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* LOG-DONE
* @author Ian P. Springer (Hewlett-Packard Company)
*/
public abstract class SaajUtils
{
public static final Messages MSG = MessagesImpl.getInstance();
/**
* Given a {@link SOAPElement}, returns a list containing the children of that element. If at least one of the
* children is a SOAPElement, any {@link Text} nodes are excluded from the list. This is because it is assumed that
* if the specified element has mixed content, it is only because of insignificant whitespace text nodes.
*
* @param soapElem a SAAJ {@link SOAPElement}
*
* @return a list containing the children of the specified {@link SOAPElement}
*/
public static List getChildNodes( SOAPElement soapElem )
{
List elem_children = new ArrayList( );
List text_children = new ArrayList( );
Iterator node_iter = soapElem.getChildElements( );
while ( node_iter.hasNext( ) )
{
Node child_node = (Node) node_iter.next( );
if ( isSOAPElement( child_node ) )
{
elem_children.add( child_node );
}
else
{
text_children.add( child_node );
}
}
return ( elem_children.isEmpty( ) ? text_children : elem_children );
}
/**
* DOCUMENT_ME
*
* @param elem DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
public static QName getQualifiedName( SOAPElement elem )
{
return ( elem != null ) ? NameUtils.toQName( elem.getElementName( ) ) : null;
}
/**
* Returns true if the specified SAAJ node is an element, or false otherwise.
*
* @param soap_node a SAAJ node
*
* @return true if the specified SAAJ node is an element, or false otherwise
*/
public static boolean isSOAPElement( Node soap_node )
{
// NOTE: the second condition below is nessary for Axis, because its Text impl also implements SOAPElement
return ( soap_node instanceof SOAPElement && !( soap_node instanceof Text ) );
}
/**
* Returns true if the specified SAAJ node is text, or false otherwise.
*
* @param soap_node a SAAJ node
*
* @return true if the specified SAAJ node is text, or false otherwise
*/
public static boolean isText( Node soap_node )
{
return ( soap_node instanceof Text );
}
/**
* Returns the value of the specified SAAJ text node.
*
* @param soapNode a SAAJ text node
*
* @return the value of the specified SAAJ text node
*
* @throws SOAPException
*/
public static String getValue( Node soapNode )
throws SOAPException
{
if ( soapNode == null )
{
throw new IllegalArgumentException( MSG.getMessage( Keys.PARAM_MAY_NOT_BE_NULL) );
}
if ( isText( soapNode ) )
{
return getTextValue( (Text) soapNode );
}
else
{
return getSOAPElementValue( (SOAPElement) soapNode );
}
}
/**
* DOCUMENT_ME
*
* @param soapElem DOCUMENT_ME
* @param value DOCUMENT_ME
*
* @throws SOAPException DOCUMENT_ME
*/
public static void addTextNode( SOAPElement soapElem,
String value )
throws SOAPException
{
soapElem.addTextNode( value );
if ( soapElem.getValue( ) == null )
{
Iterator childElemIter = soapElem.getChildElements( );
if ( childElemIter.hasNext( ) )
{
Node firstChild = (Node) childElemIter.next( );
if ( firstChild instanceof Text )
{
firstChild.setValue( value );
return;
}
}
}
}
/**
* DOCUMENT_ME
*
* @param value DOCUMENT_ME
*
* @return DOCUMENT_ME
*
* @throws SOAPException DOCUMENT_ME
*/
public static Text createTextNode( String value )
throws SOAPException
{
return ( (Text) SOAPFactory.newInstance( ).createElement( "foo" ).addTextNode( value ).getChildElements( )
.next( ) );
}
/**
* Converts a DOM {@link org.w3c.dom.Element} to a SAAJ {@link javax.xml.soap.SOAPElement}.
*
* @param elem a DOM {@link org.w3c.dom.Element}
*
* @return a {@link javax.xml.soap.SOAPElement}
*
* @throws javax.xml.soap.SOAPException
*/
public static SOAPElement toSOAPElement( Element elem )
throws SOAPException
{
return ( new Dom2SaajConverter( ).toSOAPElement( elem ) );
}
private static String getSOAPElementValue( SOAPElement soapElem )
{
String value = soapElem.getValue( );
if ( value == null )
{
Text textNode = getTextNode( soapElem );
if ( textNode != null )
{
value = textNode.getValue( );
}
}
if ( value == null )
{
value = soapElem.getNodeValue( );
}
return value;
}
private static Text getTextNode( SOAPElement soapElem )
{
Iterator childElemIter = soapElem.getChildElements( );
if ( childElemIter.hasNext( ) )
{
Node firstChild = (Node) childElemIter.next( );
if ( firstChild instanceof Text )
{
return (Text) firstChild;
}
}
return null;
}
private static String getTextValue( Text soapText )
throws SOAPException
{
String value = soapText.getValue( );
if ( value == null )
{
value = soapText.toString( );
}
if ( value == null )
{
throw new SOAPException( MSG.getMessage( Keys.TEXT_NODE_IS_NULL) );
}
return value;
}
}