1 /***
2 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3 */
4 package net.sourceforge.pmd.util;
5
6 import net.sourceforge.pmd.ast.ASTCompilationUnit;
7 import net.sourceforge.pmd.ast.JavaParser;
8 import net.sourceforge.pmd.ast.ParseException;
9 import net.sourceforge.pmd.ast.SimpleNode;
10 import net.sourceforge.pmd.jaxen.DocumentNavigator;
11 import org.jaxen.BaseXPath;
12 import org.jaxen.JaxenException;
13 import org.jaxen.XPath;
14
15 import javax.swing.BorderFactory;
16 import javax.swing.JButton;
17 import javax.swing.JFrame;
18 import javax.swing.JLabel;
19 import javax.swing.JPanel;
20 import javax.swing.JScrollPane;
21 import javax.swing.JSplitPane;
22 import javax.swing.JTextArea;
23 import javax.swing.JTextPane;
24 import java.awt.Color;
25 import java.awt.Component;
26 import java.awt.GridBagConstraints;
27 import java.awt.GridBagLayout;
28 import java.awt.Insets;
29 import java.awt.Toolkit;
30 import java.awt.event.ActionEvent;
31 import java.awt.event.ActionListener;
32 import java.io.BufferedReader;
33 import java.io.File;
34 import java.io.FileReader;
35 import java.io.FileWriter;
36 import java.io.IOException;
37 import java.io.PrintStream;
38 import java.io.StringReader;
39 import java.util.Iterator;
40
41 public class ASTViewer {
42
43 private static class JSmartPanel extends JPanel {
44
45 private GridBagConstraints constraints = new GridBagConstraints();
46
47 public JSmartPanel() {
48 super(new GridBagLayout());
49 }
50
51 public void add(Component comp, int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int anchor, int fill, Insets insets) {
52 constraints.gridx = gridx;
53 constraints.gridy = gridy;
54 constraints.gridwidth = gridwidth;
55 constraints.gridheight = gridheight;
56 constraints.weightx = weightx;
57 constraints.weighty = weighty;
58 constraints.anchor = anchor;
59 constraints.fill = fill;
60 constraints.insets = insets;
61
62 add(comp, constraints);
63 }
64 }
65
66 private static class MyPrintStream extends PrintStream {
67
68 public MyPrintStream() {
69 super(System.out);
70 }
71
72 private StringBuffer buf = new StringBuffer();
73
74 public void println(String s) {
75 super.println(s);
76 buf.append(s);
77 buf.append(System.getProperty("line.separator"));
78 }
79
80 public String getString() {
81 return buf.toString();
82 }
83 }
84
85 private class ShowListener implements ActionListener {
86 public void actionPerformed(ActionEvent ae) {
87 StringReader sr = new StringReader(codeEditorPane.getText());
88 JavaParser parser = new JavaParser(sr);
89 MyPrintStream ps = new MyPrintStream();
90 System.setOut(ps);
91 try {
92 ASTCompilationUnit c = parser.CompilationUnit();
93 c.dump("");
94 astArea.setText(ps.getString());
95 } catch (ParseException pe) {
96 astArea.setText(pe.fillInStackTrace().getMessage());
97 }
98 }
99 }
100
101 private class SaveListener implements ActionListener {
102 public void actionPerformed(ActionEvent ae) {
103 try {
104 File f = new File(SETTINGS_FILE_NAME);
105 FileWriter fw = new FileWriter(f);
106 fw.write(codeEditorPane.getText());
107 fw.close();
108 } catch (IOException ioe) {
109 }
110 }
111 }
112
113 private class XPathListener implements ActionListener {
114 public void actionPerformed(ActionEvent ae) {
115 if (xpathQueryArea.getText().length() == 0) {
116 xpathResultArea.setText("XPath query field is empty");
117 codeEditorPane.requestFocus();
118 return;
119 }
120 StringReader sr = new StringReader(codeEditorPane.getText());
121 JavaParser parser = new JavaParser(sr);
122 try {
123 XPath xpath = new BaseXPath(xpathQueryArea.getText(), new DocumentNavigator());
124 ASTCompilationUnit c = parser.CompilationUnit();
125 StringBuffer sb = new StringBuffer();
126 for (Iterator iter = xpath.selectNodes(c).iterator(); iter.hasNext();) {
127 SimpleNode node = (SimpleNode) iter.next();
128 String name = node.getClass().getName().substring(node.getClass().getName().lastIndexOf('.')+1);
129 String line = " at line " + String.valueOf(node.getBeginLine());
130 sb.append(name).append(line).append(System.getProperty("line.separator"));
131 }
132 xpathResultArea.setText(sb.toString());
133 if (sb.length() == 0) {
134 xpathResultArea.setText("No results returned " + System.currentTimeMillis());
135 }
136 } catch (ParseException pe) {
137 xpathResultArea.setText(pe.fillInStackTrace().getMessage());
138 } catch (JaxenException je) {
139 xpathResultArea.setText(je.fillInStackTrace().getMessage());
140 }
141 xpathQueryArea.requestFocus();
142 }
143 }
144
145 private static final String SETTINGS_FILE_NAME = System.getProperty("user.home") + System.getProperty("file.separator") + ".pmd_astviewer";
146
147 private JTextPane codeEditorPane = new JTextPane();
148 private JTextArea astArea = new JTextArea();
149 private JTextArea xpathResultArea = new JTextArea();
150 private JTextArea xpathQueryArea = new JTextArea(8, 40);
151 private JFrame frame = new JFrame("AST Viewer");
152
153 public ASTViewer() {
154 JSmartPanel codePanel = new JSmartPanel();
155 JScrollPane codeScrollPane = new JScrollPane(codeEditorPane);
156 codePanel.add(codeScrollPane, 0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0));
157
158 JSmartPanel astPanel = new JSmartPanel();
159 astArea.setRows(20);
160 astArea.setColumns(20);
161 JScrollPane astScrollPane = new JScrollPane(astArea);
162 astPanel.add(astScrollPane, 0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0));
163
164 JSmartPanel xpathResultPanel = new JSmartPanel();
165 xpathResultArea.setRows(20);
166 xpathResultArea.setColumns(20);
167 JScrollPane xpathResultScrollPane = new JScrollPane(xpathResultArea);
168 xpathResultPanel.add(xpathResultScrollPane, 0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0));
169
170 JButton goButton = new JButton("Go");
171 goButton.setMnemonic('g');
172 goButton.addActionListener(new ShowListener());
173 goButton.addActionListener(new SaveListener());
174 goButton.addActionListener(new XPathListener());
175
176 JPanel controlPanel = new JPanel();
177 controlPanel.add(new JLabel("XPath Query (if any) ->"));
178 xpathQueryArea.setBorder(BorderFactory.createLineBorder(Color.black));
179 controlPanel.add(new JScrollPane(xpathQueryArea));
180 controlPanel.add(goButton);
181
182 JSplitPane resultsSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, astPanel, xpathResultPanel);
183 JSplitPane upperSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, codePanel, resultsSplitPane);
184 JSplitPane containerSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, upperSplitPane, controlPanel);
185
186 frame.getContentPane().add(containerSplitPane);
187
188 frame.setSize(1000, 500);
189 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
190 int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
191 int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
192 frame.setLocation((screenWidth/2) - frame.getWidth()/2, (screenHeight/2) - frame.getHeight()/2);
193 frame.setVisible(true);
194 frame.show();
195
196 containerSplitPane.setDividerLocation(containerSplitPane.getMaximumDividerLocation() - (containerSplitPane.getMaximumDividerLocation()/4));
197 upperSplitPane.setDividerLocation(upperSplitPane.getMaximumDividerLocation() / 3);
198 codeEditorPane.setText(loadText());
199 codeEditorPane.setSize(upperSplitPane.getMaximumDividerLocation() / 3, containerSplitPane.getMaximumDividerLocation() - (containerSplitPane.getMaximumDividerLocation()/4));
200 }
201
202 private String loadText() {
203 try {
204 BufferedReader br = new BufferedReader(new FileReader(new File(SETTINGS_FILE_NAME)));
205 StringBuffer text = new StringBuffer();
206 String hold = null;
207 while ( (hold = br.readLine()) != null) {
208 text.append(hold);
209 text.append(System.getProperty("line.separator"));
210 }
211 return text.toString();
212 } catch (IOException e) {
213 e.printStackTrace();
214 return "";
215 }
216 }
217
218 public static void main(String[] args) {
219 new ASTViewer();
220 }
221 }
This page was automatically generated by Maven