Refactor main template (+ fix some strange regex bug)
authorFelix Dörre <felix@dogcraft.de>
Thu, 3 Jul 2014 19:21:09 +0000 (21:21 +0200)
committerFelix Dörre <felix@dogcraft.de>
Thu, 3 Jul 2014 19:21:09 +0000 (21:21 +0200)
src/org/cacert/gigi/Gigi.java
src/org/cacert/gigi/Gigi.templ [moved from templates/base.html with 94% similarity]
src/org/cacert/gigi/output/Template.java

index fa259c96c77cbf99835c8df72e374c863935bd3e..4b6b382699702b775cd25dd3e390f85495772015 100644 (file)
@@ -1,12 +1,11 @@
 package org.cacert.gigi;
 
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.io.PrintWriter;
 import java.util.Calendar;
 import java.util.HashMap;
+import java.util.Map;
 import java.util.Properties;
 
 import javax.servlet.ServletException;
@@ -17,6 +16,8 @@ import javax.servlet.http.HttpSession;
 
 import org.cacert.gigi.database.DatabaseConnection;
 import org.cacert.gigi.email.EmailProvider;
+import org.cacert.gigi.output.Outputable;
+import org.cacert.gigi.output.Template;
 import org.cacert.gigi.pages.LoginPage;
 import org.cacert.gigi.pages.MainPage;
 import org.cacert.gigi.pages.Page;
@@ -29,13 +30,12 @@ import org.cacert.gigi.pages.account.MyDetails;
 import org.cacert.gigi.pages.main.RegisterPage;
 import org.cacert.gigi.pages.wot.AssurePage;
 import org.cacert.gigi.util.ServerConstants;
