Shared utility methods used by various implementation classes.
| Method from org.apache.tapestry5.internal.TapestryInternalUtils Detail: |
public static String defaultLabel(String id,
Messages messages,
String propertyExpression) {
String key = id + "-label";
if (messages.contains(key)) return messages.get(key);
return toUserPresentable(extractIdFromPropertyExpression(lastTerm(propertyExpression)));
}
Looks for a label within the messages based on the id. If found, it is used, otherwise the name is converted to a
user presentable form. |
public static String extractIdFromPropertyExpression(String expression) {
return replace(expression, NON_WORD_PATTERN, "");
}
Used to convert a property expression into a key that can be used to locate various resources (Blocks, messages,
etc.). Strips out any punctuation characters, leaving just words characters (letters, number and the
underscore). |
public static String getLabelForEnum(Messages messages,
Enum value) {
String prefix = lastTerm(value.getClass().getName());
return getLabelForEnum(messages, prefix, value);
}
|
public static String getLabelForEnum(Messages messages,
String prefix,
Enum value) {
String name = value.name();
String key = prefix + "." + name;
if (messages.contains(key)) return messages.get(key);
if (messages.contains(name)) return messages.get(name);
return toUserPresentable(name.toLowerCase());
}
Converts an enum to a label string, allowing for overrides from a message catalog.
- As key prefix.name if present. Ex: "ElementType.LOCAL_VARIABLE"
- As key
name if present, i.e., "LOCAL_VARIABLE".
- As a user-presentable version of the name, i.e., "Local
Variable".
|
public static boolean isEqual(T left,
T right) {
if (left == right) return true;
if (left == null) return right == null;
return left.equals(right);
}
Determines if the two values are equal. They are equal if they are the exact same value (including if they are
both null). Otherwise standard equals() comparison is used. |
public static String lastTerm(String input) {
int dotx = input.lastIndexOf('.');
return input.substring(dotx + 1);
}
Strips a dotted sequence (such as a property expression, or a qualified class name) down to the last term of that
expression, by locating the last period ('.') in the string. |
public static Map<String, String> mapFromKeysAndValues(String keysAndValues) {
Map< String, String > result = CollectionFactory.newMap();
int i = 0;
while (i < keysAndValues.length)
{
String key = keysAndValues[i++];
String value = keysAndValues[i++];
result.put(key, value);
}
return result;
}
|
public static KeyValue parseKeyValue(String input) {
int pos = input.indexOf('=');
if (pos < 1) throw new IllegalArgumentException(InternalMessages.badKeyValue(input));
String key = input.substring(0, pos);
String value = input.substring(pos + 1);
return new KeyValue(key.trim(), value.trim());
}
Parses a key/value pair where the key and the value are seperated by an equals sign. The key and value are
trimmed of leading and trailing whitespace, and returned as a KeyValue . |
public static String[] splitPath(String path) {
return SLASH_PATTERN.split(path);
}
Splits a path at each slash. |
public static String toClassAttributeValue(List<String> classes) {
if (classes.isEmpty()) return null;
return InternalUtils.join(classes, " ");
}
Converts an list of strings into a space-separated string combining them all, suitable for use as an HTML class
attribute value. |
public static OptionModel toOptionModel(String input) {
Defense.notNull(input, "input");
int equalsx = input.indexOf('=');
if (equalsx < 0) return new OptionModelImpl(input);
String value = input.substring(0, equalsx);
String label = input.substring(equalsx + 1);
return new OptionModelImpl(label, value);
}
Converts a string to an OptionModel . The string is of the form "value=label". If the equals sign is
omitted, then the same value is used for both value and label. |
public static OptionModel toOptionModel(Entry input) {
Defense.notNull(input, "input");
String label = input.getValue() != null ? String.valueOf(input.getValue()) : "";
return new OptionModelImpl(label, input.getKey());
}
|
public static OptionModel toOptionModel(Object input) {
String label = (input != null ? String.valueOf(input) : "");
return new OptionModelImpl(label, input);
}
|
public static List<OptionModel> toOptionModels(String input) {
Defense.notNull(input, "input");
List< OptionModel > result = CollectionFactory.newList();
for (String term : input.split(","))
result.add(toOptionModel(term.trim()));
return result;
}
Parses a string input into a series of value=label pairs compatible with #toOptionModel(String) . Splits
on commas. Ignores whitespace around commas. |
public static List<OptionModel> toOptionModels(Map<K, V> input) {
Defense.notNull(input, "input");
List< OptionModel > result = CollectionFactory.newList();
for (Map.Entry entry : input.entrySet())
result.add(toOptionModel(entry));
return result;
}
|
public static List<OptionModel> toOptionModels(List<E> input) {
Defense.notNull(input, "input");
List< OptionModel > result = CollectionFactory.newList();
for (E element : input)
result.add(toOptionModel(element));
return result;
}
|
public static SelectModel toSelectModel(String input) {
List< OptionModel > options = toOptionModels(input);
return new SelectModelImpl(null, options);
}
|
public static SelectModel toSelectModel(Map<K, V> input) {
List< OptionModel > options = toOptionModels(input);
return new SelectModelImpl(null, options);
}
|
public static SelectModel toSelectModel(List<E> input) {
List< OptionModel > options = toOptionModels(input);
return new SelectModelImpl(null, options);
}
|
public static String toUserPresentable(String id) {
StringBuilder builder = new StringBuilder(id.length() * 2);
char[] chars = id.toCharArray();
boolean postSpace = true;
boolean upcaseNext = true;
for (char ch : chars)
{
if (upcaseNext)
{
builder.append(Character.toUpperCase(ch));
upcaseNext = false;
continue;
}
if (ch == '_')
{
builder.append(' ');
upcaseNext = true;
continue;
}
boolean upperCase = Character.isUpperCase(ch);
if (upperCase && !postSpace) builder.append(' ');
builder.append(ch);
postSpace = upperCase;
}
return builder.toString();
}
Capitalizes the string, and inserts a space before each upper case character (or sequence of upper case
characters). Thus "userId" becomes "User Id", etc. Also, converts underscore into space (and capitalizes the
following word), thus "user_id" also becomes "User Id". |