components/visual-panels/core/src/java/util/com/oracle/solaris/vp/util/misc/TextUtil.java
changeset 3553 f1d133b09a8c
parent 3552 077ebe3d0d24
child 3554 ef58713bafc4
equal deleted inserted replaced
3552:077ebe3d0d24 3553:f1d133b09a8c
     1 /*
       
     2  * CDDL HEADER START
       
     3  *
       
     4  * The contents of this file are subject to the terms of the
       
     5  * Common Development and Distribution License (the "License").
       
     6  * You may not use this file except in compliance with the License.
       
     7  *
       
     8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
     9  * or http://www.opensolaris.org/os/licensing.
       
    10  * See the License for the specific language governing permissions
       
    11  * and limitations under the License.
       
    12  *
       
    13  * When distributing Covered Code, include this CDDL HEADER in each
       
    14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    15  * If applicable, add the following below this CDDL HEADER, with the
       
    16  * fields enclosed by brackets "[]" replaced with your own identifying
       
    17  * information: Portions Copyright [yyyy] [name of copyright owner]
       
    18  *
       
    19  * CDDL HEADER END
       
    20  */
       
    21 
       
    22 /*
       
    23  * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
       
    24  */
       
    25 
       
    26 package com.oracle.solaris.vp.util.misc;
       
    27 
       
    28 import java.util.Collection;
       
    29 import java.util.regex.*;
       
    30 
       
    31 public class TextUtil {
       
    32     //
       
    33     // Static methods
       
    34     //
       
    35 
       
    36     /**
       
    37      * Returns the base portion of a fully-qualified class name.
       
    38      */
       
    39     public static String getClassBaseName(String className) {
       
    40 	return className.replaceFirst(".*\\.", "");
       
    41     }
       
    42 
       
    43     /**
       
    44      * Returns the base portion of the name of the given class.  In
       
    45      * constrast with the simple name, which is empty for anonymous
       
    46      * classes and the Java identifier for local classes, this is the
       
    47      * last component of the specified class's binary name.
       
    48      */
       
    49     public static String getBaseName(Class clazz) {
       
    50 	return getClassBaseName(clazz.getName());
       
    51     }
       
    52 
       
    53     /**
       
    54      * Returns the package portion of a fully-qualified class name.
       
    55      */
       
    56     public static String getPackageName(String name) {
       
    57 	return name.replaceFirst("\\.?\\.*[^.]*$", "");
       
    58     }
       
    59 
       
    60     /**
       
    61      * Returns a random alpha {@code String} with length between {@code minLen}
       
    62      * and {@code maxLen}.
       
    63      */
       
    64     public static String getRandomAlphaString(int minLen, int maxLen) {
       
    65 	StringBuilder buffer = new StringBuilder();
       
    66 	int length = (int)(Math.random() * (maxLen - minLen + 1)) + minLen;
       
    67 	for (int j = 0; j < length; j++) {
       
    68 	    char c = (char)((Math.random() * 26) + 97);
       
    69 	    buffer.append(c);
       
    70 	}
       
    71 	return buffer.toString();
       
    72     }
       
    73 
       
    74     /**
       
    75      * Replace characters with special meaning to HTML with their HTML entity
       
    76      * equivalents.
       
    77      */
       
    78     public static String escapeHTMLChars(String text) {
       
    79 	StringBuilder buffer = new StringBuilder();
       
    80 
       
    81 	for (char c : text.toCharArray()) {
       
    82 	    switch (c) {
       
    83 		case '<':
       
    84 		    buffer.append("&lt;");
       
    85 		    break;
       
    86 
       
    87 		case '>':
       
    88 		    buffer.append("&gt;");
       
    89 		    break;
       
    90 
       
    91 		case '\"':
       
    92 		    buffer.append("&quot;");
       
    93 		    break;
       
    94 
       
    95 		case '\'':
       
    96 		    buffer.append("&#039;");
       
    97 		    break;
       
    98 
       
    99 		case '\\':
       
   100 		    buffer.append("&#092;");
       
   101 		    break;
       
   102 
       
   103 		case '&':
       
   104 		    buffer.append("&amp;");
       
   105 		    break;
       
   106 
       
   107 		case '\n':
       
   108 		    buffer.append("<br/>");
       
   109 		    break;
       
   110 
       
   111 		default:
       
   112 		    buffer.append(c);
       
   113 	    }
       
   114 	}
       
   115 
       
   116 	return buffer.toString();
       
   117     }
       
   118 
       
   119     /**
       
   120      * Splits the given string into lines.
       
   121      *
       
   122      * @param	    text
       
   123      *		    the text to format
       
   124      *
       
   125      * @param	    width
       
   126      *		    the maximum width of each line
       
   127      *
       
   128      * @param	    indent1
       
   129      *		    the indent to put on the first line
       
   130      *
       
   131      * @param	    indent2
       
   132      *		    the indent to put on the second and remaining lines
       
   133      *
       
   134      * @param	    forceBreak
       
   135      *		    whether to force a break in the middle of a word
       
   136      *		    if the length of the word is greater than
       
   137      *		    <code>width</code>
       
   138      *
       
   139      * @return	    a wrapped string separated by newlines
       
   140      */
       
   141     public static String format(String text, int width,
       
   142 	String indent1, String indent2, boolean forceBreak) {
       
   143 
       
   144 	if (text == null) {
       
   145 	    return text;
       
   146 	}
       
   147 
       
   148 	// Replace newlines
       
   149 	text = text.replaceAll("\\s*[\\r\\n]\\s*", " ");
       
   150 
       
   151 	StringBuilder buffer = new StringBuilder();
       
   152 
       
   153 	String indent = indent1;
       
   154 	int w = width - indent.length();
       
   155 	if (w < 1) {
       
   156 	    w = 1;
       
   157 	}
       
   158 
       
   159 	for (int i = 0; text.length() > 0; i++) {
       
   160 	    String regex = "^\\s*(.{1," + w + "})($|\\s+.*)";
       
   161 	    String[] groups = match(text, regex);
       
   162 
       
   163 	    if (groups == null) {
       
   164 		// Can't break line on whitespace without exceeding width
       
   165 		if (forceBreak) {
       
   166 		    // Break line in middle of word
       
   167 		    regex = "^\\s*(.{1," + w + "})(.*)";
       
   168 		} else {
       
   169 		    // Break line at next whitespace
       
   170 		    regex = "^\\s*(.+?)($|\\s+.*)";
       
   171 		}
       
   172 
       
   173 		groups = match(text, regex);
       
   174 	    }
       
   175 
       
   176 	    if (i != 0) {
       
   177 		buffer.append("\n");
       
   178 	    }
       
   179 
       
   180 	    buffer.append(indent).append(groups[1].trim());
       
   181 	    text = groups[2];
       
   182 
       
   183 	    if (i == 0) {
       
   184 		indent = indent2;
       
   185 		w = width - indent.length();
       
   186 		if (w < 1) {
       
   187 		    w = 1;
       
   188 		}
       
   189 	    }
       
   190 	}
       
   191 
       
   192 	return buffer.toString();
       
   193     }
       
   194 
       
   195     /**
       
   196      * Shortcut for:
       
   197      * <p>
       
   198      *	 <code>
       
   199      *	   {@link #format(String,int,String,String,boolean) format}
       
   200      *	   (text, width, "", "", forceBreak);
       
   201      *	 </code>
       
   202      * </p>
       
   203      */
       
   204     public static String format(String text, int width, boolean forceBreak) {
       
   205 	return format(text, width, "", "", forceBreak);
       
   206     }
       
   207 
       
   208     public static boolean isPrintable(char c) {
       
   209 	return c >= 32 && c <= 126;
       
   210     }
       
   211 
       
   212     public static String join(String delim, Object... list) {
       
   213 	StringBuilder buffer = new StringBuilder();
       
   214 
       
   215 	for (int i = 0; i < list.length; i++) {
       
   216 	    if (i != 0 && delim != null) {
       
   217 		buffer.append(delim);
       
   218 	    }
       
   219 	    if (list[i] != null) {
       
   220 		buffer.append(list[i]);
       
   221 	    }
       
   222 	}
       
   223 
       
   224 	return buffer.toString();
       
   225     }
       
   226 
       
   227     public static String join(String delim, byte... list) {
       
   228 	Byte[] array = new Byte[list.length];
       
   229 	for (int i = 0; i < array.length; i++) {
       
   230 	    array[i] = list[i];
       
   231 	}
       
   232 	return join(delim, (Object[])array);
       
   233     }
       
   234 
       
   235     public static String join(String delim, char... list) {
       
   236 	Character[] array = new Character[list.length];
       
   237 	for (int i = 0; i < array.length; i++) {
       
   238 	    array[i] = list[i];
       
   239 	}
       
   240 	return join(delim, (Object[])array);
       
   241     }
       
   242 
       
   243     public static String join(String delim, double... list) {
       
   244 	Double[] array = new Double[list.length];
       
   245 	for (int i = 0; i < array.length; i++) {
       
   246 	    array[i] = list[i];
       
   247 	}
       
   248 	return join(delim, (Object[])array);
       
   249     }
       
   250 
       
   251     public static String join(String delim, float... list) {
       
   252 	Float[] array = new Float[list.length];
       
   253 	for (int i = 0; i < array.length; i++) {
       
   254 	    array[i] = list[i];
       
   255 	}
       
   256 	return join(delim, (Object[])array);
       
   257     }
       
   258 
       
   259     public static String join(String delim, int... list) {
       
   260 	Integer[] array = new Integer[list.length];
       
   261 	for (int i = 0; i < array.length; i++) {
       
   262 	    array[i] = list[i];
       
   263 	}
       
   264 	return join(delim, (Object[])array);
       
   265     }
       
   266 
       
   267     public static String join(String delim, long... list) {
       
   268 	Long[] array = new Long[list.length];
       
   269 	for (int i = 0; i < array.length; i++) {
       
   270 	    array[i] = list[i];
       
   271 	}
       
   272 	return join(delim, (Object[])array);
       
   273     }
       
   274 
       
   275     public static String join(String delim, short... list) {
       
   276 	Short[] array = new Short[list.length];
       
   277 	for (int i = 0; i < array.length; i++) {
       
   278 	    array[i] = list[i];
       
   279 	}
       
   280 	return join(delim, (Object[])array);
       
   281     }
       
   282 
       
   283     public static String join(String delim, Collection list) {
       
   284 	return join(delim, list.toArray());
       
   285     }
       
   286 
       
   287     /**
       
   288      * Matches the given string against the given regular expression,
       
   289      * returning an array of the matched regular expression groups.
       
   290      *
       
   291      * @param	    text
       
   292      *		    the String to which to apply the regular
       
   293      *		    expression
       
   294      *
       
   295      * @param	    regex
       
   296      *		    a regular expression containing groups
       
   297      *
       
   298      * @return	    an array of the matched regular expression groups
       
   299      *		    (group 0 is the entire pattern), if the given
       
   300      *		    pattern matched, or {@code null} if the
       
   301      *		    pattern did not match
       
   302      */
       
   303     public static String[] match(String text, Pattern regex) {
       
   304 	Matcher matcher = regex.matcher(text);
       
   305 	if (!matcher.matches()) {
       
   306 	    return null;
       
   307 	}
       
   308 
       
   309 	String[] groups = new String[matcher.groupCount() + 1];
       
   310 	for (int i = 0; i < groups.length; i++) {
       
   311 	    groups[i] = matcher.group(i);
       
   312 	}
       
   313 
       
   314 	return groups;
       
   315     }
       
   316 
       
   317     /**
       
   318      * Compiles the given regular expression and calls {@link
       
   319      * #match(String,Pattern)}.
       
   320      *
       
   321      * @exception   PatternSyntaxException
       
   322      *		    if the given pattern was malformed
       
   323      */
       
   324     public static String[] match(String text, String regex)
       
   325 	throws PatternSyntaxException {
       
   326 
       
   327 	return match(text, Pattern.compile(regex));
       
   328     }
       
   329 
       
   330     /**
       
   331      * Converts {@code str} to a Java identifier by replacing any illegal
       
   332      * characters with {@code repl}.
       
   333      */
       
   334     public static String toJavaIdentifier(String str, String repl) {
       
   335 	StringBuilder buffer = new StringBuilder();
       
   336 	for (char c : str.toCharArray()) {
       
   337 	    boolean empty = buffer.length() == 0;
       
   338 	    if ((empty && Character.isJavaIdentifierStart(c)) ||
       
   339 		(!empty && Character.isJavaIdentifierPart(c))) {
       
   340 		buffer.append(c);
       
   341 	    } else {
       
   342 		buffer.append(repl);
       
   343 	    }
       
   344 	}
       
   345 	return buffer.toString();
       
   346     }
       
   347 
       
   348     public static String toJavaMethodName(String... words) {
       
   349 	StringBuilder buffer = new StringBuilder();
       
   350 	for (String word : words) {
       
   351 	    try {
       
   352 		buffer.append(word.substring(0, 1).toUpperCase());
       
   353 		buffer.append(word.substring(1).toLowerCase());
       
   354 	    } catch (IndexOutOfBoundsException ignore) {
       
   355 	    }
       
   356 	}
       
   357 	return buffer.toString();
       
   358     }
       
   359 }