A template is constructed from a charstream. Everything that is not in "<?" to "?>" will be outputted directly. Text in these markers will be interpreted is template scripting syntax. The following strings are valid:
-- <?=$variblename?> will output "variablename".
+General remarks:
+- $variablename: a variablename matches the regex [a-zA-Z0-9_-]
+Syntax:
+- <?=$variablename?> will output "variablename".
if "variablename" is an Outputable output this thing recursively.
else turn it into a String (Object.toString()) and output it.
+
+- <?=$!variablename?> will output the variable "variablename" but not HTML-escaped
- <?=_This is free Text.?> will translate "This is free Text." into the users language, (escaped) and output it.
-
-- <?=s,$var1,$var2,$var3,...,$varn,This %s is, %s free.?>
- Translate the last string. Output $var1,...,$varn replaced into the %s positions in the translated string.
+ Text may not contain "?>".
+ If the text contains "$" or "!'" it is interpreted as "advanced replacement".
+ - ${variablename} is interpreted as "output this variable at this point"
+ - !'literal content' output "literal content" here and do not translate or escape. (literal content may not contain any of: {}'$ )
+ Then the whole text than also may not contain "{" and "}".
-- <?=$!variablename?> will output the variable "variablename" but not HTML-escaped
-- <?=s,$!variablename,My %s text?> will insert the variable "variablename" into the translated text but not HTML-escaped
-- <?=s,"some data",My %s text?> will insert "some data" into the translated text
-- <?=s,!"some data",My %s text?> will insert "some data" into the translated text literally (not HTML-escaped)
-
- <? if($variable) { ?> ... <? } ?>
Output/execute the text until "<? } ?>" only if $variable is Boolean.TRUE (<=> !Boolean.FALSE) or not null.
- <? if(...) { ?> ... <? } else { ?> ... <? } ?>
package org.cacert.gigi.output.template;
import java.io.PrintWriter;
+import java.util.LinkedList;
import java.util.List;
import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import org.cacert.gigi.localisation.Language;
import org.cacert.gigi.util.HTMLEncoder;
this.store = store.toArray(new String[store.size()]);
}
+ private static final String VARIABLE = "\\$!?\\{[a-zA-Z0-9_-]+\\}";
+
+ private static final Pattern processingInstruction = Pattern.compile("(" + VARIABLE + ")|(!'[^{}'\\$]*)'");
+
+ public SprintfCommand(String content) {
+ StringBuffer raw = new StringBuffer();
+ List<String> var = new LinkedList<String>();
+ int counter = 0;
+ Matcher m = processingInstruction.matcher(content);
+ int last = 0;
+ while (m.find()) {
+ raw.append(content.substring(last, m.start()));
+ String group = null;
+ if ((group = m.group(1)) != null) {
+ var.add(group);
+ } else if ((group = m.group(2)) != null) {
+ var.add(group);
+ } else {
+ throw new Error("Regex is broken??");
+ }
+ last = m.end();
+ raw.append("{" + (counter++) + "}");
+ }
+ raw.append(content.substring(last));
+ text = raw.toString();
+ store = var.toArray(new String[var.size()]);
+ }
+
+ private final Pattern replacant = Pattern.compile("\\{([0-9]+)\\}");
+
@Override
public void output(PrintWriter out, Language l, Map<String, Object> vars) {
- String[] parts = l.getTranslation(text).split("%s", -1);
- String[] myvars = store;
- out.print(HTMLEncoder.encodeHTML(parts[0]));
- for (int j = 1; j < parts.length; j++) {
- String var = myvars[j - 1];
+ String parts = l.getTranslation(text);
+ Matcher m = replacant.matcher(parts);
+ int pos = 0;
+ while (m.find()) {
+ out.print(HTMLEncoder.encodeHTML(parts.substring(pos, m.start())));
+ String var = store[Integer.parseInt(m.group(1))];
if (var.startsWith("$!")) {
- Template.outputVar(out, l, vars, myvars[j - 1].substring(2), true);
- } else if (var.startsWith("!\"")) {
+ Template.outputVar(out, l, vars, var.substring(3, var.length() - 1), true);
+ } else if (var.startsWith("!'")) {
out.print(var.substring(2));
- } else if (var.startsWith("\"")) {
- out.print(HTMLEncoder.encodeHTML(var.substring(1)));
+ } else if (var.startsWith("$")) {
+ Template.outputVar(out, l, vars, var.substring(2, var.length() - 1), false);
} else {
- Template.outputVar(out, l, vars, myvars[j - 1].substring(1), false);
+ throw new Error("Processing error in template.");
}
- out.print(HTMLEncoder.encodeHTML(parts[j]));
+ pos = m.end();
+
}
+ out.print(HTMLEncoder.encodeHTML(parts.substring(pos)));
}
}
}
public TemplateBlock getBlock(String reqType) {
- if (endType == null && reqType == null)
+ if (endType == null && reqType == null) {
return block;
+ }
if (endType == null || reqType == null) {
throw new Error("Invalid block type: " + endType);
}
- if (endType.equals(reqType))
+ if (endType.equals(reqType)) {
return block;
+ }
throw new Error("Invalid block type: " + endType);
}
}
private Outputable parseCommand(String s2) {
if (s2.startsWith("=_")) {
final String raw = s2.substring(2);
- return new TranslateCommand(raw);
+ if ( !s2.contains("$") && !s2.contains("!'")) {
+ return new TranslateCommand(raw);
+ } else {
+ return new SprintfCommand(raw);
+ }
} else if (s2.startsWith("=$")) {
final String raw = s2.substring(2);
return new OutputVariableCommand(raw);
- } else if (s2.startsWith("=s,")) {
- String command = s2.substring(3);
- final LinkedList<String> store = new LinkedList<String>();
- while (command.startsWith("$") || command.startsWith("\"") || command.startsWith("!\"")) {
- int idx;
- if (command.startsWith("\"") || command.startsWith("!\"")) {
- idx = command.indexOf("\"", command.charAt(0) == '!' ? 2 : 1) + 1;
- store.add(command.substring(0, idx - 1));
- } else {
- idx = command.indexOf(",");
- store.add(command.substring(0, idx));
- }
- command = command.substring(idx + 1);
- }
- final String text = command;
- return new SprintfCommand(text, store);
} else {
- System.out.println("Unknown processing instruction: " + s2);
+ throw new Error("Unknown processing instruction: " + s2);
}
- return null;
}
@Override
vars.put("var", "val\">");
vars.put("var2", "val3<\"");
vars.put("var3", "val4>");
- assertEquals("This val"> textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=s,$var,$var2,$var3,This %s text?>l"));
- assertEquals("This val\"> textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=s,$!var,$!var2,$!var3,This %s text?>l"));
+ assertEquals("This val"> textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=_This ${var} text?>l"));
+ assertEquals("This val\"> textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=_This $!{var} text?>l"));
- assertEquals("This val"> val3<" the val4> textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=s,$var,$var2,$var3,This %s %s the %s text?>l"));
- assertEquals("This val\"> val3<\" the val4> textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=s,$!var,$!var2,$!var3,This %s %s the %s text?>l"));
+ assertEquals("This val"> val3<" the val4> textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=_This ${var} ${var2} the ${var3} text?>l"));
+ assertEquals("This val\"> val3<\" the val4> textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=_This $!{var} $!{var2} the $!{var3} text?>l"));
- assertEquals("This blargh<>!, <>! textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=s,\"blargh<>!\",!\"<>!\",This %s, %s text?>l"));
- assertEquals("This blargh<>!, <>!l", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=s,\"blargh<>!\",!\"<>!\",This %s, %s?>l"));
+ assertEquals("This blargh<>!, <>! textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=_This !'blargh<>!', !'<>! 'text?>l"));
+ assertEquals("This blargh<>!, <>!l", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=_This !'blargh<>!', !'<>!'?>l"));
}
@Test