1 22 23 package org.cofax; 24 25 import java.io.*; 26 import javax.servlet.http.*; 27 28 46 public class FilesTemplateLoader extends TemplateLoader { 47 48 59 private String find(String path, String fileName) { 60 61 String templateRoot = getTemplateRoot(); 62 int templateMode = getTemplateMode(); 63 int templateSearchLimit = getTemplateSearchLimit(); 64 65 if (templateSearchLimit > 0) { 66 String calcPath = CofaxUtil.replace(path, templateRoot, "") + "/"; 68 path = templateRoot; 70 if (templateSearchLimit > 1) { 73 String newPath = ""; 74 int slashLoc = 0; 75 for (int x = 0; x < templateSearchLimit - 1; x++) { 76 slashLoc = calcPath.indexOf("/", slashLoc + 1); 77 if (slashLoc > -1) { 78 newPath = calcPath.substring(0, slashLoc); 79 } else { 80 break; 81 } 82 } 83 path = path + newPath; 84 } 85 } 86 87 String jspFileName = ""; 88 if (templateMode == 0 || templateMode == 2) { 89 int dotLoc = fileName.lastIndexOf('.'); 90 if (dotLoc > -1) { 91 jspFileName = fileName.substring(0, dotLoc); 92 jspFileName = jspFileName + ".jsp"; 93 } 94 } 95 96 String theTemplate; 97 while (true) { 98 99 if (templateMode == 0 || templateMode == 2) { 100 theTemplate = path + "/" + jspFileName; 101 if (new File(theTemplate).exists()) { 102 return theTemplate; 103 } 104 } 105 106 if (templateMode == 0 || templateMode == 1) { 107 theTemplate = path + "/" + fileName; 108 if (new File(theTemplate).exists()) { 109 return theTemplate; 110 } 111 } 112 113 if (templateRoot.equals(path)) { 114 return ""; 115 } 116 117 int positionOfLastSlash = path.lastIndexOf('/'); 118 if (positionOfLastSlash < 0) { 119 return ""; 120 } else { 121 path = path.substring(0, positionOfLastSlash); 122 } 123 } 124 } 125 126 128 136 public final CofaxPage load(String templateFileName) { 137 138 CofaxPage template = new CofaxPage(); 139 140 String line; 141 try { 142 template.reset(); 143 144 BufferedReader in = new BufferedReader(new FileReader(templateFileName)); 145 while ((line = in.readLine()) != null) { 146 int incLoc = line.indexOf("<!-- #include file=\""); 147 if (incLoc > -1) { 148 int firstQuote = line.indexOf("\""); 149 int lastQuote = line.indexOf("\"", ++firstQuote); 150 String fileName = line.substring(firstQuote, lastQuote); 151 if ((fileName != null) && (!fileName.equals(""))) { 152 String includeFoundAt = ""; 153 if (fileName.startsWith("/")) { 154 includeFoundAt = getTemplateRoot() + fileName; 155 } else { 156 includeFoundAt = find(getTemplateSearch(), fileName); 157 } 158 while (includeFoundAt.indexOf("..") > -1) { 161 int i = includeFoundAt.indexOf(".."); 162 String sub1 = includeFoundAt.substring(0, i - 1); 163 String sub2 = includeFoundAt.substring(i + 3); 164 int j = sub1.lastIndexOf("/"); 165 String sub3 = sub1.substring(0, j + 1); 166 includeFoundAt = sub3 + sub2; 167 } 168 169 if (includeFoundAt.indexOf(getTemplateRoot()) > -1) { 171 if ((includeFoundAt != null) && (!includeFoundAt.equals(""))) { 172 try { 173 BufferedReader in2 = new BufferedReader(new FileReader(includeFoundAt)); 174 String line2; 175 while ((line2 = in2.readLine()) != null) { 176 template.append(line2); 177 template.append(CofaxUtil.NEW_LINE); 178 } 179 } catch (Exception e) { 180 System.err.println("FilesTemplateLoader.load(): include file: erreur: " + e); 181 } 182 } 183 } else { 184 System.out.println("Access to a forbidden template out of the template root."); 185 } 186 } 187 } else { 188 template.append(line); 189 } 190 template.append(CofaxUtil.NEW_LINE); 191 } 192 in.close(); 193 194 } catch (IOException e) { 195 template.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 196 template.setErrorMsg(e.toString()); 197 } 198 199 return template; 200 } 201 202 204 217 public final String choose(String templateFile, String overrideTemplate, String fileNameExtension) { 218 219 if (fileNameExtension.equals("")) { 222 fileNameExtension = ".htm"; 223 } 224 225 String defaultIndex = getDefaultIndex() + fileNameExtension; 226 String alternateIndex = ""; 227 String defaultFile = getDefaultObject() + fileNameExtension; 228 String alternateFile = ""; 229 String templateSearch = getTemplateSearch(); 230 String templateFoundAt; 231 232 if ((templateFile.equals("")) && (overrideTemplate.equals(""))) { 236 237 templateFoundAt = find(templateSearch, defaultIndex); 238 239 } else { 242 243 246 if (!overrideTemplate.equals("")) { 250 251 257 if (overrideTemplate.startsWith("/")) { 258 259 int positionOfLastSlash = overrideTemplate.lastIndexOf('/'); 261 String folderPart = overrideTemplate.substring(0, positionOfLastSlash); 262 String filePart = overrideTemplate.substring(positionOfLastSlash); 263 264 templateFoundAt = find(templateSearch + folderPart, filePart); 265 266 } else { 269 270 templateFoundAt = find(templateSearch, overrideTemplate); 271 } 272 273 } else { 274 275 286 287 templateFoundAt = find(templateSearch, templateFile); 288 289 if (templateFoundAt.equals("")) { 291 templateFoundAt = find(templateSearch, defaultFile); 292 } 293 } 294 } 295 296 return templateFoundAt; 297 } 298 299 301 311 public String getResource(String templateId, String applicationPath) { 312 313 String resourceId = CofaxUtil.replace(templateId, applicationPath, "/"); 314 315 return resourceId; 316 } 317 318 } 319 | Popular Tags |