1 |
| |
2 |
| |
3 |
| |
4 |
| package net.sourceforge.pmd.util.designer; |
5 |
| |
6 |
| import net.sourceforge.pmd.PMD; |
7 |
| import net.sourceforge.pmd.RuleContext; |
8 |
| import net.sourceforge.pmd.RuleSet; |
9 |
| import net.sourceforge.pmd.TargetJDK1_4; |
10 |
| import net.sourceforge.pmd.TargetJDK1_5; |
11 |
| import net.sourceforge.pmd.TargetJDKVersion; |
12 |
| import net.sourceforge.pmd.ast.ASTCompilationUnit; |
13 |
| import net.sourceforge.pmd.ast.JavaParser; |
14 |
| import net.sourceforge.pmd.ast.ParseException; |
15 |
| import net.sourceforge.pmd.ast.SimpleNode; |
16 |
| import net.sourceforge.pmd.jaxen.DocumentNavigator; |
17 |
| import org.apache.xml.serialize.OutputFormat; |
18 |
| import org.apache.xml.serialize.XMLSerializer; |
19 |
| import org.jaxen.BaseXPath; |
20 |
| import org.jaxen.JaxenException; |
21 |
| import org.jaxen.XPath; |
22 |
| |
23 |
| import javax.swing.*; |
24 |
| import java.awt.BorderLayout; |
25 |
| import java.awt.Color; |
26 |
| import java.awt.FlowLayout; |
27 |
| import java.awt.Toolkit; |
28 |
| import java.awt.datatransfer.Clipboard; |
29 |
| import java.awt.datatransfer.ClipboardOwner; |
30 |
| import java.awt.datatransfer.StringSelection; |
31 |
| import java.awt.datatransfer.Transferable; |
32 |
| import java.awt.event.ActionEvent; |
33 |
| import java.awt.event.ActionListener; |
34 |
| import java.awt.event.KeyEvent; |
35 |
| import java.io.IOException; |
36 |
| import java.io.StringReader; |
37 |
| import java.io.StringWriter; |
38 |
| import java.util.Iterator; |
39 |
| import java.util.List; |
40 |
| |
41 |
| public class Designer implements ClipboardOwner { |
42 |
| |
43 |
0
| private JavaParser createParser() {
|
44 |
0
| return getJDKVersion().createParser(new StringReader(codeEditorPane.getText()));
|
45 |
| } |
46 |
| |
47 |
0
| private TargetJDKVersion getJDKVersion() {
|
48 |
0
| if (jdk14MenuItem.isSelected()) {
|
49 |
0
| return new TargetJDK1_4();
|
50 |
| } |
51 |
0
| return new TargetJDK1_5();
|
52 |
| } |
53 |
| |
54 |
| private class ShowListener implements ActionListener { |
55 |
0
| public void actionPerformed(ActionEvent ae) {
|
56 |
0
| MyPrintStream ps = new MyPrintStream();
|
57 |
0
| System.setOut(ps);
|
58 |
0
| try {
|
59 |
0
| ASTCompilationUnit lastCompilationUnit = createParser().CompilationUnit();
|
60 |
0
| lastCompilationUnit.dump("");
|
61 |
0
| astArea.setText(ps.getString());
|
62 |
| } catch (ParseException pe) { |
63 |
0
| astArea.setText(pe.fillInStackTrace().getMessage());
|
64 |
| } |
65 |
| } |
66 |
| } |
67 |
| |
68 |
| private class DFAListener implements ActionListener { |
69 |
0
| public void actionPerformed(ActionEvent ae) {
|
70 |
0
| try {
|
71 |
0
| DFAGraphRule dfaGraphRule = new DFAGraphRule();
|
72 |
0
| RuleSet rs = new RuleSet();
|
73 |
0
| rs.addRule(dfaGraphRule);
|
74 |
0
| RuleContext ctx = new RuleContext();
|
75 |
0
| ctx.setSourceCodeFilename("[no filename]");
|
76 |
0
| StringReader reader = new StringReader(codeEditorPane.getText());
|
77 |
0
| new PMD(getJDKVersion()).processFile(reader, rs, ctx);
|
78 |
0
| List methods = dfaGraphRule.getMethods();
|
79 |
0
| if (!methods.isEmpty()) {
|
80 |
0
| dfaPanel.resetTo(methods, codeEditorPane);
|
81 |
0
| dfaPanel.repaint();
|
82 |
| } |
83 |
| } catch (Exception e) { |
84 |
0
| e.printStackTrace();
|
85 |
| } |
86 |
| } |
87 |
| |
88 |
| } |
89 |
| |
90 |
| private class XPathListener implements ActionListener { |
91 |
0
| public void actionPerformed(ActionEvent ae) {
|
92 |
0
| xpathResults.clear();
|
93 |
0
| if (xpathQueryArea.getText().length() == 0) {
|
94 |
0
| xpathResults.addElement("XPath query field is empty");
|
95 |
0
| xpathResultList.repaint();
|
96 |
0
| codeEditorPane.requestFocus();
|
97 |
0
| return;
|
98 |
| } |
99 |
0
| JavaParser parser = createParser();
|
100 |
0
| try {
|
101 |
0
| XPath xpath = new BaseXPath(xpathQueryArea.getText(), new DocumentNavigator());
|
102 |
0
| ASTCompilationUnit c = parser.CompilationUnit();
|
103 |
0
| for (Iterator iter = xpath.selectNodes(c).iterator(); iter.hasNext();) {
|
104 |
0
| StringBuffer sb = new StringBuffer();
|
105 |
0
| SimpleNode node = (SimpleNode) iter.next();
|
106 |
0
| String name = node.getClass().getName().substring(node.getClass().getName().lastIndexOf('.') + 1);
|
107 |
0
| String line = " at line " + String.valueOf(node.getBeginLine());
|
108 |
0
| sb.append(name).append(line).append(System.getProperty("line.separator"));
|
109 |
0
| xpathResults.addElement(sb.toString().trim());
|
110 |
| } |
111 |
0
| if (xpathResults.isEmpty()) {
|
112 |
0
| xpathResults.addElement("No matching nodes " + System.currentTimeMillis());
|
113 |
| } |
114 |
| } catch (ParseException pe) { |
115 |
0
| xpathResults.addElement(pe.fillInStackTrace().getMessage());
|
116 |
| } catch (JaxenException je) { |
117 |
0
| xpathResults.addElement(je.fillInStackTrace().getMessage());
|
118 |
| } |
119 |
0
| xpathResultList.repaint();
|
120 |
0
| xpathQueryArea.requestFocus();
|
121 |
| } |
122 |
| } |
123 |
| |
124 |
| |
125 |
| private final CodeEditorTextPane codeEditorPane = new CodeEditorTextPane(); |
126 |
| private final JTextArea astArea = new JTextArea(); |
127 |
| private DefaultListModel xpathResults = new DefaultListModel(); |
128 |
| private final JList xpathResultList = new JList(xpathResults); |
129 |
| private final JTextArea xpathQueryArea = new JTextArea(15, 30); |
130 |
| private final JFrame frame = new JFrame("PMD Rule Designer"); |
131 |
| private final DFAPanel dfaPanel = new DFAPanel(); |
132 |
| private JRadioButtonMenuItem jdk14MenuItem; |
133 |
| private JRadioButtonMenuItem jdk15MenuItem; |
134 |
| |
135 |
0
| public Designer() {
|
136 |
0
| JSplitPane controlSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(codeEditorPane), createXPathQueryPanel());
|
137 |
0
| JSplitPane resultsSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, createASTPanel(), createXPathResultPanel());
|
138 |
| |
139 |
0
| JTabbedPane tabbed = new JTabbedPane();
|
140 |
0
| tabbed.addTab("Abstract Syntax Tree / XPath", resultsSplitPane);
|
141 |
0
| tabbed.addTab("Data Flow Analysis", dfaPanel);
|
142 |
0
| try {
|
143 |
| |
144 |
0
| if (JTabbedPane.class.getMethod("setMnemonicAt", new Class[]{Integer.TYPE, Integer.TYPE}) != null) {
|
145 |
| |
146 |
0
| tabbed.setMnemonicAt(0, KeyEvent.VK_A);
|
147 |
0
| tabbed.setMnemonicAt(1, KeyEvent.VK_D);
|
148 |
| } |
149 |
| } catch (NoSuchMethodException nsme) {} |
150 |
| |
151 |
0
| JSplitPane containerSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, controlSplitPane, tabbed);
|
152 |
0
| containerSplitPane.setContinuousLayout(true);
|
153 |
| |
154 |
0
| JMenuBar menuBar = createMenuBar();
|
155 |
0
| frame.setJMenuBar(menuBar);
|
156 |
0
| frame.getContentPane().add(containerSplitPane);
|
157 |
0
| frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
158 |
| |
159 |
0
| int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
|
160 |
0
| int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
|
161 |
0
| frame.setSize(screenHeight - (screenHeight/4), screenHeight - (screenHeight/4));
|
162 |
0
| frame.setLocation((screenWidth / 2) - frame.getWidth() / 2, (screenHeight / 2) - frame.getHeight() / 2);
|
163 |
0
| frame.setVisible(true);
|
164 |
0
| frame.pack();
|
165 |
0
| frame.show();
|
166 |
0
| resultsSplitPane.setDividerLocation(resultsSplitPane.getMaximumDividerLocation() - (resultsSplitPane.getMaximumDividerLocation() / 2));
|
167 |
| |
168 |
| } |
169 |
| |
170 |
0
| private JMenuBar createMenuBar() {
|
171 |
0
| JMenuBar menuBar = new JMenuBar();
|
172 |
0
| JMenu menu = new JMenu("JDK");
|
173 |
0
| ButtonGroup group = new ButtonGroup();
|
174 |
0
| jdk14MenuItem = new JRadioButtonMenuItem("JDK 1.4");
|
175 |
0
| jdk14MenuItem.setSelected(true);
|
176 |
0
| group.add(jdk14MenuItem);
|
177 |
0
| menu.add(jdk14MenuItem);
|
178 |
0
| jdk15MenuItem = new JRadioButtonMenuItem("JDK 1.5");
|
179 |
0
| jdk15MenuItem.setSelected(true);
|
180 |
0
| group.add(jdk15MenuItem);
|
181 |
0
| menu.add(jdk15MenuItem);
|
182 |
0
| menuBar.add(menu);
|
183 |
| |
184 |
0
| JMenu actionsMenu = new JMenu("Actions");
|
185 |
0
| JMenuItem copyXMLItem = new JMenuItem("Copy xml to clipboard");
|
186 |
0
| copyXMLItem.addActionListener(new ActionListener() {
|
187 |
0
| public void actionPerformed(ActionEvent e) {
|
188 |
0
| copyXmlToClipboard();
|
189 |
| } |
190 |
| }); |
191 |
0
| actionsMenu.add(copyXMLItem);
|
192 |
0
| JMenuItem createRuleXMLItem = new JMenuItem("Create rule XML");
|
193 |
0
| createRuleXMLItem.addActionListener(new ActionListener() {
|
194 |
0
| public void actionPerformed(ActionEvent e) {
|
195 |
0
| createRuleXML();
|
196 |
| } |
197 |
| }); |
198 |
0
| actionsMenu.add(createRuleXMLItem);
|
199 |
0
| menuBar.add(actionsMenu);
|
200 |
0
| return menuBar;
|
201 |
| } |
202 |
| |
203 |
| |
204 |
| |
205 |
0
| private void createRuleXML() {
|
206 |
0
| JPanel rulenamePanel = new JPanel();
|
207 |
0
| rulenamePanel.setLayout(new FlowLayout());
|
208 |
0
| rulenamePanel.add(new JLabel("Rule name"));
|
209 |
0
| final JTextField rulenameField = new JTextField(30);
|
210 |
0
| rulenamePanel.add(rulenameField);
|
211 |
0
| JPanel rulemsgPanel = new JPanel();
|
212 |
0
| rulemsgPanel.setLayout(new FlowLayout());
|
213 |
0
| rulemsgPanel.add(new JLabel("Rule msg"));
|
214 |
0
| final JTextField rulemsgField = new JTextField(60);
|
215 |
0
| rulemsgPanel.add(rulemsgField);
|
216 |
0
| JPanel ruledescPanel = new JPanel();
|
217 |
0
| ruledescPanel.setLayout(new FlowLayout());
|
218 |
0
| ruledescPanel.add(new JLabel("Rule desc"));
|
219 |
0
| final JTextField ruledescField = new JTextField(60);
|
220 |
0
| ruledescPanel.add(ruledescField);
|
221 |
0
| JPanel ruleXMLPanel = new JPanel();
|
222 |
0
| final JTextArea ruleXMLArea = new JTextArea(30, 50);
|
223 |
0
| ruleXMLPanel.add(ruleXMLArea);
|
224 |
0
| JButton go = new JButton("Create rule XML");
|
225 |
0
| go.addActionListener(new ActionListener() {
|
226 |
0
| public void actionPerformed(ActionEvent e) {
|
227 |
0
| StringBuffer sb = new StringBuffer();
|
228 |
0
| sb.append("<rule name=\"" + rulenameField.getText() + "\"" + PMD.EOL);
|
229 |
0
| sb.append(" message=\"" + rulemsgField.getText() + "\"" + PMD.EOL);
|
230 |
0
| sb.append(" class=\"" + (xpathQueryArea.getText().length() == 0 ? "" : "net.sourceforge.pmd.rules.XPathRule") + "\">" + PMD.EOL);
|
231 |
0
| sb.append(" <description>" + PMD.EOL);
|
232 |
0
| sb.append(" " + ruledescField.getText() + PMD.EOL);
|
233 |
0
| sb.append(" </description>" + PMD.EOL);
|
234 |
0
| if (xpathQueryArea.getText().length()!=0) {
|
235 |
0
| sb.append(" <properties>" + PMD.EOL);
|
236 |
0
| sb.append(" <property name=\"xpath\">" + PMD.EOL);
|
237 |
0
| sb.append(" <value>" + PMD.EOL);
|
238 |
0
| sb.append("<![CDATA[" + PMD.EOL);
|
239 |
0
| sb.append(xpathQueryArea.getText() + PMD.EOL);
|
240 |
0
| sb.append("]]>" + PMD.EOL);
|
241 |
0
| sb.append(" </value>" + PMD.EOL);
|
242 |
0
| sb.append(" </property>" + PMD.EOL);
|
243 |
0
| sb.append(" </properties>" + PMD.EOL);
|
244 |
| } |
245 |
0
| sb.append(" <priority>3</priority>" + PMD.EOL);
|
246 |
0
| sb.append(" <example>" + PMD.EOL);
|
247 |
0
| sb.append("<![CDATA[" + PMD.EOL);
|
248 |
0
| sb.append(codeEditorPane.getText());
|
249 |
0
| sb.append("]]>" + PMD.EOL);
|
250 |
0
| sb.append(" </example>" + PMD.EOL);
|
251 |
0
| sb.append("</rule>" + PMD.EOL);
|
252 |
| |
253 |
0
| ruleXMLArea.setText(sb.toString());
|
254 |
| } |
255 |
| }); |
256 |
| |
257 |
0
| JPanel fieldsPanel = new JPanel();
|
258 |
0
| fieldsPanel.setLayout(new BorderLayout());
|
259 |
0
| fieldsPanel.add(rulenamePanel, BorderLayout.NORTH);
|
260 |
0
| fieldsPanel.add(rulemsgPanel, BorderLayout.CENTER);
|
261 |
0
| fieldsPanel.add(ruledescPanel, BorderLayout.SOUTH);
|
262 |
| |
263 |
0
| JPanel fieldBtnPanel = new JPanel();
|
264 |
0
| fieldBtnPanel.setLayout(new BorderLayout());
|
265 |
0
| fieldBtnPanel.add(fieldsPanel, BorderLayout.NORTH);
|
266 |
0
| fieldBtnPanel.add(go, BorderLayout.SOUTH);
|
267 |
| |
268 |
0
| JPanel outer = new JPanel(new BorderLayout());
|
269 |
0
| outer.add(fieldBtnPanel, BorderLayout.NORTH);
|
270 |
0
| outer.add(ruleXMLPanel, BorderLayout.SOUTH);
|
271 |
| |
272 |
0
| JDialog d = new JDialog(frame);
|
273 |
0
| d.setSize(200,300);
|
274 |
0
| d.getContentPane().add(outer);
|
275 |
0
| int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
|
276 |
0
| int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
|
277 |
0
| d.setLocation((screenWidth / 2) - frame.getWidth() / 2, (screenHeight / 2) - frame.getHeight() / 2);
|
278 |
0
| d.setVisible(true);
|
279 |
0
| d.pack();
|
280 |
0
| d.show();
|
281 |
| } |
282 |
| |
283 |
0
| private JComponent createASTPanel() {
|
284 |
0
| astArea.setRows(10);
|
285 |
0
| astArea.setColumns(20);
|
286 |
0
| JScrollPane astScrollPane = new JScrollPane(astArea);
|
287 |
0
| return astScrollPane;
|
288 |
| } |
289 |
| |
290 |
0
| private JComponent createXPathResultPanel() {
|
291 |
0
| xpathResults.addElement("No results yet");
|
292 |
0
| xpathResultList.setBorder(BorderFactory.createLineBorder(Color.black));
|
293 |
0
| xpathResultList.setFixedCellWidth(300);
|
294 |
0
| JScrollPane scrollPane = new JScrollPane();
|
295 |
0
| scrollPane.getViewport().setView(xpathResultList);
|
296 |
0
| return scrollPane;
|
297 |
| } |
298 |
| |
299 |
0
| private JPanel createXPathQueryPanel() {
|
300 |
0
| JPanel p = new JPanel();
|
301 |
0
| p.setLayout(new BorderLayout());
|
302 |
0
| xpathQueryArea.setBorder(BorderFactory.createLineBorder(Color.black));
|
303 |
0
| JScrollPane scrollPane = new JScrollPane(xpathQueryArea);
|
304 |
0
| scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
|
305 |
0
| scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
|
306 |
0
| final JButton b = createGoButton();
|
307 |
| |
308 |
0
| p.add(new JLabel("XPath Query (if any)"), BorderLayout.NORTH);
|
309 |
0
| p.add(scrollPane, BorderLayout.CENTER);
|
310 |
0
| p.add(b, BorderLayout.SOUTH);
|
311 |
| |
312 |
0
| return p;
|
313 |
| } |
314 |
| |
315 |
0
| private JButton createGoButton() {
|
316 |
0
| JButton b = new JButton("Go");
|
317 |
0
| b.setMnemonic('g');
|
318 |
0
| b.addActionListener(new ShowListener());
|
319 |
0
| b.addActionListener(codeEditorPane);
|
320 |
0
| b.addActionListener(new XPathListener());
|
321 |
0
| b.addActionListener(new DFAListener());
|
322 |
0
| return b;
|
323 |
| } |
324 |
| |
325 |
0
| public static void main(String[] args) {
|
326 |
0
| new Designer();
|
327 |
| } |
328 |
| |
329 |
0
| private final void copyXmlToClipboard() {
|
330 |
0
| if (codeEditorPane.getText()!=null && codeEditorPane.getText().trim().length()>0) {
|
331 |
0
| ASTCompilationUnit cu = createParser().CompilationUnit();
|
332 |
0
| String xml = "";
|
333 |
0
| if (cu!=null) {
|
334 |
0
| try {
|
335 |
0
| xml = getXmlString( cu );
|
336 |
| } catch (IOException e) { |
337 |
0
| e.printStackTrace();
|
338 |
0
| xml = "Error trying to construct XML representation";
|
339 |
| } |
340 |
| } |
341 |
0
| Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(xml), this);
|
342 |
| } |
343 |
| } |
344 |
| |
345 |
| |
346 |
| |
347 |
| |
348 |
| |
349 |
| |
350 |
| |
351 |
| |
352 |
0
| private String getXmlString(SimpleNode node) throws IOException {
|
353 |
0
| StringWriter writer = new StringWriter();
|
354 |
0
| XMLSerializer xmlSerializer = new XMLSerializer(writer, new OutputFormat("XML", "UTF-8", true));
|
355 |
0
| xmlSerializer.asDOMSerializer();
|
356 |
0
| xmlSerializer.serialize(node.asXml());
|
357 |
0
| return writer.toString();
|
358 |
| } |
359 |
| |
360 |
0
| public void lostOwnership(Clipboard clipboard, Transferable contents) {}
|
361 |
| } |