Skip to content

Commit

Permalink
Refactor the JXPathFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
kyakdan committed Oct 19, 2022
1 parent 510a5fe commit ea34611
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
16 changes: 6 additions & 10 deletions src/main/java/org/apache/commons/jxpath/ri/JXPathFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,18 @@

/**
* A filter to be used by JXPath, to evaluate the xpath string values to impose any restrictions.
* This class implements specific filter interfaces, and implements methods in those.
* This class implements specific filter interfaces, and implements methods in those.
* For instance, it JXPathClassFilter interface, which is used to check if any restricted java classes are passed via xpath
* JXPath uses this filter instance when an extension function instance is created.
*/
public class JXPathFilter implements JXPathClassFilter {
private Set<String> allowedClassesList = null;
private final Set<String> allowedClasses;

public JXPathFilter() {
init();
}

public void init() {
this.allowedClasses = new HashSet<>();
final String allowedClasses = System.getProperty("jxpath.class.allow");
if (allowedClasses != null && !allowedClasses.isEmpty()) {
allowedClassesList = new HashSet<>();
allowedClassesList.addAll(Arrays.asList(allowedClasses.split(",")));
this.allowedClasses.addAll(Arrays.asList(allowedClasses.split(",")));
}
}

Expand All @@ -50,10 +46,10 @@ public void init() {
*/
@Override
public boolean exposeToXPath(String className) {
if (allowedClassesList == null || allowedClassesList.size() < 1) {
if (allowedClasses.isEmpty()) {
return false;
}

return allowedClassesList.contains(className) || allowedClassesList.contains("*");
return allowedClasses.contains(className) || allowedClasses.contains("*");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ public class JXPath154Test extends JXPathTestCase {

protected JXPathContext context;

@Override
public void setUp() throws Exception {
super.setUp();
System.setProperty("jxpath.class.allow", "*");
}

@Override
public void tearDown() throws Exception {
System.clearProperty("jxpath.class.allow");
super.tearDown();
}

protected DocumentContainer createDocumentContainer(final String model) {
return new DocumentContainer(JXPathTestCase.class.getResource("InnerEmptyNamespace.xml"), model);
}
Expand Down

0 comments on commit ea34611

Please sign in to comment.