1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.gwe.app.client.web.view;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21
22 import org.gwe.utils.IOUtils;
23
24
25
26
27
28 public class MainVelocityTemplate {
29
30 private static final String CONTENT_BOX = "${meta_contentBox}";
31
32 public static String readTemplateFile(String templateName) throws IOException {
33 templateName = MainVelocityTemplate.class.getPackage().getName().replace('.','/') + "/" + templateName;
34 InputStream is = MainVelocityTemplate.class.getClassLoader().getResourceAsStream(templateName);
35 return new String(IOUtils.readStream(is, null).toByteArray());
36 }
37
38 private String mainPage = "";
39 private String contentMacros = "";
40
41 private String layoutWithMacros = "";
42
43 public MainVelocityTemplate() throws IOException {
44 this("main.html", "macros.vm");
45 }
46
47 public MainVelocityTemplate(String mainTemplate, String contentMacros) throws IOException {
48 this.mainPage = readTemplateFile(mainTemplate);
49 this.contentMacros = readTemplateFile(contentMacros);
50 this.layoutWithMacros = this.contentMacros + "\n" + mainPage;
51 }
52
53 public String createPageTemplate(String templateFileName) throws IOException {
54 String template = readTemplateFile(templateFileName);
55
56 return layoutWithMacros.replace(CONTENT_BOX, template);
57 }
58
59 public String getMainPage() {
60 return mainPage;
61 }
62
63 public static void main(String[] args) throws IOException {
64 String page = new MainVelocityTemplate().createPageTemplate("grid.html");
65 System.out.println(page);
66 }
67 }