Skip to content

Commit

Permalink
added data source support
Browse files Browse the repository at this point in the history
  • Loading branch information
dprzybyl committed Sep 28, 2023
1 parent d062258 commit bf3234d
Show file tree
Hide file tree
Showing 8 changed files with 265 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -41,7 +60,7 @@ private ApmType traverseTree(Resource root, int depth, List<Config> 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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ApmType> parameters) {
String value = parameters.get(0).getString();
return new ApmString(value.toLowerCase());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<ApmType> 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<ApmMap> values = StreamSupport.stream(root.getChildren().spliterator(), false)
.filter(resource -> pattern.matcher(resource.getName()).matches())
.map(resource -> {
Map<String, ApmType> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<ApmType> parameters) {
String value = parameters.get(0).getString();
return new ApmString(value.toUpperCase());
}
}
Original file line number Diff line number Diff line change
@@ -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<ApmType> parameters) {
String path = parameters.get(0).getString();
ValueMap valueMap = resolver.getResource(path).getValueMap();
Map<String, ApmType> map = new HashMap<>();
for (Map.Entry<String, Object> entry : valueMap.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (value instanceof Object[]) {
List<ApmType> 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);
}
}

0 comments on commit bf3234d

Please sign in to comment.