From bf3234daa0e3dd368df5b1d2c02ffb910867b50f Mon Sep 17 00:00:00 2001 From: Dominik Przybyl Date: Thu, 28 Sep 2023 09:19:10 +0200 Subject: [PATCH] added data source support --- .../grammar/datasource/ContentDataSource.java | 21 ++++++- .../core/grammar/datasource/DataSource.java | 20 +++++- .../grammar/datasource/DataSourceInvoker.java | 19 ++++++ .../grammar/datasource/LowerDataSource.java | 42 +++++++++++++ .../datasource/NodeNamesDataSource.java | 36 ----------- .../grammar/datasource/NodesDataSource.java | 62 +++++++++++++++++++ .../grammar/datasource/UpperDataSource.java | 42 +++++++++++++ .../datasource/ValueMapDataSource.java | 61 ++++++++++++++++++ 8 files changed, 265 insertions(+), 38 deletions(-) create mode 100644 app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/LowerDataSource.java delete mode 100644 app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/NodeNamesDataSource.java create mode 100644 app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/NodesDataSource.java create mode 100644 app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/UpperDataSource.java create mode 100644 app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/ValueMapDataSource.java diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/ContentDataSource.java b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/ContentDataSource.java index 7d205d2b..db7661ff 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/ContentDataSource.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/ContentDataSource.java @@ -1,3 +1,22 @@ +/* + * ========================LICENSE_START================================= + * AEM Permission Management + * %% + * Copyright (C) 2013 Wunderman Thompson Technology + * %% + * 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. + * =========================LICENSE_END================================== + */ package com.cognifide.apm.core.grammar.datasource; import com.cognifide.apm.core.grammar.ApmList; @@ -41,7 +60,7 @@ private ApmType traverseTree(Resource root, int depth, List configs) { map.put("path", new ApmString(resource.getPath())); map.put("name", new ApmString(resource.getName())); for (int i = 0; i < config.paramNames.size(); i++) { - map.put(config.paramNames.get(i), new ApmString(matcher.group(i + 1).toLowerCase())); + map.put(config.paramNames.get(i), new ApmString(matcher.group(i + 1))); } if (depth < configs.size() - 1) { map.put("pages", traverseTree(resource, depth + 1, configs)); diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/DataSource.java b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/DataSource.java index e3e7f78c..cbc145e1 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/DataSource.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/DataSource.java @@ -1,6 +1,24 @@ +/* + * ========================LICENSE_START================================= + * AEM Permission Management + * %% + * Copyright (C) 2013 Wunderman Thompson Technology + * %% + * 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. + * =========================LICENSE_END================================== + */ package com.cognifide.apm.core.grammar.datasource; -import com.cognifide.apm.core.grammar.ApmEmpty; import com.cognifide.apm.core.grammar.ApmType; import org.apache.sling.api.resource.ResourceResolver; diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/DataSourceInvoker.java b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/DataSourceInvoker.java index ed4d6f94..e596df06 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/DataSourceInvoker.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/DataSourceInvoker.java @@ -1,3 +1,22 @@ +/* + * ========================LICENSE_START================================= + * AEM Permission Management + * %% + * Copyright (C) 2013 Wunderman Thompson Technology + * %% + * 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. + * =========================LICENSE_END================================== + */ package com.cognifide.apm.core.grammar.datasource; import com.cognifide.apm.core.grammar.ApmType; diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/LowerDataSource.java b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/LowerDataSource.java new file mode 100644 index 00000000..01ddf7ee --- /dev/null +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/LowerDataSource.java @@ -0,0 +1,42 @@ +/* + * ========================LICENSE_START================================= + * AEM Permission Management + * %% + * Copyright (C) 2013 Wunderman Thompson Technology + * %% + * 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. + * =========================LICENSE_END================================== + */ +package com.cognifide.apm.core.grammar.datasource; + +import com.cognifide.apm.core.grammar.ApmString; +import com.cognifide.apm.core.grammar.ApmType; +import org.apache.sling.api.resource.ResourceResolver; +import org.osgi.service.component.annotations.Component; + +import java.util.List; + +@Component +public class LowerDataSource implements DataSource { + + @Override + public String getName() { + return "LOWER"; + } + + @Override + public ApmType determine(ResourceResolver resolver, List parameters) { + String value = parameters.get(0).getString(); + return new ApmString(value.toLowerCase()); + } +} diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/NodeNamesDataSource.java b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/NodeNamesDataSource.java deleted file mode 100644 index ed7338e1..00000000 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/NodeNamesDataSource.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.cognifide.apm.core.grammar.datasource; - -import com.cognifide.apm.core.grammar.ApmList; -import com.cognifide.apm.core.grammar.ApmString; -import com.cognifide.apm.core.grammar.ApmType; -import org.apache.sling.api.resource.Resource; -import org.apache.sling.api.resource.ResourceResolver; -import org.osgi.service.component.annotations.Component; - -import java.util.List; -import java.util.regex.Pattern; -import java.util.stream.Collectors; -import java.util.stream.StreamSupport; - -@Component -public class NodeNamesDataSource implements DataSource { - - @Override - public String getName() { - return "NODE_NAMES"; - } - - @Override - public ApmType determine(ResourceResolver resolver, List parameters) { - String path = parameters.get(0).getString(); - String regex = parameters.size() >= 2 ? parameters.get(1).getString() : "[^:]+"; - Pattern pattern = Pattern.compile(regex); - Resource resource = resolver.getResource(path); - List values = StreamSupport.stream(resource.getChildren().spliterator(), false) - .map(Resource::getName) - .filter(name -> pattern.matcher(name).matches()) - .map(ApmString::new) - .collect(Collectors.toList()); - return new ApmList(values); - } -} diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/NodesDataSource.java b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/NodesDataSource.java new file mode 100644 index 00000000..0d10e7f0 --- /dev/null +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/NodesDataSource.java @@ -0,0 +1,62 @@ +/* + * ========================LICENSE_START================================= + * AEM Permission Management + * %% + * Copyright (C) 2013 Wunderman Thompson Technology + * %% + * 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. + * =========================LICENSE_END================================== + */ +package com.cognifide.apm.core.grammar.datasource; + +import com.cognifide.apm.core.grammar.ApmList; +import com.cognifide.apm.core.grammar.ApmMap; +import com.cognifide.apm.core.grammar.ApmString; +import com.cognifide.apm.core.grammar.ApmType; +import org.apache.sling.api.resource.Resource; +import org.apache.sling.api.resource.ResourceResolver; +import org.osgi.service.component.annotations.Component; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.regex.Pattern; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +@Component +public class NodesDataSource implements DataSource { + + @Override + public String getName() { + return "NODES"; + } + + @Override + public ApmType determine(ResourceResolver resolver, List parameters) { + String path = parameters.get(0).getString(); + String regex = parameters.size() >= 2 ? parameters.get(1).getString() : "[^:]+"; + Pattern pattern = Pattern.compile(regex); + Resource root = resolver.getResource(path); + List values = StreamSupport.stream(root.getChildren().spliterator(), false) + .filter(resource -> pattern.matcher(resource.getName()).matches()) + .map(resource -> { + Map map = new HashMap<>(); + map.put("path", new ApmString(resource.getPath())); + map.put("name", new ApmString(resource.getName())); + return new ApmMap(map); + }) + .collect(Collectors.toList()); + return new ApmList(values); + } +} diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/UpperDataSource.java b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/UpperDataSource.java new file mode 100644 index 00000000..e72cac08 --- /dev/null +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/UpperDataSource.java @@ -0,0 +1,42 @@ +/* + * ========================LICENSE_START================================= + * AEM Permission Management + * %% + * Copyright (C) 2013 Wunderman Thompson Technology + * %% + * 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. + * =========================LICENSE_END================================== + */ +package com.cognifide.apm.core.grammar.datasource; + +import com.cognifide.apm.core.grammar.ApmString; +import com.cognifide.apm.core.grammar.ApmType; +import org.apache.sling.api.resource.ResourceResolver; +import org.osgi.service.component.annotations.Component; + +import java.util.List; + +@Component +public class UpperDataSource implements DataSource { + + @Override + public String getName() { + return "UPPER"; + } + + @Override + public ApmType determine(ResourceResolver resolver, List parameters) { + String value = parameters.get(0).getString(); + return new ApmString(value.toUpperCase()); + } +} diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/ValueMapDataSource.java b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/ValueMapDataSource.java new file mode 100644 index 00000000..8a18e4d5 --- /dev/null +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/ValueMapDataSource.java @@ -0,0 +1,61 @@ +/* + * ========================LICENSE_START================================= + * AEM Permission Management + * %% + * Copyright (C) 2013 Wunderman Thompson Technology + * %% + * 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. + * =========================LICENSE_END================================== + */ +package com.cognifide.apm.core.grammar.datasource; + +import com.cognifide.apm.core.grammar.*; +import org.apache.sling.api.resource.ResourceResolver; +import org.apache.sling.api.resource.ValueMap; +import org.osgi.service.component.annotations.Component; + +import java.io.InputStream; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +@Component +public class ValueMapDataSource implements DataSource { + + @Override + public String getName() { + return "VALUEMAP"; + } + + @Override + public ApmType determine(ResourceResolver resolver, List parameters) { + String path = parameters.get(0).getString(); + ValueMap valueMap = resolver.getResource(path).getValueMap(); + Map map = new HashMap<>(); + for (Map.Entry entry : valueMap.entrySet()) { + String key = entry.getKey(); + Object value = entry.getValue(); + if (value instanceof Object[]) { + List list = Arrays.stream((Object[]) value) + .map(item -> new ApmString(item.toString())) + .collect(Collectors.toList()); + map.put(key, new ApmList(list)); + } else { + map.put(key, new ApmString(value.toString())); + } + } + return new ApmMap(map); + } +}