1 package net.sourceforge.pmd.lang.java.xpath;
2
3 import java.util.List;
4
5 import net.sourceforge.pmd.lang.ast.AbstractNode;
6 import net.sourceforge.pmd.lang.ast.Node;
7 import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
8 import net.sourceforge.pmd.lang.java.ast.Comment;
9
10 import org.jaxen.Context;
11 import org.jaxen.Function;
12 import org.jaxen.FunctionCallException;
13 import org.jaxen.SimpleFunctionContext;
14 import org.jaxen.XPathFunctionContext;
15
16
17
18
19
20
21
22 public class GetCommentOnFunction implements Function {
23
24 public static void registerSelfInSimpleContext() {
25
26 ((SimpleFunctionContext) XPathFunctionContext.getInstance()).registerFunction(null, "getCommentOn", new GetCommentOnFunction());
27 }
28
29 public Object call(Context context, List args) throws FunctionCallException {
30 if (!args.isEmpty()) {
31 return Boolean.FALSE;
32 }
33 Node n = (Node) context.getNodeSet().get(0);
34 if (n instanceof AbstractNode) {
35 int codeBeginLine = ((AbstractNode) n).getBeginLine();
36 int codeEndLine = ((AbstractNode) n).getEndLine();
37
38 List<Comment> commentList = ((AbstractNode)n).getFirstParentOfType(ASTCompilationUnit.class).getComments();
39 for (Comment comment : commentList) {
40 if (comment.getBeginLine() == codeBeginLine || comment.getEndLine() == codeEndLine) {
41 return comment.getImage();
42 }
43 }
44 }
45 return Boolean.FALSE;
46 }
47 }