public class StringUtilities
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static int |
ELLIPSIS_LENGTH_LONG
Maximum length in characters of a long string before
ellipsis(String, int) truncates and add a
trailing ". . .". |
static int |
ELLIPSIS_LENGTH_SHORT
Maximum length in characters of a short string before
ellipsis(String, int) truncates and add a
trailing ". . .". |
static java.lang.String |
LINE_SEPARATOR
The line separator string.
|
static java.lang.String |
PREFERENCES_DIRECTORY
Location of Application preferences such as the user library.
|
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
abbreviate(java.lang.String longName)
Abbreviate a string.
|
static void |
addDirectoryToJavaLibraryPath(java.lang.String directoryName)
Add a directory to the java.library.path directory..
|
static void |
addPtolemyLibraryDirectoryToJavaLibraryPath()
Add the $PTII/lib directory to the java.library.path directory.
|
static java.lang.String |
ellipsis(java.lang.String string,
int length)
Return a string with a maximum line length of length
characters, limited to the given number of characters.
|
static java.lang.String |
escapeForXML(java.lang.String string)
Given a string, replace all the instances of XML special characters
with their corresponding XML entities.
|
static java.lang.String |
escapeForXML(java.lang.String string,
boolean escapeLinebreaks)
Given a string, replace all the instances of XML special characters
with their corresponding XML entities.
|
static java.lang.String |
escapeString(java.lang.String string)
Given a string, return a string that when fed to the
Ptolemy expression parser, will turn into the argument
string.
|
static void |
exit(int returnValue)
If the ptolemy.ptII.exitAfterWrapup or the
ptolemy.ptII.doNotExit properties are not set, then call
System.exit().
|
static java.lang.String |
getIndentPrefix(int level)
Return a number of spaces that is proportional to the argument.
|
static java.lang.String |
getProperty(java.lang.String propertyName)
Get the specified property from the environment.
|
static boolean |
inApplet()
Return true if we are in an applet.
|
static boolean |
isValidIdentifier(java.lang.String name)
Test whether a string is a valid Java identifier.
|
static void |
mergePropertiesFile()
Merge the properties in lib/ptII.properties with the current
properties.
|
static java.lang.String |
objectToSourceFileName(java.lang.Object object)
Return a string representing the name of the file expected to
contain the source code for the specified object.
|
static java.lang.String |
preferencesDirectory()
Return the preferences directory, creating it if necessary.
|
static java.lang.String |
propertiesFileName()
Return the name of the properties file.
|
static java.util.LinkedList<java.lang.String> |
readLines(java.lang.String lines)
Return a LinkedList of the lines in a string that aren't comments.
|
static java.lang.String |
sanitizeName(java.lang.String name)
Sanitize a String so that it can be used as a Java identifier.
|
static java.lang.String |
split(java.lang.String longName)
If the string is longer than 79 characters, split it up by
adding newlines in all newline delimited substrings
that are longer than 79 characters.
|
static java.lang.String |
split(java.lang.String longName,
int length)
If the string is longer than length characters,
split the string up by adding newlines in all
newline delimited substrings that are longer than length
characters.
|
static java.net.URL |
stringToURL(java.lang.String name,
java.net.URI baseDirectory,
java.lang.ClassLoader classLoader)
Deprecated.
Use FileUtilities.nameToURL instead.
|
static java.lang.String |
substitute(java.lang.String string,
java.lang.String pattern,
java.lang.String replacement)
Replace all occurrences of pattern in the specified
string with replacement.
|
static java.lang.String |
substituteFilePrefix(java.lang.String prefix,
java.lang.String string,
java.lang.String replacement)
Perform file prefix substitution.
|
static java.lang.String[] |
tokenizeForExec(java.lang.String inputString)
Tokenize a String to an array of Strings for use with
Runtime.exec(String []).
|
static java.lang.String |
truncateString(java.lang.String string,
int lineLength,
int numberOfLines)
Return a string with a maximum line length of lineLength
and a maximum number of lines numberOfLines.
|
static java.lang.String |
unescapeForXML(java.lang.String string)
Given a string, replace all the instances of XML entities
with their corresponding XML special characters.
|
static java.lang.String |
usageString(java.lang.String commandTemplate,
java.lang.String[][] commandOptions,
java.lang.String[] commandFlags)
Return a string that contains a description of how to use a
class that calls this method.
|
static java.lang.String |
usageString(java.lang.String commandTemplate,
java.lang.String[][] commandOptions,
java.lang.String[][] commandFlagsWithDescriptions)
Return a string that contains a description of how to use a
class that calls this method.
|
public static final int ELLIPSIS_LENGTH_LONG
ellipsis(String, int)
truncates and add a
trailing ". . .". This variable is used by callers
of ellipsis(String, int).public static final int ELLIPSIS_LENGTH_SHORT
ellipsis(String, int)
truncates and add a
trailing ". . .". This variable is used by callers
of ellipsis(String, int).public static final java.lang.String LINE_SEPARATOR
public static final java.lang.String PREFERENCES_DIRECTORY
preferencesDirectory()
,
Constant Field Valuespublic static java.lang.String abbreviate(java.lang.String longName)
longName
- The string to be abbreviated.split(String)
public static void addDirectoryToJavaLibraryPath(java.lang.String directoryName) throws java.io.IOException
This code may only work on certain JVMs
Based on code from http://forums.sun.com/thread.jspa?threadID=707176 and http://stackoverflow.com/questions/5419039/is-djava-library-path-equivalent-to-system-setpropertyjava-library-path
directoryName
- The directory to be added.java.io.IOException
- If there are insufficient permissions to
get set the java.library.path environment variable or there
is no such field as usr_paths in the ClassLoader.public static void addPtolemyLibraryDirectoryToJavaLibraryPath() throws java.io.IOException
This code may only work on certain JVMs
java.io.IOException
- If there are insufficient permissions to
get set the java.library.path environment variable or there
is no such field as usr_paths in the ClassLoader.public static java.lang.String ellipsis(java.lang.String string, int length)
string
- The string to truncate.length
- The number of characters to which to truncate the string.public static java.lang.String escapeForXML(java.lang.String string)
In this method, we make the following translations:
& becomes & " becomes " < becomes < > becomes > newline becomes carriage return becomes $amp;#13;
string
- The string to escape.unescapeForXML(String)
public static java.lang.String escapeForXML(java.lang.String string, boolean escapeLinebreaks)
In this method, we make the following translations:
& becomes & " becomes " < becomes < > becomes > newline becomes , if requested. carriage return becomes $amp;#13;, if requested.
string
- The string to escape.string
- Whether or not to escape line break characters.unescapeForXML(String)
public static java.lang.String escapeString(java.lang.String string)
x"y becomes x\"y; x\"y becomes x\\\"y; x\y"z becomes x\\y\"z; x\\y\"z becomes x\\\\y\\\"Similarly, this method replaces the following characters exactly as defined in Java strings: \n, \t, \b, \r, and \f.
string
- The string to escape.public static void exit(int returnValue)
returnValue
- The return value of this process, where
non-zero values indicate an error.public static java.lang.String getIndentPrefix(int level)
level
- The level of indenting represented by the spaces.public static java.lang.String getProperty(java.lang.String propertyName)
The following properties are handled specially
c:/foo
whereas most of the other
methods that operate on path names return
C:/foo
.
propertyName
- The name of property.public static boolean inApplet()
public static boolean isValidIdentifier(java.lang.String name)
"An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter. An identifier cannot have the same spelling (Unicode character sequence) as a keyword (3.9), boolean literal (3.10.3), or the null literal (3.10.7)."Java characters are A-Z, a-z, $ and _.
Characters that are not permitted in a Java identifier are changed to underscores. This method does not check whether the string is a keyword or literal.
name
- The name to be checked.public static void mergePropertiesFile() throws java.io.IOException
java.io.IOException
- If thrown while looking for the
$CLASSPATH/lib/ptII.properties file.public static java.lang.String objectToSourceFileName(java.lang.Object object)
object
- The object.public static java.lang.String preferencesDirectory() throws java.io.IOException
java.io.IOException
- If the directory could not be created.PREFERENCES_DIRECTORY
public static java.lang.String propertiesFileName() throws java.io.IOException
PREFERENCES_DIRECTORY
directory that is returned
by preferencesDirectory()
. Typically, this value
is "$HOME/.ptolemyII/ptII.properties".java.io.IOException
- If preferencesDirectory()
throws it.preferencesDirectory()
,
PREFERENCES_DIRECTORY
public static java.util.LinkedList<java.lang.String> readLines(java.lang.String lines) throws java.io.IOException
lines
- A String containing the lines to be separated.java.io.IOException
- If thrown when reading from the input String.public static java.lang.String sanitizeName(java.lang.String name)
"An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter. An identifier cannot have the same spelling (Unicode character sequence) as a keyword (3.9), boolean literal (3.10.3), or the null literal (3.10.7)."Java characters are A-Z, a-z, $ and _.
Characters that are not permitted in a Java identifier are changed to underscores. This method does not check that the returned string is a keyword or literal. Note that two different strings can sanitize to the same string. This method is commonly used during code generation to map the name of a ptolemy object to a valid identifier name.
name
- A string with spaces and other characters that
cannot be in a Java name.public static java.lang.String split(java.lang.String longName)
longName
- The string to optionally split upabbreviate(String)
,
split(String, int)
public static java.lang.String split(java.lang.String longName, int length)
longName
- The string to optionally split.length
- The maximum length of the sequence of characters
before a newline is inserted.abbreviate(String)
,
split(String)
@Deprecated public static java.net.URL stringToURL(java.lang.String name, java.net.URI baseDirectory, java.lang.ClassLoader classLoader) throws java.io.IOException
name
- The name of a file or URL.baseDirectory
- The base directory for relative file names,
or null to specify none.classLoader
- The class loader to use to locate system
resources, or null to use the system class loader.java.io.IOException
- If the file cannot be read, or
if the file cannot be represented as a URL (e.g. System.in), or
the name specification cannot be parsed.java.net.MalformedURLException
- If the URL is malformed.public static java.lang.String substitute(java.lang.String string, java.lang.String pattern, java.lang.String replacement)
string
- The string to edit.pattern
- The string to replace.replacement
- The string to replace it with.public static java.lang.String substituteFilePrefix(java.lang.String prefix, java.lang.String string, java.lang.String replacement)
If prefix is not a simple prefix of string, then we use the file system to find the canonical names of the files. For this to work, prefix and string should name files that exist, see java.io.File.getCanonicalFile() for details.
If prefix is not a prefix of string, then we return string
prefix
- The prefix string, for example, "c:/ptII".string
- The string to be substituted, for example,
"c:/ptII/ptolemy".replacement
- The replacement to be substituted in, for example,
"$PTII"public static java.lang.String[] tokenizeForExec(java.lang.String inputString) throws java.io.IOException
Lines that begin with an octothorpe '#' are ignored. Substrings that start and end with a double quote are considered to be a single token and are returned as a single array element.
inputString
- The String to tokenizejava.io.IOException
- If StreamTokenizer.nextToken() throws it.public static java.lang.String truncateString(java.lang.String string, int lineLength, int numberOfLines)
string
- The string to truncate.lineLength
- The number of characters to which to truncate each line.numberOfLines
- The maximum number of lines.public static java.lang.String unescapeForXML(java.lang.String string)
In this method, we make the following translations:
& becomes & " becomes " < becomes < > becomes > becomes newline becomes carriage return
string
- The string to escape.escapeForXML(String)
public static java.lang.String usageString(java.lang.String commandTemplate, java.lang.String[][] commandOptions, java.lang.String[] commandFlags)
commandTemplate
- A string naming the command and the
format of the arguments, for example
"moml [options] [file . . .]"commandOptions
- A 2xN array of Strings that list command-line
options that take arguments where the first
element is a String naming the command line option, and the
second element is the argument, for example
{"-class", "<classname>")
commandFlags
- An array of Strings that list command-line
options that are either present or not.public static java.lang.String usageString(java.lang.String commandTemplate, java.lang.String[][] commandOptions, java.lang.String[][] commandFlagsWithDescriptions)
commandTemplate
- A string naming the command and the
format of the arguments, for example
"moml [options] [file . . .]"commandOptions
- A 2xN array of Strings that list command-line
options that take arguments where the first
element is a String naming the command line option, and the
second element is the argument, for example
{"-class", "<classname>")
commandFlagsWithDescriptions
- A 2xM array of Strings that list
command-line options that are either present or not and a description
of what the command line option does.