KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cofax > cms > CofaxToolsPreview


1 /*
2  * CofaxToolsPreview is part of the Cofax content management system library.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Please see http://www.cofax.org for contact information and other related informaion.
19  *
20  * $Header: /cvsroot/cofax/cofax/src/org/cofax/cms/CofaxToolsPreview.java,v 1.8.2.1 2006/12/11 16:28:23 fxrobin Exp $
21  */

22
23 package org.cofax.cms;
24
25 import java.util.*;
26 import java.io.*;
27 import java.net.*;
28 import javax.servlet.*;
29 import javax.servlet.http.*;
30 import org.cofax.*;
31 import org.cofax.cds.*;
32
33 public class CofaxToolsPreview extends HttpServlet {
34
35     // Description of the fields
36
private static String JavaDoc listTemplate; // Template for index pages.
37

38     private static String JavaDoc fileTemplate; // Template for file pages
39

40     private static String JavaDoc templatePath; // Path to find templates
41

42     private static String JavaDoc templateLoaderClass; // Class name from config for
43

44     // template loading
45

46     private static TemplateLoader templateLoader; // Class to find and load
47

48     // templates
49

50     private static int intRdm = 0;
51
52     private static String JavaDoc templateString = "";
53
54     private static ArrayList templatesToDelete = new ArrayList();
55
56     // function to get the template, replace getArticle package tags by
57
// getArticleTemp package tags and display
58
// preview page.
59

60     public static void preview(HashMap glossary, HttpServletResponse res) throws ServletException, IOException {
61         String JavaDoc templateId = "";
62         String JavaDoc pageId = "";
63
64         try {
65             Class JavaDoc c = Class.forName(CDSServlet.templateLoaderClass);
66             templateLoader = (TemplateLoader) c.newInstance();
67             templateLoader.setTemplateRoot(CDSServlet.templatePath);
68             templateLoader.setDefaultIndex(CDSServlet.listTemplate);
69             templateLoader.setDefaultObject(CDSServlet.fileTemplate);
70         } catch (Exception JavaDoc e) {
71             System.err.println("Error loading template loader class:");
72             e.printStackTrace(System.err);
73             boolean errorInitializing = true;
74         }
75
76         // initialize
77
CofaxPage page = new CofaxPage();
78         page.reset();
79         PrintWriter out = res.getWriter();
80
81         try {
82             // find the template file
83
pageId = glossary.get("request:pageId").toString();
84             templateLoader.setTemplateSearch(CDSServlet.templatePath + "/" + glossary.get("request:templateSearch").toString());
85             templateId = templateLoader.choose(glossary.get("request:FILENAME").toString() + ".htm", "", ".htm");
86             if (templateId.equals("")) {
87                 page.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
88                 page.setErrorMsg("Error: Template not found to satisfy request.");
89             }
90
91             // load the template and replace tags
92
modifyTemplateToPreview(templateId);
93
94             // get a random number to set new file names
95
String JavaDoc suffix = templateId.substring(templateId.lastIndexOf(".") + 1);
96             String JavaDoc templatePreview = "articlePreview" + intRdm + "." + suffix;
97             String JavaDoc path = templateId.substring(0, templateId.lastIndexOf("/")) + "/" + templatePreview;
98             templatesToDelete.add(path);
99
100             // Save the template and its subtemplates
101
templateString = replaceTagsInTemplate(templateString);
102
103             File deletefile = new File(path);
104             boolean delete = true;
105             if (deletefile.exists()) {
106                 delete = deletefile.delete();
107             }
108             if (delete) {
109                 File newTemplate = new File(path);
110                 newTemplate.createNewFile();
111                 PrintWriter fileOutputStream = new PrintWriter(new FileWriter(path, true));
112                 fileOutputStream.println(templateString);
113                 fileOutputStream.close();
114             } else {
115                 System.err.println("CofaxToolsPreview : Can not delete " + templatePreview);
116             }
117
118             // Send the requested url with the new template
119
String JavaDoc urlString = glossary.get("request:requestedUrl") + "?template=" + templatePreview + "&ITEMID=" + glossary.get("request:ITEMID");
120             URL urlcnx = new URL(urlString);
121             InputStream is = urlcnx.openStream();
122             BufferedReader bf = new BufferedReader(new InputStreamReader(is));
123             String JavaDoc line;
124             PrintWriter status = res.getWriter();
125             while ((line = bf.readLine()) != null) {
126                 status.print(line);
127             }
128             status.close();
129
130             // Delete all the templates created
131
while (templatesToDelete.size() > 0) {
132                 try {
133                     File deletefile2 = new File((String JavaDoc) templatesToDelete.get(0));
134                     if (deletefile2.exists()) {
135                         delete = deletefile2.delete();
136                     }
137                 } catch (Exception JavaDoc e) {
138                     System.err.println("CofaxToolsPreview : Exception:" + e);
139                 }
140                 templatesToDelete.remove(0);
141             }
142
143         } catch (Exception JavaDoc e) {
144             System.err.println("CofaxToolsPreview : Exception:" + e);
145         }
146
147     }
148
149     // function to initialize glossary for PREVIEW ONLY !!
150
public static HashMap initglossary(String JavaDoc code, DataStore db, HttpServletRequest req, boolean articleSection) {
151
152         HashMap ht = new HashMap();
153         String JavaDoc tag, pathInfo;
154         if (articleSection) {
155             ht.put("ITEMID", code);
156             tag = CofaxToolsDbUtils.fillTag(db, "getArticleByItemID");
157         } else {
158             ht.put("MAPPINGCODE", code);
159             tag = CofaxToolsDbUtils.fillTag(db, "getSectionByMappingCode");
160         }
161         ht = CofaxToolsDbUtils.getNameValuePackageHash(db, ht, tag);
162
163         HashMap glossary = new HashMap();
164         glossary.put("request:SECTION", ht.get("SECTION"));
165         glossary.put("request:SECTIONNAME", ht.get("SECTIONNAME"));
166         glossary.put("request:PUBNAME", ht.get("PUBNAME"));
167         if (articleSection)
168             glossary.put("request:ITEMID", code);
169         if (articleSection)
170             glossary.put("request:FILENAME", ht.get("FILENAME"));
171         // Setting the url
172
Object JavaDoc tmp = req.getHeader("host");
173         String JavaDoc hostName;
174         if (tmp != null) {
175             hostName = (String JavaDoc) tmp;
176         } else {
177             hostName = req.getServerName();
178         }
179         pathInfo = "/" + (String JavaDoc) glossary.get("request:PUBNAME") + "/" + (String JavaDoc) glossary.get("request:SECTIONNAME") + "/";
180         if (articleSection)
181             pathInfo = pathInfo + (String JavaDoc) glossary.get("request:FILENAME") + ".htm";
182
183         String JavaDoc RequestURI = CofaxToolsServlet.aliasPath + pathInfo;
184         String JavaDoc requestedUrl = req.getScheme() + "://" + hostName + RequestURI;
185         glossary.put("request:pageId", pathInfo);
186         glossary.put("request:requestedUrl", requestedUrl);
187         glossary.put("request:pathInfo", pathInfo);
188         glossary.put("request:templateSearch", (String JavaDoc) glossary.get("request:PUBNAME") + "/" + (String JavaDoc) glossary.get("request:SECTIONNAME"));
189
190         return (glossary);
191     }
192
193     // function to initialize glossary for PREVIEW ONLY !!
194
public static HashMap initglossary(String JavaDoc itemID, DataStore db, HttpServletRequest req) {
195         return initglossary(itemID, db, req, true);
196     }
197
198     private static void modifyTemplateToPreview(String JavaDoc templateId) {
199         CofaxPage template = templateLoader.load(templateId);
200         templateString = template.toString();
201         intRdm = (int) java.lang.Math.rint((double) java.lang.Math.random() * 100);
202         String JavaDoc path = templateId.substring(0, templateId.lastIndexOf("/")) + "/";
203
204         // see if there are sub templates
205
int done = 0;
206         while (templateString.substring(done).indexOf("<%@ include file=") > -1) {
207             String JavaDoc templateStringWork = templateString.substring(done);
208             int i = templateStringWork.indexOf("<%@ include file=") + 18;
209             templateStringWork = templateStringWork.substring(i);
210             String JavaDoc subTemplate = templateStringWork.substring(0, templateStringWork.indexOf("\""));
211             String JavaDoc suffix = subTemplate.substring(subTemplate.lastIndexOf(".") + 1);
212             String JavaDoc subTemplateToReplace = subTemplate.substring(0, subTemplate.lastIndexOf(".")) + intRdm + "." + suffix;
213             templateString = CofaxUtil.replace(templateString, "<%@ include file=\"" + subTemplate + "\"", "<%@ include file=\"" + subTemplateToReplace + "\"");
214             saveRdmTemplate(intRdm, path + subTemplate);
215             done += i;
216
217         }
218     }
219
220     private static void saveRdmTemplate(int intRdm, String JavaDoc templatePath) {
221         try {
222             // get the path of the temporary template
223
String JavaDoc suffix = templatePath.substring(templatePath.lastIndexOf(".") + 1);
224             String JavaDoc filename = templatePath.substring(templatePath.lastIndexOf("/") + 1, templatePath.lastIndexOf("."));
225             String JavaDoc templatePreview = filename + intRdm + "." + suffix;
226             String JavaDoc path = templatePath.substring(0, templatePath.lastIndexOf("/")) + "/" + templatePreview;
227             templatesToDelete.add(path);
228
229             // replace tags in the current template
230
String JavaDoc templateString2 = replaceTagsInTemplateFile(templatePath);
231
232             File deletefile = new File(path);
233             boolean delete = true;
234             if (deletefile.exists()) {
235                 delete = deletefile.delete();
236             }
237             if (delete) {
238                 File newTemplate = new File(path);
239                 newTemplate.createNewFile();
240                 PrintWriter fileOutputStream = new PrintWriter(new FileWriter(path, true));
241                 fileOutputStream.println(templateString2);
242                 fileOutputStream.close();
243             } else {
244                 System.err.println("CofaxToolsPreview : Can not delete " + templatePreview);
245             }
246         } catch (Exception JavaDoc e) {
247             System.err.println("CofaxToolsPreview : Error while processing template '" + templatePath + "' : " + e);
248         }
249
250     }
251
252     private static String JavaDoc replaceTagsInTemplateFile(String JavaDoc templatePath) {
253         CofaxPage template2 = templateLoader.load(templatePath);
254         String JavaDoc templateString2 = template2.toString();
255         return replaceTagsInTemplate(templateString2);
256
257     }
258
259     private static String JavaDoc replaceTagsInTemplate(String JavaDoc templateString) {
260         String JavaDoc templateString2 = templateString;
261         try {
262             // replace tags in the current template
263
templateString2 = CofaxUtil.replace(templateString2, "getArticle", "getArticleTemp");
264             templateString2 = CofaxUtil.replace(templateString2, "getSection", "getSectionTemp");
265             templateString2 = CofaxUtil.replace(templateString2, "getRelatedLinks", "getRelatedLinksTemp");
266
267         } catch (Exception JavaDoc e) {
268             System.err.println("CofaxToolsPreview : replaceTagsInTemplate : '" + templatePath + "' : " + e);
269         }
270         return templateString2;
271
272     }
273
274 }
275
Popular Tags