From ea3461138c01be3e647ef7520be071b99047f812 Mon Sep 17 00:00:00 2001 From: Khaled Yakdan Date: Wed, 19 Oct 2022 22:05:57 +0200 Subject: [PATCH] Refactor the JXPathFilter --- .../apache/commons/jxpath/ri/JXPathFilter.java | 16 ++++++---------- .../commons/jxpath/ri/model/JXPath154Test.java | 12 ++++++++++++ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/apache/commons/jxpath/ri/JXPathFilter.java b/src/main/java/org/apache/commons/jxpath/ri/JXPathFilter.java index 92554ed9b..e5fe2ff78 100644 --- a/src/main/java/org/apache/commons/jxpath/ri/JXPathFilter.java +++ b/src/main/java/org/apache/commons/jxpath/ri/JXPathFilter.java @@ -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 allowedClassesList = null; + private final Set 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(","))); } } @@ -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("*"); } } diff --git a/src/test/java/org/apache/commons/jxpath/ri/model/JXPath154Test.java b/src/test/java/org/apache/commons/jxpath/ri/model/JXPath154Test.java index eff6253eb..7352365c9 100644 --- a/src/test/java/org/apache/commons/jxpath/ri/model/JXPath154Test.java +++ b/src/test/java/org/apache/commons/jxpath/ri/model/JXPath154Test.java @@ -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); }