1 /*
2 * $Id: ContextFactory.java 2993 2011-11-24 19:51:48Z andrewinkler $
3 * ============================================================================
4 * Project gluehloch-homepage-core
5 * Copyright (c) 2004-2010 by Andre Winkler. All rights reserved.
6 * ============================================================================
7 * GNU LESSER GENERAL PUBLIC LICENSE
8 * TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 */
25
26 package de.awtools.homegen.velocity;
27
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.text.SimpleDateFormat;
31 import java.util.Date;
32 import java.util.Properties;
33
34 import org.apache.velocity.VelocityContext;
35 import org.apache.velocity.app.Velocity;
36 import org.slf4j.Logger;
37 import org.xml.sax.SAXException;
38
39 import de.awtools.basic.LoggerFactory;
40
41 /**
42 * Generiert einen Velocity Kontext.
43 *
44 * @version $LastChangedRevision: 2993 $ $LastChangedDate: 2011-11-24 20:51:48 +0100 (Thu, 24 Nov 2011) $
45 * @author by Andre Winkler, $LastChangedBy: andrewinkler $
46 *
47 * @since 1.2
48 */
49 public class ContextFactory {
50
51 /** Der Logger der Klasse. */
52 private final static Logger log = LoggerFactory.make();
53
54 /** Der Pfad in das Wurzelverzeichnis der Homepage. */
55 public static final String ROOT_DIR = "wwwRootDir";
56
57 /**
58 * Das Default-Encoding.
59 *
60 * TODO: Die feste Verdrahtung gehört hier eigentlich nicht hin.
61 */
62 private final String encoding;
63
64 /**
65 * Konstruktor.
66 *
67 * @param _encoding Die Zeichensatzkodierung.
68 */
69 public ContextFactory(final String _encoding) {
70 encoding = _encoding;
71 }
72
73 /**
74 * Initialisiert den Velocity Kontext.
75 *
76 * @param _encoding Die Zeichensatzkodierung.
77 * @return Ein <code>VelocityContext</code>.
78 */
79 public static VelocityContext createContext(final String _encoding) {
80 try {
81 return (new ContextFactory(_encoding).createContext0());
82 } catch (SAXException ex) {
83 log.error("SAXException", ex);
84 throw new RuntimeException(ex);
85 } catch (IOException ex) {
86 log.error("IOException", ex);
87 throw new RuntimeException(ex);
88 } catch (Exception ex) {
89 log.error("Exception", ex);
90 throw new RuntimeException(ex);
91 }
92 }
93
94 /**
95 * Initialisiert den Velocity Kontext.
96 *
97 * @return Ein <code>VelocityContext</code>.
98 * @throws IOException IO Operation schlug fehl.
99 * @throws SAXException XML-Parser-Fehler.
100 * @throws Exception Fehler in die Initialisierung von Velocity.
101 */
102 private VelocityContext createContext0() throws IOException, SAXException,
103 Exception {
104
105 initVelocity();
106
107 VelocityContext context = new VelocityContext();
108 SimpleDateFormat formatter = new SimpleDateFormat("dd. MMMMM yyyy");
109 String date = formatter.format(new Date());
110
111 context.put("date", date);
112 context.put("jscriptDir", "js");
113 context.put("cssDir", "css");
114 context.put("imageDir", "images");
115 context.put(ROOT_DIR, ".");
116
117 return context;
118 }
119
120 /**
121 * Initialisiert Velocity.
122 *
123 * @throws Exception Da ging was schief.
124 */
125 private void initVelocity() throws Exception {
126 Properties p = new Properties();
127 InputStream in = ContextFactory.class
128 .getResourceAsStream("/plugin-resources/velocity.properties");
129
130 try {
131 p.load(in);
132 } finally {
133 in.close();
134 }
135
136 // p.setProperty("resource.loader", "class");
137 // p.setProperty("class.resource.loader.description",
138 // "Velocity Classpath Resource Loader");
139 // p.setProperty("class.resource.loader.class",
140 // "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
141
142 p.setProperty("input.encoding", encoding);
143 p.setProperty("output.encoding", encoding);
144 Velocity.init(p);
145 }
146
147 }