Skip to content

Commit

Permalink
Implement has-variant conditional
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Herrera <[email protected]>
  • Loading branch information
Pablete1234 committed Oct 1, 2023
1 parent 3762751 commit 83f481d
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions core/src/main/java/tc/oc/pgm/map/MapFilePreprocessor.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package tc.oc.pgm.map;

import static tc.oc.pgm.api.map.MapSource.DEFAULT_VARIANT;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jdom2.Attribute;
Expand Down Expand Up @@ -45,6 +49,7 @@ public class MapFilePreprocessor {
private final List<MapInclude> includes;

private final Map<String, String> constants;
private final Set<String> variantIds;

public static Document getDocument(MapSource source, MapIncludeProcessor includes)
throws MapMissingException, IOException, JDOMException, InvalidXMLException {
Expand All @@ -57,6 +62,7 @@ private MapFilePreprocessor(MapSource source, MapIncludeProcessor includeProcess
this.variant = source.getVariantId();
this.includes = new ArrayList<>();
this.constants = new HashMap<>();
this.variantIds = new HashSet<>();
}

public Document getDocument()
Expand All @@ -67,6 +73,11 @@ public Document getDocument()
document.setBaseURI(source.getId());
}

variantIds.add(DEFAULT_VARIANT);
for (Element variant : document.getRootElement().getChildren("variant")) {
variantIds.add(XMLUtils.parseRequiredId(variant));
}

document.runWithoutVisitation(
() -> {
MapInclude global = includeProcessor.getGlobalInclude();
Expand Down Expand Up @@ -133,9 +144,14 @@ private List<Content> processIncludeElement(Element element) throws InvalidXMLEx

private List<Content> processConditional(Element el, boolean shouldContain)
throws InvalidXMLException {

Node node = Node.fromRequiredAttr(el, "variant", "has-variant");
List<String> filter = Arrays.asList(node.getValue().split("[\\s,]+"));

boolean contains =
Arrays.asList(Node.fromRequiredAttr(el, "variant").getValue().split("[\\s,]+"))
.contains(variant);
"variant".equals(node.getName())
? filter.contains(this.variant)
: filter.stream().anyMatch(variantIds::contains);

return contains == shouldContain ? el.cloneContent() : Collections.emptyList();
}
Expand Down

0 comments on commit 83f481d

Please sign in to comment.