1 22 package org.enhydra.kelp.common; 23 24 import org.enhydra.tool.ToolBoxInfo; 26 import org.enhydra.tool.common.PathHandle; 27 import org.enhydra.tool.common.FileUtil; 28 import org.enhydra.tool.common.ExtensionFilter; 29 import org.enhydra.tool.configure.ConfigTool; 30 31 import org.enhydra.kelp.common.Constants; 33 import org.enhydra.kelp.common.importer.JavaFilter; 34 import org.enhydra.kelp.common.node.OtterTemplateNode; 35 import org.enhydra.kelp.common.node.OtterProject; 36 37 import java.io.BufferedReader ; 39 import java.io.IOException ; 40 import java.io.File ; 41 import java.io.FileFilter ; 42 import java.io.FileNotFoundException ; 43 import java.io.FileReader ; 44 import java.util.Vector ; 45 46 public class Backward { 48 49 private final static String RELATIVE_CLASSES = "= ../classes"; 51 52 private Vector phVector = new Vector (); 54 55 public static boolean createIsWebApp(String [] sourcePaths) { 56 boolean webApp = true; 57 PresentationFilter filter = new PresentationFilter(); 58 59 for (int i = 0; i < sourcePaths.length; i++) { 60 File found = FileUtil.findFirst(filter, sourcePaths[i]); 61 62 if (found != null) { 63 webApp = filter.isWebApp(); 64 break; 65 } 66 } 67 return webApp; 68 } 69 70 public static String createDefaultInputPath(String in, 71 String [] sourcePaths) { 72 PathHandle handle = null; 73 File file = null; 74 String out = new String (in); 75 76 file = new File (out); 77 if (file.isDirectory()) { 78 79 } else if (file.getParentFile().exists()) { 81 file = new File (file.getParentFile(), Constants.DIR_INPUT); 82 if (file.exists()) { 83 out = file.getAbsolutePath(); 84 85 } else { 87 ExtensionFilter filter = new ExtensionFilter(); 88 StringBuffer mkIn = new StringBuffer (); 89 90 mkIn.append('.'); 91 mkIn.append(Constants.TYPE_MK); 92 mkIn.append('.'); 93 mkIn.append(Constants.TYPE_IN); 94 filter.setDirectoryValid(false); 95 filter.addExtension(Constants.TYPE_IN); 96 filter.addExclusion(mkIn.toString()); 97 for (int i = 0; i < sourcePaths.length; i++) { 98 file = FileUtil.findFirst(filter, sourcePaths[i]); 99 if (file != null) { 100 out = file.getParent(); 101 break; 102 } 103 } 104 } 105 } 106 out = PathHandle.createPathString(out); 107 return out; 108 } 109 110 public static String createDefaultBootstrapPath(String in, 111 OtterProject project) { 112 String out = new String (in); 113 String deployPath = new String (); 114 String inputPath = new String (); 115 File file = new File (out); 116 BootstrapFilter filter = null; 117 118 if (file.isFile()) { 119 out = PathHandle.createPathString(file); 120 121 } else { 123 deployPath = project.getDeployRootPath(); 124 filter = new BootstrapFilter(false); 125 file = FileUtil.findFirst(filter, deployPath); 126 if (file != null && file.isFile()) { 127 out = PathHandle.createPathString(file); 128 129 } else { 131 inputPath = project.getDeployInputPath(); 132 filter = new BootstrapFilter(true); 133 file = FileUtil.findFirst(filter, inputPath); 134 if (file != null && file.isFile()) { 135 out = PathHandle.createPathString(file); 136 out = deployPath 137 + out.substring(inputPath.length(), 138 out.length() - 3); 139 } 140 } 141 } 142 return out; 143 } 144 145 public static String [][] createReplacementTable(String [][] tableIn, 146 OtterTemplateNode[] templates) { 147 String [][] tableOut = new String [0][2]; 148 String [] placeholders = new String [0]; 149 Backward back = null; 150 151 back = new Backward(); 152 placeholders = back.findPlaceholders(templates); 153 if (placeholders.length == 0) { 154 tableOut = tableIn; 155 } else { 156 tableOut = new String [tableIn.length + placeholders.length][2]; 157 for (int i = 0; i < placeholders.length; i++) { 158 tableOut[i][0] = placeholders[i]; 159 160 if (placeholders[i].equals(Backward.RELATIVE_CLASSES)) { 162 tableOut[i][1] = "= @JAVA_DEPLOY_PATH@/../classes"; 163 } else if (placeholders[i].equals("@CLASSES@")) { 164 tableOut[i][1] = "@JAVA_DEPLOY_PATH@/../classes"; 165 } else if (placeholders[i].equals("@SERVLET_CONF_DIR@")) { 166 tableOut[i][1] = "@JAVA_DEPLOY_PATH@/servlet"; 167 } else if (placeholders[i].equals("@OUTPUT@")) { 168 tableOut[i][1] = "@JAVA_DEPLOY_PATH@"; 169 } else { 170 tableOut[i][1] = placeholders[i]; 171 } 172 } 173 for (int i = 0; i < tableIn.length; i++) { 174 tableOut[i + placeholders.length][0] = tableIn[i][0]; 175 tableOut[i + placeholders.length][1] = tableIn[i][1]; 176 } 177 } 178 return tableOut; 179 } 180 181 private String [] findPlaceholders(OtterTemplateNode[] templates) { 182 String placeholders[] = new String [0]; 183 184 phVector.clear(); 185 for (int i = 0; i < templates.length; i++) { 186 findPlaceholdersInFile(templates[i].getFilePath()); 187 } 188 placeholders = new String [phVector.size()]; 189 placeholders = (String []) phVector.toArray(placeholders); 190 phVector.clear(); 191 return placeholders; 192 } 193 194 private void findPlaceholdersInFile(String path) { 195 BufferedReader reader = null; 196 File file = null; 197 String line = null; 198 199 file = new File (path); 200 if (file.isFile()) { 201 try { 202 reader = new BufferedReader (new FileReader (file)); 203 line = reader.readLine(); 204 while (line != null) { 205 findRelativeClasses(line); 206 findPlaceholdersInLine(line); 207 line = reader.readLine(); 208 } 209 reader.close(); 210 } catch (FileNotFoundException e) { 211 e.printStackTrace(); 212 } catch (IOException e) { 213 e.printStackTrace(); 214 } 215 } 216 } 217 218 private void findRelativeClasses(String line) { 219 int index = line.indexOf(Backward.RELATIVE_CLASSES); 220 221 if (index > 0) { 222 if (!phVector.contains(Backward.RELATIVE_CLASSES)) { 223 phVector.insertElementAt(Backward.RELATIVE_CLASSES, 0); 224 } 225 } 226 } 227 228 private void findPlaceholdersInLine(String line) { 229 String search = new String (line); 230 String [] exclude = ConfigTool.getSuffixArray(); 231 StringBuffer ph = new StringBuffer (); 232 int index = -1; 233 boolean start = false; 234 boolean include = true; 235 236 index = search.indexOf('@'); 237 while (index > -1) { 238 if (start) { 239 ph.setLength(0); 240 ph.append('@'); 241 ph.append(search.substring(0, index)); 242 ph.append('@'); 243 if (!phVector.contains(ph.toString())) { 244 include = true; 245 for (int i = 0; i < exclude.length; i++) { 246 if (ph.toString().endsWith(exclude[i])) { 247 include = false; 248 break; 249 } 250 } 251 if (include) { 252 phVector.addElement(ph.toString()); 253 } 254 } 255 } 256 if (search.length() > index) { 257 search = search.substring(index + 1); 258 } else { 259 search = new String (); 260 } 261 start = (!start); 262 index = search.indexOf('@'); 263 } 264 } 265 266 } 267 268 class BootstrapFilter implements FileFilter { 270 private boolean template = false; 271 272 public BootstrapFilter(boolean t) { 273 template = true; 274 } 275 276 public boolean accept(File file) { 277 boolean accept = false; 278 String path = file.getAbsolutePath().toLowerCase(); 279 StringBuffer multiserverBuf = new StringBuffer (); 280 StringBuffer servletBuf = new StringBuffer (); 281 StringBuffer kernelBuf = new StringBuffer (); 282 StringBuffer bootstrapBuf = new StringBuffer (); 283 StringBuffer bootstrap4Buf = new StringBuffer (); 284 285 multiserverBuf.append(Constants.FILE_MULTISERVER_CONF); 287 servletBuf.append(Constants.FILE_SERVLET_CONF); 288 kernelBuf.append(Constants.FILE_KERNEL_CONF); 289 bootstrapBuf.append(Constants.FILE_BOOTSTRAP_CONF); 290 bootstrap4Buf.append(Constants.FILE_BOOTSTRAP4_CONF); 291 if (template) { 292 multiserverBuf.append('.'); 293 multiserverBuf.append(Constants.TYPE_IN); 294 servletBuf.append('.'); 295 servletBuf.append(Constants.TYPE_IN); 296 kernelBuf.append('.'); 297 kernelBuf.append(Constants.TYPE_IN); 298 bootstrapBuf.append('.'); 299 bootstrapBuf.append(Constants.TYPE_IN); 300 bootstrap4Buf.append('.'); 301 bootstrap4Buf.append(Constants.TYPE_IN); 302 } 303 if (file.isFile()) { 304 if (ToolBoxInfo.isEnhydra3()) { 305 if (path.endsWith(multiserverBuf.toString()) 306 || path.endsWith(servletBuf.toString())) { 307 accept = true; 308 } 309 } else { 310 if (path.endsWith(kernelBuf.toString()) 311 || path.endsWith(bootstrapBuf.toString()) 312 || path.endsWith(bootstrap4Buf.toString())) { 313 accept = true; 314 } 315 } 316 } 317 return accept; 318 } 319 320 } 321 322 class PresentationFilter extends JavaFilter { 324 325 private final static String HTTP_PRESENTATION = "HttpPresentation"; 327 private final static String HTTP_SERVLET = "HttpServlet"; 328 329 private boolean webApp = true; 331 332 public boolean accept(File f) { 333 boolean accept = super.accept(f); 334 335 if (accept && f.canRead()) { 336 accept = false; 337 BufferedReader reader = null; 338 String line = new String (); 339 340 try { 341 reader = new BufferedReader (new FileReader (f)); 342 line = reader.readLine(); 343 while (line != null) { 344 if (line.indexOf(HTTP_PRESENTATION) > -1) { 345 webApp = false; 346 accept = true; 347 break; 348 } else if (line.indexOf(HTTP_SERVLET) > -1) { 349 webApp = true; 350 accept = true; 351 break; 352 } 353 line = reader.readLine(); 354 } 355 reader.close(); 356 } catch (IOException e) { 357 e.printStackTrace(); 358 accept = false; 359 } 360 } 361 return accept; 362 } 363 364 protected boolean isWebApp() { 365 return webApp; 366 } 367 368 } 369 | Popular Tags |