View Javadoc

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