usr/src/java/util/org/opensolaris/os/vp/util/misc/TextUtil.java
author Stephen Talley <stephen.talley@sun.com>
Mon, 23 Mar 2009 11:12:21 -0400
changeset 243 979bb698977b
parent 219 57841c113efe
child 253 f219d4576960
permissions -rw-r--r--
7608 Prefer Class.getSimpleName to TextUtil.getClassBaseName
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
/*
219
57841c113efe 6788 package names should share o.o.o.vp prefix
Stephen Talley <stephen.talley@sun.com>
parents: 78
diff changeset
    23
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
0
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    24
 * Use is subject to license terms.
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    25
 */
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    26
219
57841c113efe 6788 package names should share o.o.o.vp prefix
Stephen Talley <stephen.talley@sun.com>
parents: 78
diff changeset
    27
package org.opensolaris.os.vp.util.misc;
0
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    28
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    29
import java.util.*;
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    30
import java.util.regex.*;
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    31
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    32
public class TextUtil {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    33
    //
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    34
    // Static methods
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
    /**
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    38
     * Returns the base portion of a fully-qualified class name.
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    39
     */
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    40
    public static String getClassBaseName(String className) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    41
	return className.replaceFirst(".*\\.", "");
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
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    44
    /**
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    45
     * Returns the package portion of a fully-qualified class name.
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    46
     */
13
b48b988cc94d 2655 s/getParentPackageName/getPackageName/g
Stephen Talley <stephen.talley@sun.com>
parents: 0
diff changeset
    47
    public static String getPackageName(String name) {
0
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    48
	return name.replaceFirst("\\.?\\.*[^.]*$", "");
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    49
    }
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    50
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    51
    /**
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    52
     * Returns a random alpha {@code String} with length between {@code minLen}
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    53
     * and {@code maxLen}.
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    54
     */
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    55
    public static String getRandomAlphaString(int minLen, int maxLen) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    56
	StringBuffer buffer = new StringBuffer();
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    57
	int length = (int)(Math.random() * (maxLen - minLen + 1)) + minLen;
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    58
	for (int j = 0; j < length; j++) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    59
	    char c = (char)((Math.random() * 26) + 97);
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    60
	    buffer.append(c);
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    61
	}
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    62
	return buffer.toString();
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
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    65
    /**
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    66
     * Replace characters with special meaning to HTML with their HTML entity
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    67
     * equivalents.
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    68
     */
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    69
    public static String escapeHTMLChars(String text) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    70
	StringBuffer buffer = new StringBuffer();
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    71
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    72
	for (char c : text.toCharArray()) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    73
	    switch (c) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    74
		case '<':
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    75
		    buffer.append("&lt;");
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    76
		    break;
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
		case '>':
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    79
		    buffer.append("&gt;");
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    80
		    break;
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    81
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    82
		case '\"':
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    83
		    buffer.append("&quot;");
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    84
		    break;
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    85
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    86
		case '\'':
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    87
		    buffer.append("&#039;");
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    88
		    break;
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    89
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    90
		case '\\':
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    91
		    buffer.append("&#092;");
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    92
		    break;
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    93
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    94
		case '&':
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    95
		    buffer.append("&amp;");
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    96
		    break;
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    97
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    98
		case '\n':
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
    99
		    buffer.append("<br/>");
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   100
		    break;
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   101
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   102
		default:
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   103
		    buffer.append(c);
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   104
	    }
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   105
	}
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
	return buffer.toString();
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   108
    }
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   109
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
     * Splits the given string into lines.
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   112
     *
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   113
     * @param	    text
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   114
     *		    the text to format
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
     * @param	    width
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   117
     *		    the maximum width of each line
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   118
     *
78
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents: 17
diff changeset
   119
     * @param	    indent1
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents: 17
diff changeset
   120
     *		    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
   121
     *
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents: 17
diff changeset
   122
     * @param	    indent2
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents: 17
diff changeset
   123
     *		    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
   124
     *
