BrightSide Workbench Full Report + Source Code
ElephantMarker.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2013 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.marker;
20 
21 import freemarker.template.Configuration;
22 import freemarker.template.DefaultObjectWrapperBuilder;
23 import freemarker.template.Template;
24 import freemarker.template.TemplateException;
25 import freemarker.template.TemplateExceptionHandler;
26 import java.io.File;
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29 import java.io.StringWriter;
30 import java.io.Writer;
31 import java.util.HashMap;
32 import java.util.Map;
33 import java.util.logging.Level;
34 import java.util.logging.Logger;
35 import org.turro.string.Strings;
36 import org.turro.action.EntityInfoContent;
37 import org.turro.action.EntityInfoType;
38 import org.turro.action.LinkType;
39 import org.turro.elephant.context.ElephantContext;
40 import org.turro.elephant.context.History;
41 import org.turro.elephant.context.IConstructor;
42 import org.turro.elephant.direct.DirectContents;
43 import org.turro.i18n.I_;
44 
49 public class ElephantMarker extends Configuration {
50 
51  private static final String TEMPLATE_DIR = "/WEB-INF/elephant/";
52 
53  private IConstructor constructor;
54  private final Map root = new HashMap();
55 
56  public ElephantMarker() {
58  }
59 
60  public ElephantMarker(IConstructor constructor) {
61  this(constructor, false);
62  }
63 
64  public ElephantMarker(IConstructor constructor, boolean mail) {
65  this.constructor = constructor;
66  try {
67  setDirectoryForTemplateLoading(new File(templateRoot(constructor, mail)));
68  DefaultObjectWrapperBuilder dowb = new DefaultObjectWrapperBuilder(VERSION_2_3_30);
69  dowb.setForceLegacyNonListCollections(false);
70  setObjectWrapper(dowb.build());
71  setLocale(I_.api().used());
72  setDefaultEncoding(ElephantContext.getEncoding());
73  setOutputEncoding(ElephantContext.getEncoding());
74  setDateFormat("short");
75  setDateTimeFormat("short_short");
76  setEncoding(I_.api().used(), ElephantContext.getEncoding());
77  setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
78  setURLEscapingCharset("UTF-8");
79  setAPIBuiltinEnabled(true);
80  root.put("constructor", constructor);
81  root.put("history", History.getInstance(constructor));
82  root.put("repository", IConstructor.currentRepository(constructor));
83  root.put("grepository", IConstructor.globalRepository());
84  root.put("serverURL", ElephantContext.getServerUrl("http"));
85  root.put("rootWebPath", ElephantContext.getRootWebPath());
86  root.put("siteName", ElephantContext.getSiteName());
87  root.put("labels", I_.compatibilityMap());
88  root.put("i_", I_.api());
89  root.put("i", I_.map());
90  root.put("k", I_.byKeyMap());
92  root.put("logoutURL", DirectContents.createRelativeURL("authentication") + "/logout");
93  root.put("helper", new MarkerHelper(constructor));
94  ContactIdentity identity = new ContactIdentity();
95  root.put("identity", identity);
96  root.put("rolemap", identity.getPermissionMap());
97  root.put("cookies", new Cookies(constructor.getRequest(), constructor.getResponse()));
98  if(getLocale() != null) {
99  root.put("lang", getLocale().getLanguage());
100  }
102  } catch (IOException ex) {
103  Logger.getLogger(ElephantMarker.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
104  }
105  }
106 
108  return constructor;
109  }
110 
111  public Map getRoot() {
112  return root;
113  }
114 
115  public String parse(String rootTmpl, String tmpl) {
116  StringWriter sw = new StringWriter();
117  process(rootTmpl, tmpl, sw);
118  return sw.toString();
119  }
120 
121  public void process(String rootTmpl, String tmpl) {
122  try {
123  process(rootTmpl, tmpl, constructor.getOut());
124  } catch (IOException ex) {
125  Logger.getLogger(ElephantMarker.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
126  }
127  }
128 
129  public void process(String rootTmpl, String tmpl, Writer out) {
130  try {
131  Template template = null;
132  try {
133  template = getTemplate(getSiteTemplateString(rootTmpl, tmpl));
134  } catch(FileNotFoundException ex) {
135  template = getTemplate(getTemplateString(rootTmpl, tmpl));
136  }
137  template.process(root, out);
138  } catch (IOException | TemplateException ex) {
139  //MailError.getInstance(ElephantMarker.class, ex, tmpl)
140  // .sendEmail(Application.getApplication());
141  Logger.getLogger(ElephantMarker.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
142  }
143  }
144 
145  public Object get(Object key) {
146  return root.get(key);
147  }
148 
149  public Object put(Object key, Object value) {
150  return root.put(key, value);
151  }
152 
153  public Object remove(Object key) {
154  return root.remove(key);
155  }
156 
157  public boolean contains(Object key) {
158  return root.containsKey(key);
159  }
160 
161  public void putAll(Map m) {
162  root.putAll(m);
163  }
164 
165  public String getTemplateString(String root, String template) {
166  if(!Strings.isBlank(template)) {
167  if(!template.contains("/")) {
168  template = "/" + root + "/" + template + "Template.html";
169  }
170  }
171  return template;
172  }
173 
174  public String getSiteTemplateString(String root, String template) {
175  if(!Strings.isBlank(template)) {
176  if(!template.contains("/")) {
177  template = "/" + root + "/site/" + template + "Template.html";
178  }
179  }
180  return template;
181  }
182 
183  public static boolean existsTemplate(IConstructor constructor, boolean mail, String root, String name) {
184  return existsTemplate(constructor, mail, root, name, false);
185  }
186 
187  public static boolean existsTemplate(IConstructor constructor, boolean mail, String root, String name, boolean overriden) {
188  return fileTemplate(constructor, mail, root, name, overriden).exists();
189  }
190 
191  public static File fileTemplate(IConstructor constructor, boolean mail, String root, String name) {
192  return fileTemplate(constructor, mail, root, name, false);
193  }
194 
195  public static File fileTemplate(IConstructor constructor, boolean mail, String root, String name, boolean overriden) {
196  StringBuilder sb = new StringBuilder(templateRoot(constructor, mail));
197  sb.append("/").append(root).append("/");
198  if(overriden) sb.append("site").append("/");
199  sb.append(name).append("Template.html");
200  return new File(sb.toString());
201  }
202 
203  public static String templateRoot(IConstructor constructor, boolean mail) {
204  if(mail) {
205  return ElephantContext.getRealPath(TEMPLATE_DIR + "templates-mail");
206  } else if(constructor != null) {
207  return ElephantContext.getRealPath(TEMPLATE_DIR + constructor.getTemplateRoot());
208  } else {
210  }
211  }
212 
213 }
static String createURL(EntityInfoType type, LinkType link)
static String getServerUrl(String scheme)
static History getInstance(IConstructor constructor)
Definition: History.java:91
static String createRelativeURL(String id)
static I18nByKey byKeyMap()
Definition: I_.java:91
static I18nApiWrapper api()
Definition: I_.java:65
static I18nMapWrapper map()
Definition: I_.java:69
static I18nCompatibilityMap compatibilityMap()
Definition: I_.java:117
static String templateRoot(IConstructor constructor, boolean mail)
static File fileTemplate(IConstructor constructor, boolean mail, String root, String name)
ElephantMarker(IConstructor constructor)
void process(String rootTmpl, String tmpl)
static boolean existsTemplate(IConstructor constructor, boolean mail, String root, String name, boolean overriden)
static boolean existsTemplate(IConstructor constructor, boolean mail, String root, String name)
String parse(String rootTmpl, String tmpl)
void process(String rootTmpl, String tmpl, Writer out)
Object put(Object key, Object value)
String getSiteTemplateString(String root, String template)
String getTemplateString(String root, String template)
static File fileTemplate(IConstructor constructor, boolean mail, String root, String name, boolean overriden)
ElephantMarker(IConstructor constructor, boolean mail)
static String currentRepository(IConstructor constructor)