You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by je...@apache.org on 2018/07/23 17:46:29 UTC
[sling-org-apache-sling-resource-filter] branch master updated:
updated javadocs and reduced complexity
This is an automated email from the ASF dual-hosted git repository.
jeb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resource-filter.git
The following commit(s) were added to refs/heads/master by this push:
new f79f7f9 updated javadocs and reduced complexity
f79f7f9 is described below
commit f79f7f9aa6d759d6346eaac262b24eb0531d30a2
Author: JE Bailey <ja...@sas.com>
AuthorDate: Mon Jul 23 13:18:37 2018 -0400
updated javadocs and reduced complexity
---
.../apache/sling/resource/filter/api/Context.java | 60 +++++++++++++++-
.../resource/filter/api/ResourceFilterFactory.java | 22 ------
.../filter/api/ResourceFilterFunction.java | 11 +--
.../filter/api/impl/ComparisonVisitor.java | 80 ++++++++++++----------
.../filter/api/impl/ResourceFactoryImpl.java | 51 --------------
.../sling/resource/filter/ResourceFilterTest.java | 7 +-
6 files changed, 103 insertions(+), 128 deletions(-)
diff --git a/src/main/java/org/apache/sling/resource/filter/api/Context.java b/src/main/java/org/apache/sling/resource/filter/api/Context.java
index 4e90f2a..4a0bb8c 100644
--- a/src/main/java/org/apache/sling/resource/filter/api/Context.java
+++ b/src/main/java/org/apache/sling/resource/filter/api/Context.java
@@ -22,22 +22,78 @@ import org.apache.sling.api.resource.Resource;
public interface Context {
+ /**
+ * Adds a Function to the script to allow for customization.
+ *
+ *
+ * @param name
+ * of the function as it appears in the script
+ * @param functionImpl
+ * defines the function in terms of passed in arguments, the resource
+ * that is being acted on and the return object
+ * @return this Context
+ */
Context addFunction(String name, BiFunction<Object[], Resource, Object> functionImpl);
+ /**
+ * Remove the function with the given name from the context.
+ *
+ * @param name
+ * @return
+ */
Context removeFunction(String name);
+ /**
+ * Allows an object to be represented in the script for evaluation.
+ *
+ * @param name of the argument
+ * @param object value that is represented
+ * @return this Context
+ */
Context addArgument(String name, Object object);
+ /**
+ * Retrieve the currently defined Logic Visitor
+ *
+ * @return Visitor
+ */
Visitor<Predicate<Resource>> getLogicVisitor();
+ /**
+ * Retrieve the currently defined Comparison Visitor
+ *
+ * @return Visitor
+ */
Visitor<Function<Resource, Object>> getComparisonVisitor();
+ /**
+ * Replaces the existing Logic Visitor, if present, with the provided Visitor
+ *
+ * @param Visitor
+ */
void setLogicVisitor(Visitor<Predicate<Resource>> logicVisitor);
+ /**
+ * Replaces the existing Comparison Visitor, if present, with the provided Visitor
+ *
+ * @param comparisonVisitor
+ */
void setComparionVisitor(Visitor<Function<Resource, Object>> comparisonVisitor);
- Optional<BiFunction<Object[], Resource, Object>> getFunction(String text);
+ /**
+ * Used to retrieve the function during script processing
+ *
+ * @param name of the function in the context
+ * @return Optional BiFunction object representing the name
+ */
+ Optional<BiFunction<Object[], Resource, Object>> getFunction(String name);
- Optional<Object> getArgument(String text);
+ /**
+ * Retrieves the value object associated with the name
+ *
+ * @param name of the argument
+ * @return Optional object represented by the name
+ */
+ Optional<Object> getArgument(String name);
}
diff --git a/src/main/java/org/apache/sling/resource/filter/api/ResourceFilterFactory.java b/src/main/java/org/apache/sling/resource/filter/api/ResourceFilterFactory.java
deleted file mode 100644
index c9e2fb0..0000000
--- a/src/main/java/org/apache/sling/resource/filter/api/ResourceFilterFactory.java
+++ /dev/null
@@ -1,22 +0,0 @@
-/*
- * 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.
- */
-package org.apache.sling.resource.filter.api;
-
-import org.apache.sling.resource.filter.ResourceFilter;
-
-public interface ResourceFilterFactory {
-
- ResourceFilter getResourceFilter(String script);
-
-}
diff --git a/src/main/java/org/apache/sling/resource/filter/api/ResourceFilterFunction.java b/src/main/java/org/apache/sling/resource/filter/api/ResourceFilterFunction.java
index 1633bc1..b633fbf 100644
--- a/src/main/java/org/apache/sling/resource/filter/api/ResourceFilterFunction.java
+++ b/src/main/java/org/apache/sling/resource/filter/api/ResourceFilterFunction.java
@@ -18,9 +18,8 @@ import java.util.function.BiFunction;
import org.apache.sling.api.resource.Resource;
/**
- * A CustomFilterFunction implementation is used to translate a command in a
- * script or an Object that is a result of a custom function. Into a value that
- * is used for Comparison
+ * A ResourceFilterFunction implementation is used to translate a command in a
+ * script to a value object that will be used as part of a comparison
*
*/
public interface ResourceFilterFunction extends BiFunction<Object[], Resource, Object> {
@@ -37,11 +36,5 @@ public interface ResourceFilterFunction extends BiFunction<Object[], Resource, O
*/
Object apply(Object[] arguments, Resource resource);
- /**
- * Allows the name of the function to be defined
- *
- * @return name to be used in the script
- */
- String getName();
}
diff --git a/src/main/java/org/apache/sling/resource/filter/api/impl/ComparisonVisitor.java b/src/main/java/org/apache/sling/resource/filter/api/impl/ComparisonVisitor.java
index 43f42a8..fe85f03 100644
--- a/src/main/java/org/apache/sling/resource/filter/api/impl/ComparisonVisitor.java
+++ b/src/main/java/org/apache/sling/resource/filter/api/impl/ComparisonVisitor.java
@@ -48,48 +48,13 @@ public class ComparisonVisitor implements Visitor<Function<Resource, Object>> {
@Override
public Function<Resource, Object> visit(Node node) {
-
switch (node.kind) {
case FilterParserConstants.FUNCTION_NAME:
- // will only get here in the case of the 'FUNCTION' switch case
- switch (node.text) {
- case "name":
- return Resource::getName;
- case "path":
- return Resource::getPath;
- case "date":
- return resource -> {
- Object[] arguments = node.visitChildren(this).stream().map(funct -> funct.apply(resource))
- .toArray();
- return dateHandler(arguments);
- };
- default:
- Optional<BiFunction<Object[], Resource, Object>> temp = context.getFunction(node.text);
- if (temp.isPresent()) {
- final List<Function<Resource, Object>> children2 = node.visitChildren(this);
- return resource -> {
- Object[] arguments = children2.stream().map(funct -> funct.apply(resource)).toArray();
- return temp.get().apply(arguments, resource);
- };
- }
- }
- break;
+ return functionHandler(node);
case FilterParserConstants.NULL:
return resource -> new Null();
case FilterParserConstants.NUMBER:
- Number numericValue = null;
- String numberText = node.text;
- try {
- numericValue = Integer.valueOf(numberText);
- } catch (NumberFormatException nfe1) {
- try {
- numericValue = new BigDecimal(numberText);
- } catch (NumberFormatException nfe2) {
- // swallow
- }
- }
- final Number numericReply = numericValue;
- return resource -> numericReply;
+ return resource -> numericHandler(node.text);
case FilterParserConstants.OFFSETDATETIME:
return resource -> OffsetDateTime.parse(node.text).toInstant();
case FilterParserConstants.DATETIME:
@@ -115,7 +80,6 @@ public class ComparisonVisitor implements Visitor<Function<Resource, Object>> {
default:
return resource -> node.text;
}
- return null;
}
private ValueMap valueMapOf(Resource resource) {
@@ -143,5 +107,45 @@ public class ComparisonVisitor implements Visitor<Function<Resource, Object>> {
return DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse(dateString, OffsetDateTime::from).toInstant();
}
}
+
+ private Function<Resource, Object> functionHandler(Node node){
+ // will only get here in the case of the 'FUNCTION' switch case
+ switch (node.text) {
+ case "name":
+ return Resource::getName;
+ case "path":
+ return Resource::getPath;
+ case "date":
+ return resource -> {
+ Object[] arguments = node.visitChildren(this).stream().map(funct -> funct.apply(resource))
+ .toArray();
+ return dateHandler(arguments);
+ };
+ default:
+ Optional<BiFunction<Object[], Resource, Object>> temp = context.getFunction(node.text);
+ if (temp.isPresent()) {
+ final List<Function<Resource, Object>> children2 = node.visitChildren(this);
+ return resource -> {
+ Object[] arguments = children2.stream().map(funct -> funct.apply(resource)).toArray();
+ return temp.get().apply(arguments, resource);
+ };
+ }
+ }
+ return null;
+ }
+
+ private static Number numericHandler(String numberText) {
+ Number numericValue = null;
+ try {
+ numericValue = Integer.valueOf(numberText);
+ } catch (NumberFormatException nfe1) {
+ try {
+ numericValue = new BigDecimal(numberText);
+ } catch (NumberFormatException nfe2) {
+ // swallow
+ }
+ }
+ return numericValue;
+ }
}
diff --git a/src/main/java/org/apache/sling/resource/filter/api/impl/ResourceFactoryImpl.java b/src/main/java/org/apache/sling/resource/filter/api/impl/ResourceFactoryImpl.java
deleted file mode 100644
index 5915b67..0000000
--- a/src/main/java/org/apache/sling/resource/filter/api/impl/ResourceFactoryImpl.java
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * 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.
- */
-package org.apache.sling.resource.filter.api.impl;
-
-import java.util.List;
-
-import org.apache.sling.resource.filter.ResourceFilter;
-import org.apache.sling.resource.filter.api.Context;
-import org.apache.sling.resource.filter.api.ResourceFilterFactory;
-import org.apache.sling.resource.filter.api.ResourceFilterFunction;
-import org.apache.sling.resource.filter.impl.ParseException;
-import org.osgi.service.component.annotations.Component;
-import org.osgi.service.component.annotations.Reference;
-import org.osgi.service.component.annotations.ReferencePolicyOption;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-@Component(property = { "service.description=ResourceFilter Factory", "service.vendor=The Apache Software Foundation" })
-public class ResourceFactoryImpl implements ResourceFilterFactory {
-
- @Reference(policyOption=ReferencePolicyOption.GREEDY)
- List<ResourceFilterFunction> functions;
-
- protected final Logger log = LoggerFactory.getLogger(getClass());
-
- @Override
- public ResourceFilter getResourceFilter(String script) {
- try {
- ResourceFilter filter = new ResourceFilter(script);
- Context context = filter.getContext();
- for (ResourceFilterFunction func : functions) {
- context.addArgument(func.getName(), func);
- }
- } catch (ParseException e) {
- log.error(e.getLocalizedMessage());
- }
- return null;
- }
-
-}
diff --git a/src/test/java/org/apache/sling/resource/filter/ResourceFilterTest.java b/src/test/java/org/apache/sling/resource/filter/ResourceFilterTest.java
index 1fc6cc8..293213a 100644
--- a/src/test/java/org/apache/sling/resource/filter/ResourceFilterTest.java
+++ b/src/test/java/org/apache/sling/resource/filter/ResourceFilterTest.java
@@ -15,15 +15,11 @@ package org.apache.sling.resource.filter;
import static org.junit.Assert.assertEquals;
-import java.text.SimpleDateFormat;
-import java.util.Date;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.apache.sling.api.resource.Resource;
-import org.apache.sling.resource.filter.ResourceFilter;
-import org.apache.sling.resource.filter.ResourceStream;
import org.apache.sling.resource.filter.impl.ParseException;
import org.apache.sling.testing.mock.sling.junit.SlingContext;
import org.junit.Before;
@@ -36,7 +32,7 @@ public class ResourceFilterTest {
public final SlingContext context = new SlingContext();
private static String START_PATH = "/content/sample/en";
- private Date midPoint;
+
private static String DATE_STRING = "Thu Aug 07 2013 16:32:59 GMT+0200";
private static String NEW_DATE = "2013-08-08T16:32:59.000+02:00";
@@ -45,7 +41,6 @@ public class ResourceFilterTest {
@Before
public void setUp() throws ParseException, java.text.ParseException {
context.load().json("/data.json", "/content/sample/en");
- midPoint = new SimpleDateFormat(DATE_FORMAT).parse(DATE_STRING);
}
@Test