View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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.support.AbstractHttpRequest;
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 filterAbstractHttpRequest(SubmitContext context, AbstractHttpRequest<?> 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  				boolean flag = false;
68  				if( cursor.isContainer() )
69  				{
70  					Element elm = ( Element ) cursor.getDomNode();
71  					NamedNodeMap attributes = elm.getAttributes();
72  					if( attributes != null && attributes.getLength() > 0 )
73  					{
74  					   for( int c = 0; c < attributes.getLength(); c++ )
75  					   {
76  					   	Node node = attributes.item( c );
77  					   	if( node.getNodeValue() == null || node.getNodeValue().trim().length() == 0 )
78  					   	{
79  					   		cursor.removeAttribute( XmlUtils.getQName( node ) );
80  					   		removed = true;
81  					   	}
82  					   }
83  					}
84  					
85  					if( cursor.getTextValue() == null || cursor.getTextValue().trim().length() == 0 && 
86  								XmlUtils.getFirstChildElement( elm ) == null )
87  					{
88  						if( cursor.removeXml())
89  						{
90  							removed = true;
91  							flag = true;
92  						}
93  					}
94  				}
95  				
96  				if( !flag )
97  					cursor.toNextToken();
98  			}
99  			
100 			if( removed )
101 			{
102 				return xmlObject.xmlText(); 
103 			}
104 		}
105 		catch( Exception e )
106 		{
107 			SoapUI.logError( e );
108 		}
109 		finally
110 		{
111 			if( cursor != null )
112 				cursor.dispose();
113 		}
114 		
115 		return content;
116 	}
117 }