usr/src/java/util/org/opensolaris/os/vp/util/misc/TextUtil.java
changeset 685 767674b0a2fb
parent 648 05d2cd78123d
equal deleted inserted replaced
684:f7b1d1fbdb8e 685:767674b0a2fb
    60     /**
    60     /**
    61      * Returns a random alpha {@code String} with length between {@code minLen}
    61      * Returns a random alpha {@code String} with length between {@code minLen}
    62      * and {@code maxLen}.
    62      * and {@code maxLen}.
    63      */
    63      */
    64     public static String getRandomAlphaString(int minLen, int maxLen) {
    64     public static String getRandomAlphaString(int minLen, int maxLen) {
    65 	StringBuffer buffer = new StringBuffer();
    65 	StringBuilder buffer = new StringBuilder();
    66 	int length = (int)(Math.random() * (maxLen - minLen + 1)) + minLen;
    66 	int length = (int)(Math.random() * (maxLen - minLen + 1)) + minLen;
    67 	for (int j = 0; j < length; j++) {
    67 	for (int j = 0; j < length; j++) {
    68 	    char c = (char)((Math.random() * 26) + 97);
    68 	    char c = (char)((Math.random() * 26) + 97);
    69 	    buffer.append(c);
    69 	    buffer.append(c);
    70 	}
    70 	}
    74     /**
    74     /**
    75      * Replace characters with special meaning to HTML with their HTML entity
    75      * Replace characters with special meaning to HTML with their HTML entity
    76      * equivalents.
    76      * equivalents.
    77      */
    77      */
    78     public static String escapeHTMLChars(String text) {
    78     public static String escapeHTMLChars(String text) {
    79 	StringBuffer buffer = new StringBuffer();
    79 	StringBuilder buffer = new StringBuilder();
    80 
    80 
    81 	for (char c : text.toCharArray()) {
    81 	for (char c : text.toCharArray()) {
    82 	    switch (c) {
    82 	    switch (c) {
    83 		case '<':
    83 		case '<':
    84 		    buffer.append("&lt;");
    84 		    buffer.append("&lt;");
   146 	}
   146 	}
   147 
   147 
   148 	// Replace newlines
   148 	// Replace newlines
   149 	text = text.replaceAll("\\s*[\\r\\n]\\s*", " ");
   149 	text = text.replaceAll("\\s*[\\r\\n]\\s*", " ");
   150 
   150 
   151 	StringBuffer buffer = new StringBuffer();
   151 	StringBuilder buffer = new StringBuilder();
   152 
   152 
   153 	String indent = indent1;
   153 	String indent = indent1;
   154 	int w = width - indent.length();
   154 	int w = width - indent.length();
   155 	if (w < 1) {
   155 	if (w < 1) {
   156 	    w = 1;
   156 	    w = 1;
   208     public static boolean isPrintable(char c) {
   208     public static boolean isPrintable(char c) {
   209 	return c >= 32 && c <= 126;
   209 	return c >= 32 && c <= 126;
   210     }
   210     }
   211 
   211 
   212     public static String join(String delim, Object... list) {
   212     public static String join(String delim, Object... list) {
   213 	StringBuffer buffer = new StringBuffer();
   213 	StringBuilder buffer = new StringBuilder();
   214 
   214 
   215 	for (int i = 0; i < list.length; i++) {
   215 	for (int i = 0; i < list.length; i++) {
   216 	    if (i != 0 && delim != null) {
   216 	    if (i != 0 && delim != null) {
   217 		buffer.append(delim);
   217 		buffer.append(delim);
   218 	    }
   218 	    }
   330     /**
   330     /**
   331      * Converts {@code str} to a Java identifier by replacing any illegal
   331      * Converts {@code str} to a Java identifier by replacing any illegal
   332      * characters with {@code repl}.
   332      * characters with {@code repl}.
   333      */
   333      */
   334     public static String toJavaIdentifier(String str, String repl) {
   334     public static String toJavaIdentifier(String str, String repl) {
   335 	StringBuffer buffer = new StringBuffer();
   335 	StringBuilder buffer = new StringBuilder();
   336 	for (char c : str.toCharArray()) {
   336 	for (char c : str.toCharArray()) {
   337 	    boolean empty = buffer.length() == 0;
   337 	    boolean empty = buffer.length() == 0;
   338 	    if ((empty && Character.isJavaIdentifierStart(c)) ||
   338 	    if ((empty && Character.isJavaIdentifierStart(c)) ||
   339 		(!empty && Character.isJavaIdentifierPart(c))) {
   339 		(!empty && Character.isJavaIdentifierPart(c))) {
   340 		buffer.append(c);
   340 		buffer.append(c);
   344 	}
   344 	}
   345 	return buffer.toString();
   345 	return buffer.toString();
   346     }
   346     }
   347 
   347 
   348     public static String toJavaMethodName(String... words) {
   348     public static String toJavaMethodName(String... words) {
   349 	StringBuffer buffer = new StringBuffer();
   349 	StringBuilder buffer = new StringBuilder();
   350 	for (String word : words) {
   350 	for (String word : words) {
   351 	    try {
   351 	    try {
   352 		buffer.append(word.substring(0, 1).toUpperCase());
   352 		buffer.append(word.substring(0, 1).toUpperCase());
   353 		buffer.append(word.substring(1).toLowerCase());
   353 		buffer.append(word.substring(1).toLowerCase());
   354 	    } catch (IndexOutOfBoundsException ignore) {
   354 	    } catch (IndexOutOfBoundsException ignore) {