In addition to the standard FreeMarker template constructs, there are several Java utility classes, template macros and functions that can be used in your wizard templates to ease the development of custom wizards. The following sections briefly describe these classes and methods.
iif(condition, valueIfTrue, valueIfFalse)
Inline if. The FreeMarker language does not have a conditional operator
(like ?: of C/C++ ), but the iif()
function can save you from
having to spell out <#if>..<#else>..</#if>
, when required.
It is important to note that unlike in C/C++, the evaluation is not lazy (i.e. both the "then" and
the "else" expressions are always evaluated).
<@do expression !/>
FreeMarker does not have a construct for calling a function and then discarding
the result. One could use <#assign dummy = expression>
, but this will fail if the called (Java) function is void or returns null.
We recommend our small <@do ..!/>
macro which takes one argument and does nothing,
and the exclamation mark (the FreeMarker default value operator) cures the
void/null problem.
The following Java classes are available during template processing:
Represents the Java Math class, which contains mathematical functions. See http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Math.html .
Math has the following methods:
double cos(double x)
double sin(double x)
double pow(double x, double y)
etc.
Represents the Apache Commons StringUtils class, which contains over a hundred utility functions for manipulating strings. See: http://commons.apache.org/lang/api/org/apache/commons/lang/StringUtils.html .
StringUtils has the following methods:
boolean isEmpty(String s)
boolean isBlank(String s)
String capitalize(String s)
String upperCase(String s)
String lowerCase(String s)
boolean startsWith(String s, String suffix)
boolean endsWith(String s, String prefix)
String[] split(String s)
String join(String[] strings)
etc.
Represents the Apache Commons WordUtils class, which contains utility functions for manipulating strings as word sequences. See http://commons.apache.org/lang/api/org/apache/commons/lang/WordUtils.html .
WordUtils has the following methods:
String wrap(String str, int wrapLength)
String capitalize(String str)
String swapCase(String str)
etc.
Represents the Apache Commons CollectionUtils class, which contains some useful functions for manipulating collections (like lists). Functions include computing set union, intersection, and difference. See http://commons.apache.org/collections/apidocs/org/apache/commons/collections/CollectionUtils.html
CollectionUtils has the following methods:
Collection union(Collection a, Collection b)
Collection intersection(Collection a, Collection b)
Collection subtract(Collection a, Collection b)
etc.
Contains utility functions for reading files in the following formats: XML, JSON, CSV, property file, and functions to read and return a text file as a single string, as an array of lines, and as a an array of string arrays (where string arrays were created by splitting each by whitespace).
There are two sets of functions; one works on files in the Eclipse workspace and the other on "external" files (i.e. files in the file system). Files are interpreted in the Java platform’s default encoding (unless they are XML files, which specify their own encoding).
FileUtils has the following methods:
org.w3c.dom.Document readXMLFile(String fileName) org.w3c.dom.Document readExternalXMLFile(String fileName)
Parses an XML file, and return the Document object of the resulting DOM tree.
Object readJSONFile(String fileName) Object readExternalJSONFile(String fileName)
Parses a JSON file. The result is a Boolean, Integer, Double, String, List, Map, or any data structure composed of them. The JSON syntax is documented at http://json.org ; if you want to check whether a particular text file corresponds to the JSON syntax, use http://jsonlint.com .
String[][] readCSVFile(String fileName, boolean ignoreFirstLine, boolean ignoreBlankLines, boolean ignoreCommentLines) String[][] readExternalCSVFile(String fileName, boolean ignoreFirstLine, boolean ignoreBlankLines, boolean ignoreCommentLines)
Reads a CSV file. The result is an array of lines, where each line is a string array. Additional method parameters control whether to discard the first line of the file (which is usually a header line), whether to ignore blank lines, and whether to ignore comment lines (those starting with the # character). Comment lines are not part of the commonly accepted CSV format, but they are supported here nevertheless due to their usefulness.
Properties readPropertyFile(String fileName) Properties readExternalPropertyFile(String fileName)
Parses a Java property file (key=value lines) in the workspace. The result is a Properties object, which is a hash of key-value pairs.
String[][] readSpaceSeparatedTextFile(String fileName, boolean ignoreBlankLines, boolean ignoreCommentLines) String[][] readExternalSpaceSeparatedTextFile(String fileName, boolean ignoreBlankLines, boolean ignoreCommentLines)
Reads a text file and return its contents, split by lines with each line split by whitespace. Additional method parameters control whether to ignore blank lines and/or comment lines (those starting with the # character). The result is an array of lines, where each line is a string array of the items on the line.
String[] readLineOrientedTextFile(String fileName) String[] readExternalLineOrientedTextFile(String fileName)
Reads a text file in the workspace and returns its lines. Comment lines (those starting with a hash mark, #) are discarded. The result is a string array.
String readTextFile(String fileName) String readExternalTextFile(String fileName)
Reads a text file, and return its contents unchanged as a single string.
boolean isValidWorkspacePath(String path)
Returns true if the given string is syntactically a valid workspace path.
boolean isValidWorkspaceFilePath(String path)
Returns true if the given string is syntactically a valid workspace file path. This function does not check whether the file exists, or whether the given path already points to a resource of a different type.
IWorkspaceRoot getWorkspaceRoot()
Returns the workspace root object. The workspace contains the user’s projects.
IProject asProject(String path)
Returns the handle for the workspace project with the given name. Throws exception if the path is not a valid workspace project path. This function does not test whether the project exists. To test that, call the exists() method on the returned handle.
IContainer asContainer(String path)
Returns the handle for the workspace container (i.e. project or folder) with the given name. Throws exception if the path is not a valid workspace container path. This function does not test whether the container exists. To test that, call the exists() method on the returned handle.
IFile asFile(String path)
Returns the handle for the workspace file with the given name. Throws exception if the path is not a valid workspace file path. This function does not test whether the file exists. To test that, call the exists() method on the returned handle.
IResource asResource(String pathName)
Returns the handle for the workspace project, folder or file with the given name. If the resource does not exist and the path contains more than one segment (i.e. it cannot be a project), it is returned as a file handle if it has a file extension, and as a folder if it does not.
File asExternalFile(String path)
Returns a java.io.File object for the given path. The object can be used to access operations provided by the File API, such as exists(), length(), etc.
void copy(String path, String destPath, IProgressMonitor monitor)
Copies a workspace resource (file, folder or project) specified with its path to the destination path. For projects and folders, it copies recursively (i.e. copies the whole folder tree). From the project root directory, it leaves out dot files, hidden files, and team private files.
void copyURL(String url, String destFilePath, IProgressMonitor monitor)
Copies the file at the given URL to the given destination workspace file.
String createTempFile(String content)
Writes the given string to a temporary file and returns the path of the temporary file in the file system. The file will be automatically deleted when the IDE exits, but it can be also deleted earlier via deleteExternalFile().
void createFile(String fileName, String content)
Creates a workspaces text file with the given contents in the platform’s default encoding.
void createExternalFile(String fileName, String content)
Creates a text file in the file system with the given contents in the platform’s default encoding.
void deleteFile(String fileName)
Deletes the given workspace file. It is acceptable to invoke it on a nonexistent file.
void deleteExternalFile(String fileName)
Deletes the given file from the file system. It is acceptable to invoke it on a nonexistent file.
void createDirectory(String fileName)
Creates a workspace folder. The parent must exist.
void createExternalDirectory(String fileName)
Creates a directory in the file system. The parent must exist.
void removeDirectory(String fileName)
Deletes a workspace folder. The folder must be empty. It is acceptable to invoke it on a nonexistent folder.
void removeExternalDirectory(String fileName)
Deletes a directory in the file system. The directory must be empty. It is acceptable to invoke it on a nonexistent directory.
Provides utility methods to work with NED types and check their existence.
NedUtils has the following methods:
boolean isVisibleType(String typeName, String inFolder)
Returns whether the given NED type is visible in the given folder. If the type is a fully qualified name, it is recognized if it is defined in the same project as the given folder, or in one of its referenced projects. If the type is a simple name (without package), it is recognized if it is in the NED package of the given folder.
INEDTypeInfo getNedType(String typeName, String inFolder)
Like isVisibleNedType(), but actually returns the given NED type if it was found; otherwise, it returns null. Useful if you implement a complex wizard page in Java.
Provides entry points into various aspects of the IDE. This includes access to the Eclipse workspace (projects, folders, files) and the NED index. The former is documented in the Eclipse Platform help; documentation for the latter can be found in the sources (Javadoc). See http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.platform.doc.isv/guide/resInt.htm and http://help.eclipse.org/galileo/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/core/resources/IWorkspaceRoot.html
IDEUtils has the following methods:
boolean openConfirm(String title, String message, String detailsMessage) boolean openQuestion(String title, String message, String detailsMessage) boolean openError(String title, String message, String detailsMessage) boolean openWarning(String title, String message, String detailsMessage) boolean openInformation(String title, String message, String detailsMessage)
Opens a standard message dialog with a closable details message.
Provides a collection of Java language related utility functions.
LangUtils has the following methods:
boolean hasMethod(Object object, String methodName)
Returns true if the object has a method with the given name. Method args and return type are not taken into account. Search is performed on the object’s class and all super classes.
boolean hasField(Object object, String fieldName)
Returns true if the object has a field with the given name. Field type is not taken into account. Search is performed on the object’s class and all super classes.
boolean instanceOf(Object object, String classOrInterfaceName)
Returns true if the given object is an instance of (subclasses from or implements) the given class or interface. To simplify usage, the class or interface name is accepted both with and without the package name.
String toString(Object object)
Produces a user-friendly representation of the object. In case of collections (lists, maps, etc.), the representation is JSON-like.
List<Object> newList()
Creates and returns a new mutable List object (currently ArrayList).
Map<Object, Object> newMap()
Creates and returns a new mutable Map object (currently HashMap).
Set<Object> newSet()
Creates and returns a new mutable Set object (currently HashSet).
Class<?> getClass(Object object)
Returns the class of the given object. Provided because BeanWrapper seems to have a problem with the getClass() method.
Provides functionality to start external applications from the wizard.
ProcessUtis has the following methods:
ProcessResult exec(String command, String[] arguments, String workingDirectory, String standardInput, double timeout)
Executes the given command with the arguments as a separate process. The standard input is fed into the spawn process and the output is read until the process finishes or a timeout occurs. The timeout value 0 means wait infinitely long to finish the process. Arguments at the end of the argument list are optional.
String lookupExecutable(String name)
Finds the given executable in the path and returns it with full path. If not found, it returns the original string.