Search in sources :

Example 1 with Expression

use of org.apache.commons.jxpath.ri.compiler.Expression in project collect by openforis.

the class ModelExtensionFunction method computeValue.

@Override
public Object computeValue(EvalContext context) {
    Object[] parameters = null;
    if (args != null) {
        parameters = new Object[args.length];
        for (int i = 0; i < args.length; i++) {
            Expression expression = args[i];
            Iterator<?> computedValues = expression.iterate(context);
            parameters[i] = convert(computedValues);
        }
    }
    Object result = invoke(context, parameters);
    return result instanceof NodeSet ? new NodeSetContext(context, (NodeSet) result) : result;
}
Also used : NodeSet(org.apache.commons.jxpath.NodeSet) Expression(org.apache.commons.jxpath.ri.compiler.Expression) NodeSetContext(org.apache.commons.jxpath.ri.axes.NodeSetContext)

Example 2 with Expression

use of org.apache.commons.jxpath.ri.compiler.Expression in project collect by openforis.

the class ModelJXPathCompiledExpression method getFunctionNames.

public Set<String> getFunctionNames() {
    Set<String> names = new HashSet<String>();
    Deque<Expression> stack = new LinkedList<Expression>();
    stack.push(getExpression());
    while (!stack.isEmpty()) {
        Expression expression = stack.pop();
        if (expression instanceof Operation) {
            if (expression instanceof CoreFunction || expression instanceof ModelExtensionFunction) {
                String name = expression.toString().replaceAll("\\(.*\\)", "");
                names.add(name);
            }
            Expression[] arguments = ((Operation) expression).getArguments();
            if (arguments != null && arguments.length > 0) {
                for (Expression arg : arguments) {
                    stack.push(arg);
                }
            }
        }
    }
    return names;
}
Also used : CoreFunction(org.apache.commons.jxpath.ri.compiler.CoreFunction) JXPathCompiledExpression(org.apache.commons.jxpath.ri.JXPathCompiledExpression) Expression(org.apache.commons.jxpath.ri.compiler.Expression) Operation(org.apache.commons.jxpath.ri.compiler.Operation) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet)

Example 3 with Expression

use of org.apache.commons.jxpath.ri.compiler.Expression in project collect by openforis.

the class ModelJXPathContext method compilePath.

private ModelJXPathCompiledExpression compilePath(String xpath, boolean normalizeNumbers) {
    Expression expr = compileExpression(xpath, normalizeNumbers);
    ModelJXPathCompiledExpression compiledExpression = new ModelJXPathCompiledExpression(expressionFactory, xpath, expr);
    return compiledExpression;
}
Also used : Expression(org.apache.commons.jxpath.ri.compiler.Expression)

Example 4 with Expression

use of org.apache.commons.jxpath.ri.compiler.Expression in project collect by openforis.

the class ModelJXPathContext method compileExpression.

@SuppressWarnings("unchecked")
private Expression compileExpression(String xpath, boolean normalizeNumbers) {
    Expression expr;
    synchronized (compiled) {
        if (USE_SOFT_CACHE) {
            expr = null;
            SoftReference ref = (SoftReference) compiled.get(xpath);
            if (ref != null) {
                expr = (Expression) ref.get();
            }
        } else {
            expr = (Expression) compiled.get(xpath);
        }
    }
    if (expr != null) {
        return expr;
    }
    expr = (Expression) Parser.parseExpression(xpath, getCompiler(normalizeNumbers));
    synchronized (compiled) {
        if (USE_SOFT_CACHE) {
            if (cleanupCount++ >= CLEANUP_THRESHOLD) {
                Iterator it = compiled.entrySet().iterator();
                while (it.hasNext()) {
                    Entry me = (Entry) it.next();
                    if (((SoftReference) me.getValue()).get() == null) {
                        it.remove();
                    }
                }
                cleanupCount = 0;
            }
            compiled.put(xpath, new SoftReference(expr));
        } else {
            compiled.put(xpath, expr);
        }
    }
    return expr;
}
Also used : Entry(java.util.Map.Entry) SoftReference(java.lang.ref.SoftReference) Expression(org.apache.commons.jxpath.ri.compiler.Expression) Iterator(java.util.Iterator)

Example 5 with Expression

use of org.apache.commons.jxpath.ri.compiler.Expression in project collect by openforis.

the class ReferencedPathEvaluator method determineReferencedPaths.

private Set<String> determineReferencedPaths(org.apache.commons.jxpath.ri.compiler.Path modelLocationPath) {
    Set<String> paths = new HashSet<String>();
    if (modelLocationPath.getSteps().length > 0) {
        paths.add(Path.getAbsolutePath(modelLocationPath.toString()));
        StringBuilder predicateBasePathSB = new StringBuilder();
        for (Step step : modelLocationPath.getSteps()) {
            String stepVal = step.toString();
            predicateBasePathSB.append(Path.getAbsolutePath(stepVal)).append(Path.SEPARATOR);
            for (Expression predicate : step.getPredicates()) {
                Set<String> predicatePaths = determineReferencedPaths(predicate);
                for (String predicateReferencePath : predicatePaths) {
                    if (predicateReferencePath.startsWith(Path.THIS_VARIABLE) || predicateReferencePath.startsWith(Path.CONTEXT_VARIABLE)) {
                        paths.add(predicateReferencePath);
                    } else {
                        paths.add(predicateBasePathSB.toString() + predicateReferencePath);
                    }
                }
            }
        }
    }
    return paths;
}
Also used : Expression(org.apache.commons.jxpath.ri.compiler.Expression) Step(org.apache.commons.jxpath.ri.compiler.Step) HashSet(java.util.HashSet)

Aggregations

Expression (org.apache.commons.jxpath.ri.compiler.Expression)7 HashSet (java.util.HashSet)3 LinkedList (java.util.LinkedList)2 JXPathCompiledExpression (org.apache.commons.jxpath.ri.JXPathCompiledExpression)2 Operation (org.apache.commons.jxpath.ri.compiler.Operation)2 SoftReference (java.lang.ref.SoftReference)1 Iterator (java.util.Iterator)1 Entry (java.util.Map.Entry)1 NodeSet (org.apache.commons.jxpath.NodeSet)1 NodeSetContext (org.apache.commons.jxpath.ri.axes.NodeSetContext)1 CoreFunction (org.apache.commons.jxpath.ri.compiler.CoreFunction)1 Step (org.apache.commons.jxpath.ri.compiler.Step)1 ExpressionValidationResult (org.openforis.idm.metamodel.expression.ExpressionValidator.ExpressionValidationResult)1