Copyright© 2008-2022 Sitevision AB, all rights reserved.
@Requireable(value="ScriptUtil") public interface ScriptUtil
An instance of the Sitevision class implementing this interface can be obtained via Utils.getScriptUtil().
See Utils for how to obtain an instance of the Utils interface.
| Modifier and Type | Method and Description |
|---|---|
String |
flatten(String aString)
Flattens a multi-lined string to a trimmed single-line string.
|
String |
getCalendarAsString(String aFormatPattern,
Calendar aCalendar)
Deprecated.
|
String |
getDateAsString(String aFormatPattern,
Date aDate)
Deprecated.
use
DateUtil.getDateAsString(String, java.util.Date) instead |
boolean |
getFalse()
Gets the boolean false primitive.
|
String |
getFormatPattern(Locale aLocale)
Deprecated.
|
String |
getHumanPresentableSize(long aSize)
Methods that returns a human presentable size with an appropriate byte-suffix (bytes/kB/MB/GB/TB).
|
Object |
getInstance(String aQualifiedClassName)
Deprecated.
Use
InstanceCreatorUtil.getDefaultInstance(String) instead |
List |
getList(Object[] anArray)
Methods that wraps an array in a
List. |
Object |
getNonBlank(String aString)
Gets an object that is ensured not to be null or a whitespace only string.
|
Object |
getNonBlank(String aString,
Object aDefaultValue)
Gets a default value if a string is null or whitespace only.
|
Object |
getNonNull(Object anObject)
Gets an object that is ensured not to be null.
|
Object |
getNonNull(Object anObject,
Object aDefaultValue)
Gets a default value if an object is null.
|
Object |
getNull()
Gets the null reference.
|
String |
getSystemProperty(String aName)
Deprecated.
Exposes server information and will therefore be removed in future releases of Sitevision
|
boolean |
getTrue()
Gets the boolean true primitive.
|
boolean |
isBlank(String aString)
Checks if a string is null, empty or whitespace only.
|
boolean |
isEmpty(String aString)
Checks if a string is null or empty.
|
boolean |
isFalse(Object anObject)
Method that returns true for values that can be interpreted as the false value.
|
boolean |
isNotBlank(String aString)
Checks if a string is not null, not empty and not whitespace only.
|
boolean |
isNotEmpty(String aString)
Checks if a string is not null and not empty.
|
boolean |
isTrue(Object anObject)
Method that returns true for values that can be interpreted as the true value.
|
String |
joinArray(Object[] anArray,
String aSeparator)
Joins the elements of an array to a single String.
|
String |
joinCollection(Collection aCollection,
String aSeparator)
Joins the items of a Collection to a single String.
|
String |
messageFormat(String aMessageFormatPattern,
List anArguments)
A utility method to get a formatted string based on a pattern and some pattern arguments.
|
double |
stringToDouble(String aString)
Method that converts a string to a double.
|
float |
stringToFloat(String aString)
Method that converts a string to a float.
|
int |
stringToInt(String aString)
Method that converts a string to an int.
|
long |
stringToLong(String aString)
Method that converts a string to a long.
|
void |
swallow(Object anObject)
A utility method that can be used to prevent method invocation return values to be added to the Velocity output.
|
String |
trimToEmpty(String aString)
Removes leading and ending control characters (char <= 32) from a String and returns the result (empty String (
"")
if the string is empty or null). |
String |
trimToNull(String aString)
Removes leading and ending control characters (char <= 32) from a String and returns the result (
null
if the string is empty or null). |
boolean getTrue()
trueboolean getFalse()
falseObject getNull()
nullObject getNonNull(Object anObject)
This is a convenience method that can be used to avoid unnecessary subsequent method invocations in Velocity.
Velocity ignores null assignments and trying to assign a null reference will be logged as a WARNING message.
A common strategy to work around this is to use the the Velocity #if clause. It allows for easy non-null
checks - a null object is considered to be false. The downside with this strategy is that it
requires two subsequent method invocations. That can of course be a really bad thing if the method does costly things. This method
can be used to avoid such problems.
An example
This Velocity code:
#if ($anObject.getPersonOrNull())
#set ($person = $anObject.getPersonOrNull())
$person.getName()
...
#end
Can be better utilized as:
#set ($scriptUtil = ...)
#set ($person = $scriptUtil.getNonNull(${anObject.getPersonOrNull()}))
#if ($person)
$person.getName()
...
#end
anObject - the object that might be nullBoolean.FALSE otherwisegetNonNull(Object, Object),
getNonBlank(String)Object getNonBlank(String aString)
This is a convenience method that can be used to avoid unnecessary subsequent method invocations in Velocity.
Velocity ignores null assignments and trying to assign a null reference will be logged as a WARNING message.
A common strategy to work around this is to use the the Velocity #if clause. It allows for easy non-null
checks - a null object is considered to be false. The downside with this strategy is that it
requires two subsequent method invocations. That can of course be a really bad thing if the method does costly things. This method
can be used to avoid such problems.
An example
This Velocity code:
#if ($anObject.getPersonNameThatCanBeNullOrEmptyOrWhitespaceOnly())
#set ($name = $anObject.getPersonNameThatCanBeNullOrEmptyOrWhitespaceOnly())
#if ($name.trim().length() > 0)
<p>
$name
</p>
#end
#end
Can be better utilized as:
#set ($scriptUtil = ...)
#set ($name = $scriptUtil.getNonBlank(${anObject.getPersonNameThatCanBeNullOrEmptyOrWhitespaceOnly()}))
#if ($name)
<p>
$name
</p>
#end
A typical example when using PropertyUtil and whitespace is an issue
This Velocity code:
#if ($propertyUtil.getString($theNode, 'thePropertyName'))
#set ($propertyValue = $propertyUtil.getString($theNode, 'thePropertyName'))
#if ($propertyValue.trim().length() > 0)
<p>
$propertyValue
</p>
#end
#end
Can be better utilized as:
#set ($scriptUtil = ...)
#set ($propertyValue = $scriptUtil.getNonBlank(${propertyUtil.getString($theNode, 'thePropertyName')}))
#if ($propertyValue)
<p>
$propertyValue
</p>
#end
aString - the string that might be null or whitespace onlyaString is non-null and not whitespace only, Boolean.FALSE otherwisegetNonBlank(String, Object)Object getNonNull(Object anObject, Object aDefaultValue)
anObject - the object that might be nullaDefaultValue - the default value to return if anObject is nullaDefaultValue otherwisegetNonNull(Object)Object getNonBlank(String aString, Object aDefaultValue)
aString - the string that might be null or whitespace onlyaDefaultValue - the default value to return if aString is null or whitespace onlyaString is non-null and not whitespace only, aDefaultValue otherwisegetNonBlank(String)boolean isNotEmpty(String aString)
aString - the string to checktrue if aString is not null and not empty, false otherwiseisNotBlank(String)boolean isEmpty(String aString)
aString - the string to checktrue if aString is null or empty, false otherwiseisBlank(String)boolean isNotBlank(String aString)
aString - the string to checktrue if aString is not null, not empty and not whitespace only - false otherwiseisNotEmpty(String)boolean isBlank(String aString)
aString - the string to checktrue if aString is null, empty or whitespace only - false otherwiseisEmpty(String)@Deprecated String getSystemProperty(String aName)
System.getProperty(String).
Important note!
This method can be used to reveal potential sensitive information about the server.
Allowed properties:
A legacy mode that enables the API to return all properties can be enabled using the following system property:
sitevision.api.scriptutil.legacy_system_properties=true
A tip if you need to do backward compatibility workarounds: The Sitevision version is accessible as a system property named sitevision.version.
aName - the name of the system propertynull if indeterminable (e.g. no accessible property named aName exist)@Deprecated Object getInstance(String aQualifiedClassName)
InstanceCreatorUtil.getDefaultInstance(String) insteadaQualifiedClassName - a fully qualified name of the class (i.e. including packages)null if an instance can not be createdList getList(Object[] anArray)
List. Arrays are sub optimal in Velocity and JavaScript.anArray - an array that needs to be wrappednull, null is returned@Deprecated String getFormatPattern(Locale aLocale)
DateUtil.getEditorFormatPattern(java.util.Locale) insteadaLocale - the Locale that specifies the date format patternaLocale.
If aLocale is null then the current locale (as of
PortletContextUtil.getCurrentLocale())
is used to locate the format pattern. If no pattern exist for aLocale then the pattern that corresponds to
Locale.ENGLISH will be returned.@Deprecated String getDateAsString(String aFormatPattern, Date aDate)
DateUtil.getDateAsString(String, java.util.Date) insteadSimpleDateFormat patternaFormatPattern - date to string pattern according to SimpleDateFormat. If null
is specified the default format is usedaDate - the DateString representation of aDate according to aFormatPattern.
Returns null if aDate is null.@Deprecated String getCalendarAsString(String aFormatPattern, Calendar aCalendar)
DateUtil.getCalendarAsString(String, java.util.Calendar) insteadSimpleDateFormat patternaFormatPattern - date to string pattern according to SimpleDateFormat. If null
is specified the default format is usedaCalendar - the CalendarString representation of the date of aCalendar according to aFormatPattern.
Returns null if aDate is null.String getHumanPresentableSize(long aSize)
Note! This method uses two significant digits and 1024 as base. Some examples:
126 -> "130 bytes"
126975 -> "120 kB"
456456456 -> "440 MB"
aSize - a size that should be presented for humansaSize is less than 0 or larger than
1125899906842624 (1024 TB), an empty String will be returned.int stringToInt(String aString)
aString - the string to be convertedaString as int. If aString is null or can't be converted, -1 is returnedlong stringToLong(String aString)
aString - the string to be convertedaString as long. If aString is null or can't be converted, -1 is returneddouble stringToDouble(String aString)
aString - the string to be convertedaString as double. If aString is null or can't be converted, -1.0 is returnedfloat stringToFloat(String aString)
aString - the string to be convertedaString as float. If aString is null or can't be converted, -1.0 is returnedboolean isTrue(Object anObject)
anObject - an Object that might be interpreted as a true valuetrue if anObject is a Boolean with a true value
or a String with a "true" value, false otherwise.boolean isFalse(Object anObject)
anObject - an Object that might be interpreted as a false valuetrue if anObject is a Boolean with a false value
or a String with a "false" value, false otherwise.String joinArray(Object[] anArray, String aSeparator)
Some examples:
joinArray(["one", "two", "three"], ";") -> "one;two;three"
joinArray(["a", myTwoInteger], "-") -> "a-2"
joinArray(["", null, "three"], "-") -> "--three"
joinArray([], "-") -> ""
joinArray(null, "-") -> null
anArray - the array that should be joined to a single string. null elements will be treated as empty
("") and for non-String elements toString()
will be invoked on the element to get a string representation.aSeparator - the separator that should delimit the elements of anArray in the resulting joined string.
null will be treated as empty ("")String. If anArray is null, null will be returnedjoinCollection(java.util.Collection, String)String joinCollection(Collection aCollection, String aSeparator)
For examples, see joinArray(Object[], String)
aCollection - the collection that should be joined to a single string. null items will be treated as empty
("") and for non-String items toString()
will be invoked on the item to get a string representation.aSeparator - the separator that should delimit the items of aCollection in the resulting joined string.
null will be treated as empty ("")String. If aCollection is null, null will be returnedjoinArray(Object[], String)String messageFormat(String aMessageFormatPattern, List anArguments)
A Velocity example:
#set ($instanceCreatorUtil = ...)
#set ($scriptUtil = ...)
#set ($searchPhrase = ...)
## Determine pattern
#if ($language == 'sv')
#set ($pattern = 'Din sökning {0} gav inga träffar')
#else
#set ($pattern = 'No hits for your search {0}')
#end
## Set up pattern argument(s)
#set ($argsList = $instanceCreatorUtil.list)
$scriptUtil.swallow(${argsList.add($searchPhrase)})
## Format and print result
<p>$scriptUtil.messageFormat($pattern, $argsList)</p>
aMessageFormatPattern - a pattern accepted by the Java
MessageFormat classanArguments - arguments needed by the patternnull if evaluation fails. Returns null if
aMessageFormatPattern is null or if anArguments doesn't contain any arguments.void swallow(Object anObject)
An example:
This Velocity code:
#set ($myList = $instanceCreatorUtil.list)
$myList.add("Magnus Lövgren")
$myList.add("Mikael Wikblom")
will result in the following Velocity output (the add method of ArrayList returns a boolean):
true true
If a non-void method is invoked and the return value isn't handled, it will be part of the Velocity output.
The swallow method can be used to prevent that (i.e. use it to "swallow" the result):
#set ($myList = $instanceCreatorUtil.list)
$scriptUtil.swallow(${myList.add("Magnus Lövgren")})
$scriptUtil.swallow(${myList.add("Mikael Wikblom")})
Note! The code above is just an example that illustrates a common problem. When creating and working with a
List from Velocity, you should probably use a ListWrapper instead
anObject - the object that should be "swallowed"String trimToNull(String aString)
null
if the string is empty or null).
The String is trimmed using String.trim()
aString - the string that should be trimmednull if aString is null or the trimmed String is empty.String trimToEmpty(String aString)
"")
if the string is empty or null).
The String is trimmed using String.trim()
aString - the string that should be trimmed"") if aString is null or the trimmed String is empty.String flatten(String aString)
This method replaces all whitespace (space, line break and such) with a space, but will never add multiple spaces.
aString - the multi-lined stringnull if aString is null.Sitevision - Portal and Content Management Made Easy
Sitevision is an advanced Java enterprise portal product and a portlet container (JSR 286) that implements Java Content Repository (JSR 283).
Copyright© 2008-2022 Sitevision AB, all rights reserved.