在XPath中访问注释和/或琐事文件
我在使用XPath时遇到了一些问题。(稍后我想将SonarQube上的表达式添加到xpath模式的规则中,这样我就可以在那里创建自己的规则。但SonarQube还不是必需的) 我想创建一个规则,如果分析了一个项目,并且在我的java项目(例如myXPathTry.java)的注释(例如“ERROR”)中写了一些特殊的东西,就会抛出异常 我正在用这个测试我所有的表情。只需要下载并执行它 用法很简单:在左边添加你的代码,点击右边框下的“解析源代码”。然后在底部的框中添加xpath规则,最后单击“评估xpath”。应标记结果。如果单击右框上方的XML按钮,您还可以查看源代码的自动创建的XML 我的问题是:所有评论都保存在“不可访问”的琐事路径中(如AST中所示)。我可以访问所有其他内容,并检查这些值和参数,但不能检查这些琐事。(下面添加了JavaCode、AST(抽象语法树)和XML。) 你知道我怎样才能得到评论吗? 谢谢你的帮助!:) Java代码:在XPath中访问注释和/或琐事文件,xpath,abstract-syntax-tree,sonarqube,Xpath,Abstract Syntax Tree,Sonarqube,我在使用XPath时遇到了一些问题。(稍后我想将SonarQube上的表达式添加到xpath模式的规则中,这样我就可以在那里创建自己的规则。但SonarQube还不是必需的) 我想创建一个规则,如果分析了一个项目,并且在我的java项目(例如myXPathTry.java)的注释(例如“ERROR”)中写了一些特殊的东西,就会抛出异常 我正在用这个测试我所有的表情。只需要下载并执行它 用法很简单:在左边添加你的代码,点击右边框下的“解析源代码”。然后在底部的框中添加xpath规则,最后单击“评估
public class MyXPathTry{
public static void main(String[] args){
System.out.println("Just to have some code");
//This is a comment
/* This is a second comment */
/* This is a second comment with ERROR*/
}
}
在AST中,单击工具箱左侧的任何注释后,蓝色线(RWING,如果图像不可见)被选中。默认情况下未打开向下路径。我自己打开了RWING和TOKEN子文件夹,只是告诉你我对琐事文件的意思)
工具包自动创建的XML:
.这是不可能的。关于您的信息,使用XPath编写自定义规则的能力将被放弃,转而使用Java编写。这是一个与
sonarqube
相关的问题,为什么要删除标记?因为我不确定它以何种方式影响sonarqube。该工具包介绍了SonarQube,但您可以不使用它。如果它在工具箱中工作,那么稍后它也将在SonarQube(V.3.7)中工作。到目前为止,SonarQube并没有影响这个xpath表达式,我想。但是我添加了SonarQube标记,在您展示给我们的XML中,我没有看到任何类似于这些注释的引用。如果它不在那里,XPath就无法访问它。@keshlam我认为问题更多的是“如何将它引入”。我已经看了SSLR工具包的源代码一段时间了,特别是,但它不是决定性的。毕竟,注释节点在AST中,由于某些原因,它们没有输出。那么,我将与该工具的作者进行讨论。谢谢您的回答!但我必须说:我没有用Java编写自己的xpath规则的经验。输入评论的问题对我来说也是一样的。对我来说,最简单的方法是:忽略注释,就像Sonar中XPath所做的那样:D谢谢你的帮助!
public class MyXPathTry{
public static void main(String[] args){
System.out.println("Just to have some code");
//This is a comment
/* This is a second comment */
/* This is a second comment with ERROR*/
}
}
Toolkit Auto-Created XML:
<compilationUnit tokenValue="public" tokenLine="1" tokenColumn="0">
<typeDeclaration tokenValue="public" tokenLine="1" tokenColumn="0">
<modifier tokenValue="public" tokenLine="1" tokenColumn="0">
<PUBLIC tokenValue="public" tokenLine="1" tokenColumn="0">
<TOKEN tokenValue="public" tokenLine="1" tokenColumn="0"/>
</PUBLIC>
</modifier>
<classDeclaration tokenValue="class" tokenLine="1" tokenColumn="7">
<CLASS tokenValue="class" tokenLine="1" tokenColumn="7">
<TOKEN tokenValue="class" tokenLine="1" tokenColumn="7"/>
</CLASS>
<IDENTIFIER tokenValue="MyXPathTry" tokenLine="1" tokenColumn="13">
<TOKEN tokenValue="MyXPathTry" tokenLine="1" tokenColumn="13"/>
</IDENTIFIER>
<classBody tokenValue="{" tokenLine="1" tokenColumn="23">
<LWING tokenValue="{" tokenLine="1" tokenColumn="23">
<TOKEN tokenValue="{" tokenLine="1" tokenColumn="23"/>
</LWING>
<classBodyDeclaration tokenValue="public" tokenLine="2" tokenColumn="4">
<modifier tokenValue="public" tokenLine="2" tokenColumn="4">
<PUBLIC tokenValue="public" tokenLine="2" tokenColumn="4">
<TOKEN tokenValue="public" tokenLine="2" tokenColumn="4"/>
</PUBLIC>
</modifier>
<modifier tokenValue="static" tokenLine="2" tokenColumn="11">
<STATIC tokenValue="static" tokenLine="2" tokenColumn="11">
<TOKEN tokenValue="static" tokenLine="2" tokenColumn="11"/>
</STATIC>
</modifier>
<memberDecl tokenValue="void" tokenLine="2" tokenColumn="18">
<VOID tokenValue="void" tokenLine="2" tokenColumn="18">
<TOKEN tokenValue="void" tokenLine="2" tokenColumn="18"/>
</VOID>
<IDENTIFIER tokenValue="main" tokenLine="2" tokenColumn="23">
<TOKEN tokenValue="main" tokenLine="2" tokenColumn="23"/>
</IDENTIFIER>
<voidMethodDeclaratorRest tokenValue="(" tokenLine="2" tokenColumn="27">
<formalParameters tokenValue="(" tokenLine="2" tokenColumn="27">
<LPAR tokenValue="(" tokenLine="2" tokenColumn="27">
<TOKEN tokenValue="(" tokenLine="2" tokenColumn="27"/>
</LPAR>
<formalParameterDecls tokenValue="String" tokenLine="2" tokenColumn="28">
<type tokenValue="String" tokenLine="2" tokenColumn="28">
<classType tokenValue="String" tokenLine="2" tokenColumn="28">
<IDENTIFIER tokenValue="String" tokenLine="2" tokenColumn="28">
<TOKEN tokenValue="String" tokenLine="2" tokenColumn="28"/>
</IDENTIFIER>
</classType>
<dim tokenValue="[" tokenLine="2" tokenColumn="34">
<LBRK tokenValue="[" tokenLine="2" tokenColumn="34">
<TOKEN tokenValue="[" tokenLine="2" tokenColumn="34"/>
</LBRK>
<RBRK tokenValue="]" tokenLine="2" tokenColumn="35">
<TOKEN tokenValue="]" tokenLine="2" tokenColumn="35"/>
</RBRK>
</dim>
</type>
<formalParametersDeclsRest tokenValue="args" tokenLine="2" tokenColumn="37">
<variableDeclaratorId tokenValue="args" tokenLine="2" tokenColumn="37">
<IDENTIFIER tokenValue="args" tokenLine="2" tokenColumn="37">
<TOKEN tokenValue="args" tokenLine="2" tokenColumn="37"/>
</IDENTIFIER>
</variableDeclaratorId>
</formalParametersDeclsRest>
</formalParameterDecls>
<RPAR tokenValue=")" tokenLine="2" tokenColumn="41">
<TOKEN tokenValue=")" tokenLine="2" tokenColumn="41"/>
</RPAR>
</formalParameters>
<methodBody tokenValue="{" tokenLine="2" tokenColumn="42">
<block tokenValue="{" tokenLine="2" tokenColumn="42">
<LWING tokenValue="{" tokenLine="2" tokenColumn="42">
<TOKEN tokenValue="{" tokenLine="2" tokenColumn="42"/>
</LWING>
<blockStatements tokenValue="System" tokenLine="3" tokenColumn="1">
<blockStatement tokenValue="System" tokenLine="3" tokenColumn="1">
<statement tokenValue="System" tokenLine="3" tokenColumn="1">
<expressionStatement tokenValue="System" tokenLine="3" tokenColumn="1">
<statementExpression tokenValue="System" tokenLine="3" tokenColumn="1">
<expression tokenValue="System" tokenLine="3" tokenColumn="1">
<primary tokenValue="System" tokenLine="3" tokenColumn="1">
<qualifiedIdentifier tokenValue="System" tokenLine="3" tokenColumn="1">
<IDENTIFIER tokenValue="System" tokenLine="3" tokenColumn="1">
<TOKEN tokenValue="System" tokenLine="3" tokenColumn="1"/>
</IDENTIFIER>
<DOT tokenValue="." tokenLine="3" tokenColumn="7">
<TOKEN tokenValue="." tokenLine="3" tokenColumn="7"/>
</DOT>
<IDENTIFIER tokenValue="out" tokenLine="3" tokenColumn="8">
<TOKEN tokenValue="out" tokenLine="3" tokenColumn="8"/>
</IDENTIFIER>
<DOT tokenValue="." tokenLine="3" tokenColumn="11">
<TOKEN tokenValue="." tokenLine="3" tokenColumn="11"/>
</DOT>
<IDENTIFIER tokenValue="println" tokenLine="3" tokenColumn="12">
<TOKEN tokenValue="println" tokenLine="3" tokenColumn="12"/>
</IDENTIFIER>
</qualifiedIdentifier>
<identifierSuffix tokenValue="(" tokenLine="3" tokenColumn="19">
<arguments tokenValue="(" tokenLine="3" tokenColumn="19">
<LPAR tokenValue="(" tokenLine="3" tokenColumn="19">
<TOKEN tokenValue="(" tokenLine="3" tokenColumn="19"/>
</LPAR>
<expression tokenValue=""Just to have some code"" tokenLine="3" tokenColumn="20">
<primary tokenValue=""Just to have some code"" tokenLine="3" tokenColumn="20">
<literal tokenValue=""Just to have some code"" tokenLine="3" tokenColumn="20">
<LITERAL tokenValue=""Just to have some code"" tokenLine="3" tokenColumn="20">
<TOKEN tokenValue=""Just to have some code"" tokenLine="3" tokenColumn="20"/>
</LITERAL>
</literal>
</primary>
</expression>
<RPAR tokenValue=")" tokenLine="3" tokenColumn="44">
<TOKEN tokenValue=")" tokenLine="3" tokenColumn="44"/>
</RPAR>
</arguments>
</identifierSuffix>
</primary>
</expression>
</statementExpression>
<SEMI tokenValue=";" tokenLine="3" tokenColumn="45">
<TOKEN tokenValue=";" tokenLine="3" tokenColumn="45"/>
</SEMI>
</expressionStatement>
</statement>
</blockStatement>
</blockStatements>
<RWING tokenValue="}" tokenLine="7" tokenColumn="4">
<TOKEN tokenValue="}" tokenLine="7" tokenColumn="4"/>
</RWING>
</block>
</methodBody>
</voidMethodDeclaratorRest>
</memberDecl>
</classBodyDeclaration>
<RWING tokenValue="}" tokenLine="8" tokenColumn="0">
<TOKEN tokenValue="}" tokenLine="8" tokenColumn="0"/>
</RWING>
</classBody>
</classDeclaration>
</typeDeclaration>
<EOF tokenValue="" tokenLine="8" tokenColumn="1"/>
</compilationUnit>