Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package net.sf.gluebooster.java.booster.basic.transformation;
import java.util.Properties;
import net.sf.gluebooster.java.booster.essentials.eventsCommands.CallableAbstraction;
import net.sf.gluebooster.java.booster.essentials.utils.Constants;
/**
* Transformation of a string.
*
* @defaultParamText name name of the callable
* @defaultParamText type type of the callable
* @author cbauer
*
* @param
*/
public class CallableFromString extends CallableAbstraction {
/**
* the type to remove a prefix
*/
private static final String TYPE_REMOVE_PREFIX = "remove prefix";
/**
* the type to remove a suffix
*/
private static final String TYPE_REMOVE_SUFFIX = "remove suffix";
/**
* the type to replace placeholders with property values
*/
private static final String TYPE_REPLACE_PROPERTIES = "replace from properties";
/**
* first configurable string, for example the prefix to remove or the key of a property
*/
private String string1;
/**
* second configurable string, for example the value of a property
*/
private String string2;
/**
* Properties with replacements
*/
private Properties properties;
/**
* Used by tests
*/
private CallableFromString() {
}
/**
*
* @param strings
* optional string1 and string2
*/
private CallableFromString(Object name, Object type, String... strings) {
super(name, type);
if (strings.length > 0) {
string1 = strings[0];
}
if (strings.length > 1) {
string2 = strings[1];
}
}
/**
* Creates a callable that replaces a string with another
*
* @param replaceThisString
* this text will be replaced
* @param byThisString
* the replacement
* @return the created callable
*/
public static CallableFromString replace(Object name, String replaceThisString, String byThisString) {
return new CallableFromString("" + name + "(" + replaceThisString + "->" + byThisString + ")", Constants.REPLACE, replaceThisString, byThisString);
}
/**
*
* Creates a callable that replaces strings (the keys of some properties) with other (the corresponding values of the properties).
*
* @param replacements
* the key/value-Pairs that configure the replacement
* @param optionalKeyPrefix
* optional prefix that is added to the keys of the replacements before searching
* @param optionalKeySuffix
* optional suffix that is added to the keys of the replacements before searching
* @return the created callable
*/
public static CallableFromString replaceByProperties(Object name, Properties replacements, String optionalKeyPrefix, String optionalKeySuffix) {
CallableFromString result = new CallableFromString(name, TYPE_REPLACE_PROPERTIES, optionalKeyPrefix, optionalKeySuffix);
result.properties = replacements;
return result;
}
/**
* Creates a callable that trims texts
*
* @return the created callable
*/
public static CallableFromString trim() {
return trim(null);
}
/**
* Creates a callable that trims texts
*
* @return the created callable
*/
public static CallableFromString trim(Object name) {
return new CallableFromString(name == null ? "trim" : name, Constants.TRIM);
}
/**
* Creates a callable that removes a prefix from the call parameters
*
* @param prefix
* the prefix to be removed
* @return the created callable
*/
public static CallableFromString removePrefix(String prefix) {
return removePrefix(null, prefix);
}
/**
* Creates a callable that removes a prefix from the call parameters
*
* @param prefix
* the prefix to be removed
* @return the created callable
*/
public static CallableFromString removePrefix(Object name, String prefix) {
return new CallableFromString(name == null ? "" : name + "( remove prefix " + prefix + ")", TYPE_REMOVE_PREFIX, prefix);
}
/**
* Creates a callable that removes a suffix from the call parameters
*
* @param suffix
* the suffix to be removed
* @return the created callable
*/
public static CallableFromString removeSuffix(String suffix) {
return removeSuffix(null, suffix);
}
/**
* Creates a callable that removes a suffix from the call parameters
*
* @param suffix
* the suffix to be removed
* @return the created callable
*/
public static CallableFromString removeSuffix(Object name, String suffix) {
return new CallableFromString(name == null ? "" : name + "( remove suffix " + suffix + ")", TYPE_REMOVE_SUFFIX, suffix);
}
/**
* Creates a callable that splits according to a regex
*
* @param regex
* used for splitting
* @return the created callable
*/
public static CallableFromString split(String regex) {
return split(null, regex);
}
/**
* Creates a callable that splits according to a regex
*
* @param regex
* used for splitting
* @return the created callable
*/
public static CallableFromString split(Object name, String regex) {
return new CallableFromString(name == null ? "" : name + "( split " + regex + ")", Constants.SPLIT, regex);
}
@Override
protected Result callImpl(String... parameters) throws Exception {
Object type = getType();
String input = (String) parameters[0];
Object result;
if (TYPE_REMOVE_PREFIX.equals(type)) {
if (input.startsWith(string1)) {
result = input.substring(string1.length());
} else {
result = input;
}
} else if (TYPE_REMOVE_SUFFIX.equals(type)) {
if (input.endsWith(string1)) {
result = input.substring(0, input.length() - string1.length());
} else {
result = input;
}
} else if (Constants.REPLACE.is(type)) {
result = input.replace(string1, string2);
} else if (TYPE_REPLACE_PROPERTIES.equals(type)) {
for (Object key : properties.keySet()) {
String replace = (String) key;
if (string1 != null)
replace = string1 + replace;
if (string2 != null)
replace = replace + string2;
input = input.replace(replace, properties.getProperty((String) key));
}
result = input;
} else if (Constants.SPLIT.is(type)) {
result = input.split(string1);
} else if (Constants.TRIM.is(type)) {
result = input.trim();
} else {
throw new IllegalStateException("type not supported: " + type);
}
return (Result) result;
}
}