KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > template > FreeMarkerProcessor


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.template;
18
19 import java.io.IOException JavaDoc;
20 import java.io.Writer JavaDoc;
21
22 import org.alfresco.service.cmr.repository.ContentService;
23 import org.alfresco.service.cmr.repository.NodeService;
24 import org.alfresco.service.cmr.repository.TemplateException;
25 import org.alfresco.service.cmr.repository.TemplateProcessor;
26 import org.apache.log4j.Logger;
27
28 import freemarker.cache.MruCacheStorage;
29 import freemarker.template.Configuration;
30 import freemarker.template.Template;
31 import freemarker.template.TemplateExceptionHandler;
32
33 /**
34  * FreeMarker implementation the template processor interface
35  *
36  * @author Kevin Roast
37  */

38 public class FreeMarkerProcessor implements TemplateProcessor
39 {
40     private final static String JavaDoc MSG_ERROR_NO_TEMPLATE = "error_no_template";
41     private final static String JavaDoc MSG_ERROR_TEMPLATE_FAIL = "error_template_fail";
42     private final static String JavaDoc MSG_ERROR_TEMPLATE_IO = "error_template_io";
43     
44     private static Logger logger = Logger.getLogger(FreeMarkerProcessor.class);
45     
46     /** FreeMarker processor configuration */
47     private Configuration config = null;
48     
49     /** The permission-safe node service */
50     private NodeService nodeService;
51     
52     /** The Content Service to use */
53     private ContentService contentService;
54     
55     /**
56      * Set the node service
57      *
58      * @param nodeService The permission-safe node service
59      */

60     public void setNodeService(NodeService nodeService)
61     {
62         this.nodeService = nodeService;
63     }
64     
65     /**
66      * Set the content service
67      *
68      * @param contentService The ContentService to use
69      */

70     public void setContentService(ContentService contentService)
71     {
72         this.contentService = contentService;
73     }
74     
75     /**
76      * @return The FreeMarker config instance for this processor
77      */

78     private Configuration getConfig()
79     {
80         if (this.config == null)
81         {
82             Configuration config = new Configuration();
83             
84             // setup template cache
85
config.setCacheStorage(new MruCacheStorage(20, 0));
86             
87             // use our custom loader to find templates on the ClassPath
88
config.setTemplateLoader(new ClassPathRepoTemplateLoader(nodeService, contentService));
89             
90             // use our custom object wrapper that can deal with QNameMap objects directly
91
config.setObjectWrapper(new QNameAwareObjectWrapper());
92             
93             // rethrow any exception so we can deal with them
94
config.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
95             
96             this.config = config;
97         }
98         return this.config;
99     }
100     
101     /**
102      * @see org.alfresco.service.cmr.repository.TemplateProcessor#process(java.lang.String, java.lang.Object, java.io.Writer)
103      */

104     public void process(String JavaDoc template, Object JavaDoc model, Writer JavaDoc out)
105     {
106         if (template == null || template.length() == 0)
107         {
108             throw new IllegalArgumentException JavaDoc("Template name is mandatory.");
109         }
110         if (model == null)
111         {
112             throw new IllegalArgumentException JavaDoc("Model is mandatory.");
113         }
114         if (out == null)
115         {
116             throw new IllegalArgumentException JavaDoc("Output Writer is mandatory.");
117         }
118         
119         try
120         {
121             if (logger.isDebugEnabled())
122                 logger.debug("Executing template: " + template + " on model: " + model);
123             
124             Template t = getConfig().getTemplate(template);
125             if (t != null)
126             {
127                 try
128                 {
129                     // perform the template processing against supplied data model
130
t.process(model, out);
131                 }
132                 catch (Throwable JavaDoc err)
133                 {
134                     throw new TemplateException(MSG_ERROR_TEMPLATE_FAIL, new Object JavaDoc[] {err.getMessage()}, err);
135                 }
136             }
137             else
138             {
139                 throw new TemplateException(MSG_ERROR_NO_TEMPLATE, new Object JavaDoc[] {template});
140             }
141         }
142         catch (IOException JavaDoc ioerr)
143         {
144             throw new TemplateException(MSG_ERROR_TEMPLATE_IO, new Object JavaDoc[] {template}, ioerr);
145         }
146     }
147 }
148
Popular Tags