View Javadoc

1   /*
2    *  soapui, copyright (C) 2005 Ole Matzura / eviware.com 
3    *
4    *  SoapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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.actions.request;
14  
15  import java.awt.event.ActionEvent;
16  import java.io.StringWriter;
17  
18  import javax.swing.AbstractAction;
19  import javax.swing.Action;
20  import javax.xml.namespace.QName;
21  
22  import org.apache.xmlbeans.XmlCursor;
23  import org.apache.xmlbeans.XmlObject;
24  
25  import com.eviware.soapui.SoapUI;
26  import com.eviware.soapui.impl.wsdl.WsdlRequest;
27  import com.eviware.soapui.support.XmlUtils;
28  
29  /***
30   * Creates an empty WsdlRequest containing a SOAP Envelope and empty Body
31   * 
32   * @author Ole.Matzura
33   */
34  
35  public class CreateEmptyRequestAction extends AbstractAction
36  {
37     private final WsdlRequest request;
38  
39  	public CreateEmptyRequestAction( WsdlRequest request )
40     {
41        super( "Create empty" );
42  		this.request = request;
43        putValue( Action.SMALL_ICON, SoapUI.createImageIcon( "/create_empty_request.gif"));
44        putValue( Action.SHORT_DESCRIPTION, "Creates an empty SOAP request" );
45     }
46     
47     public void actionPerformed(ActionEvent e)
48     {
49        XmlObject object = XmlObject.Factory.newInstance();
50        XmlCursor cursor = object.newCursor();
51        cursor.toNextToken();
52        QName envelopeName = new QName("http://schemas.xmlsoap.org/soap/envelope/", "Envelope");
53        cursor.beginElement(envelopeName);
54        cursor.toFirstChild();
55  
56        cursor.beginElement(new QName("http://schemas.xmlsoap.org/soap/envelope/", "Body"));
57        cursor.insertComment( " request body goes here ");
58  
59        try
60  		{
61  			StringWriter writer = new StringWriter();
62  			XmlUtils.serializePretty(object, writer);
63  			request.setRequest(writer.toString());
64  		}
65  		catch (Exception e1)
66  		{
67  			e1.printStackTrace();
68  			request.setRequest( object.xmlText() );
69  		}
70     }
71  }