Separated ResourceStream and ResourceFilter and corrected bug
bug is where the Null object was being used when a variable was being
assigned and resulted in true in conditions where the property was
absent.
diff --git a/pom.xml b/pom.xml
index 872f2c1..513a9aa 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1,87 +1,87 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--Licensed under the Apache License, Version 2.0 (the "License"); you may
- not use this file except in compliance with the License. You may obtain a
- copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless
- required by applicable law or agreed to in writing, software distributed
- under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
- OR CONDITIONS OF ANY KIND, either express or implied. See the License for
- the specific language governing permissions and limitations under the License. -->
+ not use this file except in compliance with the License. You may obtain a
+ copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless
+ required by applicable law or agreed to in writing, software distributed
+ under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
+ OR CONDITIONS OF ANY KIND, either express or implied. See the License for
+ the specific language governing permissions and limitations under the License. -->
<project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.apache.sling</groupId>
- <artifactId>sling</artifactId>
- <version>32</version>
- </parent>
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>sling</artifactId>
+ <version>32</version>
+ </parent>
- <artifactId>org.apache.sling.resource.filter</artifactId>
- <version>1.0.0-SNAPSHOT</version>
- <packaging>bundle</packaging>
+ <artifactId>org.apache.sling.resource.filter</artifactId>
+ <version>1.0.0-SNAPSHOT</version>
+ <packaging>bundle</packaging>
- <name>Apache Sling Resource Filter</name>
+ <name>Apache Sling Resource Filter</name>
- <properties>
- <sling.java.version>8</sling.java.version>
- </properties>
+ <properties>
+ <sling.java.version>8</sling.java.version>
+ </properties>
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.felix</groupId>
- <artifactId>maven-bundle-plugin</artifactId>
- <extensions>true</extensions>
- </plugin>
- <plugin>
- <groupId>org.apache.sling</groupId>
- <artifactId>maven-sling-plugin</artifactId>
- </plugin>
- <plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>javacc-maven-plugin</artifactId>
- <version>2.6</version>
- <executions>
- <execution>
- <id>javacc</id>
- <goals>
- <goal>javacc</goal>
- </goals>
- <configuration>
- <jdkVersion>1.8</jdkVersion>
- <javadocFriendlyComments>true</javadocFriendlyComments>
- <outputDirectory>${basedir}/src/main/java</outputDirectory>
- </configuration>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
- <dependencies>
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-lang3</artifactId>
- <version>3.4</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>org.apache.sling</groupId>
- <artifactId>org.apache.sling.testing.sling-mock</artifactId>
- <version>2.2.10</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.apache.sling</groupId>
- <artifactId>org.apache.sling.api</artifactId>
- <version>2.16.4</version>
- <scope>provided</scope>
- </dependency>
- </dependencies>
- <description>provides a set of utilities to create and handle streams</description>
- <scm>
- <url>https://gitbox.apache.org/repos/asf?p=sling-org-apache-sling-resource-filter.git</url>
- <connection>scm:git:https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resource-filter.git</connection>
- <developerConnection>scm:git:https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resource-filter.git</developerConnection>
- <tag>HEAD</tag>
- </scm>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <extensions>true</extensions>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>maven-sling-plugin</artifactId>
+ </plugin>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>javacc-maven-plugin</artifactId>
+ <version>2.6</version>
+ <executions>
+ <execution>
+ <id>javacc</id>
+ <goals>
+ <goal>javacc</goal>
+ </goals>
+ <configuration>
+ <jdkVersion>1.8</jdkVersion>
+ <javadocFriendlyComments>true</javadocFriendlyComments>
+ <outputDirectory>${basedir}/src/main/java</outputDirectory>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-lang3</artifactId>
+ <version>3.4</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.testing.sling-mock</artifactId>
+ <version>2.2.10</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.api</artifactId>
+ <version>2.16.4</version>
+ <scope>provided</scope>
+ </dependency>
+ </dependencies>
+ <description>provides a set of utilities to create and handle streams</description>
+ <scm>
+ <url>https://gitbox.apache.org/repos/asf?p=sling-org-apache-sling-resource-filter.git</url>
+ <connection>scm:git:https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resource-filter.git</connection>
+ <developerConnection>scm:git:https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resource-filter.git</developerConnection>
+ <tag>HEAD</tag>
+ </scm>
</project>
diff --git a/src/main/java/org/apache/sling/resource/filter/ResourceFilter.java b/src/main/java/org/apache/sling/resource/filter/ResourceFilter.java
index a204ec5..795f94f 100644
--- a/src/main/java/org/apache/sling/resource/filter/ResourceFilter.java
+++ b/src/main/java/org/apache/sling/resource/filter/ResourceFilter.java
@@ -25,8 +25,23 @@
public interface ResourceFilter {
+ /**
+ * Creates a Predicate<Resource> based on the scripted matching
+ *
+ * @param filter
+ * @return
+ * @throws ResourceFilterException
+ */
public Predicate<Resource> parse(String filter) throws ResourceFilterException;
+ /**
+ * Creates a Predicate<Resource> based on the scripted matching
+ *
+ * @param filter
+ * @param charEncoding
+ * @return
+ * @throws ResourceFilterException
+ */
public Predicate<Resource> parse(String filter, String charEncoding) throws ResourceFilterException;
/**
@@ -34,7 +49,7 @@
* filter creation
*
* @param params
- * @return
+ * @return this
*/
public abstract ResourceFilter addParams(Map<String, Object> params);
@@ -43,7 +58,7 @@
* creation
*
* @param params
- * @return
+ * @return this
*/
public abstract ResourceFilter addParam(String key, Object value);
}
diff --git a/src/main/java/org/apache/sling/resource/filter/ResourceFilterStream.java b/src/main/java/org/apache/sling/resource/filter/ResourceFilterStream.java
index ae86dd4..6bf389b 100644
--- a/src/main/java/org/apache/sling/resource/filter/ResourceFilterStream.java
+++ b/src/main/java/org/apache/sling/resource/filter/ResourceFilterStream.java
@@ -19,19 +19,25 @@
import org.apache.sling.api.resource.Resource;
import org.apache.sling.resource.filter.ResourceFilterStream;
-import org.apache.sling.resource.filter.impl.script.ParseException;
+
/**
* Creates a {@link Predicate} of type {@link Resource} to identify matching
* Resource objects
*
*/
-public class ResourceFilterStream extends ResourceStream {
+public class ResourceFilterStream {
+
+ private ResourceStream resources;
private ResourceFilter resourceFilter;
+ private Predicate<Resource> branchSelector = resource -> true;
+
+ private Predicate<Resource> childSelector = resource -> true;
+
public ResourceFilterStream(Resource resource, ResourceFilter filter) {
- super(resource);
+ resources = new ResourceStream(resource);
this.resourceFilter = filter;
}
@@ -44,8 +50,9 @@
* @return ResourceStream
* @throws ParseException
*/
- public Stream<Resource> stream(String branchSelector) throws ResourceFilterException {
- return stream(resourceFilter.parse(branchSelector));
+ public ResourceFilterStream setBranchSelector(String branchSelector) throws ResourceFilterException {
+ this.branchSelector = resourceFilter.parse(branchSelector);
+ return this;
}
/**
@@ -59,35 +66,9 @@
* @return ResourceStream
* @throws ParseException
*/
- public Stream<Resource> stream(String branchSelector, String charEncoding) throws ResourceFilterException {
- return stream(resourceFilter.parse(branchSelector, charEncoding));
- }
-
- /**
- * Provides a stream of the child resources filtered by the child selector
- *
- * @param childSelector
- * @return
- * @throws ResourceFilterException
- * @throws ParseException
- */
- public Stream<Resource> listChildren(String childSelector) throws ResourceFilterException {
- return listChildren(resourceFilter.parse(childSelector));
- }
-
- /**
- * Provides a stream of the child resources filtered by the child selector
- *
- * @param childSelector
- * text based definition of the Predicate to use
- * @param charEncoding
- * char encoding of the branch selector String
- * @return
- * @throws ResourceFilterException
- * @throws ParseException
- */
- public Stream<Resource> listChildren(String childSelector, String charEncoding) throws ResourceFilterException {
- return listChildren(resourceFilter.parse(childSelector, charEncoding));
+ public ResourceFilterStream setChildSelector(String childSelector) throws ResourceFilterException {
+ this.childSelector = resourceFilter.parse(childSelector);
+ return this;
}
/**
@@ -112,4 +93,8 @@
resourceFilter.addParams(params);
return this;
}
+
+ public Stream<Resource> stream() {
+ return resources.stream(branchSelector).filter(childSelector);
+ }
}
diff --git a/src/main/java/org/apache/sling/resource/filter/impl/ComparisonVisitor.java b/src/main/java/org/apache/sling/resource/filter/impl/ComparisonVisitor.java
index fce1870..ee71126 100644
--- a/src/main/java/org/apache/sling/resource/filter/impl/ComparisonVisitor.java
+++ b/src/main/java/org/apache/sling/resource/filter/impl/ComparisonVisitor.java
@@ -24,6 +24,7 @@
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.List;
+import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Function;
@@ -73,7 +74,11 @@
case FilterParserConstants.DYNAMIC_ARG:
return resource -> {
String argument = node.text;
- return context.getArgument(argument).orElse(new Null());
+ Optional<Object> arg = context.getArgument(argument);
+ if (!arg.isPresent()) {
+ throw new NoSuchElementException(String.format("No value present for '%s'",argument));
+ }
+ return arg.get();
};
default:
return resource -> node.text;
diff --git a/src/main/java/org/apache/sling/resource/filter/impl/Visitor.java b/src/main/java/org/apache/sling/resource/filter/impl/Visitor.java
index b9c234d..de9ec58 100644
--- a/src/main/java/org/apache/sling/resource/filter/impl/Visitor.java
+++ b/src/main/java/org/apache/sling/resource/filter/impl/Visitor.java
@@ -13,6 +13,8 @@
*/
package org.apache.sling.resource.filter.impl;
+import java.text.ParseException;
+
import org.apache.sling.resource.filter.impl.node.Node;
/**
diff --git a/src/test/java/org/apache/sling/resource/filter/ResourceFilterArgTest.java b/src/test/java/org/apache/sling/resource/filter/ResourceFilterArgTest.java
index 3d7efee..b91723b 100644
--- a/src/test/java/org/apache/sling/resource/filter/ResourceFilterArgTest.java
+++ b/src/test/java/org/apache/sling/resource/filter/ResourceFilterArgTest.java
@@ -53,8 +53,8 @@
Map<String,Object> params = new HashMap<>();
params.put("date", "2013-08-08T16:32:59");
params.put("lang", "Mongolian");
- Predicate<Resource> filter = resourceFilter.addParams(params).parse("[jcr:content/jcr:title] == $lang");
- List<Resource> found = handle(new ResourceStream(resource), filter);
+ ResourceFilterStream rfs = resource.adaptTo(ResourceFilterStream.class);
+ List<Resource> found = rfs.addParams(params).setChildSelector("[jcr:content/jcr:title] == $lang").stream().collect(Collectors.toList());
assertEquals(1, found.size());
}
@@ -63,9 +63,9 @@
Map<String,Object> params = new HashMap<>();
params.put("date", "2013-08-08T16:32:59");
params.put("lang", "Mongolian");
- Predicate<Resource> filter = resourceFilter.addParams(params).parse("[jcr:content/created] > $date and [jcr:content/jcr:title] == $lang");
- List<Resource> found = handle(new ResourceStream(resource), filter);
- assertEquals(1, found.size());
+ ResourceFilterStream rfs = resource.adaptTo(ResourceFilterStream.class);
+ long size = rfs.addParams(params).setChildSelector("[jcr:content/created] > $date and [jcr:content/jcr:title] == $lang").stream().count();
+ assertEquals(1, size);
}
@Test