View Javadoc

1   /***
2    Copyright (C) 2005 The Java Community
3   
4    This program is free software; you can redistribute it and/or modify  it under
5    the terms of the GNU General Public License as published by  the Free Software
6    Foundation; either version 2 of the License, or  (at your option) any later
7    version.
8   
9    This program is distributed in the hope that it will be useful,  but WITHOUT
10   ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS
11   FOR A PARTICULAR PURPOSE. See the  GNU General Public License for more details.
12  
13   You should have received a copy of the GNU General Public License  along with
14   this program; if not, write to the Free Software  Foundation, Inc., 59 Temple
15   Place, Suite 330, Boston, MA 02111-1307 USA.
16   */
17  package org.bejug.javacareers.common.templates;
18  
19  import java.io.FileWriter;
20  import java.io.IOException;
21  import java.io.StringWriter;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.velocity.Template;
26  import org.apache.velocity.VelocityContext;
27  import org.apache.velocity.exception.VelocityException;
28  
29  /***
30   * @author bbr (last modified by $Author: shally $)
31   * @version $Revision: 1.12 $ - $Date: 2005/12/20 15:36:46 $
32   */
33  public class TemplateHolder {
34  
35      private static final Log LOG = LogFactory.getLog(TemplateHolder.class);
36      private Template template;
37      private String locale;
38      private String content;
39      private String filepath;
40      
41      /***
42       * sets the template.
43       * @param velTemplate the template to set.
44       */
45      public void setTemplate(Template velTemplate) {
46          this.template = velTemplate;
47      }
48      
49      /***
50       * @return the short name, this is the name up to '_' or up to '.'
51       */
52  	public String getShortName(){
53  		String s = template.getName();
54  		int ix = s.lastIndexOf('_');
55          ix = ix<0 ? s.lastIndexOf('.') : ix;
56  		return ix>0 ? s.substring(0,ix) : s;
57  	}
58  
59      /***
60       * @return the filepath.
61       */
62      public String getFilepath() {
63          return filepath;
64      }
65  
66      /***
67       * sets the filepath.
68       * @param filepath a String indicating the filepath to set.
69       */
70      public void setFilepath(String filepath) {
71          this.filepath = filepath;
72      }
73  
74      /***
75       * sets the locale.
76       * @param locale a String indicating the locale to set.
77       */
78      public void setLocale(String locale) {
79          this.locale = locale;
80          
81      }
82      
83      /***
84       * @return the wanted template.
85       */
86      public Template getTemplate() {
87          return template;
88      }
89  
90      /***
91       * @return a String containing the locale.
92       */
93      public String getLocale() {
94          return locale;
95      }
96  
97      /***
98       * @return a String containing the content of the velocitytemplate.
99       */
100     public String getContent() {
101         StringWriter writer = new StringWriter();
102         try {
103             template.merge(new VelocityContext(),writer);
104             content = writer.getBuffer().toString();
105         } 
106         catch (VelocityException e) {
107             LOG.info("Debug: Error reading content, exception:"+e.getMessage());
108         }
109     	catch (Exception e) {
110     		LOG.info("Debug: Error reading content, exception:"+e.getMessage());
111     	}
112         return content;
113     }
114 
115     /***
116      * sets the content.
117      * @param content a String containing the content.
118      */
119     public void setContent(String content) {
120         this.content = content;
121 
122     }
123 
124     
125     /***
126      * to update the template.
127      * @throws Exception on template failures
128      *
129      */
130     public void update() throws Exception {
131         FileWriter writer = null;
132         try {
133             LOG.info("Debug: Writing to "+filepath);
134             writer = new FileWriter(filepath);
135             writer.write(content);
136             writer.close();
137             //template.touch();
138             boolean processed = template.process();
139             LOG.info("Debug: Processed: "+processed);
140         }
141         finally {
142             try {
143                 if (writer != null) {
144                     writer.close();
145 
146                 }
147             }
148             catch (IOException e2) {
149                 LOG.info("Debug: "+e2);
150             }
151         }
152         LOG.info("Debug: Written.");
153     }
154     
155 	/***
156      * Gets a String representation of this obfeject.
157      *
158      * @return String A string representation of the properties
159      */
160     public String toString() {
161        final StringBuffer buf = new StringBuffer();
162        buf.append("TemplateHolder=");
163        buf.append("{template=").append(template==null?"null":template.toString());
164        buf.append(",locale=").append(locale==null?"null":locale);
165        buf.append(",filepath=").append(filepath==null?"null":filepath);
166        buf.append(",content=").append(content==null?"null":content);
167        buf.append('}');
168        return buf.toString();
169    }    
170 
171     
172 }
173 
174 /***
175  * $Log: TemplateHolder.java,v $
176  * Revision 1.12  2005/12/20 15:36:46  shally
177  * CheckStyle and PMD changes.
178  *
179  * Revision 1.11  2005/12/08 14:53:46  shally
180  * Opkuis voor checkstyle.
181  *
182  * Revision 1.10  2005/12/07 23:07:43  shally
183  * Adding editing for velocity templates, fixing some cosmetics
184  *
185  * Revision 1.9  2005/09/30 14:38:07  bavo_jcs
186  * Fixed URL
187  *
188  * Revision 1.8  2005/09/13 08:11:06  schauwvliege
189  * organize imports
190  *
191  * Revision 1.7  2005/09/01 14:32:18  bavo_jcs
192  * Velocity update
193  *
194  * Revision 1.6  2005/08/10 09:04:46  bavo_jcs
195  * Optimized imports according to checkstyle
196  *
197  * Revision 1.5  2005/08/09 12:59:53  bavo_jcs
198  * Optimized imports
199  *
200  * Revision 1.4  2005/08/05 07:00:59  bme_jcs
201  * resolved checkstyle errors
202  *
203  * Revision 1.3  2005/07/18 15:42:30  bavo_jcs
204  * search fields alignment
205  *
206  * Revision 1.2  2005/07/15 15:58:30  bavo_jcs
207  * Templates I18N and writing
208  *
209  * Revision 1.1  2005/07/15 09:31:41  bavo_jcs
210  * Templates early update
211  *
212  */