001: /*
002: * Copyright 2007 Gerd Ziegler (www.gerdziegler.de)
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: * http://www.apache.org/licenses/LICENSE-2.0
007: * Unless required by applicable law or agreed to in writing,
008: * software distributed under the License is distributed on an
009: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
010: * either express or implied. See the License for the specific
011: * language governing permissions and limitations under the License.
012: * @author www.gerdziegler.de
013: */
014:
015: package org.ztemplates.render;
016:
017: import java.io.Serializable;
018: import java.lang.annotation.Annotation;
019: import java.util.ArrayList;
020: import java.util.HashMap;
021: import java.util.List;
022: import java.util.Map;
023:
024: import org.ztemplates.classpath.ZIClassRepository;
025: import org.ztemplates.render.script.ZScriptRepository;
026:
027: public class ZRenderEnvironment implements Serializable {
028: private Map<String, String> templateNameCache = new HashMap<String, String>();
029:
030: private Map<String, String> cssIdCache = new HashMap<String, String>();
031:
032: private final boolean shouldGenerateComments;
033:
034: private final String contextPath;
035:
036: private final ZIClassRepository classRepository;
037:
038: private final ZScriptRepository scriptRepository;
039:
040: public ZRenderEnvironment(ZIClassRepository classRepository)
041: throws Exception {
042: this (classRepository, null, new HashMap<String, String>());
043: }
044:
045: public ZRenderEnvironment(ZIClassRepository classRepository,
046: String contextPath, Map<String, String> initParameters)
047: throws Exception {
048: this .classRepository = classRepository;
049: this .contextPath = contextPath;
050: this .shouldGenerateComments = "true".equals(initParameters
051: .get("shouldGenerateComments"));
052:
053: List<ZScript> scripts = new ArrayList<ZScript>();
054: List<Class> scriptClasses = classRepository
055: .getClassesAnnotatedWith(ZScript.class);
056: for (Class c : scriptClasses) {
057: ZScript sc = (ZScript) c.getAnnotation(ZScript.class);
058: scripts.add(sc);
059: }
060: scriptRepository = new ZScriptRepository(scripts);
061: }
062:
063: public String getTemplateName(final Class clazz, final String suffix) {
064: String key = clazz.getName() + suffix;
065: String ret = templateNameCache.get(key);
066: if (ret != null) {
067: return ret;
068: }
069:
070: Class pojoClass = clazz;
071: ZTemplate templateAnnot = (ZTemplate) pojoClass
072: .getAnnotation(ZTemplate.class);
073: if (templateAnnot != null) {
074: String tpl = templateAnnot.value();
075: if ("".equals(templateAnnot.value())) {
076: pojoClass = getDeclaringClass(pojoClass,
077: ZTemplate.class);
078: } else if (templateAnnot.relative()) {
079: pojoClass = getDeclaringClass(pojoClass,
080: ZTemplate.class);
081: String template = pojoClass.getName();
082: template = template.substring(0, template
083: .lastIndexOf('.'));
084: template = template.replace('.', '/');
085: template += "/" + tpl;
086: templateNameCache.put(key, template);
087: return template;
088: } else {
089: templateNameCache.put(key, tpl);
090: return tpl;
091: }
092: }
093:
094: String className = pojoClass.getName();
095: if (className.lastIndexOf('$') >= 0) {
096: className = pojoClass.getSuperclass().getName();
097: }
098: ret = className.replace('.', '/') + '.' + suffix;
099: templateNameCache.put(key, ret);
100: return ret;
101: }
102:
103: public String computeCssId(final Class clazz) {
104: String key = clazz.getName();
105: String ret = cssIdCache.get(key);
106: if (ret != null) {
107: return ret;
108: }
109: Class pojoClass = getDeclaringClass(clazz, ZRenderer.class);
110: if (pojoClass == null) {
111: pojoClass = clazz;
112: }
113: ret = pojoClass.getSimpleName();
114: cssIdCache.put(key, ret);
115: return ret;
116: }
117:
118: public Class getDeclaringClass(Class pojoClass,
119: Class<? extends Annotation> findAnnot) {
120: if (pojoClass == null) {
121: return null;
122: }
123: Annotation[] annot = pojoClass.getDeclaredAnnotations();
124: for (Annotation crtAnnot : annot) {
125: if (crtAnnot.annotationType().equals(findAnnot)) {
126: return pojoClass;
127: }
128: }
129: return getDeclaringClass(pojoClass.getSuperclass(), findAnnot);
130: }
131:
132: public boolean isShouldGenerateComments() {
133: return shouldGenerateComments;
134: }
135:
136: public String getContextPath() {
137: return contextPath;
138: }
139:
140: public ZIClassRepository getClassRepository() {
141: return classRepository;
142: }
143:
144: public ZScriptRepository getScriptRepository() {
145: return scriptRepository;
146: }
147: }
|