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

changeset 1410
bfec2a1cc869
parent 1397
8abc56be3131
child 1521
71f35e4b93a5
equal deleted inserted replaced
1409:33abf479f202 1410:bfec2a1cc869
44 * @author Jamie Ho 44 * @author Jamie Ho
45 */ 45 */
46 public class Util { 46 public class Util {
47 47
48 /** 48 /**
49 * A mapping between characters and their
50 * corresponding HTML escape character.
51 */
52 public static final String[][] HTML_ESCAPE_CHARS =
53 {{"&", "&amp;"}, {"<", "&lt;"}, {">", "&gt;"}};
54
55 /**
56 * Return array of class members whose documentation is to be generated. 49 * Return array of class members whose documentation is to be generated.
57 * If the member is deprecated do not include such a member in the 50 * If the member is deprecated do not include such a member in the
58 * returned array. 51 * returned array.
59 * 52 *
60 * @param members Array of members to choose from. 53 * @param members Array of members to choose from.
422 /** 415 /**
423 * Given a string, escape all special html characters and 416 * Given a string, escape all special html characters and
424 * return the result. 417 * return the result.
425 * 418 *
426 * @param s The string to check. 419 * @param s The string to check.
427 * @return the original string with all of the HTML characters 420 * @return the original string with all of the HTML characters escaped.
428 * escaped.
429 *
430 * @see #HTML_ESCAPE_CHARS
431 */ 421 */
432 public static String escapeHtmlChars(String s) { 422 public static String escapeHtmlChars(String s) {
433 String result = s; 423 for (int i = 0; i < s.length(); i++) {
434 for (int i = 0; i < HTML_ESCAPE_CHARS.length; i++) { 424 char ch = s.charAt(i);
435 result = Util.replaceText(result, 425 switch (ch) {
436 HTML_ESCAPE_CHARS[i][0], HTML_ESCAPE_CHARS[i][1]); 426 // only start building a new string if we need to
437 } 427 case '<': case '>': case '&':
438 return result; 428 StringBuilder sb = new StringBuilder(s.substring(0, i));
429 for ( ; i < s.length(); i++) {
430 ch = s.charAt(i);
431 switch (ch) {
432 case '<': sb.append("&lt;"); break;
433 case '>': sb.append("&gt;"); break;
434 case '&': sb.append("&amp;"); break;
435 default: sb.append(ch); break;
436 }
437 }
438 return sb.toString();
439 }
440 }
441 return s;
442 }
443
444 /**
445 * Escape all special html characters in a string buffer.
446 *
447 * @param sb The string buffer to update
448 */
449 public static void escapeHtmlChars(StringBuilder sb) {
450 // scan backwards, replacing characters as needed.
451 for (int i = sb.length() - 1; i >= 0; i--) {
452 switch (sb.charAt(i)) {
453 case '<': sb.replace(i, i+1, "&lt;"); break;
454 case '>': sb.replace(i, i+1, "&gt;"); break;
455 case '&': sb.replace(i, i+1, "&amp;"); break;
456 }
457 }
439 } 458 }
440 459
441 /** 460 /**
442 * Given a string, strips all html characters and 461 * Given a string, strips all html characters and
443 * return the result. 462 * return the result.
577 return config.getText( 596 return config.getText(
578 lowerCaseOnly ? typeName.toLowerCase() : typeName); 597 lowerCaseOnly ? typeName.toLowerCase() : typeName);
579 } 598 }
580 599
581 /** 600 /**
582 * Given a string, replace all tabs with the appropriate 601 * Replace all tabs with the appropriate number of spaces.
583 * number of spaces. 602 * @param configuration the doclet configuration defining the setting for the
584 * @param tabLength the length of each tab. 603 * tab length.
585 * @param s the String to scan. 604 * @param sb the StringBuilder in which to replace the tabs
586 */ 605 */
587 public static void replaceTabs(int tabLength, StringBuilder s) { 606 public static void replaceTabs(Configuration configuration, StringBuilder sb) {
588 if (whitespace == null || whitespace.length() < tabLength) 607 int tabLength = configuration.sourcetab;
589 whitespace = String.format("%" + tabLength + "s", " "); 608 String whitespace = configuration.tabSpaces;
590 int index = 0; 609 int index = 0;
591 while ((index = s.indexOf("\t", index)) != -1) { 610 while ((index = sb.indexOf("\t", index)) != -1) {
592 int spaceCount = tabLength - index % tabLength; 611 int spaceCount = tabLength - index % tabLength;
593 s.replace(index, index+1, whitespace.substring(0, spaceCount)); 612 sb.replace(index, index+1, whitespace.substring(0, spaceCount));
594 index += spaceCount; 613 index += spaceCount;
595 } 614 }
596 } 615 }
597 private static String whitespace;
598 616
599 /** 617 /**
600 * The documentation for values() and valueOf() in Enums are set by the 618 * The documentation for values() and valueOf() in Enums are set by the
601 * doclet. 619 * doclet.
602 */ 620 */

mercurial