-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3447 from oowekyala:issue3446-apex-file-name
[core] Add access to file name in XPath rules #3447
- Loading branch information
Showing
26 changed files
with
357 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 0 additions & 33 deletions
33
pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/ast/ApexParserXPathTest.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/rule/ApexXPathRuleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html | ||
*/ | ||
|
||
package net.sourceforge.pmd.lang.apex.rule; | ||
|
||
import static net.sourceforge.pmd.lang.ast.test.TestUtilsKt.assertSize; | ||
|
||
import org.junit.Test; | ||
|
||
import net.sourceforge.pmd.Report; | ||
import net.sourceforge.pmd.lang.LanguageRegistry; | ||
import net.sourceforge.pmd.lang.apex.ApexLanguageModule; | ||
import net.sourceforge.pmd.lang.apex.ast.ApexParserTestBase; | ||
import net.sourceforge.pmd.lang.rule.XPathRule; | ||
import net.sourceforge.pmd.lang.rule.xpath.XPathVersion; | ||
|
||
/** | ||
* @author daniels | ||
*/ | ||
public class ApexXPathRuleTest extends ApexParserTestBase { | ||
|
||
private XPathRule makeXPath(String expression) { | ||
XPathRule rule = new XPathRule(XPathVersion.XPATH_2_0, expression); | ||
rule.setLanguage(LanguageRegistry.getLanguage(ApexLanguageModule.NAME)); | ||
rule.setMessage("XPath Rule Failed"); | ||
return rule; | ||
} | ||
|
||
|
||
@Test | ||
public void testFileNameInXpath() { | ||
Report report = apex.executeRule(makeXPath("/UserClass[pmd:fileName() = 'Foo.cls']"), | ||
"class Foo {}", | ||
"src/Foo.cls"); | ||
|
||
assertSize(report, 1); | ||
} | ||
|
||
@Test | ||
public void testBooleanExpressions() { | ||
Report report = apex.executeRuleOnResource(makeXPath("//BooleanExpression[@Operator='&&']"), | ||
"BooleanExpressions.cls"); | ||
assertSize(report, 1); | ||
} | ||
|
||
|
||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/internal/FileNameXPathFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html | ||
*/ | ||
|
||
package net.sourceforge.pmd.lang.ast.xpath.internal; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import org.jaxen.Context; | ||
import org.jaxen.FunctionCallException; | ||
import org.jaxen.SimpleFunctionContext; | ||
import org.jaxen.XPathFunctionContext; | ||
|
||
import net.sourceforge.pmd.lang.ast.Node; | ||
import net.sourceforge.pmd.util.DataMap; | ||
import net.sourceforge.pmd.util.DataMap.SimpleDataKey; | ||
|
||
/** | ||
* A function that returns the current file name. | ||
* | ||
* @author Clément Fournier | ||
*/ | ||
public class FileNameXPathFunction implements org.jaxen.Function { | ||
|
||
/** | ||
* The name of the file, including its extension. This | ||
* excludes any segments for containing directories. | ||
*/ | ||
public static final SimpleDataKey<String> FILE_NAME_KEY = DataMap.simpleDataKey("pmd.fileName"); | ||
|
||
public static void registerSelfInSimpleContext() { | ||
((SimpleFunctionContext) XPathFunctionContext.getInstance()).registerFunction(null, "fileName", | ||
new FileNameXPathFunction("fileName")); | ||
} | ||
|
||
private final String name; | ||
|
||
public FileNameXPathFunction(String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public Object call(Context context, List args) throws FunctionCallException { | ||
if (!args.isEmpty()) { | ||
throw new IllegalArgumentException(name + " function takes no arguments."); | ||
} | ||
Node n = (Node) context.getNodeSet().get(0); | ||
|
||
return getFileName(n); | ||
} | ||
|
||
public static String getFileName(Node n) { | ||
// todo pmd7: replace with Node.getRoot() | ||
while (n.getParent() != null) { | ||
n = n.getParent(); | ||
} | ||
Objects.requireNonNull(n, "No root node in tree?"); | ||
|
||
String fileName = n.getUserMap().get(FILE_NAME_KEY); | ||
return Objects.requireNonNull(fileName, "File name was not set"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.