edu.mit.ai.psg.strings
Class Prettify

java.lang.Object
  |
  +--edu.mit.ai.psg.strings.Prettify

public class Prettify
extends Object

Prettify: Reformats line breaks and indentation of strings with nested parens, braces, etc. such as those produced by toString() methods of collections and other complex objects.

A utility to take strings of the form
Set(Set(Array("Eucalyptus", "Mulberry", "Chestnut"), Array("Oak", "Maple", "Ironwood")), Set( "Apple", "Cherry", "Plum", "Peach"))
and format them into something more readable, such as the following (minus the dots, which are there to force JavaDoc to preserve indentation):

.   Set(Set(Array("Eucalyptus", "Mulberry", "Chestnut"), 
.           Array("Oak", "Maple", "Ironwood")),
.       Set("Apple", "Cherry", "Plum", "Peach"))
Assumes result string will be displayed in a fixed width font, though still a great improvement even if the font is variable width.

Note that static parameters are "globally" shared, so in multithread code the verbose interface to string(..) should be called to tailor parameters, rather than changing the static default values.


Inner Class Summary
static class Prettify.ASCIISyntax
          ASCII-only syntax
static class Prettify.JavaSyntax
          Uses Character predicates for Unicode syntax
static class Prettify.JavaSyntaxNoEscapes
          Ignores escape characters (e.g., for strings with pathnames using '\')
static interface Prettify.Syntax
          Syntax used by Prettify
 
Field Summary
static Prettify.Syntax defaultSyntax
          default is Prettify.JavaSyntaxNoEscapes
static int depthLimit
          Max levels of nesting (count parens; -1 [default] means include everything to unbounded depth)
static int lengthLimit
          Max number of elements (count commas; -1 [default] means include everything to unbounded length)
static boolean shorterNotFaster
          shorterNotFaster controls which algorithm to use.
true = use slower algorithm that produces shorter strings [default];
false = use faster algorithm that produces longer strings;
static int widthLimit
          Max width of output string (count chars; default is 80, -1 for no limit).
 
Constructor Summary
Prettify()
           
 
Method Summary
static String getCopyright()
           
static String getHomepage()
           
static String getNoWarranty()
           
static String getVersion()
           
static String string(int indent, String uglyString)
          Prettify string with additional indentation after first line.
static String string(int indent, String uglyString, boolean doShorterNotFaster)
          Prettify string, specifying indentation and choosing algorithm.
static String string(int indent, String uglyString, int maxWidth)
          Prettify string, specifying indentation and maximum width.
static String string(int indent, String uglyString, int maxWidth, int maxDepth, int maxLength, boolean doShorterNotFaster)
          Deprecated. 1999.04, replaced by string(int, String, int, int, int, boolean, Prettify.Syntax)
static String string(int indent, String uglyString, int maxWidth, int maxDepth, int maxLength, boolean doShorterNotFaster, Prettify.Syntax syntax)
          Prettify string, specifying everything.
static String string(String uglyString)
          Prettify a string, reformatting indentation and line breaks.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

depthLimit

public static int depthLimit
Max levels of nesting (count parens; -1 [default] means include everything to unbounded depth)

lengthLimit

public static int lengthLimit
Max number of elements (count commas; -1 [default] means include everything to unbounded length)

widthLimit

public static int widthLimit
Max width of output string (count chars; default is 80, -1 for no limit). If string cannot be formatted within maxWidth, unformatted trimmed string will be returned.

shorterNotFaster

public static boolean shorterNotFaster
shorterNotFaster controls which algorithm to use.
true = use slower algorithm that produces shorter strings [default];
false = use faster algorithm that produces longer strings;

defaultSyntax

public static Prettify.Syntax defaultSyntax
default is Prettify.JavaSyntaxNoEscapes
See Also:
Prettify.Syntax
Constructor Detail

Prettify

public Prettify()
Method Detail

getCopyright

public static String getCopyright()

getNoWarranty

public static String getNoWarranty()

getVersion

public static String getVersion()

getHomepage

public static String getHomepage()

string

public static String string(String uglyString)
Prettify a string, reformatting indentation and line breaks.
Parameters:
uglyString - string to be reformatted

string

public static String string(int indent,
                            String uglyString)
Prettify string with additional indentation after first line. Good for cases where the first line is after a title, e.g.,:
.        Food:   Set(Apples, Oranges, Sandwiches,
.                    Carrots, Raisins)
(Note that spaces at beginning of uglyString will be removed.)
Parameters:
indent - Number of spaces to add to indentation of lines after first
uglyString - string to be reformatted

string

public static String string(int indent,
                            String uglyString,
                            boolean doShorterNotFaster)
Prettify string, specifying indentation and choosing algorithm.
Parameters:
indent - Number of spaces to add to indentation of lines after first
uglyString - String to be reformatted
doShorterNotFaster - Selects algorithm:
true = use slower algorithm that produces shorter strings;
false = use faster algorithm that produces longer strings.

string

public static String string(int indent,
                            String uglyString,
                            int maxWidth)
Prettify string, specifying indentation and maximum width.
Parameters:
indent - Number of spaces to add to indentation of lines after first
uglyString - String to be reformatted
maxWidth - Limit on number of chars per line (including indent, -1 for no limit). If string cannot be formatted within maxWidth, returns unformatted trimmed string.

string

public static String string(int indent,
                            String uglyString,
                            int maxWidth,
                            int maxDepth,
                            int maxLength,
                            boolean doShorterNotFaster)
Deprecated. 1999.04, replaced by string(int, String, int, int, int, boolean, Prettify.Syntax)


string

public static String string(int indent,
                            String uglyString,
                            int maxWidth,
                            int maxDepth,
                            int maxLength,
                            boolean doShorterNotFaster,
                            Prettify.Syntax syntax)
Prettify string, specifying everything.
Parameters:
indent - Number of spaces to add to indentation of lines after first
uglyString - String to be reformatted
maxWidth - Limit on number of chars per line (including indent, -1 for no limit). If string cannot be formatted within maxWidth, returns unformatted trimmed string.
maxDepth - Limit on number of levels of parentheses, braces, etc. shown (-1 for no limit). Omit with ellipses ... substrings nested in more than maxDepth levels of parentheses, brackets, or braces.
maxLength - Limit on number of terms at same level of parenthesization (-1 for no limit). Omit with ellipses ... substrings in lists after maxLength terms.
doShorterNotFaster - Selects: algorithm:
true = use slower algorithm that produces shorter strings;
false = use faster algorithm that produces longer strings.
syntax - Specifies what to treat as whitespace, unbreakable.words, list and string delimiters, etc.

Copyright (c) 1996-1999
Massachusetts Institute of Technology

Feedback: jeva-feedback@ai.mit.edu