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