1
2
3
4
5
6
7
8
9
10
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 }