Skip to content

Commit

Permalink
introduced URL_ENCODE flag
Browse files Browse the repository at this point in the history
  • Loading branch information
pdbro2k committed May 16, 2024
1 parent aa4572a commit 7f51b76
Showing 1 changed file with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
*/
package org.adwmainz.da.extensions.askmore.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -35,6 +38,7 @@ public class AskMoreAnnotationParser {
+ " ?: ?\\(" + "(.*?)" /* Options */ + "\\)" + "((![^$]+)*)" /* Flags */ + "\\$\\$";
protected static final String DEFAULT_VALUE_PATTERN = "DEFAULT\\(\"" + "(.*?)" /* value */ + "\"\\)";
protected static final String IS_EDITABLE_PATTERN = "EDITABLE";
protected static final String URL_ENCODE_PATTERN = "URL_ENCODE";
protected static final String REGEX_FLAG_PATTERN = "REGEX\\(\"" + "(.*?)" /* regex */ + "\"\\)";
protected static final String CONTAINS_WHITESPACE_PATTERN = ".*?\\s+.*?";

Expand Down Expand Up @@ -88,6 +92,8 @@ public static Map<String, BasicInputField<String>> createDialogModel(List<String
}
else if (flag.matches(IS_EDITABLE_PATTERN))
isEditable = true; // set editable
else if (flag.matches(URL_ENCODE_PATTERN))
;
else
inputVerifiers.add(createInputVerifier(flag)); // create input verifier
}
Expand Down Expand Up @@ -163,7 +169,21 @@ public static String replaceAnnotations(String annotatedText, List<String> askMo
label = StringUtils.removeEscapedQuotes(label);

// replace annotation with user input
annotatedText = annotatedText.replace(askMoreAnnotation, userInput.get(label));
String input = userInput.get(label);
String flags = matcher.group(3);
if (flags.length() > 1) {
flags = flags.substring(1); // remove first exclamation mark
for (String flag: flags.split("!")) {
if (flag.matches(URL_ENCODE_PATTERN)) {
try {
input = URLEncoder.encode(input, StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException(e.getCause());
}
}
}
}
annotatedText = annotatedText.replace(askMoreAnnotation, input);
}
}
return annotatedText;
Expand All @@ -188,6 +208,7 @@ public static String getDescription() {
+ " inserted and the !DEFAULT() flag for a preselection which must always reference a real value: "
+ "e.g. $$\"LABEL4\":(\"A\", \"REAL_B\"|\"RENDERED_B\")!DEFAULT(\"REAL_B\")$$ is valid)\n"
+ "- $$\"LABEL5\":(\"A\", \"B\")!DEFAULT(\"B\")!EDITABLE$$ creates an editable combo box with the label LABEL5\n"
+ "You can also use the !URL_ENCODE flag to let the input be URL encoded.\n"
+ "Besides the !DEFAULT() and !EDITABLE flags you may also add the following restriction flags to any annotation:\n"
+ "- !NO_XML for text input without the chars " + Arrays.toString(XMLUtils.getSpecialChars()) + "\n"
+ "- !NO_SPACE for text input without whitespace\n"
Expand Down

0 comments on commit 7f51b76

Please sign in to comment.