1 23 package org.enhydra.kelp.common.importer; 24 25 import org.enhydra.tool.common.FileUtil; 27 import org.enhydra.tool.common.FilenameFilter; 28 import org.enhydra.tool.common.PathHandle; 29 30 import org.enhydra.kelp.common.Constants; 32 import org.enhydra.kelp.common.PathUtil; 33 import org.enhydra.kelp.common.ValidationException; 34 import org.enhydra.kelp.common.ValidationUtil; 35 import org.enhydra.kelp.common.map.MapEntry; 36 import org.enhydra.kelp.common.map.MapUtil; 37 import org.enhydra.kelp.common.node.OtterProject; 38 import org.enhydra.kelp.common.node.PropertyKeys; 39 40 import java.io.File ; 42 import java.util.ResourceBundle ; 43 44 public class ImportPaths implements PropertyKeys { 46 47 static ResourceBundle res = 49 ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); private final String PRESENTATION = "presentation"; private final String [] PACKAGE_KEYS = { 52 ".business", ".data", '.' + PRESENTATION 53 }; 55 private OtterProject project = null; 57 private String pack = new String (); 58 private String [][] packMap = new String [0][2]; 59 private String deployRootPath = null; 60 private String resourcePath = null; 61 private String sourcePath = null; 62 private String rootPath = null; 63 private String defaultSourcePath = null; 64 private String defaultPackage = null; 65 private ResourceFilter resourceFilter = new ResourceFilter(); 66 private JavaFilter javaFilter = new JavaFilter(); 67 private MakefileReader makefileReader = new MakefileReader(); 68 69 public ImportPaths() {} 70 71 protected void initProject() { 72 FilenameFilter filter = null; 73 File firstFound = null; 74 75 getProject().setWorkingPath(getRootPath()); getProject().setDeployRootPath(getDeployRootPath()); 77 getProject().setDeployResourcePath(getResourcePath()); 78 getProject().setClassOutputPath(getClassOutputPath()); 79 getProject().setPackageMap(getPackageMap()); 80 81 filter = new FilenameFilter(); 83 filter.setDirectoryValid(false); 84 filter.setIncludeName(Constants.FILE_WEB_XML); 85 firstFound = FileUtil.findFirst(filter, getSourcePath()); 86 if (firstFound == null) { 87 filter.setIncludeName(Constants.FILE_ENHYDRA_SERVICE_XML); 88 firstFound = FileUtil.findFirst(filter, getSourcePath()); 89 if (firstFound == null) { 90 getProject().setDeployType(OtterProject.TYPE_EN3APP); 91 } else { 92 } 95 } else { 96 getProject().setDeployType(OtterProject.TYPE_WEBAPP); 97 } 98 getProject().setProperty(PropertyKeys.NAME_LIBRARIES, 99 PropertyKeys.VALUE_ENHYDRA); 100 } 101 102 public OtterProject getProject() { 103 return project; 104 } 105 106 public MakefileReader getMakefileReader() { 107 return makefileReader; 108 } 109 110 public JavaFilter getJavaFilter() { 111 return javaFilter; 112 } 113 114 public void setProject(OtterProject p) { 115 project = p; 116 resourceFilter.setProject(project); 117 makefileReader.setProject(project); 118 } 119 120 126 public String getPackage() { 127 return pack; 128 } 129 130 138 public void setPackage(String pac) throws ValidationException { 139 if (ValidationUtil.isJavaPackage(pac)) { 140 pack = pac; 141 } else { 142 throw new ValidationException(res.getString("Not_a_valid_package") 143 + pac); 144 } 145 } 146 147 public void initPackageMap(boolean write) { 148 String [][] readMap = makefileReader.getPackageMap(); 149 150 makefileReader.invalidate(); 151 String [][] defMap = getDefaultPackageMap(); 152 MapEntry[] entries = MapUtil.combine(MapUtil.toEntryArray(readMap), 153 MapUtil.toEntryArray(defMap)); 154 155 packMap = MapUtil.toStringArray(entries); 156 if (write) { 157 getProject().setPackageMap(packMap); 158 } 159 } 160 161 167 public String [][] getPackageMap() { 168 return packMap; 169 } 170 171 177 public void setPackageMap(String [][] m) { 178 packMap = m; 179 } 180 181 184 protected String getPackagePath() { 185 return createPackagePath(getPackage()); 186 } 187 188 191 private String createPackagePath(String packPath) { 192 String path = new String (); 193 194 if (packPath != null) { 195 path = packPath.replace('.', '/'); 196 } 197 return path; 198 } 199 200 206 public String getResourcePath() { 207 if (resourcePath == null) { 208 resourcePath = getDefaultResourcePath(); 209 } 210 return resourcePath; 211 } 212 213 221 public void setResourcePath(String path) throws ValidationException { 222 if (ValidationUtil.isDirectory(path)) { 223 resourcePath = PathHandle.createPathString(path); 224 } else { 225 throw new ValidationException(res.getString("Resource_not_found") 226 + path); 227 } 228 } 229 230 236 public String getRootPath() { 237 return rootPath; 238 } 239 240 248 public void setRootPath(String path) throws ValidationException { 249 if (ValidationUtil.isDirectory(path)) { 250 rootPath = PathHandle.createPathString(path); 251 defaultSourcePath = null; 252 } else { 253 throw new ValidationException(res.getString("Project_root_not_found")); 254 } 255 } 256 257 263 public String getSourcePath() { 264 return sourcePath; 265 } 266 267 275 public void setSourcePath(String path) throws ValidationException { 276 File dir = null; 277 278 if ((path == null) || (path.trim().length() == 0)) { 279 throw new ValidationException(res.getString("Source_path_empty")); 280 } else { 281 dir = new File (path); 282 } 283 if (ValidationUtil.isDirectory(dir)) { 284 sourcePath = PathHandle.createPathString(dir); 285 defaultPackage = null; 286 } else { 287 StringBuffer message = new StringBuffer (); 288 289 if (dir == null) { 290 message.append(res.getString("Source_null")); 291 } else { 292 message.append(res.getString("Source_not_found")); 293 message.append(dir.getAbsolutePath()); 294 } 295 throw new ValidationException(message.toString()); 296 } 297 } 298 299 305 public String getDefaultPackage() { 306 File firstFound = null; 307 int index = 0; 308 309 if (defaultPackage == null) { 310 if (getSourcePath() != null) { 311 firstFound = FileUtil.findFirst(javaFilter, getSourcePath()); 312 } 313 if (firstFound != null) { 314 defaultPackage = ImportTool.readPackage(firstFound); 315 for (int i = 0; i < PACKAGE_KEYS.length; i++) { 316 index = defaultPackage.indexOf(PACKAGE_KEYS[i]); 317 if (index > 0) { 318 defaultPackage = defaultPackage.substring(0, index); 319 break; 320 } 321 } 322 } 323 } 324 return defaultPackage; 325 } 326 327 333 public String [][] getDefaultPackageMap() { 334 String [][] defaultMap = new String [1][2]; 335 StringBuffer buf = new StringBuffer (); 336 File firstFound = null; 337 338 if (getPackage().length() == 0) { 339 buf.append(getDefaultPackage()); 340 } else { 341 buf.append(getPackage()); 342 } 343 if (buf.toString().trim().length() > 0) { 344 buf.append('.'); 345 } 346 buf.append(PRESENTATION); 347 defaultMap[0][0] = getResourcePath(); 348 defaultMap[0][1] = buf.toString(); 349 350 if (getResourcePath() != null) { 353 File resource = new File (getResourcePath()); 354 355 firstFound = FileUtil.findFirst(resourceFilter, resource, 0); 356 } 357 if (firstFound != null) { 358 File parent = firstFound.getParentFile().getParentFile(); 359 File [] dirList = parent.listFiles(); 360 File [] javaList = null; 361 String baseName = firstFound.getName(); 362 363 baseName = baseName.substring(0, baseName.indexOf('.')); 364 for (int i = 0; i < dirList.length; i++) { 365 if (dirList[i].isDirectory()) { 366 javaList = dirList[i].listFiles(javaFilter); 367 for (int j = 0; j < javaList.length; j++) { 368 if (javaList[j].getName().startsWith(baseName)) { 369 defaultMap[0][0] = 370 PathHandle.createPathString(firstFound.getParentFile()); 371 defaultMap[0][1] = 372 ImportTool.readPackage(javaList[j]); 373 break; 374 } 375 } 376 } 377 } 378 } 379 return defaultMap; 380 } 381 382 public String getDefaultResourcePath() { 383 String srcPath = new String (); 384 String appPackagePath = new String (); 385 String path = new String (); 386 387 if (getPackage() == null) { 388 appPackagePath = createPackagePath(getDefaultPackage()); 389 } else { 390 appPackagePath = getPackagePath(); 391 } 392 if (getSourcePath() != null) { 393 srcPath = getSourcePath(); 394 } else if (getDefaultSourcePath() != null) { 395 srcPath = getDefaultSourcePath(); 396 } else if (getRootPath() != null) { 397 srcPath = getRootPath(); 398 } 399 path = PathUtil.getDefaultDeployResourcePath(project, appPackagePath, 400 srcPath); 401 return path; 402 } 403 404 410 public String getDefaultSourcePath() { 411 File firstFound = null; 412 String inPack = new String (); 413 int depth = 0; 414 415 if (defaultSourcePath == null) { 416 if (rootPath != null) { 417 defaultSourcePath = rootPath; 418 firstFound = FileUtil.findFirst(javaFilter, rootPath); 419 } 420 if (firstFound != null) { 421 inPack = ImportTool.readPackage(firstFound); 422 if (ValidationUtil.isJavaPackage(inPack)) { 423 if (inPack.length() > 0) { 424 depth = charCount(inPack, '.') + 1; 425 } 426 } 427 File foundParent = firstFound.getParentFile(); 428 429 while (depth > 0) { 430 foundParent = foundParent.getParentFile(); 431 depth--; 432 } 433 defaultSourcePath = foundParent.getAbsolutePath(); 434 } 435 } 436 defaultSourcePath = PathHandle.createPathString(defaultSourcePath); 437 return defaultSourcePath; 438 } 439 440 public void setDeployRootPath(String p) { 441 deployRootPath = p; 442 } 443 444 public String getDeployRootPath() { 445 String p = deployRootPath; 446 447 if (p == null) { 448 StringBuffer buf = new StringBuffer (); 449 450 buf.append(getRootPath()); 451 buf.append(File.separator); 452 buf.append(Constants.DIR_OUTPUT); 453 p = PathHandle.createPathString(buf.toString()); 454 } 455 return p; 456 } 457 458 protected String getClassOutputPath() { 462 StringBuffer buf = new StringBuffer (); 463 464 buf.append(getRootPath()); 465 buf.append(File.separator); 466 buf.append(Constants.DIR_CLASSES); 467 return PathHandle.createPathString(buf.toString()); 468 } 469 470 479 private int charCount(String in, char lookFor) { 480 String search = new String (in); 481 int index = search.indexOf(lookFor); 482 int count = 0; 483 484 while (index > 0) { 485 count++; 486 search = search.substring(index + 1); 487 index = search.indexOf(lookFor); 488 } 489 return count; 490 } 491 492 } 493 | Popular Tags |