0
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   125
     * @param	    forceBreak
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   126
     *		    whether to force a break in the middle of a word
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   127
     *		    if the length of the word is greater than
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   128
     *		    <code>width</code>
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   129
     *
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   130
     * @return	    a wrapped string separated by newlines
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   131
     */
78
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents: 17
diff changeset
   132
    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
   133
	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
   134
0
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   135
	if (text == null) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   136
	    return text;
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   137
	}
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
	// Replace newlines
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   140
	text = text.replaceAll("\\s*[\\r\\n]\\s*", " ");
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   141
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   142
	StringBuffer buffer = new StringBuffer();
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   143
78
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents: 17
diff changeset
   144
	String indent = indent1;
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents: 17
diff changeset
   145
	int w = width - indent.length();
0
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   146
	if (w < 1) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   147
	    w = 1;
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   148
	}
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   149
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   150
	for (int i = 0; text.length() > 0; i++) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   151
	    String regex = "^\\s*(.{1," + w + "})($|\\s+.*)";
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   152
	    String[] groups = match(text, regex);
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   153
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   154
	    if (groups == null) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   155
		// Can't break line on whitespace without exceeding width
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   156
		if (forceBreak) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   157
		    // Break line in middle of word
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   158
		    regex = "^\\s*(.{1," + w + "})(.*)";
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   159
		} else {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   160
		    // Break line at next whitespace
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   161
		    regex = "^\\s*(.+?)($|\\s+.*)";
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
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   164
		groups = match(text, regex);
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   165
	    }
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   166
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   167
	    if (i != 0) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   168
		buffer.append("\n");
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   169
	    }
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   170
78
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents: 17
diff changeset
   171
	    buffer.append(indent).append(groups[1].trim());
0
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   172
	    text = groups[2];
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   173
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   174
	    if (i == 0) {
78
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents: 17
diff changeset
   175
		indent = indent2;
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents: 17
diff changeset
   176
		w = width - indent.length();
0
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   177
		if (w < 1) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   178
		    w = 1;
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   179
		}
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   180
	    }
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   181
	}
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
	return buffer.toString();
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   184
    }
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   185
78
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents: 17
diff changeset
   186
    /**
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents: 17
diff changeset
   187
     * Shortcut for:
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents: 17
diff changeset
   188
     * <p>
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents: 17
diff changeset
   189
     *	 <code>
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents: 17
diff changeset
   190
     *	   {@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
   191
     *	   (text, width, "", "", forceBreak);
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents: 17
diff changeset
   192
     *	 </code>
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents: 17
diff changeset
   193
     * </p>
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents: 17
diff changeset
   194
     */
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents: 17
diff changeset
   195
    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
   196
	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
   197
    }
6336fcea8db0 2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents: 17
diff changeset
   198
