001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020 021 package org.apache.xbean.blueprint.cm; 022 023 import java.util.Map; 024 025 import org.apache.commons.jexl2.JexlContext; 026 import org.apache.commons.jexl2.JexlEngine; 027 import org.apache.commons.jexl2.MapContext; 028 import org.apache.commons.jexl2.UnifiedJEXL; 029 import org.slf4j.Logger; 030 import org.slf4j.LoggerFactory; 031 032 /** 033 * @version $Rev: 907189 $ $Date: 2010-02-06 03:01:43 -0500 (Sat, 06 Feb 2010) $ 034 */ 035 public class JexlExpressionParser { 036 private static final Logger log = LoggerFactory.getLogger(JexlExpressionParser.class); 037 038 private final JexlEngine engine; 039 private final UnifiedJEXL jexl; 040 protected final JexlContext context; 041 042 public JexlExpressionParser(final Map<String, Object> vars) { 043 if (vars == null) { 044 throw new IllegalArgumentException("vars"); 045 } 046 engine = new JexlEngine(); 047 jexl = new UnifiedJEXL(engine); 048 context = new MapContext(vars); 049 050 log.trace("Using variables: {}", vars); 051 } 052 053 // private FlatResolver resolver = new FlatResolver(true); 054 // 055 // protected Expression createExpression(final String expression) throws Exception { 056 // // assert expression != null; 057 // 058 // Expression expr = engine.createExpression(expression); 059 // expr.addPreResolver(resolver); 060 // 061 // return expr; 062 // } 063 064 public Object evaluate(final String expression) throws Exception { 065 if (expression == null) { 066 throw new IllegalArgumentException("expression"); 067 } 068 069 log.trace("Evaluating expression: {}", expression); 070 return jexl.parse(expression).evaluate(context); 071 // Expression expr = createExpression(expression); 072 // Object obj = expr.evaluate(context); 073 // log.trace("Result: {}", obj); 074 // 075 // return obj; 076 } 077 078 // public String parse(final String input) { 079 // if (input == null) { 080 // throw new IllegalArgumentException("input"); 081 // } 082 // 083 // log.trace("Parsing input: {}", input); 084 // 085 // StringBuilder buff = new StringBuilder(); 086 // 087 // int cur = 0; 088 // int prefixLoc; 089 // int suffixLoc; 090 // 091 // while (cur < input.length()) { 092 // prefixLoc = input.indexOf("${", cur); 093 // 094 // if (prefixLoc < 0) { 095 // break; 096 // } 097 // 098 // suffixLoc = findBlockEnd(prefixLoc + 2, input); 099 // if (suffixLoc < 0) { 100 // throw new RuntimeException("Missing '}': " + input); 101 // } 102 // 103 // String expr = input.substring(prefixLoc + 2, suffixLoc); 104 // buff.append(input.substring(cur, prefixLoc)); 105 // 106 // try { 107 // buff.append(evaluate(expr)); 108 // } 109 // catch (Exception e) { 110 // throw new RuntimeException("Failed to evaluate: " + expr, e); 111 // } 112 // 113 // cur = suffixLoc + 1; 114 // } 115 // 116 // buff.append(input.substring(cur)); 117 // 118 // log.trace("Parsed result: {}", buff); 119 // 120 // return buff.toString(); 121 // } 122 123 // private int findBlockEnd(int pos, String input) { 124 // int nested = 0; 125 // while (pos < input.length()) { 126 // char ch = input.charAt(pos); 127 // if (ch == '{') { 128 // nested++; 129 // } else if (ch == '}') { 130 // if (nested == 0) { 131 // return pos; 132 // } else { 133 // nested--; 134 // } 135 // } 136 // pos++; 137 // } 138 // return -1; 139 // } 140 141 // public String parse(final String input, final boolean trim) { 142 // String output = parse(input); 143 // if (trim && output != null) { 144 // output = output.trim(); 145 // } 146 // 147 // return output; 148 // } 149 }