KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opencms > template > CmsDumpTemplate


1 /*
2 * File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/template/CmsDumpTemplate.java,v $
3 * Date : $Date: 2005/05/31 15:51:19 $
4 * Version: $Revision: 1.3 $
5 *
6 * This library is part of OpenCms -
7 * the Open Source Content Mananagement System
8 *
9 * Copyright (C) 2001 The OpenCms Group
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * For further information about OpenCms, please see the
22 * OpenCms Website: http://www.opencms.org
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 */

28
29
30 package com.opencms.template;
31
32 import org.opencms.file.CmsFile;
33 import org.opencms.file.CmsObject;
34 import org.opencms.file.CmsRequestContext;
35 import org.opencms.main.CmsException;
36 import org.opencms.main.CmsLog;
37
38 import com.opencms.legacy.CmsLegacyException;
39 import com.opencms.legacy.CmsXmlTemplateLoader;
40 import com.opencms.template.cache.A_CmsElement;
41 import com.opencms.template.cache.CmsElementDump;
42
43 import java.util.Hashtable JavaDoc;
44
45 /**
46  * Template class for dumping files to the output without further
47  * interpreting or processing.
48  * This can be used for plain text files or files containing graphics.
49  *
50  * @author Alexander Lucas
51  * @version $Revision: 1.3 $ $Date: 2005/05/31 15:51:19 $
52  *
53  * @deprecated Will not be supported past the OpenCms 6 release.
54  */

55 public class CmsDumpTemplate extends A_CmsTemplate implements I_CmsDumpTemplate {
56
57     /** Boolean for additional debug output control. */
58     private static final boolean C_DEBUG = false;
59
60     /**
61      * Creates a new CmsDumpTemplate.<p>
62      */

63     public CmsDumpTemplate() {
64         // noop
65
}
66
67     /**
68      * gets the caching information from the current template class.
69      *
70      * @param cms CmsObject Object for accessing system resources
71      * @param templateFile Filename of the template file
72      * @param elementName Element name of this template in our parent template.
73      * @param parameters Hashtable with all template class parameters.
74      * @param templateSelector template section that should be processed.
75      * @return <EM>true</EM> if this class may stream it's results, <EM>false</EM> otherwise.
76      */

77     public CmsCacheDirectives getCacheDirectives(CmsObject cms, String JavaDoc templateFile, String JavaDoc elementName, Hashtable JavaDoc parameters, String JavaDoc templateSelector) {
78         // First build our own cache directives.
79
CmsCacheDirectives result = new CmsCacheDirectives(true);
80         result.setCacheUri(true);
81         return result;
82
83     }
84
85     /**
86      * Gets the content of a given template file.
87      *
88      * @param cms CmsObject Object for accessing system resources
89      * @param templateFile Filename of the template file
90      * @param elementName <em>not used here</em>.
91      * @param parameters <em>not used here</em>.
92      * @return Unprocessed content of the given template file.
93      * @throws CmsException if something goes wrong
94      */

95     public byte[] getContent(CmsObject cms, String JavaDoc templateFile, String JavaDoc elementName, Hashtable JavaDoc parameters) throws CmsException {
96         if (C_DEBUG && CmsLog.getLog(this).isDebugEnabled()) {
97             CmsLog.getLog(this).debug("Dumping contents of file " + templateFile);
98         }
99         byte[] s = null;
100         try {
101             // Encoding project:
102
CmsFile file = cms.readFile(templateFile);
103             s = file.getContents();
104         } catch (Exception JavaDoc e) {
105             s = null;
106             String JavaDoc errorMessage = "Error while reading file " + templateFile + ": " + e;
107             if (CmsLog.getLog(this).isErrorEnabled()) {
108                 CmsLog.getLog(this).error(errorMessage, e);
109             }
110             if (e instanceof CmsException) {
111                 throw (CmsException)e;
112             } else {
113                 throw new CmsLegacyException(errorMessage, CmsLegacyException.C_UNKNOWN_EXCEPTION);
114             }
115         }
116         return s;
117     }
118
119     /**
120      * Gets the content of a given template file.
121      *
122      * @param cms CmsObject Object for accessing system resources
123      * @param templateFile Filename of the template file
124      * @param elementName <em>not used here</em>.
125      * @param parameters <em>not used here</em>.
126      * @param templateSelector <em>not used here</em>.
127      * @return Unprocessed content of the given template file.
128      * @throws CmsException if something goes wrong
129      */

130     public byte[] getContent(CmsObject cms, String JavaDoc templateFile, String JavaDoc elementName, Hashtable JavaDoc parameters, String JavaDoc templateSelector) throws CmsException {
131
132         // ignore the templateSelector since we only dump the template
133
return getContent(cms, templateFile, elementName, parameters);
134     }
135
136     /**
137      * Gets the key that should be used to cache the results of
138      * this template class.
139      * <P>
140      * Since this class is quite simple it's okay to return
141      * just the name of the template file here.
142      *
143      * @param cms CmsObject Object for accessing system resources
144      * @param templateFile Filename of the template file
145      * @param parameter Hashtable with all template class parameters.
146      * @param templateSelector template section that should be processed.
147      * @return key that can be used for caching
148      */

149     public Object JavaDoc getKey(CmsObject cms, String JavaDoc templateFile, Hashtable JavaDoc parameter, String JavaDoc templateSelector) {
150         CmsRequestContext reqContext = cms.getRequestContext();
151         return "" + reqContext.currentProject().getId() + ":" + reqContext.addSiteRoot(templateFile);
152     }
153
154     /**
155      * Any results of this class are cacheable since we don't include
156      * any subtemplates. So we can always return <code>true</code> here.
157      * @return <code>true</code>
158      */

159     public boolean isTemplateCacheSet() {
160         return true;
161     }
162
163     /**
164      * Template cache is not used here since we don't include
165      * any subtemplates <em>(not implemented)</em>.
166      *
167      * @param c the cms template cache
168      */

169     public void setTemplateCache(I_CmsTemplateCache c) {
170         // do nothing.
171
}
172
173     /**
174      * Template cache is not used here since we don't include
175      * any subtemplates. So we can always return <code>false</code> here.
176      * @param cms the cms object
177      * @param templateFile the name of the template file
178      * @param elementName the name of the element
179      * @param parameters Hashtable with all template class parameters.
180      * @param templateSelector template section that should be processed.
181      * @return <code>false</code>
182      */

183     public boolean shouldReload(CmsObject cms, String JavaDoc templateFile, String JavaDoc elementName, Hashtable JavaDoc parameters, String JavaDoc templateSelector) {
184         return false;
185     }
186
187     /**
188      * Create a new element for the element cache consisting of the current template
189      * class and the given template file.
190      * <P>
191      * Complex template classes that are able to include other (sub-)templates
192      * must generate a collection of element definitions for their possible
193      * subtemplates. This collection is part of the new element.
194      * @param cms CmsObject for accessing system resources.
195      * @param templateFile Name of the template file for the new element
196      * @param parameters All parameters of the current request
197      * @return New element for the element cache
198      */

199     public A_CmsElement createElement (CmsObject cms, String JavaDoc templateFile, Hashtable JavaDoc parameters) {
200         return new CmsElementDump(getClass().getName(), templateFile, getCacheDirectives(cms, templateFile, null, parameters, null),
201                     CmsXmlTemplateLoader.getElementCache().getVariantCachesize());
202     }
203 }
204
Popular Tags