2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright (c) 2009 Regents of the University of California
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 package org.collectionspace.services.IntegrationTests.xmlreplay;
25 import org.apache.commons.jexl2.Expression;
26 import org.apache.commons.jexl2.JexlContext;
27 import org.apache.commons.jexl2.JexlEngine;
28 import org.apache.commons.jexl2.MapContext;
30 import java.util.HashMap;
31 import java.util.List;
37 * $LastChangedRevision: $
40 public class XmlReplayEval {
41 public Map<String, ServiceResult> serviceResultsMap;
42 public JexlEngine jexl;
43 public JexlContext jc;
46 * You may pass in a Jexl 2 expression, e.g. ${foo.bar} and it will be eval'd for you.
47 * We are looking at some URI like so: ${newOrgAuthority.CSID}
48 * The idea here is that the XML control file may bind to this namespace, and
49 * this module may find those values and any future extensions, specifically
50 * when someone says "I want to bind to ${CSID} and ${SUBRESOURCE.CSID}
51 * The code here is easy to extend, but the test cases build up, so you don't
52 * want to break all the config files by not being backward compatible. Binding
53 * to context variables like this makes it easy.
54 * EXAMPLE USAGE: <br />
55 * String uri = "/cspace-services/orgauthorities/${OrgAuth1.CSID}/items/${Org1.CSID}"; <br />
56 * uri = eval(uri, serviceResultsMap, jexl, jc); <br />
57 * RESULT: "/cspace-services/orgauthorities/43a2739c-4f40-49c8-a6d5/items/"
59 public static String eval(String inputJexlExpression, Map<String, ServiceResult> serviceResultsMap, Map<String,String> vars, JexlEngine jexl, JexlContext jc) {
60 //System.out.println("\r\n---- REPLACE.init-uri: "+inputJexlExpression);
63 jc.set("itemCSID", "${itemCSID}"); //noiseless passthru.
64 //System.out.println("eval :: serviceResultsMap "+serviceResultsMap.size());
65 for (ServiceResult serviceResult : serviceResultsMap.values()) {
66 jc.set(serviceResult.testID, serviceResult);
67 //System.out.println("eval :: "+serviceResult.testID+"==>"+serviceResult.minimal());
70 for (Map.Entry<String,String> entry: vars.entrySet()) {
71 String value = entry.getValue();
72 String key = entry.getKey();
74 value = parse(value, jexl, jc);
75 vars.put(key, value); //replace template value with actual value.
76 } catch (Exception e){
82 result = parse(inputJexlExpression, jexl, jc);
83 } catch (Throwable t) {
84 System.err.println("ERROR: " + t);
87 //System.out.println("---- REPLACE.uri: "+result+"\r\n");
91 private static String parse(String in, JexlEngine jexl, JexlContext jc) {
92 StringBuffer result = new StringBuffer();
100 while (start < len) {
101 end = in.indexOf("}", start);
102 start = in.indexOf("${", start);
104 String tail = in.substring(cursor);
109 return "ERROR: unbalanced ${} braces";
111 front = in.substring(cursor, start);
112 result.append(front);
113 cursor = end + 1; //bump past close brace
114 var = in.substring(start + 2, end); //+2 bump past open brace ${ and then "end" is indexed just before the close brace }
115 //s = s.substring(end+1); //bump past close brace
118 Expression expr = jexl.createExpression(var);
119 Object resultObj = expr.evaluate(jc); //REM - 5/9/2011 - Usually calls back to fields and methods in ServiceResult class to do the evaluation -e.g., the "got" method.
121 if (null == resultObj){
122 //debug: System.out.println("null found while evaluationg variable: '"+var+"' Jexl context: "+dumpContext(jc));
123 resultStr = "${"+var+"}";
125 resultStr = resultObj.toString();
128 result.append(resultStr);
130 return result.toString();
133 protected static String dumpContext(JexlContext jc){
135 if (jc instanceof MapContextWKeys){
136 Set keys = ((MapContextWKeys)jc).getKeys();
137 result = keys.toString();
139 result = jc.toString();
144 public static class MapContextWKeys extends MapContext implements JexlContext {
145 private Map<String,Object> map = new HashMap();
146 public Set getKeys(){
147 return this.map.keySet();