Skip to content

Commit

Permalink
Merge pull request #403 from wttech/bugfix/grammar-fix
Browse files Browse the repository at this point in the history
small grammar change
  • Loading branch information
dprzybyl authored Aug 2, 2023
2 parents 8751452 + 74a0473 commit 0f1539b
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ public class AllowMapper {
@Mapping(
examples = {
"ALLOW '/content/dam' [READ]",
"ALLOW '/content/dam' glob='/*' [MODIFY] ",
"ALLOW '/content/dam' properties=['jcr:title'] [MODIFY]",
"ALLOW '/content/dam' types=['nt:folder'] [MODIFY]",
"ALLOW '/content/dam' [MODIFY] glob='/*'",
"ALLOW '/content/dam' [MODIFY] properties=['jcr:title']",
"ALLOW '/content/dam' [MODIFY] types=['nt:folder']",
"ALLOW '/content/dam' [MODIFY] restrictions={'restriction1': 'value', 'restriction2': ['value1', 'value2']}",
"ALLOW '/content/dam/domain' [READ, MODIFY] --IF-EXISTS"
},
reference = REFERENCE
Expand All @@ -64,6 +65,7 @@ public Action create(
"ALLOW [MODIFY] ON '/content/dam' glob='/*'",
"ALLOW [MODIFY] ON '/content/dam' properties=['jcr:title']",
"ALLOW [MODIFY] ON '/content/dam' types=['nt:folder']",
"ALLOW [MODIFY] ON '/content/dam' restrictions={'restriction1': 'value', 'restriction2': ['value1', 'value2']}",
"ALLOW [READ, MODIFY] ON '/content/dam/domain' --IF-EXISTS"
},
reference = REFERENCE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ public class DenyMapper {
@Mapping(
examples = {
"DENY '/content/dam' [READ]",
"DENY '/content/dam' glob='/*' [MODIFY]",
"DENY '/content/dam' properties=['jcr:title'] [MODIFY]",
"DENY '/content/dam' types=['nt:folder'] [MODIFY]",
"DENY '/content/dam' [MODIFY] glob='/*'",
"DENY '/content/dam' [MODIFY] properties=['jcr:title']",
"DENY '/content/dam' [MODIFY] types=['nt:folder']",
"DENY '/content/dam' [MODIFY] restrictions={'restriction1': 'value', 'restriction2': ['value1', 'value2']}",
"DENY '/content/dam/domain' [READ, MODIFY] --IF-EXISTS"
},
reference = REFERENCE
Expand All @@ -64,6 +65,7 @@ public Action create(
"DENY [MODIFY] ON '/content/dam' glob='/*'",
"DENY [MODIFY] ON '/content/dam' properties=['jcr:title']",
"DENY [MODIFY] ON '/content/dam' types=['nt:folder']",
"DENY [MODIFY] ON '/content/dam' restrictions={'restriction1': 'value', 'restriction2': ['value1', 'value2']}",
"DENY [READ, MODIFY] ON '/content/dam/domain' --IF-EXISTS"
},
reference = REFERENCE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.jcr.PropertyType;
import javax.jcr.Value;
import javax.jcr.ValueFactory;
Expand All @@ -40,6 +42,17 @@ public class Restrictions {

private static final String STRICT = "STRICT";

private static final String REP_GLOB_PROPERTY = "rep:glob";

private static final String REP_NTNAMES_PROPERTY = "rep:ntNames";

private static final String REP_ITEMNAMES_PROPERTY = "rep:itemNames";

private static final Set<String> MULTIVALUE_REP_PROPERTIES = ImmutableSet.of(
REP_NTNAMES_PROPERTY, REP_ITEMNAMES_PROPERTY, "rep:prefixes", "rep:current", "rep:globs",
"rep:subtrees", "sling:resourceTypes", "sling:resourceTypesWithDescendants"
);

private final String glob;

private final List<String> ntNames;
Expand All @@ -65,18 +78,25 @@ private Map<String, Object> notNullCopy(Map<String, Object> items) {

public Map<String, Value> getSingleValueRestrictions(ValueFactory valueFactory) throws ValueFormatException {
Map<String, Value> result = new HashMap<>();
addRestriction(valueFactory, result, "rep:glob", glob);
addRestriction(valueFactory, result, REP_GLOB_PROPERTY, glob);
for (Map.Entry<String, Object> entry : customRestrictions.entrySet()) {
if (entry.getValue() instanceof String) {
addRestriction(valueFactory, result, entry.getKey(), (String) entry.getValue());
if (!isMultivalue(entry)) {
String value;

if (entry.getValue() instanceof String) {
value = (String) entry.getValue();
} else {
value = ((List<String>) entry.getValue()).get(0);
}
addRestriction(valueFactory, result, entry.getKey(), value);
}
}
return result;
}

private void addRestriction(ValueFactory valueFactory, Map<String, Value> result, String key, String value) throws ValueFormatException {
if (StringUtils.isNotBlank(value)) {
if (key.equals("rep:glob")) {
if (REP_GLOB_PROPERTY.equals(key)) {
result.put(key, normalizeGlob(valueFactory));
} else {
result.put(key, valueFactory.createValue(value, PropertyType.NAME));
Expand All @@ -93,11 +113,17 @@ private Value normalizeGlob(ValueFactory valueFactory) {

public Map<String, Value[]> getMultiValueRestrictions(ValueFactory valueFactory) throws ValueFormatException {
Map<String, Value[]> result = new HashMap<>();
addRestrictions(valueFactory, result, "rep:ntNames", ntNames);
addRestrictions(valueFactory, result, "rep:itemNames", itemNames);
addRestrictions(valueFactory, result, REP_NTNAMES_PROPERTY, ntNames);
addRestrictions(valueFactory, result, REP_ITEMNAMES_PROPERTY, itemNames);
for (Map.Entry<String, Object> entry : customRestrictions.entrySet()) {
if (entry.getValue() instanceof List) {
addRestrictions(valueFactory, result, entry.getKey(), (List<String>) entry.getValue());
if (isMultivalue(entry)) {
List<String> values;
if (entry.getValue() instanceof String) {
values = Collections.singletonList((String) entry.getValue());
} else {
values = (List<String>) entry.getValue();
}
addRestrictions(valueFactory, result, entry.getKey(), values);
}
}
return result;
Expand All @@ -116,4 +142,16 @@ private Value[] createRestrictions(ValueFactory valueFactory, List<String> names
}
return values;
}

private boolean isMultivalue(Map.Entry<String, Object> entry) {
boolean result;
if (REP_GLOB_PROPERTY.equals(entry.getKey())) {
result = false;
} else if (MULTIVALUE_REP_PROPERTIES.contains(entry.getKey())) {
result = true;
} else {
result = entry.getValue() instanceof List;
}
return result;
}
}
17 changes: 11 additions & 6 deletions app/aem/core/src/main/antlr/ApmLang.g4
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ structure
;

structureEntry
: IDENTIFIER ':' structureValue
: structureKey ':' structureValue
;

structureKey
: IDENTIFIER
| STRING_LITERAL
;

structureValue
Expand Down Expand Up @@ -244,23 +249,23 @@ VARIABLE_IDENTIFIER
COMMENT
: '#' (~[\r\n] )* -> skip
;
fragment Digits
: [0-9]+
;
fragment LetterOrDigit
: Letter
| [0-9]
| Digit
;
fragment Letter
: [a-zA-Z_] // these are the "java letters" below 0x7F
| ~[\u0000-\u007F\uD800-\uDBFF] // covers all characters above 0x7F which are not a surrogate
| [\uD800-\uDBFF] [\uDC00-\uDFFF] // covers UTF-16 surrogate pairs encodings for U+10000 to U+10FFFF
;
fragment Digit
: [0-9]
;
fragment IdentifierPart
: Letter LetterOrDigit*
;
fragment VariablePart
: IdentifierPart (ARRAY_BEGIN Digits ARRAY_END)?
: IdentifierPart (ARRAY_BEGIN LetterOrDigit+ ARRAY_END)?
;
fragment PathPart
: '/' (~[\r\n\t ])+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package com.cognifide.apm.core.grammar.argument
import com.cognifide.apm.core.grammar.*
import com.cognifide.apm.core.grammar.antlr.ApmLangParser.*
import com.cognifide.apm.core.grammar.common.getIdentifier
import com.cognifide.apm.core.grammar.common.getKey
import com.cognifide.apm.core.grammar.common.getPath
import com.cognifide.apm.core.grammar.executioncontext.VariableHolder
import com.google.common.primitives.Ints
Expand Down Expand Up @@ -116,7 +117,7 @@ class ArgumentResolver(private val variableHolder: VariableHolder) {
}

override fun visitStructureEntry(ctx: StructureEntryContext): ApmType {
val key = ctx.IDENTIFIER().toString()
val key = getKey(ctx.structureKey())
return ctx.structureValue()
.children
?.map { child -> child.accept(this) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ fun getIdentifier(ctx: com.cognifide.apm.core.grammar.antlr.ApmLangParser.Variab
else -> throw ScriptExecutionException("Cannot resolve identifier")
}

fun getKey(ctx: com.cognifide.apm.core.grammar.antlr.ApmLangParser.StructureKeyContext) = when {
ctx.IDENTIFIER() != null -> ctx.IDENTIFIER().toString()
ctx.STRING_LITERAL() != null -> ctx.STRING_LITERAL().toPlainString()
else -> throw ScriptExecutionException("Cannot resolve key")
}

fun getPath(ctx: com.cognifide.apm.core.grammar.antlr.ApmLangParser.PathContext) = when {
ctx.STRING_LITERAL() != null -> ctx.STRING_LITERAL().toPlainString()
ctx.PATH_IDENTIFIER() != null -> ctx.PATH_IDENTIFIER().toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ class ScriptRunnerTest extends Specification {
"Executing command SHOW [[\"a\", \"b\"], [\"c\", \"d\"]]",
"Executing command SHOW [1, 2, 3]",
"Executing command SHOW [\"a\", \"b\", 1, 2]",
"Executing command SHOW {x: \"a\", y: 1, z: [\"c\", 1]}",
"Executing command SHOW {x: \"a\", y: 1, z: [\"c\", 1], t: \"t\"}",
"Executing command SHOW 1",
"Executing command SHOW 1",
"Executing command SHOW \"t\"",
"Executing command SHOW \"t\"",
"Executing command SHOW [3, \"ab\"]",
"Executing command SHOW [\"a\", \"b\", \"c\", \"d\", 1, 2]",
"Executing command SHOW [[\"a\", \"b\"], [\"c\", \"d\"]]"]
Expand Down
4 changes: 3 additions & 1 deletion app/aem/core/src/test/resources/define.apm
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ DEFINE tab [$tab1, $tab2]
DEFINE numbers [1, 2, 3]
DEFINE mixTab ['a', 'b', 1, 2]
DEFINE tab3 [['a', 'b'], ['c', 'd']]
DEFINE obj {x:'a', y:1, z:['c', 1]}
DEFINE obj {x:'a', y:1, z:['c', 1], 't':'t'}
DEFINE tabEnum [a, b, "c", "d", 1, 2]
DEFINE tab4 [1 + 2, "a" + "b"]
SHOW $a
Expand All @@ -55,6 +55,8 @@ SHOW $mixTab
SHOW $obj
SHOW $obj.z[1]
SHOW ${obj.z[1]}
SHOW ${obj.t}
SHOW ${obj[t]}
SHOW $tab4
SHOW $tabEnum
SHOW $tab3

0 comments on commit 0f1539b

Please sign in to comment.