Search in sources :

Example 1 with HashMapContext

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;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) HashMap(java.util.HashMap) Expression(org.apache.commons.jexl.Expression) HashMapContext(org.apache.commons.jexl.context.HashMapContext) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with HashMapContext

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;
}
Also used : Expression(org.apache.commons.jexl.Expression) HashMapContext(org.apache.commons.jexl.context.HashMapContext) JexlContext(org.apache.commons.jexl.JexlContext)

Example 3 with HashMapContext

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;
}
Also used : Geometry(com.revolsys.geometry.model.Geometry) HashMapContext(org.apache.commons.jexl.context.HashMapContext) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) JexlContext(org.apache.commons.jexl.JexlContext) RecordDefinitionImpl(com.revolsys.record.schema.RecordDefinitionImpl) Record(com.revolsys.record.Record) ArrayRecord(com.revolsys.record.ArrayRecord) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 4 with HashMapContext

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);
    }
}
Also used : ClosedException(com.revolsys.parallel.channel.ClosedException) HashMap(java.util.HashMap) ThreadInterruptedException(com.revolsys.parallel.ThreadInterruptedException) HashMapContext(org.apache.commons.jexl.context.HashMapContext) Expression(org.apache.commons.jexl.Expression) JexlContext(org.apache.commons.jexl.JexlContext) Future(java.util.concurrent.Future) ScriptExecutorRunnable(com.revolsys.parallel.tools.ScriptExecutorRunnable)

Example 5 with HashMapContext

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;
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) PosixParser(org.apache.commons.cli.PosixParser) IOException(java.io.IOException) ThreadSharedProperties(com.revolsys.collection.map.ThreadSharedProperties) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) BeanCreationException(org.springframework.beans.factory.BeanCreationException) PropertyBatchUpdateException(org.springframework.beans.PropertyBatchUpdateException) PropertyAccessException(org.springframework.beans.PropertyAccessException) InvalidPropertyException(org.springframework.beans.InvalidPropertyException) IOException(java.io.IOException) MethodInvocationException(org.springframework.beans.MethodInvocationException) MissingOptionException(org.apache.commons.cli.MissingOptionException) ParseException(org.apache.commons.cli.ParseException) Expression(org.apache.commons.jexl.Expression) HashMapContext(org.apache.commons.jexl.context.HashMapContext) Option(org.apache.commons.cli.Option) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) File(java.io.File) MissingOptionException(org.apache.commons.cli.MissingOptionException)

Aggregations

HashMapContext (org.apache.commons.jexl.context.HashMapContext)5 Expression (org.apache.commons.jexl.Expression)4 HashMap (java.util.HashMap)3 JexlContext (org.apache.commons.jexl.JexlContext)3 ThreadSharedProperties (com.revolsys.collection.map.ThreadSharedProperties)1 Geometry (com.revolsys.geometry.model.Geometry)1 ThreadInterruptedException (com.revolsys.parallel.ThreadInterruptedException)1 ClosedException (com.revolsys.parallel.channel.ClosedException)1 ScriptExecutorRunnable (com.revolsys.parallel.tools.ScriptExecutorRunnable)1 ArrayRecord (com.revolsys.record.ArrayRecord)1 Record (com.revolsys.record.Record)1 RecordDefinition (com.revolsys.record.schema.RecordDefinition)1 RecordDefinitionImpl (com.revolsys.record.schema.RecordDefinitionImpl)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1