-import org.eclipse.jetty.util.log.Log;
 
 public class Gigi extends HttpServlet {
        public static final String LOGGEDIN = "loggedin";
        public static final String USER = "user";
        private static final long serialVersionUID = -6386785421902852904L;
-       private String[] baseTemplate;
+       private Template baseTemplate;
        private HashMap<String, Page> pages = new HashMap<String, Page>();
 
        public Gigi(Properties conf) {
@@ -55,23 +55,15 @@ public class Gigi extends HttpServlet {
                pages.put(MailOverview.DEFAULT_PATH, new MailOverview(
                                "My email addresses"));
                pages.put(MailAdd.DEFAULT_PATH, new MailAdd("Add new email"));
-               String templ = "";
-               try (BufferedReader reader = new BufferedReader(new InputStreamReader(
-                               new FileInputStream(new File("templates/base.html"))))) {
-                       String tmp;
-                       while ((tmp = reader.readLine()) != null) {
-                               templ += tmp + "\n";
-                       }
-                       baseTemplate = templ.split("\\$content\\$");
-               } catch (Exception e) {
-                       Log.getLogger(Gigi.class).warn("Error loading template!", e);
-               }
+               baseTemplate = new Template(new InputStreamReader(
+                               Gigi.class.getResourceAsStream("Gigi.templ")));
                super.init();
 
        }
        @Override
-       protected void service(HttpServletRequest req, HttpServletResponse resp)
-                       throws ServletException, IOException {
+       protected void service(final HttpServletRequest req,
+                       final HttpServletResponse resp) throws ServletException,
+                       IOException {
                addXSSHeaders(resp);
                if (req.getHeader("Origin") != null) {
                        resp.getWriter().println("No cross domain access allowed.");
@@ -87,7 +79,7 @@ public class Gigi extends HttpServlet {
                        return;
                }
 
-               Page p = getPage(req.getPathInfo());
+               final Page p = getPage(req.getPathInfo());
                if (p != null) {
 
                        if (p.needsLogin() && hs.getAttribute("loggedin") == null) {
@@ -100,19 +92,31 @@ public class Gigi extends HttpServlet {
                        if (p.beforeTemplate(req, resp)) {
                                return;
                        }
+                       HashMap<String, Object> vars = new HashMap<String, Object>();
 
-                       String b0 = baseTemplate[0];
-                       b0 = makeDynTempl(b0, p);
                        resp.setContentType("text/html; charset=utf-8");
-                       resp.getWriter().print(b0);
-                       if (req.getMethod().equals("POST")) {
-                               p.doPost(req, resp);
-                       } else {
-                               p.doGet(req, resp);
-                       }
-                       String b1 = baseTemplate[1];
-                       b1 = makeDynTempl(b1, p);
-                       resp.getWriter().print(b1);
+                       Outputable content = new Outputable() {
+
+                               @Override
+                               public void output(PrintWriter out, Language l,
+                                               Map<String, Object> vars) {
+                                       try {
+                                               if (req.getMethod().equals("POST")) {
+                                                       p.doPost(req, resp);
+                                               } else {
+                                                       p.doGet(req, resp);
+                                               }
+                                       } catch (IOException e) {
+                                               e.printStackTrace();
+                                       }
+
+                               }
+                       };
+                       vars.put("title", p.getTitle());
+                       vars.put("static", ServerConstants.getStaticHostNamePort());
+                       vars.put("year", Calendar.getInstance().get(Calendar.YEAR));
+                       vars.put("content", content);
+                       baseTemplate.output(resp.getWriter(), Page.getLanguage(req), vars);
                } else {
                        resp.sendError(404, "Page not found.");
                }
@@ -140,12 +144,7 @@ public class Gigi extends HttpServlet {
                return null;
 
        }
-       private String makeDynTempl(String in, Page p) {
-               int year = Calendar.getInstance().get(Calendar.YEAR);
-               in = in.replaceAll("\\$title\\$", p.getTitle());
-               in = in.replaceAll("\\$year\\$", year + "");
-               return in;
-       }
+
        public static void addXSSHeaders(HttpServletResponse hsr) {
                hsr.addHeader("Access-Control-Allow-Origin",
                                "http://cacert.org https://localhost");
similarity index 94%
rename from templates/base.html
rename to src/org/cacert/gigi/Gigi.templ
index 65ca4765f8deb833c66758ebc0556bb9b066669d..ae226ec25e387d4b3a1f304806485caa109f23dc 100644 (file)
@@ -1,9 +1,9 @@
 <!DOCTYPE html>
 <html>
 <head>
-<title>$title$</title>
-<link rel="stylesheet" href="https://static.cacert.local/default.css" type="text/css">
-<script src="https://static.cacert.local/menu.js"></script>
+<title><?=$title?></title>
+<link rel="stylesheet" href="https://<?=$static?>/default.css" type="text/css">
+<script src="https://<?=$static?>/menu.js"></script>
 </head>
 <body>
        <div id="pagecell1">
@@ -84,8 +84,8 @@
                        </div>
                </div>
                <div id="content">
-                       <h1>$title$</h1>
-                       <div class="story">$content$</div>
+                       <h1><?=$title?></h1>
+                       <div class="story"><?=$content?></div>
                </div>
                <div class="sponsorinfo">
                        Der CAcert-Betrieb wird gesponsert von
                        <a href="/policy/PrivacyPolicy.html">Datenschutzrichtlinien</a> |
                        <a href="/index.php?id=51">Unsere Ziele</a> |
                        <a href="/index.php?id=11">Kontakt</a> |
-                       ©2002-$year$ von CAcert
+                       ©2002-<?=$year?> von CAcert
                </div>
        </div>
 </body>
index 225560c8869a1ecdb842c3e608859fa5056a1c93..586c6dad495b631928b4ea5b0331c7c56f447f00 100644 (file)
@@ -18,7 +18,7 @@ public class Template implements Outputable {
                LinkedList<Outputable> commands = new LinkedList<Outputable>();
                Scanner sc = new Scanner(r);
                Pattern p1 = Pattern.compile("([^<]|<[^?])*<\\?");
-               Pattern p2 = Pattern.compile("([^<]|<[^?])*\\?>");
+               Pattern p2 = Pattern.compile("[^?]*\\?>");
                while (true) {
                        String s1 = sc.findWithinHorizon(p1, 0);
                        if (s1 == null) {