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

This page was automatically generated by Maven