View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.actions;
14  
15  import java.awt.BorderLayout;
16  import java.awt.Color;
17  import java.awt.Dimension;
18  import java.awt.event.ActionEvent;
19  import java.lang.reflect.InvocationTargetException;
20  
21  import javax.swing.AbstractAction;
22  import javax.swing.Action;
23  import javax.swing.BorderFactory;
24  import javax.swing.JButton;
25  import javax.swing.JComponent;
26  import javax.swing.JDialog;
27  import javax.swing.JLabel;
28  import javax.swing.JPanel;
29  import javax.swing.JScrollPane;
30  import javax.swing.JSplitPane;
31  import javax.swing.JTabbedPane;
32  import javax.swing.JTextArea;
33  
34  import org.apache.xmlbeans.XmlException;
35  import org.apache.xmlbeans.XmlObject;
36  import org.apache.xmlbeans.XmlOptions;
37  
38  import com.eviware.soapui.SoapUI;
39  import com.eviware.soapui.support.UISupport;
40  import com.eviware.soapui.support.xml.JXEditTextArea;
41  import com.eviware.soapui.support.xml.XmlUtils;
42  import com.jgoodies.forms.builder.ButtonBarBuilder;
43  
44  /***
45   * Action for starting XQuery/XPath tester - not used for now..
46   * 
47   * @author Ole.Matzura
48   */
49  
50  public class XQueryXPathTesterAction extends AbstractAction
51  {
52  	private JDialog dialog;
53  	private JSplitPane mainSplit;
54  	private JXEditTextArea resultArea;
55  	private JSplitPane querySplit;
56  	private JXEditTextArea inputArea;
57  	private JTextArea xqueryArea;
58  	private JTextArea xpathArea;
59  	private JTabbedPane queryTabs;
60  	private JLabel statusLabel;
61  
62  	public XQueryXPathTesterAction()
63  	{
64  		super( "XQuery/XPath Tester" );
65  	}
66  
67  	public void actionPerformed( ActionEvent e )
68  	{
69  		if( dialog == null )
70  			buildDialog();
71  
72  		dialog.setVisible( true );
73  	}
74  
75  	private void buildDialog()
76  	{
77  		JPanel panel = new JPanel( new BorderLayout() );
78  		panel.setBorder( BorderFactory.createEmptyBorder( 3, 3, 3, 3 ) );
79  
80  		mainSplit = UISupport.createHorizontalSplit( createQueryPanel(), createResultPanel() );
81  		mainSplit.setResizeWeight( 0.4 );
82  		panel.add( mainSplit, BorderLayout.CENTER );
83  		panel.add( createStatusBar(), BorderLayout.SOUTH );
84  
85  		dialog = new JDialog( UISupport.getMainFrame(), "XQuery / XPath Tester", false );
86  		dialog.getContentPane().add( panel, BorderLayout.CENTER );
87  		dialog.setPreferredSize( new Dimension( 600, 400 ) );
88  		dialog.pack();
89  
90  		mainSplit.setDividerLocation( 0.5 );
91  		querySplit.setDividerLocation( 0.3 );
92  	}
93  
94  	private JPanel createToolbar()
95  	{
96  		ButtonBarBuilder builder = new ButtonBarBuilder();
97  
98  		JButton runButton = UISupport.createToolbarButton( new RunAction() );
99  		builder.addFixed( runButton );
100 		builder.addRelatedGap();
101 		JButton declareNsButton = UISupport.createToolbarButton( new DeclareNSAction() );
102 		builder.addFixed( declareNsButton );
103 		builder.setBorder( BorderFactory.createEmptyBorder( 3, 3, 3, 3 ) );
104 		return builder.getPanel();
105 	}
106 
107 	private JComponent createStatusBar()
108 	{
109 		JPanel panel = new JPanel( new BorderLayout() );
110 		statusLabel = new JLabel();
111 		panel.add( statusLabel, BorderLayout.WEST );
112 		return panel;
113 	}
114 
115 	private JPanel createQueryPanel()
116 	{
117 		JPanel panel = new JPanel( new BorderLayout() );
118 
119 		querySplit = UISupport.createVerticalSplit( buildQueryTabs(), buildInputArea() );
120 		querySplit.setBorder( null );
121 		querySplit.setResizeWeight( 0.2 );
122 		panel.add( querySplit, BorderLayout.CENTER );
123 
124 		return panel;
125 	}
126 
127 	private JComponent buildQueryTabs()
128 	{
129 		JPanel panel = new JPanel( new BorderLayout() );
130 		panel.add( createToolbar(), BorderLayout.NORTH );
131 
132 		queryTabs = new JTabbedPane();
133 		queryTabs.addTab( "XQuery query", buildXQueryArea() );
134 		queryTabs.addTab( "XPath query", buildXPathArea() );
135 		queryTabs.setTabPlacement( JTabbedPane.BOTTOM );
136 
137 		panel.setBackground( Color.LIGHT_GRAY );
138 		panel.add( queryTabs, BorderLayout.CENTER );
139 		return panel;
140 	}
141 
142 	private JComponent buildXQueryArea()
143 	{
144 		xqueryArea = new JTextArea();
145 		return new JScrollPane( xqueryArea );
146 	}
147 
148 	private JComponent buildXPathArea()
149 	{
150 		xpathArea = new JTextArea();
151 		return new JScrollPane( xpathArea );
152 	}
153 
154 	private JComponent buildInputArea()
155 	{
156 		inputArea = JXEditTextArea.createXmlEditor( true );
157 		return inputArea;
158 	}
159 
160 	private JPanel createResultPanel()
161 	{
162 		JPanel panel = new JPanel( new BorderLayout() );
163 
164 		resultArea = JXEditTextArea.createXmlEditor( false );
165 		panel.add( resultArea, BorderLayout.CENTER );
166 
167 		return panel;
168 	}
169 
170 	private class RunAction extends AbstractAction
171 	{
172 		public RunAction()
173 		{
174 			putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/submit_request.gif" ) );
175 			putValue( Action.SHORT_DESCRIPTION, "Execute current query" );
176 			putValue( Action.ACCELERATOR_KEY, UISupport.getKeyStroke( "alt ENTER" ) );
177 		}
178 
179 		public void actionPerformed( ActionEvent e )
180 		{
181 			try
182 			{
183 				XmlObject xmlObject = XmlObject.Factory.parse( inputArea.getText() );
184 				XmlObject[] objects;
185 
186 				// xquery?
187 				if( queryTabs.getSelectedIndex() == 0 )
188 				{
189 					objects = xmlObject.execQuery( xqueryArea.getText() );
190 				}
191 				else
192 				{
193 					objects = xmlObject.selectPath( xpathArea.getText() );
194 				}
195 
196 				StringBuffer result = new StringBuffer();
197 				XmlOptions options = new XmlOptions();
198 				options.setSaveOuter();
199 
200 				for( int c = 0; c < objects.length; c++ )
201 				{
202 
203 					result.append( objects[c].xmlText( options ) );
204 					result.append( "\n" );
205 				}
206 
207 				resultArea.setText( result.toString() );
208 				statusLabel.setText( "Expression returned " + objects.length + " hits" );
209 			}
210 			catch( Throwable e1 )
211 			{
212 				if( e1 instanceof RuntimeException )
213 				{
214 					e1 = ( ( RuntimeException )e1 ).getCause();
215 					if( e1 instanceof InvocationTargetException )
216 					{
217 						e1 = ( ( InvocationTargetException )e1 ).getTargetException();
218 					}
219 				}
220 
221 				statusLabel.setText( e1.getMessage() );
222 			}
223 		}
224 	}
225 
226 	private class DeclareNSAction extends AbstractAction
227 	{
228 		public DeclareNSAction()
229 		{
230 			putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/declareNs.gif" ) );
231 			putValue( Action.SHORT_DESCRIPTION, "Declares namespaces in current input in xpath expression" );
232 		}
233 
234 		public void actionPerformed( ActionEvent e )
235 		{
236 			try
237 			{
238 				String namespaceDeclarations = XmlUtils.declareXPathNamespaces( inputArea.getText() );
239 				xpathArea.setText( namespaceDeclarations + xpathArea.getText() );
240 				xqueryArea.setText( namespaceDeclarations + xqueryArea.getText() );
241 			}
242 			catch( XmlException e1 )
243 			{
244 				SoapUI.logError( e1 );
245 			}
246 		}
247 	}
248 
249 }