package pmdopentool; import java.io.*; import java.util.*; import javax.swing.text.*; import com.borland.jbuilder.*; import com.borland.jbuilder.node.*; import com.borland.primetime.*; import com.borland.primetime.ide.*; import com.borland.primetime.node.*; import com.borland.primetime.viewer.*; import net.sourceforge.pmd.*; import net.sourceforge.pmd.reports.*; import com.borland.primetime.actions.DelegateHandler; import com.borland.primetime.actions.DelegateAction; import javax.swing.Action; import com.borland.primetime.editor.LineMark; import com.borland.primetime.editor.EditorPane; import com.borland.primetime.editor.EditorManager; import com.borland.primetime.editor.MasterStyleContext; import java.awt.Color; /** *
Title: JBuilder OpenTool for PMD
*Description: Provides an environemnACTION_PMDCheckt for using the PMD aplication from within JBuilder
*Copyright: Copyright (c) 2002
*Company: InfoEther
* @author David Craine * @version 1.0 */ public class PMDOpenTool { static MessageCategory msgCat = new MessageCategory("PMD Results"); public PMDOpenTool() { int i; int a; } public static void initOpenTool(byte majorVersion, byte minorVersion) { if (majorVersion == PrimeTime.CURRENT_MAJOR_VERSION) { JBuilderMenu.GROUP_Tools.add(ACTION_PMDCheck); registerWithContentManager(); } } /** * Registers an "PMD Checker" action with the ContentManager (Tabs) * The action will not be visible if multiple nodes are selected */ private static void registerWithContentManager() { ContextActionProvider cap = new ContextActionProvider() { public Action getContextAction(Browser browser, Node[] nodes) { return ACTION_PMDCheck; } }; ContentManager.registerContextActionProvider(cap); } public static Report instanceCheck(String text) { PMD pmd = new PMD(); ReportFactory rf = new ReportFactory(); RuleContext ctx = new RuleContext(); RuleSetFactory ruleSetFactory = new RuleSetFactory(); RuleSet rules = ruleSetFactory.createRuleSet(pmd.getClass().getClassLoader().getResourceAsStream("rulesets/unusedcode.xml")); ctx.setReport(rf.createReport("xml")); ctx.setSourceCodeFilename("this"); try { // TODO switch to use StringReader once PMD 0.4 gets released pmd.processFile(new StringBufferInputStream(text), rules, ctx); return ctx.getReport(); /*if (ctx.getReport().isEmpty()) return null; //check to see if any violations have ocurred RuleV[] msg = new String[ctx.getReport().size()]; int index = 0; for (Iterator i = ctx.getReport().iterator(); i.hasNext();) { RuleViolation rv = (RuleViolation)i.next(); msg[index++] = rv.getDescription() + " at line " + rv.getLine(); } return msg;*/ } catch (FileNotFoundException fnfe) { fnfe.printStackTrace(); } return null; } public static /*final*/ BrowserAction ACTION_PMDCheck = // A new action with short menu string, mnemonic, and long menu string new BrowserAction("PMD Checker", 'P', "Displays PMD statistics about a Java File") { // The function called when the menu is selected public void actionPerformed(Browser browser) { Node node = Browser.getActiveBrowser().getActiveNode(); if (node instanceof JavaFileNode) { TextNodeViewer viewer = (TextNodeViewer)Browser.getActiveBrowser().getViewerOfType(node, TextNodeViewer.class); if (viewer != null) { Document doc = viewer.getEditor().getDocument(); try { String[] results = instanceCheck(doc.getText(0, doc.getLength())); Browser.getActiveBrowser().getMessageView().clearMessages(msgCat);//clear the message window if (results == null) { Message msg = new Message("sdsd"); Browser.getActiveBrowser().getMessageView().addMessage(msgCat, "No violations detected."); } else { for (int i= 0; i