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-whiteboard.git
The following commit(s) were added to refs/heads/master by this push:
new bdc7291 changed api for ResourceFilterFunction. Inlined date, laid
groundwork for OSGi support
bdc7291 is described below
commit bdc7291f916739a9b5469fb300d3d2e43774afa5
Author: JE Bailey <[email protected]>
AuthorDate: Thu May 24 09:27:08 2018 -0400
changed api for ResourceFilterFunction. Inlined date, laid groundwork
for OSGi support
---
.../apache/sling/resource/stream/api/Context.java | 5 +-
.../resource/stream/api/ResourceFilterFactory.java | 2 +-
...teFunction.java => ResourceFilterFunction.java} | 15 +++--
.../stream/api/impl/ComparisonVisitor.java | 45 +++++++++++++--
.../resource/stream/api/impl/DefaultContext.java | 12 +---
.../resource/stream/api/impl/InstantProvider.java | 67 ----------------------
.../stream/api/impl/ResourceFactoryImpl.java | 22 +++++--
7 files changed, 75 insertions(+), 93 deletions(-)
diff --git
a/streams/src/main/java/org/apache/sling/resource/stream/api/Context.java
b/streams/src/main/java/org/apache/sling/resource/stream/api/Context.java
index f97c111..9b3104e 100644
--- a/streams/src/main/java/org/apache/sling/resource/stream/api/Context.java
+++ b/streams/src/main/java/org/apache/sling/resource/stream/api/Context.java
@@ -13,7 +13,6 @@
*/
package org.apache.sling.resource.stream.api;
-import java.util.List;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Function;
@@ -23,7 +22,7 @@ import org.apache.sling.api.resource.Resource;
public interface Context {
- Context addFunction(String name, BiFunction<List<Function<Resource,
Object>>, Resource, Object> functionImpl);
+ Context addFunction(String name, BiFunction<Object[], Resource, Object>
functionImpl);
Context removeFunction(String name);
@@ -37,7 +36,7 @@ public interface Context {
void setComparionVisitor(Visitor<Function<Resource, Object>>
comparisonVisitor);
- Optional<BiFunction<List<Function<Resource, Object>>, Resource, Object>>
getFunction(String text);
+ Optional<BiFunction<Object[], Resource, Object>> getFunction(String text);
Optional<Object> getArgument(String text);
diff --git
a/streams/src/main/java/org/apache/sling/resource/stream/api/ResourceFilterFactory.java
b/streams/src/main/java/org/apache/sling/resource/stream/api/ResourceFilterFactory.java
index 2fa2177..c373523 100644
---
a/streams/src/main/java/org/apache/sling/resource/stream/api/ResourceFilterFactory.java
+++
b/streams/src/main/java/org/apache/sling/resource/stream/api/ResourceFilterFactory.java
@@ -17,6 +17,6 @@ import org.apache.sling.resource.stream.ResourceFilter;
public interface ResourceFilterFactory {
- ResourceFilter getResourceFilter();
+ ResourceFilter getResourceFilter(String script);
}
diff --git
a/streams/src/main/java/org/apache/sling/resource/stream/api/CustomFilteFunction.java
b/streams/src/main/java/org/apache/sling/resource/stream/api/ResourceFilterFunction.java
similarity index 75%
rename from
streams/src/main/java/org/apache/sling/resource/stream/api/CustomFilteFunction.java
rename to
streams/src/main/java/org/apache/sling/resource/stream/api/ResourceFilterFunction.java
index 67b4459..61c30c5 100644
---
a/streams/src/main/java/org/apache/sling/resource/stream/api/CustomFilteFunction.java
+++
b/streams/src/main/java/org/apache/sling/resource/stream/api/ResourceFilterFunction.java
@@ -13,19 +13,17 @@
*/
package org.apache.sling.resource.stream.api;
-import java.util.List;
import java.util.function.BiFunction;
-import java.util.function.Function;
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
+ * script or an Object that is a result of a custom function. Into a value that
* is used for Comparison
*
*/
-public interface CustomFilteFunction extends
BiFunction<List<Function<Resource, Object>>, Resource, Object> {
+public interface ResourceFilterFunction extends BiFunction<Object[], Resource,
Object> {
/**
* This method returns a {@code Object} to be used as part of a comparison.
@@ -37,6 +35,13 @@ public interface CustomFilteFunction extends
BiFunction<List<Function<Resource,
* @return A {@code Object} which should be a String, Instant, or Number
to be
* used as part of a comparison or Function
*/
- Object apply(List<Function<Resource, Object>> arguments, Resource
resource);
+ 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/streams/src/main/java/org/apache/sling/resource/stream/api/impl/ComparisonVisitor.java
b/streams/src/main/java/org/apache/sling/resource/stream/api/impl/ComparisonVisitor.java
index b8d3fd4..a9fd7c2 100644
---
a/streams/src/main/java/org/apache/sling/resource/stream/api/impl/ComparisonVisitor.java
+++
b/streams/src/main/java/org/apache/sling/resource/stream/api/impl/ComparisonVisitor.java
@@ -14,10 +14,14 @@
package org.apache.sling.resource.stream.api.impl;
import java.math.BigDecimal;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
+import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.List;
import java.util.Optional;
@@ -44,6 +48,7 @@ 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
@@ -52,12 +57,24 @@ public class ComparisonVisitor implements
Visitor<Function<Resource, Object>> {
return Resource::getName;
case "path":
return Resource::getPath;
+ case "date":
+ final List<Function<Resource, Object>> children =
node.visitChildren(this);
+ return resource -> {
+ Object[] arguments = children.stream().map(funct -> {
+ return funct.apply(resource);
+ }).toArray();
+ return dateHandler(arguments, resource);
+ };
default:
- Optional<BiFunction<List<Function<Resource, Object>>,
Resource, Object>> temp = context
- .getFunction(node.text);
+ Optional<BiFunction<Object[], Resource, Object>> temp =
context.getFunction(node.text);
if (temp.isPresent()) {
- final List<Function<Resource, Object>> arguments =
node.visitChildren(this);
- return resource -> temp.get().apply(arguments, resource);
+ final List<Function<Resource, Object>> children2 =
node.visitChildren(this);
+ return resource -> {
+ Object[] arguments = children2.stream().map(funct -> {
+ return funct.apply(resource);
+ }).toArray();
+ return temp.get().apply(arguments, resource);
+ };
}
}
break;
@@ -108,4 +125,24 @@ public class ComparisonVisitor implements
Visitor<Function<Resource, Object>> {
return resource.adaptTo(ValueMap.class);
}
+ private static Object dateHandler(Object[] arguments, Resource resource) {
+ if (arguments.length == 0) {
+ return Instant.now();
+ }
+ String dateString = arguments[0].toString();
+ String formatString = null;
+ if (arguments.length > 1) {
+ formatString = arguments[1].toString();
+ SimpleDateFormat dateFormat = new SimpleDateFormat(formatString);
+ try {
+ return
Instant.ofEpochMilli(dateFormat.parse(dateString).getTime());
+ } catch (ParseException e) {
+ return null;
+ }
+ } else {
+ return DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse(dateString,
OffsetDateTime::from).toInstant();
+ }
+
+ }
+
}
diff --git
a/streams/src/main/java/org/apache/sling/resource/stream/api/impl/DefaultContext.java
b/streams/src/main/java/org/apache/sling/resource/stream/api/impl/DefaultContext.java
index 8904393..a53d8eb 100644
---
a/streams/src/main/java/org/apache/sling/resource/stream/api/impl/DefaultContext.java
+++
b/streams/src/main/java/org/apache/sling/resource/stream/api/impl/DefaultContext.java
@@ -14,7 +14,6 @@
package org.apache.sling.resource.stream.api.impl;
import java.util.HashMap;
-import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiFunction;
@@ -27,7 +26,7 @@ import org.apache.sling.resource.stream.api.Visitor;
public class DefaultContext implements Context {
- private Map<String, BiFunction<List<Function<Resource, Object>>, Resource,
Object>> functions = new HashMap<>();
+ private Map<String, BiFunction<Object[], Resource, Object>> functions =
new HashMap<>();
private Map<String, Object> arguments = new HashMap<>();
@@ -35,16 +34,11 @@ public class DefaultContext implements Context {
private Visitor<Function<Resource, Object>> comparisonVisitor;
- {
- functions.put("date", new InstantProvider());
- }
-
public DefaultContext() {
}
@Override
- public Context addFunction(String name,
- BiFunction<List<Function<Resource, Object>>, Resource, Object>
functionImpl) {
+ public Context addFunction(String name, BiFunction<Object[], Resource,
Object> functionImpl) {
functions.put(name, functionImpl);
return this;
}
@@ -84,7 +78,7 @@ public class DefaultContext implements Context {
}
@Override
- public Optional<BiFunction<List<Function<Resource, Object>>, Resource,
Object>> getFunction(String text) {
+ public Optional<BiFunction<Object[], Resource, Object>> getFunction(String
text) {
return Optional.ofNullable(functions.get(text));
}
diff --git
a/streams/src/main/java/org/apache/sling/resource/stream/api/impl/InstantProvider.java
b/streams/src/main/java/org/apache/sling/resource/stream/api/impl/InstantProvider.java
deleted file mode 100644
index 100916d..0000000
---
a/streams/src/main/java/org/apache/sling/resource/stream/api/impl/InstantProvider.java
+++ /dev/null
@@ -1,67 +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.stream.api.impl;
-
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.time.Instant;
-import java.time.OffsetDateTime;
-import java.time.format.DateTimeFormatter;
-import java.util.List;
-import java.util.function.Function;
-
-import org.apache.felix.scr.annotations.Component;
-import org.apache.sling.api.resource.Resource;
-import org.apache.sling.resource.stream.api.CustomFilteFunction;
-
-/**
- * Implementation of {@link CustomFilteFunction} for the 'date' function.
- *
- * The following combination of arguments are supported
- *
- * <pre>
- * arguments results
- * ======================================================
- * none | current system time
- * one | ISO88601 String with offset
- * two | Date String followed by Date Format String
- *
- * </pre>
- *
- */
-@Component
-
-public class InstantProvider implements CustomFilteFunction {
-
- @Override
- public Object apply(List<Function<Resource, Object>> arguments, Resource
resource) {
- if (arguments.isEmpty()) {
- return Instant.now();
- }
- String dateString = arguments.get(0).apply(resource).toString();
- String formatString = null;
- if (arguments.size() > 1) {
- formatString = arguments.get(1).apply(resource).toString();
- SimpleDateFormat dateFormat = new SimpleDateFormat(formatString);
- try {
- return
Instant.ofEpochMilli(dateFormat.parse(dateString).getTime());
- } catch (ParseException e) {
- return null;
- }
- } else {
- return DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse(dateString,
OffsetDateTime::from).toInstant();
- }
-
- }
-}
diff --git
a/streams/src/main/java/org/apache/sling/resource/stream/api/impl/ResourceFactoryImpl.java
b/streams/src/main/java/org/apache/sling/resource/stream/api/impl/ResourceFactoryImpl.java
index d5416e0..9f5be5a 100644
---
a/streams/src/main/java/org/apache/sling/resource/stream/api/impl/ResourceFactoryImpl.java
+++
b/streams/src/main/java/org/apache/sling/resource/stream/api/impl/ResourceFactoryImpl.java
@@ -16,20 +16,34 @@ package org.apache.sling.resource.stream.api.impl;
import java.util.List;
import org.apache.sling.resource.stream.ResourceFilter;
-import org.apache.sling.resource.stream.api.CustomFilteFunction;
+import org.apache.sling.resource.stream.api.Context;
+import org.apache.sling.resource.stream.api.ResourceFilterFunction;
import org.apache.sling.resource.stream.api.ResourceFilterFactory;
+import org.apache.sling.resource.stream.impl.ParseException;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
+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
- List<CustomFilteFunction> functions;
+ List<ResourceFilterFunction> functions;
+
+ protected final Logger log = LoggerFactory.getLogger(getClass());
@Override
- public ResourceFilter getResourceFilter() {
- // TODO Auto-generated method stub
+ 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;
}
--
To stop receiving notification emails like this one, please contact
[email protected].