-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
274 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/ContentDataSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
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.commons.lang3.StringUtils; | ||
import org.apache.sling.api.resource.Resource; | ||
import org.apache.sling.api.resource.ResourceResolver; | ||
import org.osgi.service.component.annotations.Component; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
@Component | ||
public class ContentDataSource implements DataSource { | ||
|
||
@Override | ||
public String getName() { | ||
return "CONTENT"; | ||
} | ||
|
||
@Override | ||
public ApmType determine(ResourceResolver resolver, List<ApmType> parameters) { | ||
List<Config> configs = determineConfigs(parameters); | ||
Resource root = resolver.getResource("/content"); | ||
return traverseTree(root, 0, configs); | ||
} | ||
|
||
private ApmType traverseTree(Resource root, int depth, List<Config> configs) { | ||
Config config = configs.get(depth); | ||
List<ApmType> list = new ArrayList<>(); | ||
for (Resource resource : root.getChildren()) { | ||
Matcher matcher = config.pattern.matcher(resource.getPath()); | ||
if (matcher.matches()) { | ||
Map<String, ApmType> map = new HashMap<>(); | ||
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())); | ||
} | ||
if (depth < configs.size() - 1) { | ||
map.put("pages", traverseTree(resource, depth + 1, configs)); | ||
} | ||
list.add(new ApmMap(map)); | ||
} | ||
} | ||
return new ApmList(list); | ||
} | ||
|
||
private List<Config> determineConfigs(List<ApmType> parameters) { | ||
String regex = parameters.get(0).getString(); | ||
List<ApmType> paramNames = parameters.get(1).getList(); | ||
String[] parts = StringUtils.substringAfter(regex, "/content/").split("/"); | ||
int paramIndex = 0; | ||
List<Config> configs = new ArrayList<>(); | ||
for (String part : parts) { | ||
Config config = new Config(); | ||
config.pattern = Pattern.compile(".*/" + part); | ||
config.paramNames = new ArrayList<>(); | ||
for (int i = 0; i < StringUtils.countMatches(part, "("); i++) { | ||
config.paramNames.add(paramNames.get(paramIndex++).getString()); | ||
} | ||
configs.add(config); | ||
} | ||
return configs; | ||
} | ||
|
||
private class Config { | ||
|
||
Pattern pattern; | ||
|
||
List<String> paramNames; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/DataSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
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; | ||
|
||
import java.util.List; | ||
|
||
public interface DataSource { | ||
|
||
String getName(); | ||
|
||
ApmType determine(ResourceResolver resolver, List<ApmType> parameters); | ||
} |
32 changes: 32 additions & 0 deletions
32
app/aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/DataSourceInvoker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.cognifide.apm.core.grammar.datasource; | ||
|
||
import com.cognifide.apm.core.grammar.ApmType; | ||
import org.apache.sling.api.resource.ResourceResolver; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Reference; | ||
import org.osgi.service.component.annotations.ReferenceCardinality; | ||
import org.osgi.service.component.annotations.ReferencePolicy; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Component(service = DataSourceInvoker.class) | ||
public class DataSourceInvoker { | ||
|
||
private final Map<String, DataSource> dataSources = new HashMap<>(); | ||
|
||
@Reference(policy = ReferencePolicy.DYNAMIC, cardinality = ReferenceCardinality.MULTIPLE, service = DataSource.class) | ||
protected final void bindDataSource(DataSource dataSource) { | ||
dataSources.put(dataSource.getName().toLowerCase(), dataSource); | ||
} | ||
|
||
protected final void unbindDataSource(DataSource dataSource) { | ||
dataSources.remove(dataSource.getName().toLowerCase()); | ||
} | ||
|
||
public ApmType determine(String name, ResourceResolver resolver, List<ApmType> parameters) { | ||
DataSource dataSource = dataSources.get(name.toLowerCase()); | ||
return dataSource == null ? null : dataSource.determine(resolver, parameters); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...aem/core/src/main/java/com/cognifide/apm/core/grammar/datasource/NodeNamesDataSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
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<ApmType> 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<ApmString> 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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.