460cef86f1
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@318 51baf565-9d33-0410-a72c-fc3788e3496d
198 lines
7.0 KiB
Plaintext
198 lines
7.0 KiB
Plaintext
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;
|
|
|
|
|
|
/**
|
|
* <p>Title: JBuilder OpenTool for PMD</p>
|
|
* <p>Description: Provides an environemnACTION_PMDCheckt for using the PMD aplication from within JBuilder</p>
|
|
* <p>Copyright: Copyright (c) 2002</p>
|
|
* <p>Company: InfoEther</p>
|
|
* @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<results.length; i++)
|
|
Browser.getActiveBrowser().getMessageView().addMessage(msgCat, results[i]);//add the result message
|
|
}
|
|
}
|
|
catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
};
|
|
|
|
public class PMDMessage extends Message {
|
|
|
|
final LineMark MARK = new HighlightMark();
|
|
JavaFileNode javaNode;
|
|
int line=0;
|
|
int column=0;
|
|
|
|
public PMDMessage(String msg, int line, JavaFileNode node) {
|
|
super(msg);
|
|
this.line = line;
|
|
|
|
}
|
|
public void selectAction(Browser browser) {
|
|
displayResult(browser, false);
|
|
}
|
|
|
|
public void messageAction(Browser browser) {
|
|
displayResult(browser, true);
|
|
}
|
|
|
|
private void displayResult(Browser browser, boolean requestFocus) {
|
|
try {
|
|
if (requestFocus || browser.isOpenNode(javaNode)) {
|
|
browser.setActiveNode(javaNode, requestFocus);
|
|
TextNodeViewer viewer = (TextNodeViewer)browser.getViewerOfType(javaNode, TextNodeViewer.class);
|
|
browser.setActiveViewer(javaNode, viewer, requestFocus);
|
|
EditorPane editor = viewer.getEditor();
|
|
editor.gotoPosition(line, column, false, EditorPane.CENTER_IF_NEAR_EDGE);
|
|
if (requestFocus)
|
|
editor.requestFocus();
|
|
else
|
|
editor.setTemporaryMark(line, MARK);
|
|
}
|
|
}
|
|
catch (Exception ex) {
|
|
ex.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
public static class HighlightMark extends LineMark {
|
|
static Style highlightStyle;
|
|
static {
|
|
StyleContext context = EditorManager.getStyleContext();
|
|
highlightStyle = context.addStyle("line_highlight", null);
|
|
highlightStyle.addAttribute(MasterStyleContext.DISPLAY_NAME, "Line highlight");
|
|
StyleConstants.setBackground(highlightStyle, Color.yellow);
|
|
StyleConstants.setForeground(highlightStyle, Color.black);
|
|
}
|
|
|
|
public HighlightMark() {
|
|
super(highlightStyle);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Main method for testing purposes
|
|
* @param args standard arguments
|
|
*/
|
|
public static void main(String[] args) {
|
|
String[] ret = PMDOpenTool.instanceCheck("package abc; \npublic class foo {\npublic void bar() {int i;}\n}");
|
|
System.out.println("PMD: " + ret);
|
|
}
|
|
|
|
} |