001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.actions;
014:
015: import java.awt.BorderLayout;
016: import java.awt.Color;
017: import java.awt.Dimension;
018: import java.awt.event.ActionEvent;
019: import java.lang.reflect.InvocationTargetException;
020:
021: import javax.swing.AbstractAction;
022: import javax.swing.Action;
023: import javax.swing.BorderFactory;
024: import javax.swing.JButton;
025: import javax.swing.JComponent;
026: import javax.swing.JDialog;
027: import javax.swing.JLabel;
028: import javax.swing.JPanel;
029: import javax.swing.JScrollPane;
030: import javax.swing.JSplitPane;
031: import javax.swing.JTabbedPane;
032: import javax.swing.JTextArea;
033:
034: import org.apache.xmlbeans.XmlException;
035: import org.apache.xmlbeans.XmlObject;
036: import org.apache.xmlbeans.XmlOptions;
037:
038: import com.eviware.soapui.SoapUI;
039: import com.eviware.soapui.support.UISupport;
040: import com.eviware.soapui.support.xml.JXEditTextArea;
041: import com.eviware.soapui.support.xml.XmlUtils;
042: import com.jgoodies.forms.builder.ButtonBarBuilder;
043:
044: /**
045: * Action for starting XQuery/XPath tester - not used for now..
046: *
047: * @author Ole.Matzura
048: */
049:
050: public class XQueryXPathTesterAction extends AbstractAction {
051: private JDialog dialog;
052: private JSplitPane mainSplit;
053: private JXEditTextArea resultArea;
054: private JSplitPane querySplit;
055: private JXEditTextArea inputArea;
056: private JTextArea xqueryArea;
057: private JTextArea xpathArea;
058: private JTabbedPane queryTabs;
059: private JLabel statusLabel;
060:
061: public XQueryXPathTesterAction() {
062: super ("XQuery/XPath Tester");
063: }
064:
065: public void actionPerformed(ActionEvent e) {
066: if (dialog == null)
067: buildDialog();
068:
069: dialog.setVisible(true);
070: }
071:
072: private void buildDialog() {
073: JPanel panel = new JPanel(new BorderLayout());
074: panel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
075:
076: mainSplit = UISupport.createHorizontalSplit(createQueryPanel(),
077: createResultPanel());
078: mainSplit.setResizeWeight(0.4);
079: panel.add(mainSplit, BorderLayout.CENTER);
080: panel.add(createStatusBar(), BorderLayout.SOUTH);
081:
082: dialog = new JDialog(UISupport.getMainFrame(),
083: "XQuery / XPath Tester", false);
084: dialog.getContentPane().add(panel, BorderLayout.CENTER);
085: dialog.setPreferredSize(new Dimension(600, 400));
086: dialog.pack();
087:
088: mainSplit.setDividerLocation(0.5);
089: querySplit.setDividerLocation(0.3);
090: }
091:
092: private JPanel createToolbar() {
093: ButtonBarBuilder builder = new ButtonBarBuilder();
094:
095: JButton runButton = UISupport
096: .createToolbarButton(new RunAction());
097: builder.addFixed(runButton);
098: builder.addRelatedGap();
099: JButton declareNsButton = UISupport
100: .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: JPanel panel = new JPanel(new BorderLayout());
108: statusLabel = new JLabel();
109: panel.add(statusLabel, BorderLayout.WEST);
110: return panel;
111: }
112:
113: private JPanel createQueryPanel() {
114: JPanel panel = new JPanel(new BorderLayout());
115:
116: querySplit = UISupport.createVerticalSplit(buildQueryTabs(),
117: buildInputArea());
118: querySplit.setBorder(null);
119: querySplit.setResizeWeight(0.2);
120: panel.add(querySplit, BorderLayout.CENTER);
121:
122: return panel;
123: }
124:
125: private JComponent buildQueryTabs() {
126: JPanel panel = new JPanel(new BorderLayout());
127: panel.add(createToolbar(), BorderLayout.NORTH);
128:
129: queryTabs = new JTabbedPane();
130: queryTabs.addTab("XQuery query", buildXQueryArea());
131: queryTabs.addTab("XPath query", buildXPathArea());
132: queryTabs.setTabPlacement(JTabbedPane.BOTTOM);
133:
134: panel.setBackground(Color.LIGHT_GRAY);
135: panel.add(queryTabs, BorderLayout.CENTER);
136: return panel;
137: }
138:
139: private JComponent buildXQueryArea() {
140: xqueryArea = new JTextArea();
141: return new JScrollPane(xqueryArea);
142: }
143:
144: private JComponent buildXPathArea() {
145: xpathArea = new JTextArea();
146: return new JScrollPane(xpathArea);
147: }
148:
149: private JComponent buildInputArea() {
150: inputArea = JXEditTextArea.createXmlEditor();
151: return inputArea;
152: }
153:
154: private JPanel createResultPanel() {
155: JPanel panel = new JPanel(new BorderLayout());
156:
157: resultArea = JXEditTextArea.createXmlEditor();
158: panel.add(resultArea, BorderLayout.CENTER);
159:
160: return panel;
161: }
162:
163: private class RunAction extends AbstractAction {
164: public RunAction() {
165: putValue(Action.SMALL_ICON, UISupport
166: .createImageIcon("/submit_request.gif"));
167: putValue(Action.SHORT_DESCRIPTION, "Execute current query");
168: putValue(Action.ACCELERATOR_KEY, UISupport
169: .getKeyStroke("alt ENTER"));
170: }
171:
172: public void actionPerformed(ActionEvent e) {
173: try {
174: XmlObject xmlObject = XmlObject.Factory.parse(inputArea
175: .getText());
176: XmlObject[] objects;
177:
178: // xquery?
179: if (queryTabs.getSelectedIndex() == 0) {
180: objects = xmlObject.execQuery(xqueryArea.getText());
181: } else {
182: objects = xmlObject.selectPath(xpathArea.getText());
183: }
184:
185: StringBuffer result = new StringBuffer();
186: XmlOptions options = new XmlOptions();
187: options.setSaveOuter();
188:
189: for (int c = 0; c < objects.length; c++) {
190:
191: result.append(objects[c].xmlText(options));
192: result.append("\n");
193: }
194:
195: resultArea.setText(result.toString());
196: statusLabel.setText("Expression returned "
197: + objects.length + " hits");
198: } catch (Throwable e1) {
199: if (e1 instanceof RuntimeException) {
200: e1 = ((RuntimeException) e1).getCause();
201: if (e1 instanceof InvocationTargetException) {
202: e1 = ((InvocationTargetException) e1)
203: .getTargetException();
204: }
205: }
206:
207: statusLabel.setText(e1.getMessage());
208: }
209: }
210: }
211:
212: private class DeclareNSAction extends AbstractAction {
213: public DeclareNSAction() {
214: putValue(Action.SMALL_ICON, UISupport
215: .createImageIcon("/declareNs.gif"));
216: putValue(Action.SHORT_DESCRIPTION,
217: "Declares namespaces in current input in xpath expression");
218: }
219:
220: public void actionPerformed(ActionEvent e) {
221: try {
222: String namespaceDeclarations = XmlUtils
223: .declareXPathNamespaces(inputArea.getText());
224: xpathArea.setText(namespaceDeclarations
225: + xpathArea.getText());
226: xqueryArea.setText(namespaceDeclarations
227: + xqueryArea.getText());
228: } catch (XmlException e1) {
229: SoapUI.logError(e1);
230: }
231: }
232: }
233:
234: }
|