我需要一种在 Sonar 上使用的验证,它应该查看 XHTML 页面中的 JSF 元素,以验证输入元素是否包含 ID 属性。(例子: )
最初我正在尝试使用 PMD 来做到这一点。正如我所见,PMD 可以解释 JSP 页面。这是正确的吗?实现它的方法是实现基于 AbstractJspRule 的自定义规则。
到目前为止,PMD 试图将 XHTML 页面解析为 Java 代码的问题。它应该解释为 JSP 页面。
我的代码:
类:
public class TestXhtmlRule extends AbstractJspRule {
@Override
public Object visit(final ASTElement node, final Object data) {
// ...
}
@Override
public Object visit(final ASTAttribute node, final Object data) {
// ...
}
}
xml:
<ruleset name="Basic JSF rules"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd"
xsi:noNamespaceSchemaLocation="http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
<include-pattern>**/*.xhtml</include-pattern>
<rule name="idsJSF" language="jsp"
message="..."
class="pmd.rule.TestXhtmlRule">
<description>
...
</description>
<priority>3</priority>
<example>
<![CDATA[
<h:inputText id="itNome" label="Nome" value="#{pessoaBean.nome}" />;
]]>
</example>
</rule>
</ruleset>
日志中的错误:
[pmd] net.sourceforge.pmd.lang.java.ast.ParseException: Encountered " "<" "< "" at line 1, column 1.
Was expecting one of:
<EOF>
"abstract" ...
"class" ...
"final" ...
"import" ...
"interface" ...
"native" ...
"private" ...
"protected" ...
"public" ...
"static" ...
"synchronized" ...
"transient" ...
"volatile" ...
"strictfp" ...
<IDENTIFIER> ...
";" ...
"@" ...
"\u001a" ...
"~[]" ...
"@" ...
"@" ...
"@" ...
at net.sourceforge.pmd.lang.java.ast.JavaParser.generateParseException(JavaParser.java:9867)
at net.sourceforge.pmd.lang.java.ast.JavaParser.jj_consume_token(JavaParser.java:9751)
at net.sourceforge.pmd.lang.java.ast.JavaParser.CompilationUnit(JavaParser.java:190)
at net.sourceforge.pmd.lang.java.AbstractJavaParser.parse(AbstractJavaParser.java:56)
at net.sourceforge.pmd.SourceCodeProcessor.parse(SourceCodeProcessor.java:90)
at net.sourceforge.pmd.SourceCodeProcessor.processSource(SourceCodeProcessor.java:136)
at net.sourceforge.pmd.SourceCodeProcessor.processSourceCode(SourceCodeProcessor.java:75)
at net.sourceforge.pmd.SourceCodeProcessor.processSourceCode(SourceCodeProcessor.java:42)
at net.sourceforge.pmd.processor.PmdRunnable.call(PmdRunnable.java:75)
at net.sourceforge.pmd.processor.PmdRunnable.call(PmdRunnable.java:22)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
我怎样才能让它工作?
使用 checkstyle 而不是 PMD 更容易验证此规则?
谢谢。