01: /*
02: * Copyright 2007 Gerd Ziegler (www.gerdziegler.de)
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: * http://www.apache.org/licenses/LICENSE-2.0
07: * Unless required by applicable law or agreed to in writing,
08: * software distributed under the License is distributed on an
09: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
10: * either express or implied. See the License for the specific
11: * language governing permissions and limitations under the License.
12: * @author www.gerdziegler.de
13: */
14:
15: package org.ztemplates.web.impl;
16:
17: import java.util.Enumeration;
18: import java.util.HashMap;
19: import java.util.Map;
20:
21: import javax.servlet.ServletContext;
22:
23: import org.ztemplates.render.ZRenderEngine;
24: import org.ztemplates.render.ZRenderEnvironment;
25: import org.ztemplates.web.ZIRenderService;
26: import org.ztemplates.web.ZIServiceRepository;
27: import org.ztemplates.web.css.ZCssEngine;
28:
29: public class ZRenderServiceImpl implements ZIRenderService {
30: private ZRenderEngine renderEngine;
31:
32: private ZCssEngine cssEngine;
33:
34: private ZRenderEnvironment renderEnvironment;
35:
36: private int crtId = 0;
37:
38: public void init(ZIServiceRepository serviceRepository)
39: throws Exception {
40: ServletContext ctx = serviceRepository.getServletContext();
41:
42: String renderEnvironmentKEY = ZRenderEnvironment.class
43: .getName();
44: renderEnvironment = (ZRenderEnvironment) ctx
45: .getAttribute(renderEnvironmentKEY);
46: if (renderEnvironment == null) {
47: Map<String, String> initParameters = new HashMap<String, String>();
48: Enumeration<String> en = ctx.getInitParameterNames();
49: while (en.hasMoreElements()) {
50: String key = en.nextElement();
51: String value = ctx.getInitParameter(key);
52: initParameters.put(key, value);
53: }
54:
55: String contextPath = serviceRepository.getRequest()
56: .getContextPath();
57: renderEnvironment = new ZRenderEnvironment(
58: serviceRepository.getClasspathService()
59: .getClassRepository(), contextPath,
60: initParameters);
61: ctx.setAttribute(renderEnvironmentKEY, renderEnvironment);
62: }
63:
64: String cssEngineKEY = ZCssEngine.class.getName();
65: cssEngine = (ZCssEngine) ctx.getAttribute(cssEngineKEY);
66: if (cssEngine == null) {
67: cssEngine = new ZCssEngine(renderEnvironment);
68: ctx.setAttribute(cssEngineKEY, cssEngine);
69: }
70:
71: renderEngine = new ZRenderEngine(renderEnvironment);
72: }
73:
74: public String render(Object obj) throws Exception {
75: String ret = renderEngine.render(obj);
76: return ret;
77: }
78:
79: public String renderZtemplatesCss() throws Exception {
80: return cssEngine.getCss();
81: }
82:
83: public String createJavaScriptId() throws Exception {
84: return "ztmpId" + (crtId++);
85: }
86: }
|