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