]> WPIA git - gigi.git/commitdiff
add: documentation to output classes
authorFelix Dörre <felix@dogcraft.de>
Mon, 30 May 2016 12:43:08 +0000 (14:43 +0200)
committerFelix Dörre <felix@dogcraft.de>
Mon, 30 May 2016 13:22:44 +0000 (15:22 +0200)
Change-Id: I1e68ee563fbd14a0ed8afb1840f9212691e063d7

13 files changed:
src/org/cacert/gigi/output/template/ForeachStatement.java
src/org/cacert/gigi/output/template/Form.java
src/org/cacert/gigi/output/template/IfStatement.java
src/org/cacert/gigi/output/template/IterableDataset.java
src/org/cacert/gigi/output/template/OutputVariableCommand.java
src/org/cacert/gigi/output/template/Outputable.java
src/org/cacert/gigi/output/template/OutputableArrayIterable.java
src/org/cacert/gigi/output/template/Scope.java
src/org/cacert/gigi/output/template/SprintfCommand.java
src/org/cacert/gigi/output/template/Template.java
src/org/cacert/gigi/output/template/Translatable.java
src/org/cacert/gigi/output/template/TranslateCommand.java
src/org/cacert/gigi/pages/Verify.java

index 8c12122be098374ecb376b69b2e8aae876676191..4c8b1027b5f7dfb3df420b309fffe699fad94d26 100644 (file)
@@ -7,12 +7,24 @@ import java.util.Map;
 
 import org.cacert.gigi.localisation.Language;
 
 
 import org.cacert.gigi.localisation.Language;
 
