View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.impl.wsdl.submit.filters;
14  
15  import org.apache.log4j.Logger;
16  import org.apache.xmlbeans.XmlCursor;
17  import org.apache.xmlbeans.XmlObject;
18  import org.w3c.dom.Element;
19  import org.w3c.dom.NamedNodeMap;
20  import org.w3c.dom.Node;
21  
22  import com.eviware.soapui.SoapUI;
23  import com.eviware.soapui.impl.wsdl.WsdlRequest;
24  import com.eviware.soapui.impl.wsdl.submit.transports.http.BaseHttpRequestTransport;
25  import com.eviware.soapui.model.iface.SubmitContext;
26  import com.eviware.soapui.support.xml.XmlUtils;
27  
28  /***
29   * RequestFilter for removing empty elements/attributes
30   * 
31   * @author Ole.Matzura
32   */
33  
34  public class RemoveEmptyContentRequestFilter extends AbstractRequestFilter
35  {
36  	@SuppressWarnings("unused")
37  	private final static Logger log = Logger.getLogger(PropertyExpansionRequestFilter.class);
38  	
39  	public void filterRequest(SubmitContext context, WsdlRequest wsdlRequest)
40  	{
41  		if( wsdlRequest != null && !wsdlRequest.isRemoveEmptyContent())
42  			return;
43  		
44  		String content = (String) context.getProperty( BaseHttpRequestTransport.REQUEST_CONTENT );
45  		content = removeEmptyContent( content );
46  		if( content != null )
47  			context.setProperty( BaseHttpRequestTransport.REQUEST_CONTENT, content );
48  	}
49  
50  	public static String removeEmptyContent( String content )
51  	{
52  		XmlCursor cursor = null;
53  		
54  		try
55  		{
56  			XmlObject xmlObject = XmlObject.Factory.parse( content );
57  			cursor = xmlObject.newCursor();
58  			
59  			cursor.toNextToken();
60  			
61  			// skip root element
62  			cursor.toNextToken();
63  			boolean removed = false;
64  			
65  			while( !cursor.isEnddoc() )
66  			{
67  				if( cursor.isContainer() )
68  				{
69  					Element elm = ( Element ) cursor.getDomNode();
70  					NamedNodeMap attributes = elm.getAttributes();
71  					if( attributes != null && attributes.getLength() > 0 )
72  					{
73  					   for( int c = 0; c < attributes.getLength(); c++ )
74  					   {
75  					   	Node node = attributes.item( c );
76  					   	if( node.getNodeValue() == null || node.getNodeValue().trim().length() == 0 )
77  					   	{
78  					   		cursor.removeAttribute( XmlUtils.getQName( node ) );
79  					   		removed = true;
80  					   	}
81  					   }
82  					}
83  					
84  					if( cursor.getTextValue() == null || cursor.getTextValue().trim().length() == 0 && 
85  								XmlUtils.getFirstChildElement( elm ) == null )
86  					{
87  						if( cursor.removeXml())
88  						{
89  							removed = true;
90  						}
91  					}
92  				}
93  				
94  				cursor.toNextToken();
95  			}
96  			
97  			if( removed )
98  			{
99  				return xmlObject.xmlText(); 
100 			}
101 		}
102 		catch( Exception e )
103 		{
104 			SoapUI.logError( e );
105 		}
106 		finally
107 		{
108 			if( cursor != null )
109 				cursor.dispose();
110 		}
111 		
112 		return null;
113 	}
114 }