| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.phoenix.templategen; |
| 34 | | | |
| 35 | | | import java.io.File; |
| 36 | | | import java.io.FileFilter; |
| 37 | | | import java.io.IOException; |
| 38 | | | import java.io.PrintWriter; |
| 39 | | | import java.util.HashMap; |
| 40 | | | import java.util.Iterator; |
| 41 | | | import java.util.Map; |
| 42 | | | |
| 43 | | | import javax.servlet.ServletException; |
| 44 | | | import javax.servlet.http.HttpServlet; |
| 45 | | | import javax.servlet.http.HttpServletRequest; |
| 46 | | | import javax.servlet.http.HttpServletResponse; |
| 47 | | | |
| 48 | | | |
| 49 | | | |
| 50 | | | |
| 51 | | | |
| 52 | | | |
| 53 | | | |
| 54 | | | @author |
| 55 | | | |
| 56 | 0 | | public class TemplateGeneratorServlet |
| 57 | | | extends HttpServlet |
| 58 | | | { |
| 59 | | | private static final long serialVersionUID = 1017987995368589070L; |
| 60 | | | private static final String TEMPLATE_DIR = "templates"; |
| 61 | | | |
| 62 | | | {@inheritDoc} |
| 63 | | | protected void doPost ( |
| 64 | | | HttpServletRequest request, |
| 65 | | | HttpServletResponse response) |
| 66 | | (1) | throws ServletException, IOException |
| 67 | | | { |
| 68 | 0 | | doService(request, response); |
| 69 | 0 | | } |
| 70 | | | |
| 71 | | | {@inheritDoc} |
| 72 | | | protected void doGet ( |
| 73 | | | HttpServletRequest request, |
| 74 | | | HttpServletResponse response) |
| 75 | | (2) | throws ServletException, IOException |
| 76 | | | { |
| 77 | 0 | | doService(request, response); |
| 78 | 0 | | } |
| 79 | | | |
| 80 | | | {@inheritDoc} |
| 81 | | (3) | protected void doService ( |
| 82 | | | HttpServletRequest request, |
| 83 | | | HttpServletResponse response) |
| 84 | | (4) | throws ServletException, IOException |
| 85 | | | { |
| 86 | 0 | | final String action = request.getParameter("action"); |
| 87 | 0 | | if (action == null) |
| 88 | | | { |
| 89 | | | |
| 90 | 0 | | listTemplates(response); |
| 91 | | | } |
| 92 | 0 | (5)(6) | else if (action.equals("showform")) |
| 93 | | | { |
| 94 | | | |
| 95 | 0 | | showTemplateForm(request, response); |
| 96 | | | } |
| 97 | 0 | (7)(8) | else if (action.equals("parametrize")) |
| 98 | | | { |
| 99 | | | |
| 100 | 0 | | parametrizeTemplate(request, response); |
| 101 | | | } |
| 102 | | | else |
| 103 | | | { |
| 104 | | | |
| 105 | 0 | | writeError(response, |
| 106 | | | "Parameter 'action' has unknown value '" + action + "'"); |
| 107 | | | } |
| 108 | 0 | | } |
| 109 | | | |
| 110 | | | private void writeError (HttpServletResponse response, String errorText) |
| 111 | | | throws IOException |
| 112 | | | { |
| 113 | 0 | | response.setContentType("text/html"); |
| 114 | | | |
| 115 | 0 | (9) | final PrintWriter pw = new PrintWriter(response.getOutputStream()); |
| 116 | 0 | | pw.println("<HTML><HEAD><TITLE>Error</TITLE></HEAD>"); |
| 117 | 0 | | pw.println("<BODY><H1>Error:</H1>"); |
| 118 | 0 | | pw.println(errorText); |
| 119 | 0 | | pw.println("</BODY></HTML>"); |
| 120 | 0 | | pw.flush(); |
| 121 | 0 | | } |
| 122 | | | |
| 123 | | | private void parametrizeTemplate ( |
| 124 | | | HttpServletRequest request, |
| 125 | | (10) | HttpServletResponse response) throws ServletException, IOException |
| 126 | | | { |
| 127 | 0 | | final TemplateGenerator templateGen = getTemplateGenerator(request); |
| 128 | 0 | (11) | final String template = request.getParameter("template"); |
| 129 | | | |
| 130 | 0 | (12) | final PrintWriter pw = new PrintWriter(response.getOutputStream()); |
| 131 | 0 | | pw.println("Parametrizing template " + template); |
| 132 | | | |
| 133 | 0 | | final Map paramMap = new HashMap(); |
| 134 | 0 | | for (final Iterator it = templateGen.getParameterList().iterator(); |
| 135 | 0 | | it.hasNext(); ) |
| 136 | | | { |
| 137 | 0 | | final Parameter param = (Parameter) it.next(); |
| 138 | 0 | | final String paramValue = request.getParameter(param.getName()); |
| 139 | 0 | | if (paramValue == null || paramValue.length() == 0) |
| 140 | | | { |
| 141 | 0 | | writeError(response, "ERROR: value for parameter " |
| 142 | | | + param.getName() + " missing"); |
| 143 | 0 | | return; |
| 144 | | | } |
| 145 | | | else |
| 146 | | | { |
| 147 | 0 | | paramMap.put(param.getName(), paramValue); |
| 148 | | | } |
| 149 | 0 | | } |
| 150 | | | |
| 151 | | | try |
| 152 | | | { |
| 153 | 0 | | final byte[] templateZip = templateGen.parametrizeTemplates(paramMap); |
| 154 | 0 | | response.setContentType("application/zip"); |
| 155 | 0 | | response.setHeader("Content-Disposition", |
| 156 | | | "attachment; filename=\"" + template + ".zip\""); |
| 157 | 0 | | response.setContentLength(templateZip.length); |
| 158 | 0 | | response.getOutputStream().write(templateZip); |
| 159 | 0 | | response.getOutputStream().flush(); |
| 160 | | | } |
| 161 | 0 | | catch (TemplateGeneratorException e) |
| 162 | | | { |
| 163 | 0 | (13) | throw new ServletException("Template Generator Error", e); |
| 164 | 0 | | } |
| 165 | 0 | | } |
| 166 | | | |
| 167 | | | private void showTemplateForm ( |
| 168 | | | HttpServletRequest request, |
| 169 | | (14) | HttpServletResponse response) throws IOException, ServletException |
| 170 | | | { |
| 171 | | | |
| 172 | 0 | | final TemplateGenerator templateGen = getTemplateGenerator(request); |
| 173 | 0 | | final String template = request.getParameter("template"); |
| 174 | | | |
| 175 | 0 | | response.setContentType("text/html"); |
| 176 | 0 | (15) | final PrintWriter pw = new PrintWriter(response.getOutputStream()); |
| 177 | 0 | | pw.println("<HTML><HEAD><TITLE>" + template + " Template Form" |
| 178 | | | + "</TITLE></HEAD>"); |
| 179 | 0 | | pw.println("<BODY>"); |
| 180 | 0 | | pw.println("<H1>Template " + template + "</H1>"); |
| 181 | 0 | | pw.println("<P>" + templateGen.getTemplateDescription() + "</P>"); |
| 182 | | | |
| 183 | 0 | | pw.println("<FORM ACTION='templategen?action=parametrize&template=" |
| 184 | | | + template + "' " |
| 185 | | | + "METHOD='POST'>"); |
| 186 | 0 | | pw.println("<table border='0'>"); |
| 187 | 0 | | pw.println("<tr><th>Parameter</th><th>Value</th>" |
| 188 | | | + "<th>Description</th></tr>"); |
| 189 | | | |
| 190 | 0 | | for (final Iterator it = templateGen.getParameterList().iterator(); |
| 191 | 0 | | it.hasNext(); ) |
| 192 | | | { |
| 193 | 0 | | final Parameter param = (Parameter) it.next(); |
| 194 | | | |
| 195 | 0 | | pw.println("<tr>"); |
| 196 | 0 | (16) | pw.println("<td>" + param.getName() + "</td>"); |
| 197 | 0 | | pw.println("<td>"); |
| 198 | | | |
| 199 | 0 | | if (param.isMultiLine()) |
| 200 | | | { |
| 201 | 0 | | pw.print("<textarea name='" + param.getName() + "'"); |
| 202 | 0 | | pw.print(" rows='5' cols='30'>"); |
| 203 | 0 | | if (param.getDefaultValue() != null) |
| 204 | | | { |
| 205 | 0 | | pw.print(param.getDefaultValue()); |
| 206 | | | } |
| 207 | 0 | | pw.print("</textarea>"); |
| 208 | | | } |
| 209 | | | else |
| 210 | | | { |
| 211 | 0 | | pw.print("<input name='" + param.getName() + "'"); |
| 212 | 0 | | pw.print(" size='30'"); |
| 213 | 0 | | if (param.getDefaultValue() != null) |
| 214 | | | { |
| 215 | 0 | | pw.print(" value='" + param.getDefaultValue() + "'"); |
| 216 | | | } |
| 217 | 0 | | pw.print(" maxlength='" + param.getMaxLength() + "'>"); |
| 218 | | | } |
| 219 | | | |
| 220 | 0 | | pw.println("</td>"); |
| 221 | 0 | | pw.println("<td>" + param.getDescription() + "</td>"); |
| 222 | 0 | | pw.println("<tr>"); |
| 223 | 0 | | } |
| 224 | 0 | | pw.println("</table>"); |
| 225 | 0 | | pw.println("<INPUT TYPE='submit' VALUE='Do it'>"); |
| 226 | 0 | | pw.println("</FORM>"); |
| 227 | | | |
| 228 | 0 | | pw.println("</BODY>"); |
| 229 | 0 | | pw.println("</HTML>"); |
| 230 | 0 | | pw.flush(); |
| 231 | 0 | | } |
| 232 | | | |
| 233 | | | private void listTemplates (HttpServletResponse response) |
| 234 | | (17) | throws IOException, ServletException |
| 235 | | | { |
| 236 | 0 | | response.setContentType("text/html"); |
| 237 | | | |
| 238 | 0 | | final File templateDir = getTemplateDirectory(); |
| 239 | | | |
| 240 | 0 | | final FileFilter templateFilter = new FileFilter() |
| 241 | 0 | (18) | { |
| 242 | | | public boolean accept (File file) |
| 243 | | | { |
| 244 | 0 | | return file.getName().endsWith(".zip"); |
| 245 | | | } |
| 246 | | | }; |
| 247 | 0 | | final File[] templates = templateDir.listFiles(templateFilter); |
| 248 | | | |
| 249 | 0 | (19) | final PrintWriter pw = new PrintWriter(response.getOutputStream()); |
| 250 | 0 | | pw.println("<HTML><HEAD><TITLE>Template List</TITLE></HEAD>"); |
| 251 | 0 | | pw.println("<BODY>"); |
| 252 | 0 | | if (templates != null && templates.length > 0) |
| 253 | | | { |
| 254 | 0 | | pw.println("<Table width='60%'><tr><th>Template Name</th>" |
| 255 | | | + "<th>Description</th></tr>"); |
| 256 | 0 | | for (int i = 0; i < templates.length; i++) |
| 257 | | | { |
| 258 | 0 | | pw.println("<tr>"); |
| 259 | 0 | | final String template = templates[i].getName(); |
| 260 | 0 | | final String baseName |
| 261 | | | = template.substring( |
| 262 | | | 0, template.length() - ".zip".length()); |
| 263 | | | |
| 264 | 0 | | pw.print("<td><a href='templategen?action=showform&template=" |
| 265 | | | + baseName + "'>"); |
| 266 | 0 | | pw.print(baseName); |
| 267 | 0 | | pw.println("</a></td>"); |
| 268 | | | |
| 269 | 0 | | final TemplateZip tz |
| 270 | | | = new TemplateZip(templates[i].getAbsolutePath()); |
| 271 | | | try |
| 272 | | | { |
| 273 | 0 | | tz.readTemplateFile(); |
| 274 | | | } |
| 275 | 0 | | catch (Exception e) |
| 276 | | | { |
| 277 | 0 | | throw new ServletException("Failed to read template file " |
| 278 | | | + templates[i], e); |
| 279 | 0 | | } |
| 280 | 0 | | pw.println("<td>"); |
| 281 | 0 | | pw.println(tz.getDescription().getDescription()); |
| 282 | 0 | | pw.println("<td>"); |
| 283 | 0 | | pw.println("</tr>"); |
| 284 | | | } |
| 285 | 0 | | pw.println("</table>"); |
| 286 | | | } |
| 287 | | | else |
| 288 | | | { |
| 289 | 0 | | pw.println("No templates found."); |
| 290 | | | } |
| 291 | 0 | | pw.println("</BODY></HTML>"); |
| 292 | 0 | | pw.flush(); |
| 293 | | | |
| 294 | 0 | | } |
| 295 | | | |
| 296 | | | private File getTemplateDirectory () |
| 297 | | | { |
| 298 | 0 | | final String realPath = getServletContext().getRealPath("/"); |
| 299 | 0 | | final File baseDir = new File(realPath); |
| 300 | 0 | | final File templateDir = new File(baseDir, TEMPLATE_DIR); |
| 301 | 0 | (20)(21) | return templateDir; |
| 302 | | | } |
| 303 | | | |
| 304 | | | private TemplateGenerator getTemplateGenerator (HttpServletRequest request) |
| 305 | | | throws ServletException |
| 306 | | | { |
| 307 | 0 | | final String template = request.getParameter("template"); |
| 308 | 0 | | if (template == null) |
| 309 | | | { |
| 310 | 0 | (22) | throw new ServletException("Parameter 'template' missing"); |
| 311 | | | } |
| 312 | | | |
| 313 | 0 | | final String templateFileName = template + ".zip"; |
| 314 | 0 | | final File templateFile |
| 315 | | | = new File(getTemplateDirectory(), templateFileName); |
| 316 | | | |
| 317 | 0 | | if (! templateFile.exists()) |
| 318 | | | { |
| 319 | 0 | | throw new ServletException("Template file " + templateFileName |
| 320 | | | + " does not exist"); |
| 321 | | | } |
| 322 | | | |
| 323 | | | TemplateGenerator templateGen; |
| 324 | | | try |
| 325 | | | { |
| 326 | 0 | | templateGen = new TemplateGenerator(templateFile.getAbsolutePath()); |
| 327 | | | } |
| 328 | 0 | | catch (Exception e) |
| 329 | | | { |
| 330 | 0 | (23) | throw new ServletException("Failed to open template file", e); |
| 331 | 0 | | } |
| 332 | 0 | | return templateGen; |
| 333 | | | } |
| 334 | | | } |
| 335 | | | |
| 336 | | | |
| 337 | | | |
| 338 | | | |
| 339 | | | |
| 340 | | | |
| 341 | | | |
| 342 | | | |
| 343 | | | |
| 344 | | | |
| 345 | | | |
| 346 | | | |
| 347 | | | |
| 348 | | | |
| 349 | | | |
| 350 | | | |
| 351 | | | |
| 352 | | | |
| 353 | | | |
| 354 | | | |
| 355 | | | |
| 356 | | | |
| 357 | | | |