Skip to content

Commit

Permalink
Merge pull request #808 from grzesiek2010/parseAttributes
Browse files Browse the repository at this point in the history
Improved parsing attributes in tests
  • Loading branch information
seadowg authored Dec 3, 2024
2 parents 3d3efac + d2dd309 commit 96440da
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
19 changes: 8 additions & 11 deletions src/main/java/org/javarosa/test/XFormsElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.regex.Pattern;

import static java.util.Arrays.asList;
import static java.util.Collections.emptyMap;
Expand All @@ -42,7 +43,12 @@ static Map<String, String> parseAttributes(String name) {
if (!name.contains(" "))
return emptyMap();
Map<String, String> attributes = new HashMap<>();
String[] words = name.split(" ");

// Regex to split on spaces, ignoring spaces inside quoted text
final String SPACE_OUTSIDE_QUOTES_REGEX = " (?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)";
Pattern spaceOutsideQuotesPattern = Pattern.compile(SPACE_OUTSIDE_QUOTES_REGEX);
String[] words = spaceOutsideQuotesPattern.split(name);

for (String word : asList(words).subList(1, words.length)) {
String[] parts = word.split("(?<!\\))=(\"|')", 2);
attributes.put(parts[0], parts[1].substring(0, parts[1].length() - 1));
Expand Down Expand Up @@ -135,16 +141,7 @@ static XFormsElement select1(String ref, XFormsElement... children) {
}

static XFormsElement select1Dynamic(String ref, String nodesetRef) {
XFormsElement value = t("value ref=\"value\"");
XFormsElement label = t("label ref=\"label\"");

HashMap<String, String> itemsetAttributes = new HashMap<>();
itemsetAttributes.put("nodeset", nodesetRef);
TagXFormsElement itemset = new TagXFormsElement("itemset", itemsetAttributes, asList(value, label));

HashMap<String, String> select1Attributes = new HashMap<>();
select1Attributes.put("ref", ref);
return new TagXFormsElement("select1", select1Attributes, asList(itemset));
return select1Dynamic(ref, nodesetRef, "value", "label");
}

static XFormsElement select1Dynamic(String ref, String nodesetRef, String valueRef, String labelRef) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ public void fillTemplateString_resolvesRelativeReferences_inItext() throws IOExc
title("output with relative ref in translation"),
model(
t("itext", t("translation lang=\"Français\"",
t("text id=\"/data/repeat/position_in_label:label",
t("text id=\"/data/repeat/position_in_label:label\"",
t("value", "Position: <output value=\"../position\"/>"))
)),
mainInstance(t("data id=\"relative-output\"",
Expand Down

0 comments on commit 96440da

Please sign in to comment.