use of org.apache.commons.jexl.context.HashMapContext in project com.revolsys.open by revolsys.
the class PhoneNumber method format.
/**
* Parse a phone number using the regular expression and if it matches the
* phone number, format it using the specified format otherwise return null.
*
* @param phoneNumber The normalized phone number to format.
* @param regex The regular expression to match phone numbers.
* @param format The format specification.
* @return The formatted phone number.
*/
public static String format(final String phoneNumber, final String regex, final String format) {
if (phoneNumber != null && regex != null && format != null) {
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(phoneNumber);
if (matcher.matches()) {
final Map values = new HashMap();
for (int i = 1; i <= matcher.groupCount(); i++) {
values.put("n" + i, matcher.group(i));
}
Expression expression;
try {
expression = JexlUtil.newExpression(format);
} catch (final Exception e) {
throw new IllegalArgumentException(regex + " is not a valid regular expression: " + e.getMessage());
}
final HashMapContext context = new HashMapContext();
context.setVars(values);
try {
return (String) expression.evaluate(context);
} catch (final Exception e) {
throw new IllegalArgumentException(format + " is not a valid format: " + e.getMessage());
}
}
}
return null;
}
use of org.apache.commons.jexl.context.HashMapContext in project com.revolsys.open by revolsys.
the class HtmlUiBuilder method getMessage.
public String getMessage(final String messageName, final Map<String, Object> variables) {
final String message = getMessage(messageName);
if (message != null) {
try {
final Expression expression = JexlUtil.newExpression(message);
if (expression != null) {
final JexlContext context = new HashMapContext();
context.setVars(variables);
return (String) expression.evaluate(context);
}
} catch (final Throwable e) {
this.log.error(e.getMessage(), e);
}
}
return message;
}
use of org.apache.commons.jexl.context.HashMapContext in project com.revolsys.open by revolsys.
the class CreateObjectsWithinDistanceOfGeometry method getRecordDefinitionGeometries.
private final Map<RecordDefinition, Geometry> getRecordDefinitionGeometries(final RecordDefinition recordDefinition) {
Map<RecordDefinition, Geometry> recordDefinitionGeometries = this.recordDefinitionGeometryMap.get(recordDefinition);
if (recordDefinitionGeometries == null) {
recordDefinitionGeometries = new LinkedHashMap<>();
RecordDefinition newRecordDefinition;
Geometry preparedGeometry;
for (final Record record : this.geometryObjects) {
Geometry geometry = record.getGeometry();
if (geometry != null) {
final JexlContext context = new HashMapContext();
final Map<String, Object> vars = new HashMap<>(this.attributes);
vars.putAll(record);
vars.put("typePath", recordDefinition.getPath());
context.setVars(vars);
final String typePath = (String) JexlUtil.evaluateExpression(context, this.typePathTemplateExpression);
newRecordDefinition = new RecordDefinitionImpl(PathName.newPathName(typePath), recordDefinition.getFields());
if (this.distance > 0) {
geometry = geometry.buffer(this.distance, 1, LineCap.SQUARE, LineJoin.MITER, 1.0D);
}
geometry = DouglasPeuckerSimplifier.simplify(geometry, 2D);
preparedGeometry = geometry.prepare();
recordDefinitionGeometries.put(newRecordDefinition, preparedGeometry);
}
}
this.recordDefinitionGeometryMap.put(recordDefinition, recordDefinitionGeometries);
}
return recordDefinitionGeometries;
}
use of org.apache.commons.jexl.context.HashMapContext in project com.revolsys.open by revolsys.
the class ScriptExecutorProcess method executeScript.
private void executeScript(final Record record) {
try {
final JexlContext context = new HashMapContext();
final Map<String, Object> vars = new HashMap<>(this.attributes);
vars.putAll(record);
context.setVars(vars);
final Map<String, Object> scriptParams = new HashMap<>();
scriptParams.putAll(this.attributes);
for (final Entry<String, Expression> param : this.expressions.entrySet()) {
final String key = param.getKey();
final Expression expression = param.getValue();
final Object value = JexlUtil.evaluateExpression(context, expression);
scriptParams.put(key, value);
}
final ScriptExecutorRunnable scriptRunner = new ScriptExecutorRunnable(this.script, scriptParams);
if (this.executor == null) {
scriptRunner.run();
} else {
while (this.tasks.size() >= this.maxConcurrentScripts) {
try {
synchronized (this) {
ThreadUtil.pause(1000);
for (final Iterator<Future<?>> taskIter = this.tasks.iterator(); taskIter.hasNext(); ) {
final Future<?> task = taskIter.next();
if (task.isDone()) {
taskIter.remove();
}
}
}
} catch (final ThreadInterruptedException e) {
throw new ClosedException(e);
}
}
final Future<?> future = this.executor.submit(scriptRunner);
this.tasks.add(future);
}
} catch (final ThreadDeath e) {
throw e;
} catch (final Throwable t) {
Logs.error(this, t);
}
}
use of org.apache.commons.jexl.context.HashMapContext in project com.revolsys.open by revolsys.
the class ScriptTool method processArguments.
public boolean processArguments(final String[] args) {
try {
loadProperties("script.properties");
final CommandLineParser parser = new PosixParser();
this.commandLine = parser.parse(this.options, args);
final Option[] options = this.commandLine.getOptions();
for (final Option option : options) {
final String shortOpt = option.getOpt();
if (shortOpt != null && shortOpt.equals("D")) {
final Properties properties = this.commandLine.getOptionProperties("D");
for (final Entry<Object, Object> property : properties.entrySet()) {
final String key = (String) property.getKey();
final String value = (String) property.getValue();
this.parameters.put(key, value);
System.setProperty(key, value);
ThreadSharedProperties.setProperty(key, value);
}
}
}
if (this.commandLine.hasOption(SCRIPT_OPTION)) {
if (!setScriptFileName(this.commandLine.getOptionValue(SCRIPT_OPTION))) {
return false;
}
}
if (this.commandLine.hasOption(PROPERTIES_OPTION)) {
this.propertiesName = this.commandLine.getOptionValue(PROPERTIES_OPTION);
try {
final File propertiesFile = new File(this.propertiesName);
if (propertiesFile.exists()) {
final InputStream in = new FileInputStream(propertiesFile);
loadProperties(this.propertiesName, in);
} else {
if (!loadProperties(this.propertiesName)) {
System.err.println("Properties file '" + this.propertiesName + "' does not exist");
return false;
}
}
} catch (final IOException e) {
System.err.println("Properties file '" + this.propertiesName + "' could not be read:" + e.getMessage());
return false;
}
}
if (this.commandLine.hasOption(LOG_FILE_OPTION)) {
this.logFile = new File(this.commandLine.getOptionValue(LOG_FILE_OPTION));
final File logDirectory = this.logFile.getParentFile();
if (!logDirectory.exists()) {
if (!logDirectory.mkdirs()) {
System.err.println("Unable to create Log directory '" + logDirectory.getAbsolutePath() + "'");
return false;
}
}
} else {
String logFileName = System.getProperty("logFile");
if (logFileName != null) {
try {
while (logFileName.contains("${")) {
final Expression expression = JexlUtil.newExpression(logFileName);
final HashMapContext context = new HashMapContext();
context.setVars(ThreadSharedProperties.getProperties());
logFileName = (String) JexlUtil.evaluateExpression(context, expression);
}
} catch (final Exception e) {
e.printStackTrace();
logFileName = null;
}
}
}
if (this.logFile != null) {
if (this.logFile.exists() && !this.logFile.isFile()) {
System.err.println("Log file '" + this.logFile.getAbsolutePath() + "' is not a file");
return false;
}
System.setProperty("logFile", this.logFile.getAbsolutePath());
}
if (this.commandLine.hasOption(VERSION_OPTION)) {
displayVersion();
return false;
}
if (this.scriptFileName == null) {
final String[] extraArgs = this.commandLine.getArgs();
if (extraArgs.length > 0) {
if (!setScriptFileName(extraArgs[0])) {
return false;
}
}
}
return true;
} catch (final MissingOptionException e) {
if (this.commandLine.hasOption(VERSION_OPTION)) {
displayVersion();
} else {
System.err.println("Missing " + e.getMessage() + " argument");
}
return false;
} catch (final ParseException e) {
System.err.println("Unable to process command line arguments: " + e.getMessage());
return false;
}
}
Aggregations