* & becomes & * " becomes " * < becomes < * > becomes > * newline becomes ** * @param string The string to escape. * @return A new string with special characters replaced. */ public static String escapeForXML(String string) { string = substitute(string, "&", "&"); string = substitute(string, "\"", """); string = substitute(string, "<", "<"); string = substitute(string, ">", ">"); string = substitute(string, "\n", " "); return string; } /** Return a number of spaces that is proportional to the argument. * If the argument is negative or zero, return an empty string. * @param level The level of indenting represented by the spaces. * @return A string with zero or more spaces. */ public static String getIndentPrefix(int level) { if (level <= 0) { return ""; } StringBuffer result = new StringBuffer(""); for (int i = 0; i < level; i++) { result.append(" "); } return result.toString(); } /** Return the preferences directory, creating it if necessary. * @return A string naming the preferences directory. The last * character of the string will have the file.separator character * appended. * @exception IOException If the directory could not be created. * @see #PREFERENCES_DIRECTORY */ public static String preferencesDirectory() throws IOException { String preferencesDirectoryName = StringUtilities.getProperty("user.home") + StringUtilities.getProperty("file.separator") + StringUtilities.PREFERENCES_DIRECTORY + StringUtilities.getProperty("file.separator"); File preferencesDirectory = new File(preferencesDirectoryName); if (!preferencesDirectory.isDirectory()) { if (preferencesDirectory.mkdirs() == false) { throw new IOException("Could not create user preferences " + "directory '" + preferencesDirectoryName + "'"); } } return preferencesDirectoryName; } /** Get the specified property from the environment. An empty string * is returned if the argument environment variable does not exist, * though if certain properties are not defined, then we * make various attempts to determine them and then set them. * See the javadoc page for java.util.System.getProperties() for * a list of system properties. *
The following properties are handled specially *
c:/foo
* whereas most of the other methods that operate
* on path names return C:/foo
.
* * "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 an 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.
* @param name A string with spaces and other characters that
* cannot be in a Java name.
* @return A String that follows the Java identifier rules.
*/
public static String sanitizeName(String name) {
char [] nameArray = name.toCharArray();
for (int i = 0; i < nameArray.length; i++) {
if (!Character.isJavaIdentifierPart(nameArray[i])) {
nameArray[i] = '_';
}
}
if (!Character.isJavaIdentifierStart(nameArray[0])) {
return "_" + new String(nameArray);
} else {
return new String(nameArray);
}
}
/** If the string is longer than 79 characters, split it up by
* displaying adding newlines in all newline delimited substrings
* that are longer than 79 characters.
* If the longName argument is null, then the string
* " 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.
*
* @param inputString The String to tokenize
* @return An array of substrings.
* @exception IOException Thrown if StreamTokenizer.nextToken() throws it.
*/
public static String [] tokenizeForExec(String inputString)
throws IOException {
// The java.lang.Runtime.exec(String command) call uses
// java.util.StringTokenizer() to parse the command string.
// Unfortunately, this means that double quotes are not handled
// in the same way that the shell handles them in that 'ls "foo
// 'bar"' will interpreted as three tokens 'ls', '"foo' and
// 'bar"'. In the shell, the string would be two tokens 'ls' and
// '"foo bar"'. What is worse is that the exec() behaviour is
// slightly different under Windows and Unix. To solve this
// problem, we preprocess the command argument using
// java.io.StreamTokenizer, which converts quoted substrings into
// single tokens. We then call java.lang.Runtime.exec(String []
// commands);
// Parse the command into tokens
List commandList = new LinkedList();
StreamTokenizer streamTokenizer =
new StreamTokenizer(new StringReader(inputString));
// We reset the syntax so that we don't convert to numbers,
// otherwise, if PTII is "d:\\tmp\\ptII\ 2.0", then
// we have no end of problems.
streamTokenizer.resetSyntax();
streamTokenizer.whitespaceChars(0 , 32);
streamTokenizer.wordChars(33, 127);
// We can't use quoteChar here because it does backslash
// substitution, so "c:\ptII" ends up as "c:ptII"
// Substituting forward slashes for backward slashes seems like
// overkill.
// streamTokenizer.quoteChar('"');
streamTokenizer.ordinaryChar('"');
streamTokenizer.eolIsSignificant(true);
streamTokenizer.commentChar('#');
// Current token
String token = "";
// Single character token, usually a -
String singleToken = "";
// Set to true if we are inside a double quoted String.
boolean inDoubleQuotedString = false;
while (streamTokenizer.nextToken()
!= StreamTokenizer.TT_EOF) {
switch (streamTokenizer.ttype) {
case StreamTokenizer.TT_WORD:
if (inDoubleQuotedString) {
if ( token.length() > 0 ) {
token += " ";
}
token += singleToken + streamTokenizer.sval;
} else {
token = singleToken + streamTokenizer.sval;
commandList.add(token);
}
singleToken = "";
break;
case StreamTokenizer.TT_NUMBER:
throw new RuntimeException("Internal error: Found TT_NUMBER: '"
+ streamTokenizer.nval + "'. We should not be "
+ "tokenizing numbers");
//break;
case StreamTokenizer.TT_EOL:
break;
case StreamTokenizer.TT_EOF:
break;
default:
singleToken =
(new Character((char)streamTokenizer.ttype)).toString();
if (singleToken.equals("\"")) {
if (inDoubleQuotedString) {
commandList.add(token);
}
inDoubleQuotedString = ! inDoubleQuotedString;
singleToken = "";
token = "";
}
break;
}
}
return
(String [])commandList.toArray(new String[commandList.size()]);
}
/** Return a string that contains a description of how to use this
* class.
* @param commnandTemplate A string naming the command and the
* format of the arguments, for example
* "moml [options] [file . . .]"
* @param 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", "
* @param commandFlags An array of Strings that list command-line
* options that are either present or not.
* @returns A string that descripts the command.
*/
public static String usageString(String commandTemplate,
String [][] commandOptions, String [] commandFlags) {
// This method is static so that we can reuse it in places
// like copernicus/kernel/Copernicus and actor/gui/MoMLApplication
String result = "Usage: " + commandTemplate + "\n\n"
+ "Options that take values:\n";
int i;
for (i = 0; i < commandOptions.length; i++) {
result += " " + commandOptions[i][0] +
" " + commandOptions[i][1] + "\n";
}
result += "\nBoolean flags:\n";
for (i = 0; i < commandFlags.length; i++) {
result += " " + commandFlags[i];
}
return result;
}
///////////////////////////////////////////////////////////////////
//// public variables ////
// If you change these, be sure to try running vergil on
// a HSIF moml file
// vergil ../hsif/demo/SwimmingPool/SwimmingPool.xml
/** Maximum length in characters of a long string before
* {@link #ellipse(String, int)} truncates and add a
* trailing . . .
*/
public static final int ELLIPSIS_LENGTH_LONG = 2000;
/** Maximum length in characters of a short string before
* {@link #ellipse(String, int)} truncates and add a
* trailing . . .
*/
public static final int ELLIPSIS_LENGTH_SHORT = 400;
/** Location of Application preferences such as the user library.
* @see #preferencesDirectory()
*/
public static String PREFERENCES_DIRECTORY = ".ptolemyII";
}