0
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   199
    public static boolean isPrintable(char c) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   200
	return c >= 32 && c <= 126;
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   201
    }
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   202
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   203
    public static String join(String delim, Object... list) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   204
	StringBuffer buffer = new StringBuffer();
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   205
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   206
	for (int i = 0; i < list.length; i++) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   207
	    if (i != 0 && delim != null) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   208
		buffer.append(delim);
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   209
	    }
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   210
	    buffer.append(list[i]);
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
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   213
	return buffer.toString();
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
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   216
    public static String join(String delim, byte... list) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   217
	Byte[] array = new Byte[list.length];
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   218
	for (int i = 0; i < array.length; i++) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   219
	    array[i] = list[i];
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   220
	}
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   221
	return join(delim, (Object[])array);
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
    public static String join(String delim, char... list) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   225
	Character[] array = new Character[list.length];
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   226
	for (int i = 0; i < array.length; i++) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   227
	    array[i] = list[i];
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   228
	}
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   229
	return join(delim, (Object[])array);
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   230
    }
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
    public static String join(String delim, double... list) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   233
	Double[] array = new Double[list.length];
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   234
	for (int i = 0; i < array.length; i++) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   235
	    array[i] = list[i];
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   236
	}
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   237
	return join(delim, (Object[])array);
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   238
    }
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
    public static String join(String delim, float... list) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   241
	Float[] array = new Float[list.length];
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   242
	for (int i = 0; i < array.length; i++) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   243
	    array[i] = list[i];
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   244
	}
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   245
	return join(delim, (Object[])array);
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   246
    }
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
    public static String join(String delim, int... list) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   249
	Integer[] array = new Integer[list.length];
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   250
	for (int i = 0; i < array.length; i++) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   251
	    array[i] = list[i];
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   252
	}
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   253
	return join(delim, (Object[])array);
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   254
    }
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
    public static String join(String delim, long... list) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   257
	Long[] array = new Long[list.length];
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   258
	for (int i = 0; i < array.length; i++) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   259
	    array[i] = list[i];
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   260
	}
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   261
	return join(delim, (Object[])array);
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   262
    }
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
    public static String join(String delim, short... list) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   265
	Short[] array = new Short[list.length];
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   266
	for (int i = 0; i < array.length; i++) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   267
	    array[i] = list[i];
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   268
	}
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   269
	return join(delim, (Object[])array);
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   270
    }
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
    public static String join(String delim, Collection list) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   273
	return join(delim, list.toArray());
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
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   276
    /**
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   277
     * Matches the given string against the given regular expression,
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   278
     * returning an array of the matched regular expression groups.
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
     * @param	    text
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   281
     *		    the String to which to apply the regular
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   282
     *		    expression
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   283
     *
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   284
     * @param	    regex
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   285
     *		    a regular expression containing groups
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
     * @return	    an array of the matched regular expression groups
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   288
     *		    (group 0 is the entire pattern), if the given
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   289
     *		    pattern matched, or {@code null} if the
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   290
     *		    pattern did not match
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   291
     *
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   292
     * @exception   PatternSyntaxException
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   293
     *		    if the given pattern was malformed
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
    public static String[] match(String text, String regex)
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   296
	throws PatternSyntaxException {
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
	Matcher matcher = Pattern.compile(regex).matcher(text);
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   299
	if (!matcher.matches()) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   300
	    return null;
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   301
	}
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   302
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   303
	String[] groups = new String[matcher.groupCount() + 1];
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   304
	for (int i = 0; i < groups.length; i++) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   305
	    groups[i] = matcher.group(i);
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   306
	}
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
	return groups;
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   309
    }
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   310
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   311
    /**
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   312
     * Splits the given text into fields.
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
     * @param	    delim
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   315
     *		    the regular expression delimiter for each field
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   316
     *
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   317
     * @param	    text
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   318
     *		    the text to split
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   319
     *
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   320
     * @param	    quotes
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   321
     *		    the characters that indicate a grouping of text,
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   322
     *		    eg. single quotes ("<code>'</code>") or double
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   323
     *		    quotes ("<code>"</code>"), etc., or
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   324
     *		    <code>null</code> for no quotes
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   325
     *
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   326
     * @param	    quoteEsc
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   327
     *		    an expression that can precede a quote character
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   328
     *		    so that it is interpreted normally, rather than as
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   329
     *		    a quote character, or <code>null</code> if no such
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   330
     *		    escape character is used
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   331
     *
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   332
     * @return	    the resulting fields
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   333
     */
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   334
    public static String[] split(
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   335
	String delim, String text, String quotes, String quoteEsc) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   336
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   337
	// Create the matching regular expression
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   338
	int ngroups = 0;
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   339
	int quoteGroup = -1;
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   340
	int quoteEscGroup = -1;
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   341
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   342
	StringBuffer reBuffer = new StringBuffer("^(.*?)(");
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   343
	int fieldGroup = ++ngroups;
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   344
	ngroups++;
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   345
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   346
	if (quotes != null) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   347
	    if (quoteEsc != null) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   348
		reBuffer.append("(\\Q").append(quoteEsc).append("\\E").
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   349
		    append("[").append(quotes).append("])|");
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   350
		quoteEscGroup = ++ngroups;
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   351
	    }
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   352
	    reBuffer.append("([").append(quotes).append("])|");
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   353
	    quoteGroup = ++ngroups;
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   354
	}
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   355
	reBuffer.append("(").append(delim).append("))(.*)");
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   356
	int delimGroup = ++ngroups;
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   357
	int textGroup = ++ngroups;
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   358
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   359
	String regex = reBuffer.toString();
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   360
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   361
	List<String> fields = new ArrayList<String>();
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   362
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   363
	// Build the field
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   364
	StringBuffer field = new StringBuffer();
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   365
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   366
	boolean inQuote = false;
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   367
	String curQuote = null;
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   368
	while (text != null && text.length() > 0) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   369
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   370
	    String[] groups = match(text, regex);
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   371
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   372
	    if (groups == null) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   373
		field.append(text);
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   374
		text = null;
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   375
	    } else {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   376
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   377
		field.append(groups[fieldGroup]);
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   378
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   379
		// Remaining text
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   380
		text = groups[textGroup];
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   381
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   382
		// Was a delimiter reached?
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   383
		if (groups[delimGroup] != null) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   384
		    if (inQuote) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   385
			field.append(groups[delimGroup]);
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   386
		    } else {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   387
			fields.add(field.toString());
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   388
			field.setLength(0);
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   389
		    }
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   390
		} else
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   391
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   392
		try {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   393
		    String quote = groups[quoteGroup];
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   394
		   // Was a quote reached?
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   395
		    if (quote != null) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   396
			if (inQuote) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   397
			    if (quote.equals(curQuote)) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   398
				inQuote = false;
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   399
			    } else {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   400
				field.append(quote);
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   401
			    }
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   402
			} else {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   403
			    inQuote = true;
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   404
			    curQuote = quote;
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   405
			}
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   406
		    } else
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   407
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   408
		    // Was an escaped quote reached?
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   409
		    if (groups[quoteEscGroup] != null) {
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   410
			field.append(quotes);
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   411
		    }
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   412
		}
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   413
		catch (ArrayIndexOutOfBoundsException ignore) {}
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   414
	    }
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   415
	}
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   416
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   417
	fields.add(field.toString());
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   418
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   419
	return fields.toArray(new String[fields.size()]);
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   420
    }
