src/share/classes/com/sun/tools/doclets/internal/toolkit/util/Util.java

changeset 1372
78962d89f283
parent 1364
8db45b13526e
child 1381
23fe1a96bc0f
equal deleted inserted replaced
1366:12cf6bfd8c05 1372:78962d89f283
50 */ 50 */
51 public static final String[][] HTML_ESCAPE_CHARS = 51 public static final String[][] HTML_ESCAPE_CHARS =
52 {{"&", "&amp;"}, {"<", "&lt;"}, {">", "&gt;"}}; 52 {{"&", "&amp;"}, {"<", "&lt;"}, {">", "&gt;"}};
53 53
54 /** 54 /**
55 * Name of the resource directory.
56 */
57 public static final String RESOURCESDIR = "resources";
58
59 /**
60 * Return array of class members whose documentation is to be generated. 55 * Return array of class members whose documentation is to be generated.
61 * If the member is deprecated do not include such a member in the 56 * If the member is deprecated do not include such a member in the
62 * returned array. 57 * returned array.
63 * 58 *
64 * @param members Array of members to choose from. 59 * @param members Array of members to choose from.
234 * @param path The relative path to the directory to be copied. 229 * @param path The relative path to the directory to be copied.
235 * @param dir The original directory name to copy from. 230 * @param dir The original directory name to copy from.
236 * @param overwrite Overwrite files if true. 231 * @param overwrite Overwrite files if true.
237 */ 232 */
238 public static void copyDocFiles(Configuration configuration, 233 public static void copyDocFiles(Configuration configuration,
239 String path, String dir, boolean overwrite) { 234 File path, DocPath dir, boolean overwrite) {
240 if (checkCopyDocFilesErrors(configuration, path, dir)) { 235 if (checkCopyDocFilesErrors(configuration, path, dir)) {
241 return; 236 return;
242 } 237 }
243 String destname = configuration.docFileDestDirName; 238 File srcdir = new File(path, dir.getPath());
244 File srcdir = new File(path + dir); 239 File destdir = new File(configuration.docFileDestDirName, dir.getPath());
245 if (destname.length() > 0 && !destname.endsWith(
246 DirectoryManager.URL_FILE_SEPARATOR)) {
247 destname += DirectoryManager.URL_FILE_SEPARATOR;
248 }
249 String dest = destname + dir;
250 try { 240 try {
251 File destdir = new File(dest); 241 createDirectory(configuration, destdir);
252 DirectoryManager.createDirectory(configuration, dest);
253 String[] files = srcdir.list(); 242 String[] files = srcdir.list();
254 for (int i = 0; i < files.length; i++) { 243 for (int i = 0; i < files.length; i++) {
255 File srcfile = new File(srcdir, files[i]); 244 File srcfile = new File(srcdir, files[i]);
256 File destfile = new File(destdir, files[i]); 245 File destfile = new File(destdir, files[i]);
257 if (srcfile.isFile()) { 246 if (srcfile.isFile()) {
258 if(destfile.exists() && ! overwrite) { 247 if (destfile.exists() && ! overwrite) {
259 configuration.message.warning((SourcePosition) null, 248 configuration.message.warning((SourcePosition) null,
260 "doclet.Copy_Overwrite_warning", 249 "doclet.Copy_Overwrite_warning",
261 srcfile.toString(), destdir.toString()); 250 srcfile.toString(), destdir.toString());
262 } else { 251 } else {
263 configuration.message.notice( 252 configuration.message.notice(
264 "doclet.Copying_File_0_To_Dir_1", 253 "doclet.Copying_File_0_To_Dir_1",
265 srcfile.toString(), destdir.toString()); 254 srcfile.toString(), destdir.toString());
266 Util.copyFile(destfile, srcfile); 255 Util.copyFile(destfile, srcfile);
267 } 256 }
268 } else if(srcfile.isDirectory()) { 257 } else if (srcfile.isDirectory()) {
269 if(configuration.copydocfilesubdirs 258 if (configuration.copydocfilesubdirs
270 && ! configuration.shouldExcludeDocFileDir( 259 && ! configuration.shouldExcludeDocFileDir(
271 srcfile.getName())){ 260 srcfile.getName())){
272 copyDocFiles(configuration, path, dir + 261 copyDocFiles(configuration, path, dir.resolve(files[i]),
273 DirectoryManager.URL_FILE_SEPARATOR + srcfile.getName(),
274 overwrite); 262 overwrite);
275 } 263 }
276 } 264 }
277 } 265 }
278 } catch (SecurityException exc) { 266 } catch (SecurityException exc) {
288 * @param configuration The configuration of the current doclet. 276 * @param configuration The configuration of the current doclet.
289 * @param path The relative path to the directory to be copied. 277 * @param path The relative path to the directory to be copied.
290 * @param dirName The original directory name to copy from. 278 * @param dirName The original directory name to copy from.
291 */ 279 */
292 private static boolean checkCopyDocFilesErrors (Configuration configuration, 280 private static boolean checkCopyDocFilesErrors (Configuration configuration,
293 String path, String dirName) { 281 File path, DocPath dirName) {
294 if ((configuration.sourcepath == null || configuration.sourcepath.length() == 0) && 282 if ((configuration.sourcepath == null || configuration.sourcepath.length() == 0) &&
295 (configuration.destDirName == null || configuration.destDirName.length() == 0)) { 283 (configuration.destDirName == null || configuration.destDirName.length() == 0)) {
296 //The destination path and source path are definitely equal. 284 //The destination path and source path are definitely equal.
297 return true; 285 return true;
298 } 286 }
307 if(destPath.equals(sourcePath)){ 295 if(destPath.equals(sourcePath)){
308 return true; 296 return true;
309 } 297 }
310 } 298 }
311 //Make sure the doc-file being copied exists. 299 //Make sure the doc-file being copied exists.
312 File srcdir = new File(path + dirName); 300 File srcdir = new File(path, dirName.getPath());
313 if (! srcdir.exists()) { 301 if (! srcdir.exists()) {
314 return true; 302 return true;
315 } 303 }
316 return false; 304 return false;
317 } 305 }
328 * destination directory will be overwritten if 316 * destination directory will be overwritten if
329 * it already exists. 317 * it already exists.
330 */ 318 */
331 public static void copyResourceFile(Configuration configuration, 319 public static void copyResourceFile(Configuration configuration,
332 String resourcefile, boolean overwrite) { 320 String resourcefile, boolean overwrite) {
333 String destresourcesdir = configuration.destDirName + RESOURCESDIR; 321 copyFile(configuration, resourcefile, DocPaths.RESOURCES, DocPaths.RESOURCES,
334 copyFile(configuration, resourcefile, RESOURCESDIR, destresourcesdir,
335 overwrite, false); 322 overwrite, false);
336 } 323 }
337 324
338 /** 325 /**
339 * Copy a file from a source directory to a destination directory 326 * Copy a file from a source directory to a destination directory
348 * destination directory will be overwritten if 335 * destination directory will be overwritten if
349 * it already exists. 336 * it already exists.
350 * @param replaceNewLine true if the newline needs to be replaced with platform- 337 * @param replaceNewLine true if the newline needs to be replaced with platform-
351 * specific newline. 338 * specific newline.
352 */ 339 */
340 public static void copyFile(Configuration configuration, String file, DocPath source,
341 DocPath destination, boolean overwrite, boolean replaceNewLine) {
342 copyFile(configuration, file, source.getPath(), destination.getPath(),
343 overwrite, replaceNewLine);
344 }
345
353 public static void copyFile(Configuration configuration, String file, String source, 346 public static void copyFile(Configuration configuration, String file, String source,
354 String destination, boolean overwrite, boolean replaceNewLine) { 347 String destination, boolean overwrite, boolean replaceNewLine) {
355 DirectoryManager.createDirectory(configuration, destination); 348 File destdir = configuration.destDirName.isEmpty() ?
356 File destfile = new File(destination, file); 349 (destination.isEmpty() ? null : new File(destination)) :
357 if(destfile.exists() && (! overwrite)) return; 350 new File(configuration.destDirName, destination);
351 File destfile = (destdir == null) ? new File(file) : new File(destdir, file);
352 createDirectory(configuration, destfile.getParentFile());
353 if (destfile.exists() && (! overwrite)) return;
358 try { 354 try {
359 InputStream in = Configuration.class.getResourceAsStream( 355 InputStream in = Configuration.class.getResourceAsStream(
360 source + DirectoryManager.URL_FILE_SEPARATOR + file); 356 source + '/' + file);
361 if(in==null) return; 357 if (in == null) return;
362 OutputStream out = new FileOutputStream(destfile); 358 OutputStream out = new FileOutputStream(destfile);
363 try { 359 try {
364 if (!replaceNewLine) { 360 if (!replaceNewLine) {
365 byte[] buf = new byte[2048]; 361 byte[] buf = new byte[2048];
366 int n; 362 int n;
394 throw new DocletAbortException(); 390 throw new DocletAbortException();
395 } 391 }
396 } 392 }
397 393
398 /** 394 /**
395 * Given a path string create all the directories in the path. For example,
396 * if the path string is "java/applet", the method will create directory
397 * "java" and then "java/applet" if they don't exist. The file separator
398 * string "/" is platform dependent system property.
399 *
400 * @param path Directory path string.
401 */
402 public static void createDirectory(Configuration configuration, File dir) {
403 if (dir == null) {
404 return;
405 }
406 if (dir.exists()) {
407 return;
408 } else {
409 if (dir.mkdirs()) {
410 return;
411 } else {
412 configuration.message.error(
413 "doclet.Unable_to_create_directory_0", dir.getPath());
414 throw new DocletAbortException();
415 }
416 }
417 }
418
419 /**
399 * Given a PackageDoc, return the source path for that package. 420 * Given a PackageDoc, return the source path for that package.
400 * @param configuration The Configuration for the current Doclet. 421 * @param configuration The Configuration for the current Doclet.
401 * @param pkgDoc The package to seach the path for. 422 * @param pkgDoc The package to search the path for.
402 * @return A string representing the path to the given package. 423 * @return A string representing the path to the given package.
403 */ 424 */
404 public static String getPackageSourcePath(Configuration configuration, 425 public static File getPackageSourcePath(Configuration configuration,
405 PackageDoc pkgDoc){ 426 PackageDoc pkgDoc) {
406 try{ 427 DocPath pkgPath = DocPath.forPackage(pkgDoc);
407 String pkgPath = DirectoryManager.getDirectoryPath(pkgDoc); 428 File pkgDir = new SourcePath(configuration.sourcepath).getDirectory(pkgPath);
408 String completePath = new SourcePath(configuration.sourcepath). 429 if (pkgDir == null)
409 getDirectory(pkgPath) + DirectoryManager.URL_FILE_SEPARATOR; 430 return null;
410 //Make sure that both paths are using the same separators. 431 //Make sure that both paths are using the same separators.
411 completePath = Util.replaceText(completePath, File.separator, 432 String completePath = Util.replaceText(pkgDir.getPath(), File.separator, "/");
412 DirectoryManager.URL_FILE_SEPARATOR); 433 String pathForPkg = completePath.substring(0, completePath.lastIndexOf(pkgPath.getPath()));
413 pkgPath = Util.replaceText(pkgPath, File.separator, 434 return new File(pathForPkg);
414 DirectoryManager.URL_FILE_SEPARATOR);
415 return completePath.substring(0, completePath.lastIndexOf(pkgPath));
416 } catch (Exception e){
417 return "";
418 }
419 } 435 }
420 436
421 /** 437 /**
422 * We want the list of types in alphabetical order. However, types are not 438 * We want the list of types in alphabetical order. However, types are not
423 * comparable. We need a comparator for now. 439 * comparable. We need a comparator for now.
550 public static String quote(String filepath) { 566 public static String quote(String filepath) {
551 return ("\"" + filepath + "\""); 567 return ("\"" + filepath + "\"");
552 } 568 }
553 569
554 /** 570 /**
555 * Given a package, return it's name. 571 * Given a package, return its name.
556 * @param packageDoc the package to check. 572 * @param packageDoc the package to check.
557 * @return the name of the given package. 573 * @return the name of the given package.
558 */ 574 */
559 public static String getPackageName(PackageDoc packageDoc) { 575 public static String getPackageName(PackageDoc packageDoc) {
560 return packageDoc == null || packageDoc.name().length() == 0 ? 576 return packageDoc == null || packageDoc.name().length() == 0 ?
561 DocletConstants.DEFAULT_PACKAGE_NAME : packageDoc.name(); 577 DocletConstants.DEFAULT_PACKAGE_NAME : packageDoc.name();
562 } 578 }
563 579
564 /** 580 /**
565 * Given a package, return it's file name without the extension. 581 * Given a package, return its file name without the extension.
566 * @param packageDoc the package to check. 582 * @param packageDoc the package to check.
567 * @return the file name of the given package. 583 * @return the file name of the given package.
568 */ 584 */
569 public static String getPackageFileHeadName(PackageDoc packageDoc) { 585 public static String getPackageFileHeadName(PackageDoc packageDoc) {
570 return packageDoc == null || packageDoc.name().length() == 0 ? 586 return packageDoc == null || packageDoc.name().length() == 0 ?
571 DocletConstants.DEFAULT_PACKAGE_FILE_NAME : packageDoc.name(); 587 DocletConstants.DEFAULT_PACKAGE_FILE_NAME : packageDoc.name();
572 } 588 }
573 589
574 /** 590 /**
575 * Given a string, replace all occurraces of 'newStr' with 'oldStr'. 591 * Given a string, replace all occurrences of 'newStr' with 'oldStr'.
576 * @param originalStr the string to modify. 592 * @param originalStr the string to modify.
577 * @param oldStr the string to replace. 593 * @param oldStr the string to replace.
578 * @param newStr the string to insert in place of the old string. 594 * @param newStr the string to insert in place of the old string.
579 */ 595 */
580 public static String replaceText(String originalStr, String oldStr, 596 public static String replaceText(String originalStr, String oldStr,
635 * OutputStreamWriter is passed on to next level. 651 * OutputStreamWriter is passed on to next level.
636 * @return Writer Writer for the file getting generated. 652 * @return Writer Writer for the file getting generated.
637 * @see java.io.FileOutputStream 653 * @see java.io.FileOutputStream
638 * @see java.io.OutputStreamWriter 654 * @see java.io.OutputStreamWriter
639 */ 655 */
640 public static Writer genWriter(Configuration configuration, 656 public static Writer genWriter(Configuration configuration, DocPath path)
641 String path, String filename, 657 throws IOException, UnsupportedEncodingException {
642 String docencoding) 658 File file = path.resolveAgainst(configuration.destDirName);
643 throws IOException, UnsupportedEncodingException { 659 createDirectory(configuration, file.getParentFile());
644 FileOutputStream fos; 660 FileOutputStream fos = new FileOutputStream(file);
645 if (path != null) { 661 if (configuration.docencoding == null) {
646 DirectoryManager.createDirectory(configuration, path);
647 fos = new FileOutputStream(((path.length() > 0)?
648 path + File.separator: "") + filename);
649 } else {
650 fos = new FileOutputStream(filename);
651 }
652 if (docencoding == null) {
653 return new BufferedWriter(new OutputStreamWriter(fos)); 662 return new BufferedWriter(new OutputStreamWriter(fos));
654 } else { 663 } else {
655 return new BufferedWriter(new OutputStreamWriter(fos, docencoding)); 664 return new BufferedWriter(new OutputStreamWriter(fos, configuration.docencoding));
656 } 665 }
657 } 666 }
658 667
659 /** 668 /**
660 * Given an annotation, return true if it should be documented and false 669 * Given an annotation, return true if it should be documented and false

mercurial