+/**
+ * Outputs an {@link Outputable} multiple times based on a given
+ * {@link IterableDataset}.
+ */
 public final class ForeachStatement implements Translatable {
 
     private final String variable;
 
     private final TemplateBlock body;
 
 public final class ForeachStatement implements Translatable {
 
     private final String variable;
 
     private final TemplateBlock body;
 
+    /**
+     * Creates a new {@link ForeachStatement}.
+     * 
+     * @param variable
+     *            the variable to take the {@link IterableDataset} from.
+     * @param body
+     *            the body to output multiple times.
+     */
     public ForeachStatement(String variable, TemplateBlock body) {
         this.variable = variable;
         this.body = body;
     public ForeachStatement(String variable, TemplateBlock body) {
         this.variable = variable;
         this.body = body;
index ed1a86689cd261b95aa03807e56eb40d8dd11f29..366d31c5913684a032eaf5b379996978a48673bd 100644 (file)
@@ -13,6 +13,9 @@ import org.cacert.gigi.localisation.Language;
 import org.cacert.gigi.pages.Page;
 import org.cacert.gigi.util.RandomToken;
 
 import org.cacert.gigi.pages.Page;
 import org.cacert.gigi.util.RandomToken;
 
+/**
+ * A generic HTML-form that handles CSRF-token creation.
+ */
 public abstract class Form implements Outputable {
 
     public static final String CSRF_FIELD = "csrf";
 public abstract class Form implements Outputable {
 
     public static final String CSRF_FIELD = "csrf";
@@ -21,10 +24,24 @@ public abstract class Form implements Outputable {
 
     private final String action;
 
 
     private final String action;
 
+    /**
+     * Creates a new {@link Form}.
+     * 
+     * @param hsr
+     *            the request to register the form against.
+     */
     public Form(HttpServletRequest hsr) {
         this(hsr, null);
     }
 
     public Form(HttpServletRequest hsr) {
         this(hsr, null);
     }
 
+    /**
+     * Creates a new {@link Form}.
+     * 
+     * @param hsr
+     *            the request to register the form against.
+     * @param action
+     *            the target path where the form should be submitted
+     */
     public Form(HttpServletRequest hsr, String action) {
         csrf = RandomToken.generateToken(32);
         this.action = action;
     public Form(HttpServletRequest hsr, String action) {
         csrf = RandomToken.generateToken(32);
         this.action = action;
@@ -32,6 +49,17 @@ public abstract class Form implements Outputable {
         hs.setAttribute("form/" + getClass().getName() + "/" + csrf, this);
     }
 
         hs.setAttribute("form/" + getClass().getName() + "/" + csrf, this);
     }
 
+    /**
+     * Update the forms internal state based on submitted data.
+     * 
+     * @param out
+     *            the stream to the user.
+     * @param req
+     *            the request to take the initial data from
+     * @return true, iff the form succeeded an the user should be redirected.
+     * @throws GigiApiException
+     *             if internal operations went wrong.
+     */
     public abstract boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException;
 
     protected String getCsrfFieldName() {
     public abstract boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException;
 
     protected String getCsrfFieldName() {
@@ -52,9 +80,19 @@ public abstract class Form implements Outputable {
         out.println("'></form>");
     }
 
         out.println("'></form>");
     }
 
+    /**
+     * Outputs the forms contents.
+     * 
+     * @param out
+     *            Stream to the user
+     * @param l
+     *            {@link Language} to translate text to
+     * @param vars
+     *            Variables supplied from the outside.
+     */
     protected abstract void outputContent(PrintWriter out, Language l, Map<String, Object> vars);
 
     protected abstract void outputContent(PrintWriter out, Language l, Map<String, Object> vars);
 
-    boolean failed;
+    private boolean failed;
 
     protected void outputError(PrintWriter out, ServletRequest req, String text, Object... contents) {
         if ( !failed) {
 
     protected void outputError(PrintWriter out, ServletRequest req, String text, Object... contents) {
         if ( !failed) {
@@ -91,6 +129,17 @@ public abstract class Form implements Outputable {
         return csrf;
     }
 
         return csrf;
     }
 
+    /**
+     * Re-fetches a form e.g. when a Post-request is received.
+     * 
+     * @param req
+     *            the request that is directed to the form.
+     * @param target
+     *            the {@link Class} of the expected form
+     * @return the form where this request is directed to.
+     * @throws CSRFException
+     *             if no CSRF-token is found or the token is wrong.
+     */
     public static <T extends Form> T getForm(HttpServletRequest req, Class<T> target) throws CSRFException {
         String csrf = req.getParameter(CSRF_FIELD);
         if (csrf == null) {
     public static <T extends Form> T getForm(HttpServletRequest req, Class<T> target) throws CSRFException {
         String csrf = req.getParameter(CSRF_FIELD);
         if (csrf == null) {
index 29cfc768f2ac674706a367325ef5eb6aa90b6dea..f2248bcc72fd3a3987b87cc22f7192d5c2883bdc 100644 (file)
@@ -6,6 +6,10 @@ import java.util.Map;
 
 import org.cacert.gigi.localisation.Language;
 
 
 import org.cacert.gigi.localisation.Language;
 
+/**
+ * One ore two {@link Outputable}s that are emitted conditionally if a given
+ * variable is neither <code>null</code> nor {@link Boolean#FALSE}.
+ */
 public final class IfStatement implements Translatable {
 
     private final String variable;
 public final class IfStatement implements Translatable {
 
     private final String variable;
@@ -14,12 +18,30 @@ public final class IfStatement implements Translatable {
 
     private final TemplateBlock iffalse;
 
 
     private final TemplateBlock iffalse;
 
+    /**
+     * Creates a new {@link IfStatement} with an empty else-part.
+     * 
+     * @param variable
+     *            the variable to check
+     * @param body
+     *            the body to emit conditionally.
+     */
     public IfStatement(String variable, TemplateBlock body) {
         this.variable = variable;
         this.iftrue = body;
         this.iffalse = null;
     }
 
     public IfStatement(String variable, TemplateBlock body) {
         this.variable = variable;
         this.iftrue = body;
         this.iffalse = null;
     }
 
+    /**
+     * Creates a new {@link IfStatement} with an else-block.
+     * 
+     * @param variable
+     *            the variable to check
+     * @param iftrue
+     *            the block to emit if the check succeeds.
+     * @param iffalse
+     *            the block to emit if the check fails
+     */
     public IfStatement(String variable, TemplateBlock iftrue, TemplateBlock iffalse) {
         this.variable = variable;
         this.iftrue = iftrue;
     public IfStatement(String variable, TemplateBlock iftrue, TemplateBlock iffalse) {
         this.variable = variable;
         this.iftrue = iftrue;
index e95dce8f07a4eff86c4319e4e5619e822af4fd46..8c96727b48f2459067bebb840d45fab56979ff61 100644 (file)
@@ -5,7 +5,8 @@ import java.util.Map;
 import org.cacert.gigi.localisation.Language;
 
 /**
 import org.cacert.gigi.localisation.Language;
 
 /**
- * Represents some kind of data, that may be iterated over in a template.
+ * Represents some kind of data, that may be iterated over in a template using
+ * the <code>foreach</code> statement.
  */
 public interface IterableDataset {
 
  */
 public interface IterableDataset {
 
index 908a0377ad419e70caf594ee4fbe3d343578f27b..5dce3aed211c1c45452d96929e6e0c3c9484dabe 100644 (file)
@@ -6,12 +6,22 @@ import java.util.Map;
 
 import org.cacert.gigi.localisation.Language;
 
 
 import org.cacert.gigi.localisation.Language;
 
+/**
+ * Emits a variable.
+ */
 public final class OutputVariableCommand implements Translatable {
 
     private final String raw;
 
     private final boolean unescaped;
 
 public final class OutputVariableCommand implements Translatable {
 
     private final String raw;
 
     private final boolean unescaped;
 
+    /**
+     * Creates a new OutputVariableCommand.
+     * 
+     * @param raw
+     *            the variable to emit. If starting with <code>!</code> the
+     *            variable is emitted non-HTML-escaped.
+     */
     public OutputVariableCommand(String raw) {
         if (raw.charAt(0) == '!') {
             unescaped = true;
     public OutputVariableCommand(String raw) {
         if (raw.charAt(0) == '!') {
             unescaped = true;
index 5716162a8c5e32b07aec3cb948c7e425bdfc3e32..02fea41193086bf4c2af3fa1a1ab5dbdbe644208 100644 (file)
@@ -5,7 +5,20 @@ import java.util.Map;
 
 import org.cacert.gigi.localisation.Language;
 
 
 import org.cacert.gigi.localisation.Language;
 
+/**
+ * An object that is outputable to the user normally in an HTML-page.
+ */
 public interface Outputable {
 
 public interface Outputable {
 
+    /**
+     * Writes this object's content to the given output stream.
+     * 
+     * @param out
+     *            the PrintWriter to the user.
+     * @param l
+     *            the {@link Language} to translate localizable strings to.
+     * @param vars
+     *            a map of variable assignments for this template.
+     */
     public void output(PrintWriter out, Language l, Map<String, Object> vars);
 }
     public void output(PrintWriter out, Language l, Map<String, Object> vars);
 }
index 5a78e73bfa56a40eae55f9ef07287c4b9816e408..498954074de111f942da3cf97b9b8a7bf5f0d8cd 100644 (file)
@@ -4,14 +4,26 @@ import java.util.Map;
 
 import org.cacert.gigi.localisation.Language;
 
 
 import org.cacert.gigi.localisation.Language;
 
+/**
+ * Generic implementation of {@link IterableDataset} that is fed by an array.
+ */
 public class OutputableArrayIterable implements IterableDataset {
 
 public class OutputableArrayIterable implements IterableDataset {
 
-    Object[] content;
+    private Object[] content;
 
 
-    String targetName;
+    private String targetName;
 
 
-    int index = 0;
+    private int index = 0;
 
 
+    /**
+     * Creates a new {@link OutputableArrayIterable}.
+     * 
+     * @param content
+     *            the objects to be iterated over.
+     * @param targetName
+     *            the variable where the contents of the array to be put in the
+     *            loop.
+     */
     public OutputableArrayIterable(Object[] content, String targetName) {
         this.content = content;
         this.targetName = targetName;
     public OutputableArrayIterable(Object[] content, String targetName) {
         this.content = content;
         this.targetName = targetName;
index 9a15b3a4c3ea8db8c218ff887d2d9aac80bdb322..38e25ebc5809f5dfe7e78662e005fbe41c20d7b4 100644 (file)
@@ -6,12 +6,24 @@ import java.util.Map;
 
 import org.cacert.gigi.localisation.Language;
 
 
 import org.cacert.gigi.localisation.Language;
 
+/**
+ * Builds a variable scope around another {@link Outputable}, statically filling
+ * variables.
+ */
 public class Scope implements Outputable {
 
     private Map<String, Object> vars;
 
     private Outputable out;
 
 public class Scope implements Outputable {
 
     private Map<String, Object> vars;
 
     private Outputable out;
 
+    /**
+     * Creates a new {@link Scope}.
+     * 
+     * @param out
+     *            the enclosed {@link Outputable}
+     * @param vars
+     *            the variables to assign in the inner scope.
+     */
     public Scope(Outputable out, Map<String, Object> vars) {
         this.out = out;
         this.vars = vars;
     public Scope(Outputable out, Map<String, Object> vars) {
         this.out = out;
         this.vars = vars;
index 385da25cae38a60f1b61a80ce1a39a6ee896a139..cbd79e57a14ebfface943921c8b1b0765d568406 100644 (file)
@@ -13,12 +13,24 @@ import java.util.regex.Pattern;
 import org.cacert.gigi.localisation.Language;
 import org.cacert.gigi.util.HTMLEncoder;
 
 import org.cacert.gigi.localisation.Language;
 import org.cacert.gigi.util.HTMLEncoder;
 
+/**
+ * A pattern that is to be translated before variables are inserted.
+ */
 public final class SprintfCommand implements Translatable {
 
     private final String text;
 
     private final String[] store;
 
 public final class SprintfCommand implements Translatable {
 
     private final String text;
 
     private final String[] store;
 
+    /**
+     * Creates a new SprintfCommand based on its pre-parsed contents
+     * 
+     * @param text
+     *            a string with <code>{0},{1},..</code> as placeholders.
+     * @param store
+     *            the data to put into the placeholders: ${var}, $!{var},
+     *            !'plain'.
+     */
     public SprintfCommand(String text, List<String> store) {
         this.text = text;
         this.store = store.toArray(new String[store.size()]);
     public SprintfCommand(String text, List<String> store) {
         this.text = text;
         this.store = store.toArray(new String[store.size()]);
@@ -28,7 +40,13 @@ public final class SprintfCommand implements Translatable {
 
     private static final Pattern processingInstruction = Pattern.compile("(" + VARIABLE + ")|(!'[^{}'\\$]*)'");
 
 
     private static final Pattern processingInstruction = Pattern.compile("(" + VARIABLE + ")|(!'[^{}'\\$]*)'");
 
-    public SprintfCommand(String content) {
+    /**
+     * Creates a new SprintfCommand that is parsed as from template source.
+     * 
+     * @param content
+     *            the part from the template that is to be parsed
+     */
+    protected SprintfCommand(String content) {
         StringBuffer raw = new StringBuffer();
         List<String> var = new LinkedList<String>();
         int counter = 0;
         StringBuffer raw = new StringBuffer();
         List<String> var = new LinkedList<String>();
         int counter = 0;
@@ -82,6 +100,17 @@ public final class SprintfCommand implements Translatable {
         s.add(text);
     }
 
         s.add(text);
     }
 
+    /**
+     * Creates a simple {@link SprintfCommand} wrapped in a {@link Scope} to fit
+     * in now constant variables into this template.
+     * 
+     * @param msg
+     *            the message (to be translated) with <code>{0},{1},...</code>
+     *            as placeholders.
+     * @param vars
+     *            the variables to put into the placeholders.
+     * @return the constructed {@link Outputable}
+     */
     public static Outputable createSimple(String msg, String... vars) {
         HashMap<String, Object> scope = new HashMap<>();
         String[] store = new String[vars.length];
     public static Outputable createSimple(String msg, String... vars) {
         HashMap<String, Object> scope = new HashMap<>();
         String[] store = new String[vars.length];
index 1705159514c02846432da50e6e4da76569f41861..cc56baf813b8e731c855c71cf0213de912f0651c 100644 (file)
@@ -22,6 +22,9 @@ import org.cacert.gigi.output.DateSelector;
 import org.cacert.gigi.util.DayDate;
 import org.cacert.gigi.util.HTMLEncoder;
 
 import org.cacert.gigi.util.DayDate;
 import org.cacert.gigi.util.HTMLEncoder;
 
+/**
+ * Represents a loaded template file.
+ */
 public class Template implements Outputable {
 
     private static class ParseResult {
 public class Template implements Outputable {
 
     private static class ParseResult {
@@ -63,6 +66,14 @@ public class Template implements Outputable {
 
     private static final Pattern ELSE_PATTERN = Pattern.compile(" ?\\} ?else ?\\{ ?");
 
 
     private static final Pattern ELSE_PATTERN = Pattern.compile(" ?\\} ?else ?\\{ ?");
 
+    /**
+     * Creates a new template by parsing the contents from the given URL. This
+     * constructor will fail on syntax error. When the URL points to a file,
+     * {@link File#lastModified()} is monitored for changes of the template.
+     * 
+     * @param u
+     *            the URL to load the template from. UTF-8 is chosen as charset.
+     */
     public Template(URL u) {
         try {
             Reader r = new InputStreamReader(u.openStream(), "UTF-8");
     public Template(URL u) {
         try {
             Reader r = new InputStreamReader(u.openStream(), "UTF-8");
@@ -81,6 +92,13 @@ public class Template implements Outputable {
         }
     }
 
         }
     }
 
+    /**
+     * Creates a new template by parsing the contents from the given reader.
+     * This constructor will fail on syntax error.
+     * 
+     * @param r
+     *            the Reader containing the data.
+     */
     public Template(Reader r) {
         try {
             data = parse(r).getBlock(null);
     public Template(Reader r) {
         try {
             data = parse(r).getBlock(null);
index b15ffb9354ca52aa421221b40eea84089dbea6d2..4df11474abbbe917d07172d76bffb87771e8b15d 100644 (file)
@@ -2,7 +2,17 @@ package org.cacert.gigi.output.template;
 
 import java.util.Collection;
 
 
 import java.util.Collection;
 
+/**
+ * An {@link Outputable} that wants to give static strings to translation
+ * collection.
+ */
 public interface Translatable extends Outputable {
 
 public interface Translatable extends Outputable {
 
+    /**
+     * Adds all static translation Strings to the given {@link Collection}.
+     * 
+     * @param s
+     *            the {@link Collection} to add the Strings to.
+     */
     public void addTranslations(Collection<String> s);
 }
     public void addTranslations(Collection<String> s);
 }
index 9da43524c6b6a67b6e7bb659ac726dde8b44d9e2..d291e9d414b3d4ce0111b3edf1c1ff85963b9294 100644 (file)
@@ -7,10 +7,19 @@ import java.util.Map;
 import org.cacert.gigi.localisation.Language;
 import org.cacert.gigi.util.HTMLEncoder;
 
 import org.cacert.gigi.localisation.Language;
 import org.cacert.gigi.util.HTMLEncoder;
 
+/**
+ * Wraps a String that needs to be translated before it is printed to the user.
+ */
 public final class TranslateCommand implements Translatable {
 
     private final String raw;
 
 public final class TranslateCommand implements Translatable {
 
     private final String raw;
 
+    /**
+     * Creates a new TranslateCommand that wraps the given String.
+     * 
+     * @param raw
+     *            the String to be translated.
+     */
     public TranslateCommand(String raw) {
         this.raw = raw;
     }
     public TranslateCommand(String raw) {
         this.raw = raw;
     }
@@ -20,6 +29,11 @@ public final class TranslateCommand implements Translatable {
         out.print(HTMLEncoder.encodeHTML(l.getTranslation(raw)));
     }
 
         out.print(HTMLEncoder.encodeHTML(l.getTranslation(raw)));
     }
 
+    /**
+     * Gets the raw, untranslated String.
+     * 
+     * @return the raw, untranslated String.
+     */
     public String getRaw() {
         return raw;
     }
     public String getRaw() {
         return raw;
     }
index 0c8485420208067b74aaeae436e6e972da5d1b96..0f88fe4048cd467b208fe477b8d92bce4bae2cfe 100644 (file)
@@ -2,6 +2,7 @@ package org.cacert.gigi.pages;
 
 import java.io.IOException;
 import java.io.PrintWriter;
 
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
 
 import java.util.HashMap;
 import java.util.Map;
 
@@ -18,9 +19,9 @@ import org.cacert.gigi.output.template.SprintfCommand;
 
 public class Verify extends Page {
 
 
 public class Verify extends Page {
 
-    private static final SprintfCommand emailAddressVerified = new SprintfCommand("Email address ${subject} verified");
+    private static final SprintfCommand emailAddressVerified = new SprintfCommand("Email address {0} verified", Arrays.asList("${subject}"));
 
 
-    private static final SprintfCommand domainVerified = new SprintfCommand("Domain ${subject} verified");
+    private static final SprintfCommand domainVerified = new SprintfCommand("Domain {0} verified", Arrays.asList("${subject}"));
 
     private class VerificationForm extends Form {
 
 
     private class VerificationForm extends Form {