forked from phoedos/pmd
New XPath getCommentOn function as provided by Andy Throgmorton
https://sourceforge.net/tracker/?func=detail&aid=2833791&group_id=56262&atid=479923 git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@7306 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
@ -17,6 +17,7 @@ import net.sourceforge.pmd.lang.java.dfa.DataFlowFacade;
|
||||
import net.sourceforge.pmd.lang.java.rule.JavaRuleViolationFactory;
|
||||
import net.sourceforge.pmd.lang.java.symboltable.SymbolFacade;
|
||||
import net.sourceforge.pmd.lang.java.typeresolution.TypeResolutionFacade;
|
||||
import net.sourceforge.pmd.lang.java.xpath.GetCommentOnFunction;
|
||||
import net.sourceforge.pmd.lang.java.xpath.JavaFunctions;
|
||||
import net.sourceforge.pmd.lang.java.xpath.TypeOfFunction;
|
||||
import net.sourceforge.pmd.lang.rule.RuleViolationFactory;
|
||||
@ -39,6 +40,7 @@ public abstract class AbstractJavaHandler extends AbstractLanguageVersionHandler
|
||||
return new AbstractASTXPathHandler() {
|
||||
public void initialize() {
|
||||
TypeOfFunction.registerSelfInSimpleContext();
|
||||
GetCommentOnFunction.registerSelfInSimpleContext();
|
||||
}
|
||||
|
||||
public void initialize(IndependentContext context) {
|
||||
|
@ -0,0 +1,45 @@
|
||||
package net.sourceforge.pmd.lang.java.xpath;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import net.sourceforge.pmd.lang.ast.AbstractNode;
|
||||
import net.sourceforge.pmd.lang.ast.Node;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
|
||||
import net.sourceforge.pmd.lang.java.ast.Comment;
|
||||
|
||||
import org.jaxen.Context;
|
||||
import org.jaxen.Function;
|
||||
import org.jaxen.FunctionCallException;
|
||||
import org.jaxen.SimpleFunctionContext;
|
||||
import org.jaxen.XPathFunctionContext;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Andy Throgmorton
|
||||
*/
|
||||
public class GetCommentOnFunction implements Function {
|
||||
|
||||
public static void registerSelfInSimpleContext() {
|
||||
// see http://jaxen.org/extensions.html
|
||||
((SimpleFunctionContext) XPathFunctionContext.getInstance()).registerFunction(null, "getCommentOn", new GetCommentOnFunction());
|
||||
}
|
||||
|
||||
public Object call(Context context, List args) throws FunctionCallException {
|
||||
if (!args.isEmpty()) {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
Node n = (Node) context.getNodeSet().get(0);
|
||||
if (n instanceof AbstractNode) {
|
||||
int codeBeginLine = ((AbstractNode) n).getBeginLine();
|
||||
int codeEndLine = ((AbstractNode) n).getEndLine();
|
||||
|
||||
List<Comment> commentList = ((AbstractNode)n).getFirstParentOfType(ASTCompilationUnit.class).getComments();
|
||||
for (Comment comment : commentList) {
|
||||
if (comment.getBeginLine() == codeBeginLine || comment.getEndLine() == codeEndLine) {
|
||||
return comment.getImage();
|
||||
}
|
||||
}
|
||||
}
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
}
|
@ -8,12 +8,13 @@ import net.sourceforge.pmd.lang.ast.xpath.saxon.ElementNode;
|
||||
* Exposes all Java Language specific functions for Saxon use.
|
||||
*/
|
||||
public class JavaFunctions {
|
||||
|
||||
public static boolean typeof(XPathContext context, String nodeTypeName, String fullTypeName) {
|
||||
return typeof(context, nodeTypeName, fullTypeName, null);
|
||||
return typeof(context, nodeTypeName, fullTypeName, null);
|
||||
}
|
||||
|
||||
public static boolean typeof(XPathContext context, String nodeTypeName, String fullTypeName, String shortTypeName) {
|
||||
return TypeOfFunction.typeof((Node) ((ElementNode) context.getContextItem()).getUnderlyingNode(), nodeTypeName,
|
||||
fullTypeName, shortTypeName);
|
||||
return TypeOfFunction.typeof((Node) ((ElementNode) context.getContextItem()).getUnderlyingNode(), nodeTypeName,
|
||||
fullTypeName, shortTypeName);
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,8 @@ import net.sf.saxon.sxpath.IndependentContext;
|
||||
import net.sourceforge.pmd.lang.Language;
|
||||
import net.sourceforge.pmd.lang.LanguageVersion;
|
||||
import net.sourceforge.pmd.lang.LanguageVersionHandler;
|
||||
import net.sourceforge.pmd.lang.java.xpath.GetCommentOnFunction;
|
||||
import net.sourceforge.pmd.lang.java.xpath.TypeOfFunction;
|
||||
|
||||
/**
|
||||
* This class serves as the means to perform XPath related static initialization.
|
||||
@ -27,34 +29,36 @@ public class Initializer {
|
||||
* Perform all initialization.
|
||||
*/
|
||||
public static void initialize(IndependentContext context) {
|
||||
context.declareNamespace("pmd", "java:" + PMDFunctions.class.getName());
|
||||
for (Language language : Language.values()) {
|
||||
for (LanguageVersion languageVersion : language.getVersions()) {
|
||||
LanguageVersionHandler languageVersionHandler = languageVersion.getLanguageVersionHandler();
|
||||
if (languageVersionHandler != null) {
|
||||
languageVersionHandler.getXPathHandler().initialize(context);
|
||||
context.declareNamespace("pmd", "java:" + PMDFunctions.class.getName());
|
||||
for (Language language : Language.values()) {
|
||||
for (LanguageVersion languageVersion : language.getVersions()) {
|
||||
LanguageVersionHandler languageVersionHandler = languageVersion.getLanguageVersionHandler();
|
||||
if (languageVersionHandler != null) {
|
||||
languageVersionHandler.getXPathHandler().initialize(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static {
|
||||
initializeGlobal();
|
||||
initializeLanguages();
|
||||
initializeGlobal();
|
||||
initializeLanguages();
|
||||
}
|
||||
|
||||
private static void initializeGlobal() {
|
||||
MatchesFunction.registerSelfInSimpleContext();
|
||||
GetCommentOnFunction.registerSelfInSimpleContext();
|
||||
MatchesFunction.registerSelfInSimpleContext();
|
||||
TypeOfFunction.registerSelfInSimpleContext();
|
||||
}
|
||||
|
||||
private static void initializeLanguages() {
|
||||
for (Language language : Language.values()) {
|
||||
for (LanguageVersion languageVersion : language.getVersions()) {
|
||||
LanguageVersionHandler languageVersionHandler = languageVersion.getLanguageVersionHandler();
|
||||
if (languageVersionHandler != null) {
|
||||
languageVersionHandler.getXPathHandler().initialize();
|
||||
for (Language language : Language.values()) {
|
||||
for (LanguageVersion languageVersion : language.getVersions()) {
|
||||
LanguageVersionHandler languageVersionHandler = languageVersion.getLanguageVersionHandler();
|
||||
if (languageVersionHandler != null) {
|
||||
languageVersionHandler.getXPathHandler().initialize();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user