17
b82508ab9887 2522 hard to differentiate group and share edit views
Stephen Talley <stephen.talley@sun.com>
parents: 13
diff changeset
   421
b82508ab9887 2522 hard to differentiate group and share edit views
Stephen Talley <stephen.talley@sun.com>
parents: 13
diff changeset
   422
    public static String toJavaMethodName(String... words) {
b82508ab9887 2522 hard to differentiate group and share edit views
Stephen Talley <stephen.talley@sun.com>
parents: 13
diff changeset
   423
	StringBuffer buffer = new StringBuffer();
b82508ab9887 2522 hard to differentiate group and share edit views
Stephen Talley <stephen.talley@sun.com>
parents: 13
diff changeset
   424
	for (String word : words) {
b82508ab9887 2522 hard to differentiate group and share edit views
Stephen Talley <stephen.talley@sun.com>
parents: 13
diff changeset
   425
	    try {
b82508ab9887 2522 hard to differentiate group and share edit views
Stephen Talley <stephen.talley@sun.com>
parents: 13
diff changeset
   426
		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
   427
		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
   428
	    } catch (IndexOutOfBoundsException ignore) {
b82508ab9887 2522 hard to differentiate group and share edit views
Stephen Talley <stephen.talley@sun.com>
parents: 13
diff changeset
   429
	    }
b82508ab9887 2522 hard to differentiate group and share edit views
Stephen Talley <stephen.talley@sun.com>
parents: 13
diff changeset
   430
	}
b82508ab9887 2522 hard to differentiate group and share edit views
Stephen Talley <stephen.talley@sun.com>
parents: 13
diff changeset
   431
	return buffer.toString();
b82508ab9887 2522 hard to differentiate group and share edit views
Stephen Talley <stephen.talley@sun.com>
parents: 13
diff changeset
   432
    }
0
62ac12e07fc0 Initial integration.
David Powell <David.Powell@sun.com>
parents:
diff changeset
   433
}