001: package org.acm.seguin.pmd.util;
002:
003: import net.sourceforge.jrefactory.ast.ASTCompilationUnit;
004: import net.sourceforge.jrefactory.parser.JavaParser;
005: import net.sourceforge.jrefactory.parser.ParseException;
006: import net.sourceforge.jrefactory.ast.SimpleNode;
007: import org.acm.seguin.pmd.jaxen.DocumentNavigator;
008: import org.jaxen.BaseXPath;
009: import org.jaxen.JaxenException;
010: import org.jaxen.XPath;
011:
012: import javax.swing.BorderFactory;
013: import javax.swing.JButton;
014: import javax.swing.JFrame;
015: import javax.swing.JLabel;
016: import javax.swing.JPanel;
017: import javax.swing.JScrollPane;
018: import javax.swing.JSplitPane;
019: import javax.swing.JTextArea;
020: import javax.swing.JTextPane;
021: import java.awt.Color;
022: import java.awt.Component;
023: import java.awt.GridBagConstraints;
024: import java.awt.GridBagLayout;
025: import java.awt.Insets;
026: import java.awt.Toolkit;
027: import java.awt.event.ActionEvent;
028: import java.awt.event.ActionListener;
029: import java.io.BufferedReader;
030: import java.io.File;
031: import java.io.FileReader;
032: import java.io.FileWriter;
033: import java.io.IOException;
034: import java.io.PrintStream;
035: import java.io.StringReader;
036: import java.util.Iterator;
037:
038: public class ASTViewer {
039: public static class JSmartPanel extends JPanel {
040:
041: private GridBagConstraints constraints = new GridBagConstraints();
042:
043: public JSmartPanel() {
044: super (new GridBagLayout());
045: }
046:
047: public void add(Component comp, int gridx, int gridy,
048: int gridwidth, int gridheight, double weightx,
049: double weighty, int anchor, int fill, Insets insets) {
050: constraints.gridx = gridx;
051: constraints.gridy = gridy;
052: constraints.gridwidth = gridwidth;
053: constraints.gridheight = gridheight;
054: constraints.weightx = weightx;
055: constraints.weighty = weighty;
056: constraints.anchor = anchor;
057: constraints.fill = fill;
058: constraints.insets = insets;
059:
060: add(comp, constraints);
061: }
062: }
063:
064: private static class MyPrintStream extends PrintStream {
065:
066: public MyPrintStream() {
067: super (System.out);
068: }
069:
070: private StringBuffer buf = new StringBuffer();
071:
072: public void println(String s) {
073: super .println(s);
074: buf.append(s);
075: buf.append(System.getProperty("line.separator"));
076: }
077:
078: public String getString() {
079: return buf.toString();
080: }
081: }
082:
083: private class ShowListener implements ActionListener {
084: public void actionPerformed(ActionEvent ae) {
085: StringReader sr = new StringReader(codeEditorPane.getText());
086: JavaParser parser = new JavaParser(sr);
087: //MyPrintStream ps = new MyPrintStream();
088: //System.setOut(ps);
089: try {
090: ASTCompilationUnit c = parser.CompilationUnit();
091: //c.dump("");
092: astArea.setText(c.dumpString("\r\n"));
093: } catch (ParseException pe) {
094: astArea.setText(pe.fillInStackTrace().getMessage());
095: }
096: }
097: }
098:
099: private class SaveListener implements ActionListener {
100: public void actionPerformed(ActionEvent ae) {
101: try {
102: File f = new File(SETTINGS_FILE_NAME);
103: FileWriter fw = new FileWriter(f);
104: fw.write(codeEditorPane.getText());
105: fw.close();
106: } catch (IOException ioe) {
107: }
108: }
109: }
110:
111: private class XPathListener implements ActionListener {
112: public void actionPerformed(ActionEvent ae) {
113: if (xpathQueryArea.getText().length() == 0) {
114: xpathResultArea.setText("XPath query field is empty");
115: codeEditorPane.requestFocus();
116: return;
117: }
118: StringReader sr = new StringReader(codeEditorPane.getText());
119: JavaParser parser = new JavaParser(sr);
120: try {
121: XPath xpath = new BaseXPath(xpathQueryArea.getText(),
122: new DocumentNavigator());
123: ASTCompilationUnit c = parser.CompilationUnit();
124: StringBuffer sb = new StringBuffer();
125: for (Iterator iter = xpath.selectNodes(c).iterator(); iter
126: .hasNext();) {
127: SimpleNode node = (SimpleNode) iter.next();
128: String name = node.getClass().getName()
129: .substring(
130: node.getClass().getName()
131: .lastIndexOf('.') + 1);
132: String line = " at line "
133: + String.valueOf(node.getBeginLine());
134: sb.append(name).append(line).append(
135: System.getProperty("line.separator"));
136: }
137: xpathResultArea.setText(sb.toString());
138: if (sb.length() == 0) {
139: xpathResultArea.setText("No results returned "
140: + System.currentTimeMillis());
141: }
142: } catch (ParseException pe) {
143: xpathResultArea.setText(pe.fillInStackTrace()
144: .getMessage());
145: } catch (JaxenException je) {
146: xpathResultArea.setText(je.fillInStackTrace()
147: .getMessage());
148: }
149: xpathQueryArea.requestFocus();
150: }
151: }
152:
153: private static final String SETTINGS_FILE_NAME = System
154: .getProperty("user.home")
155: + System.getProperty("file.separator") + ".pmd_astviewer";
156:
157: private JTextPane codeEditorPane = new JTextPane();
158: private JTextArea astArea = new JTextArea();
159: private JTextArea xpathResultArea = new JTextArea();
160: private JTextArea xpathQueryArea = new JTextArea(8, 40);
161: private JFrame frame = new JFrame("AST Viewer");
162:
163: private JSplitPane resultsSplitPane;
164: private JSplitPane upperSplitPane;
165: private JSplitPane containerSplitPane;
166:
167: public ASTViewer() {
168: JSmartPanel codePanel = new JSmartPanel();
169: JScrollPane codeScrollPane = new JScrollPane(codeEditorPane);
170: codePanel.add(codeScrollPane, 0, 0, 1, 1, 1.0, 1.0,
171: GridBagConstraints.NORTH, GridBagConstraints.BOTH,
172: new Insets(0, 0, 0, 0));
173:
174: JSmartPanel astPanel = new JSmartPanel();
175: astArea.setRows(20);
176: astArea.setColumns(20);
177: JScrollPane astScrollPane = new JScrollPane(astArea);
178: astPanel.add(astScrollPane, 0, 0, 1, 1, 1.0, 1.0,
179: GridBagConstraints.NORTH, GridBagConstraints.BOTH,
180: new Insets(0, 0, 0, 0));
181:
182: JSmartPanel xpathResultPanel = new JSmartPanel();
183: xpathResultArea.setRows(20);
184: xpathResultArea.setColumns(20);
185: JScrollPane xpathResultScrollPane = new JScrollPane(
186: xpathResultArea);
187: xpathResultPanel.add(xpathResultScrollPane, 0, 0, 1, 1, 1.0,
188: 1.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH,
189: new Insets(0, 0, 0, 0));
190:
191: JButton goButton = new JButton("Go");
192: goButton.setMnemonic('g');
193: goButton.addActionListener(new ShowListener());
194: goButton.addActionListener(new SaveListener());
195: goButton.addActionListener(new XPathListener());
196:
197: JPanel controlPanel = new JPanel();
198: controlPanel.add(new JLabel("XPath Query (if any) ->"));
199: xpathQueryArea.setBorder(BorderFactory
200: .createLineBorder(Color.black));
201: controlPanel.add(new JScrollPane(xpathQueryArea));
202: controlPanel.add(goButton);
203:
204: resultsSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
205: astPanel, xpathResultPanel);
206: upperSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
207: codePanel, resultsSplitPane);
208: containerSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
209: upperSplitPane, controlPanel);
210: }
211:
212: private void initFrame() {
213: frame.getContentPane().add(containerSplitPane);
214:
215: frame.setSize(1000, 500);
216: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
217: int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
218: int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
219: frame.setLocation((screenWidth / 2) - frame.getWidth() / 2,
220: (screenHeight / 2) - frame.getHeight() / 2);
221: frame.setVisible(true);
222: frame.show();
223: }
224:
225: public void initDividers() {
226: containerSplitPane.setDividerLocation(3 * containerSplitPane
227: .getMaximumDividerLocation() / 4);
228: upperSplitPane.setDividerLocation(upperSplitPane
229: .getMaximumDividerLocation() / 3);
230: codeEditorPane.setText(loadText());
231: codeEditorPane.setSize(resultsSplitPane
232: .getMaximumDividerLocation(), containerSplitPane
233: .getMaximumDividerLocation() / 2);
234: }
235:
236: public JSplitPane getMainPanel() {
237: return containerSplitPane;
238: }
239:
240: private String loadText() {
241: try {
242: BufferedReader br = new BufferedReader(new FileReader(
243: new File(SETTINGS_FILE_NAME)));
244: StringBuffer text = new StringBuffer();
245: String hold = null;
246: while ((hold = br.readLine()) != null) {
247: text.append(hold);
248: text.append(System.getProperty("line.separator"));
249: }
250: return text.toString();
251: } catch (IOException e) {
252: e.printStackTrace();
253: return "";
254: }
255: }
256:
257: public static void main(String[] args) {
258: ASTViewer stv = new ASTViewer();
259: stv.initFrame();
260: stv.initDividers();
261: }
262: }
|