1 23 package org.enhydra.kelp.common; 24 25 import org.enhydra.tool.ToolBoxInfo; 27 import org.enhydra.tool.common.PathHandle; 28 import org.enhydra.tool.common.FileUtil; 29 30 import org.enhydra.kelp.common.map.Mapper; 32 import org.enhydra.kelp.common.importer.ResourceFilter; 33 import org.enhydra.kelp.common.node.OtterNode; 34 import org.enhydra.kelp.common.node.OtterFileNode; 35 import org.enhydra.kelp.common.node.OtterNodeFactory; 36 import org.enhydra.kelp.common.node.OtterProject; 37 import org.enhydra.kelp.common.node.OtterTemplateNode; 38 39 import java.io.File ; 41 import java.io.FileFilter ; 42 import java.util.ArrayList ; 43 44 public class PathUtil { 46 47 private final static String UNTITLED = "untitled"; private final static String _PATH = "_PATH@"; 51 public PathUtil() {} 53 54 public static boolean isInputInSource(OtterProject project) { 55 PathHandle inPath = null; 56 PathHandle srcPath = null; 57 String [] srcs = new String [0]; 58 boolean inSource = false; 59 60 inPath = PathHandle.createPathHandle(project.getDeployInputPath()); 61 srcs = project.getSourcePathArray(); 62 for (int i = 0; i < srcs.length; i++) { 63 srcPath = PathHandle.createPathHandle(srcs[i]); 64 if (srcPath.parentOf(inPath) || srcPath.equals(inPath)) { 65 inSource = true; 66 break; 67 } 68 } 69 return inSource; 70 } 71 72 public static boolean isTemplate(OtterFileNode node) { 73 boolean temp = false; 74 PathHandle handleNode = null; 75 PathHandle handleDeploy = null; 76 77 handleNode = PathHandle.createPathHandle(node.getFilePath()); 78 if (handleNode.hasExtension(Constants.TYPE_IN)) { 79 OtterProject project = node.getProject(); 80 81 handleDeploy = 82 PathHandle.createPathHandle(project.getDeployInputPath()); 83 if (handleDeploy.parentOf(handleNode)) { 84 temp = true; 85 } 86 } 87 return temp; 88 } 89 90 public static String getDefaultDeployArchivePath(OtterProject project) { 91 File f = null; 92 StringBuffer path = new StringBuffer (); 93 int type = OtterProject.TYPE_UNKNOWN; 95 96 f = new File (project.getDeployRootPath()); 97 type = project.getDeployType(); 98 path.append(f.getAbsolutePath()); 99 path.append(File.separator); 100 path.append(Constants.DIR_LIB); 101 path.append(File.separator); 102 if (project == null) { 103 path.append(PathUtil.UNTITLED); 104 } else { 105 path.append(project.getProjectRootName()); 106 } 107 path.append('.'); 108 switch (type) { 109 case OtterProject.TYPE_WEBAPP: 110 path.append(Constants.TYPE_WAR); 111 break; 112 case OtterProject.TYPE_EN3APP: 113 path.append(Constants.TYPE_JAR); 116 break; 117 case OtterProject.TYPE_UNKNOWN: 118 path.append(Constants.TYPE_JAR); 119 break; 120 } 121 return PathHandle.createPathString(path.toString()); 122 } 123 124 public static String [][] expandReplacementTable(OtterProject project, 125 String [][] table) { 126 for (int i = 0; i < table.length; i++) { 127 if (table[i][0].endsWith(PathUtil._PATH)) { 128 table[i][1] = PathUtil.expandPathRelativeToProject(project, 129 table[i][1]); 130 } 131 } 132 return table; 133 } 134 135 public static String getDefaultDeployBootstrapPath(OtterProject project) { 136 String conf = PathUtil.getDeployConfPath(project); 137 String def = new String (); 138 StringBuffer path = new StringBuffer (); 139 PathHandle ph = null; 140 141 if (ToolBoxInfo.isEnhydra3()) { 142 path.append(conf); 143 path.append(File.separator); 144 path.append(Constants.FILE_MULTISERVER_CONF); 145 149 } else { 150 path.append(conf); 151 path.append(File.separator); 152 path.append(Constants.FILE_BOOTSTRAP_CONF); 153 } 154 ph = PathHandle.createPathHandle(path.toString()); 155 if (!ph.isFile()) { 156 ph.createPathHandle(Backward.createDefaultBootstrapPath(path.toString(), 157 project)); 158 } 159 return ph.getPath(); 160 } 161 162 public static String getDeployConfPath(OtterProject project) { 163 return PathUtil.getDeployConfPath(project.getDeployRootPath()); 164 } 165 166 public static String getDeployConfPath(String root) { 167 StringBuffer path = new StringBuffer (); 168 169 path.append(root); 170 path.append(File.separator); 171 path.append(Constants.DIR_CONF); 172 return PathHandle.createPathString(path.toString()); 173 } 174 175 public static String getDeployContentPath(OtterProject project) { 176 return PathUtil.getDeployContentPath(project, 177 project.getDeployRootPath()); 178 } 179 180 public static String getDeployContentPath(OtterProject project, 181 String root) { 182 StringBuffer buf = new StringBuffer (); 183 String path = new String (); 184 Mapper mapper = null; 185 int type = OtterProject.TYPE_UNKNOWN; 186 187 if (project != null) { 188 type = project.getDeployType(); 189 } 190 switch (type) { 191 195 case OtterProject.TYPE_EN3APP: 196 String resourcePath = new String (); 197 String sourcePath = new String (); 198 199 resourcePath = project.getDeployResourcePath(); 200 sourcePath = PathUtil.getSourcePathOf(project, resourcePath); 201 mapper = new Mapper(project); 202 path = mapper.getMappedOutputPath(sourcePath, resourcePath); 203 break; 204 default: 205 buf.append(root); 206 buf.append(File.separator); 207 buf.append(Constants.DIR_CONTENT); 208 path = buf.toString(); 209 break; 210 } 211 return PathHandle.createPathString(path); 212 } 213 214 public static String getDefaultDeployResourcePath(OtterProject project, 215 String appPackPath, String sourcePath) { 216 String path = sourcePath; 217 StringBuffer searchRootPath = new StringBuffer (); 218 File firstFound = null; 219 ResourceFilter resourceFilter = null; 220 221 resourceFilter = new ResourceFilter(); 222 resourceFilter.setResourcePath(true); 223 resourceFilter.setProject(project); 224 searchRootPath.append(sourcePath); 225 searchRootPath.append(File.separator); 226 searchRootPath.append(appPackPath); 227 firstFound = FileUtil.findFirst((FileFilter ) resourceFilter, 228 searchRootPath.toString()); 229 if (firstFound == null) { 230 resourceFilter.setResourcePath(false); 231 firstFound = FileUtil.findFirst((FileFilter ) resourceFilter, 232 searchRootPath.toString()); 233 } 234 if (firstFound != null) { 235 path = firstFound.getParent(); 236 } 237 return PathHandle.createPathString(path); 238 } 239 240 250 public static void putFileRelativeToProject(OtterNode node, 251 String propertyName, 252 String inFilename) { 253 String outFilename = compressPathRelativeToProject(node, inFilename); 254 255 node.setProperty(propertyName, outFilename); 256 } 257 258 269 public static File getFileRelativeToProject(OtterNode node, 270 String propertyName) { 271 File file = null; 272 String filename = node.getProperty(propertyName); 273 274 if ((filename != null) && (filename.trim().length() > 0)) { 275 filename = PathUtil.expandPathRelativeToProject(node, filename); 276 file = new File (filename); 277 } 278 return file; 279 } 280 281 292 public static String compressPathRelativeToProject(OtterNode node, 293 String in) { 294 PathHandle ph = null; 295 String out = null; 296 297 ph = PathHandle.createPathHandle(node.getProject().getRootPath()); 298 out = ph.getRelativePath(in); 299 return out; 300 } 301 302 313 public static String expandPathRelativeToProject(OtterNode node, 314 String inPath) { 315 PathHandle ph = null; 316 317 ph = PathHandle.createPathHandle(node.getProject().getRootPath()); 318 ph = ph.expandRelativePath(inPath); 319 return ph.getPath(); 320 } 321 322 public static OtterTemplateNode[] getInputTemplates(OtterProject project) { 323 OtterFileNode[] input = null; 324 OtterTemplateNode[] templates = null; 325 OtterNodeFactory factory = null; 326 ArrayList list = new ArrayList (); 327 328 input = project.getAllInput(); 329 factory = project.getNodeFactory(); 330 for (int i = 0; i < input.length; i++) { 331 PathHandle cursor = null; 332 333 cursor = PathHandle.createPathHandle(input[i].getFilePath()); 334 if (cursor.hasExtension(Constants.TYPE_IN)) { 335 OtterTemplateNode newNode = null; 336 337 newNode = factory.getTemplateNode(input[i]); 338 list.add(newNode); 339 } 340 } 341 list.trimToSize(); 342 templates = new OtterTemplateNode[list.size()]; 343 templates = (OtterTemplateNode[]) list.toArray(templates); 344 list.clear(); 345 return templates; 346 } 347 348 public static OtterFileNode[] getInputPassthrough(OtterProject project) { 349 OtterFileNode[] input = null; 350 OtterFileNode[] pass = null; 351 ArrayList list = new ArrayList (); 352 353 input = project.getAllInput(); 354 for (int i = 0; i < input.length; i++) { 355 PathHandle cursor = null; 356 357 cursor = PathHandle.createPathHandle(input[i].getFilePath()); 358 if (cursor.hasExtension(Constants.TYPE_IN)) { 359 360 } else { 362 list.add(input[i]); 363 } 364 } 365 list.trimToSize(); 366 pass = new OtterFileNode[list.size()]; 367 pass = (OtterFileNode[]) list.toArray(pass); 368 list.clear(); 369 return pass; 370 } 371 372 public static String getSourcePathOf(OtterProject project, 373 String sourceFile) { 374 String [] path = project.getSourcePathArray(); 375 PathHandle sourcePath = null; 376 PathHandle filePath = null; 377 PathHandle parentPath = null; 378 379 filePath = PathHandle.createPathHandle(sourceFile); 380 sourcePath = filePath.getParent(); 381 for (int i = 0; i < path.length; i++) { 382 parentPath = PathHandle.createPathHandle(path[i]); 383 if (parentPath.parentOf(filePath)) { 384 sourcePath.setPath(parentPath.getPath()); 385 break; 386 } 387 } 388 return sourcePath.getPath(); 389 } 390 391 public static String getOutputPath(OtterTemplateNode node) { 392 PathHandle phInput = null; 393 PathHandle phOut = null; 394 StringBuffer path = new StringBuffer (); 395 String thisPath = null; 396 OtterProject project = null; 397 398 project = node.getProject(); 399 phInput = PathHandle.createPathHandle(project.getDeployInputPath()); 400 thisPath = node.getFilePath(); 401 path.append(project.getDeployRootPath()); 402 if (phInput.parentOf(thisPath)) { 403 int start = project.getDeployInputPath().length(); 404 int end = thisPath.length() - 3; 405 406 path.append(thisPath.substring(start, end)); 407 } else { 408 File thisFile = new File (thisPath); 409 410 path.append(File.separator); 411 path.append(thisFile.getName().substring(0, 412 thisFile.getName().length() 413 - 3)); 414 } 415 phOut = PathHandle.createPathHandle(path.toString()); 416 return phOut.getPath(); 417 } 418 419 public static String getAutoDeployPath(int type) { 420 StringBuffer buf = new StringBuffer (); 421 422 buf.append(ToolBoxInfo.getEnhydraRoot()); 423 buf.append(File.separator); 424 if (type == OtterProject.TYPE_EN3APP) { 425 buf.append("apps"); 426 } else { 427 buf.append("deploy"); 428 } 429 return PathHandle.createPathString(buf.toString()); 430 } 431 432 public static String getAutoDodsPath(int type) { 433 StringBuffer buf = new StringBuffer (); 434 435 buf.append(ToolBoxInfo.getEnhydraRoot()); 436 buf.append(File.separator); 437 if (type == OtterProject.TYPE_EN3APP) { 438 buf.append("apps"); 439 } else { 440 buf.append("dods"); 441 } 442 return PathHandle.createPathString(buf.toString()); 443 } 444 445 } 446 | Popular Tags |