| 1 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 private static String listTemplate; 38 private static String fileTemplate; 40 private static String templatePath; 42 private static String templateLoaderClass; 44 46 private static TemplateLoader templateLoader; 48 50 private static int intRdm = 0; 51 52 private static String templateString = ""; 53 54 private static ArrayList templatesToDelete = new ArrayList(); 55 56 60 public static void preview(HashMap glossary, HttpServletResponse res) throws ServletException, IOException { 61 String templateId = ""; 62 String pageId = ""; 63 64 try { 65 Class 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 e) { 71 System.err.println("Error loading template loader class:"); 72 e.printStackTrace(System.err); 73 boolean errorInitializing = true; 74 } 75 76 CofaxPage page = new CofaxPage(); 78 page.reset(); 79 PrintWriter out = res.getWriter(); 80 81 try { 82 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 modifyTemplateToPreview(templateId); 93 94 String suffix = templateId.substring(templateId.lastIndexOf(".") + 1); 96 String templatePreview = "articlePreview" + intRdm + "." + suffix; 97 String path = templateId.substring(0, templateId.lastIndexOf("/")) + "/" + templatePreview; 98 templatesToDelete.add(path); 99 100 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 String 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 line; 124 PrintWriter status = res.getWriter(); 125 while ((line = bf.readLine()) != null) { 126 status.print(line); 127 } 128 status.close(); 129 130 while (templatesToDelete.size() > 0) { 132 try { 133 File deletefile2 = new File((String ) templatesToDelete.get(0)); 134 if (deletefile2.exists()) { 135 delete = deletefile2.delete(); 136 } 137 } catch (Exception e) { 138 System.err.println("CofaxToolsPreview : Exception:" + e); 139 } 140 templatesToDelete.remove(0); 141 } 142 143 } catch (Exception e) { 144 System.err.println("CofaxToolsPreview : Exception:" + e); 145 } 146 147 } 148 149 public static HashMap initglossary(String code, DataStore db, HttpServletRequest req, boolean articleSection) { 151 152 HashMap ht = new HashMap(); 153 String 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 Object tmp = req.getHeader("host"); 173 String hostName; 174 if (tmp != null) { 175 hostName = (String ) tmp; 176 } else { 177 hostName = req.getServerName(); 178 } 179 pathInfo = "/" + (String ) glossary.get("request:PUBNAME") + "/" + (String ) glossary.get("request:SECTIONNAME") + "/"; 180 if (articleSection) 181 pathInfo = pathInfo + (String ) glossary.get("request:FILENAME") + ".htm"; 182 183 String RequestURI = CofaxToolsServlet.aliasPath + pathInfo; 184 String 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 ) glossary.get("request:PUBNAME") + "/" + (String ) glossary.get("request:SECTIONNAME")); 189 190 return (glossary); 191 } 192 193 public static HashMap initglossary(String itemID, DataStore db, HttpServletRequest req) { 195 return initglossary(itemID, db, req, true); 196 } 197 198 private static void modifyTemplateToPreview(String 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 path = templateId.substring(0, templateId.lastIndexOf("/")) + "/"; 203 204 int done = 0; 206 while (templateString.substring(done).indexOf("<%@ include file=") > -1) { 207 String templateStringWork = templateString.substring(done); 208 int i = templateStringWork.indexOf("<%@ include file=") + 18; 209 templateStringWork = templateStringWork.substring(i); 210 String subTemplate = templateStringWork.substring(0, templateStringWork.indexOf("\"")); 211 String suffix = subTemplate.substring(subTemplate.lastIndexOf(".") + 1); 212 String 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 templatePath) { 221 try { 222 String suffix = templatePath.substring(templatePath.lastIndexOf(".") + 1); 224 String filename = templatePath.substring(templatePath.lastIndexOf("/") + 1, templatePath.lastIndexOf(".")); 225 String templatePreview = filename + intRdm + "." + suffix; 226 String path = templatePath.substring(0, templatePath.lastIndexOf("/")) + "/" + templatePreview; 227 templatesToDelete.add(path); 228 229 String 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 e) { 247 System.err.println("CofaxToolsPreview : Error while processing template '" + templatePath + "' : " + e); 248 } 249 250 } 251 252 private static String replaceTagsInTemplateFile(String templatePath) { 253 CofaxPage template2 = templateLoader.load(templatePath); 254 String templateString2 = template2.toString(); 255 return replaceTagsInTemplate(templateString2); 256 257 } 258 259 private static String replaceTagsInTemplate(String templateString) { 260 String templateString2 = templateString; 261 try { 262 templateString2 = CofaxUtil.replace(templateString2, "getArticle", "getArticleTemp"); 264 templateString2 = CofaxUtil.replace(templateString2, "getSection", "getSectionTemp"); 265 templateString2 = CofaxUtil.replace(templateString2, "getRelatedLinks", "getRelatedLinksTemp"); 266 267 } catch (Exception e) { 268 System.err.println("CofaxToolsPreview : replaceTagsInTemplate : '" + templatePath + "' : " + e); 269 } 270 return templateString2; 271 272 } 273 274 } 275 | Popular Tags |