KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > generators > VelocityGenerator


1 /*
2  Copyright (c) 2003 eInnovation Inc. All rights reserved
3
4  This library is free software; you can redistribute it and/or modify it under the terms
5  of the GNU Lesser General Public License as published by the Free Software Foundation;
6  either version 2.1 of the License, or (at your option) any later version.
7
8  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  See the GNU Lesser General Public License for more details.
11  */

12
13 /*
14  * (c) Copyright 2001 MyCorporation.
15  * All Rights Reserved.
16  */

17 package com.openedit.generators;
18
19 import java.io.File JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.Reader JavaDoc;
22 import java.io.Writer JavaDoc;
23 import java.util.Properties JavaDoc;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.velocity.VelocityContext;
28 import org.apache.velocity.app.Velocity;
29 import org.apache.velocity.exception.MethodInvocationException;
30 import org.apache.velocity.runtime.RuntimeConstants;
31 import org.openedit.repository.filesystem.FileItem;
32
33 import com.openedit.Generator;
34 import com.openedit.OpenEditException;
35 import com.openedit.WebPageRequest;
36 import com.openedit.error.ContentNotAvailableException;
37 import com.openedit.page.Page;
38 import com.openedit.util.OutputFiller;
39
40 /**
41  * This generator uses Velocity to process a template file, which (hopefully) uses the content on
42  * the given page.
43  *
44  * @author Chris Burkey
45  */

46 public class VelocityGenerator extends BaseGenerator implements Generator
47 {
48     public static Log log = LogFactory.getLog(VelocityGenerator.class);
49     public File JavaDoc fieldRootDirectory;
50     private static boolean initCompleted = false;
51
52     public VelocityGenerator()
53     {
54     }
55
56     public void generate( WebPageRequest inContext, Page inPage, Output inOut )
57         throws OpenEditException
58     {
59         debug( "Rendering " + inPage );
60         if( !inPage.exists() )
61         {
62             String JavaDoc vir = inPage.get("virtual");
63             if ( !Boolean.parseBoolean(vir) )
64             {
65                 log.info("Missing: " +inPage.getPath());
66                 throw new ContentNotAvailableException("Missing: " +inPage.getPath(),inPage.getPath());
67             }
68             else
69             {
70                 return; //do nothing
71
}
72         }
73         try
74         {
75             if ( inPage.isBinary() )
76             {
77                 InputStream JavaDoc in = inPage.getInputStream();
78                 new OutputFiller().fill(in, inContext.getOutputStream());
79                 return;
80             }
81
82             VelocityContext context = new VelocityContext(inContext.getPageMap());
83             Writer JavaDoc writer = inOut.getWriter();
84             
85             if( !initCompleted )
86             {
87                 initStatic();
88             }
89             
90             if ( inPage.getContentItem() instanceof FileItem) //Faster and does cache
91
{
92                 String JavaDoc path = inPage.getContentItem().getPath();
93                 Velocity.mergeTemplate( path, inPage.getCharacterEncoding(),context, writer );
94             }
95             else //do a string eval
96
{
97                 Reader JavaDoc in = inPage.getReader();
98                 Velocity.evaluate(context, writer, inPage.getPath(), in);
99             }
100             writer.flush();
101         }
102         catch (MethodInvocationException ex)
103         {
104             if (ex.getWrappedThrowable() instanceof OpenEditException)
105             {
106                 throw (OpenEditException) ex.getWrappedThrowable();
107             }
108             Throwable JavaDoc wrapped = ex.getWrappedThrowable();
109             if( ignoreError( wrapped ))
110             {
111                 //ignore
112
return;
113             }
114             throw new OpenEditException(wrapped);
115         }
116         catch ( Exception JavaDoc ioex )
117         {
118             if( ignoreError(ioex) )
119             {
120                 log.info("Browser canceled request");
121                 return;
122             }
123             if (ioex instanceof OpenEditException)
124             {
125                 throw (OpenEditException) ioex;
126             }
127             throw new OpenEditException(ioex);
128         }
129     }
130
131     protected synchronized void initStatic() throws Exception JavaDoc
132     {
133         //Velocity.setProperty( "runtime.log.logsystem.class", getClass().getName() );
134
//RuntimeSingleton.init();
135
// initialize velocity
136

137         /**
138          * Load the Velocity properties file. If the argument given is null then no properties
139          * will be used (i.e. the getVelocityProperties() methfod will return an empty Properties
140          * object.)
141          */

142         //Map velocityProperties = new HashMap();
143
Properties JavaDoc velocityProperties = new Properties JavaDoc();
144         velocityProperties.put(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
145             "com.openedit.generators.VelocityLogger");
146
147         /*
148          velocityProperties.put(
149          RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
150          "org.apache.velocity.runtime.log.SimpleLog4JLogSystem");
151
152          velocityProperties.put("runtime.log.logsystem.log4j.category", "syslog");
153          */

154         velocityProperties.put("runtime.log.invalid.references", "false");
155         velocityProperties.put("resource.manager.logwhenfound", "false");
156         velocityProperties.put("file.resource.loader.cache", "true");
157         velocityProperties.put("velocimacro.permissions.allow.inline", "true");
158
159         velocityProperties.setProperty("file.resource.loader.path", getRootDirectory()
160             .getAbsolutePath());
161
162         //Velocity has a bug that it required log4j unless you use the static instance
163
//getEngine().set(eprops);
164
Velocity.init(velocityProperties);
165         initCompleted = true;
166     }
167
168     protected void debug(String JavaDoc inMessage)
169     {
170         log.debug(inMessage);
171     }
172
173     /**
174      * DOCUMENT ME!
175      *
176      * @param inSite
177      * @param inConfiguration
178      *
179      * @throws Exception
180      */

181     public void init() throws Exception JavaDoc
182     {
183         
184     }
185
186     public File JavaDoc getRootDirectory()
187     {
188         return fieldRootDirectory;
189     }
190
191     public void setRootDirectory(File JavaDoc inRootDirectory)
192     {
193         fieldRootDirectory = inRootDirectory;
194     }
195
196 }
197
Popular Tags