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.support.actions;
14  
15  import java.awt.event.ActionEvent;
16  import java.awt.event.KeyEvent;
17  import java.io.StringWriter;
18  
19  import javax.swing.AbstractAction;
20  import javax.swing.Action;
21  import javax.swing.KeyStroke;
22  
23  import org.apache.log4j.Logger;
24  
25  import com.eviware.soapui.SoapUI;
26  import com.eviware.soapui.support.JXmlTextArea;
27  import com.eviware.soapui.support.XmlUtils;
28  
29  /***
30   * Formats the XML of a JXmlTextArea
31   * 
32   * @author Ole.Matzura
33   */
34  
35  public class FormatXmlAction extends AbstractAction
36  {
37  	private final static Logger log = Logger.getLogger( FormatXmlAction.class );
38     private final JXmlTextArea textArea;
39  
40  	public FormatXmlAction( JXmlTextArea textArea)
41     {
42        super( "Format XML" );
43        putValue( Action.SMALL_ICON, SoapUI.createImageIcon( "/format_request.gif"));
44        putValue( Action.SHORT_DESCRIPTION, "Pretty-prints the request xml" );
45        putValue( Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke( KeyEvent.VK_F, KeyEvent.ALT_MASK ));
46  		this.textArea = textArea;
47     }
48     
49  	public void actionPerformed(ActionEvent e)
50  	{
51       try
52        {
53           org.w3c.dom.Document dom = XmlUtils.parseXml(textArea.getText());
54           StringWriter writer = new StringWriter();
55           XmlUtils.serializePretty(dom, writer);
56           textArea.setText(writer.toString()); 
57           textArea.setCaretPosition( 0 );
58        }
59        catch (Exception e1)
60        {
61           log.error( e1.getMessage() );
62        }  
63     }
64  }