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 //System.out.println("eval :: serviceResultsMap "+serviceResultsMap.size());
\r
64 for (ServiceResult serviceResult : serviceResultsMap.values()) {
\r
65 jc.set(serviceResult.testID, serviceResult);
\r
66 //System.out.println("eval :: "+serviceResult.testID+"==>"+serviceResult.minimal());
\r
69 for (Map.Entry<String,String> entry: vars.entrySet()) {
\r
70 String value = entry.getValue();
\r
71 String key = entry.getKey();
\r
73 value = parse(value, jexl, jc);
\r
74 } catch (Exception e){
\r
75 value = "ERROR: "+e;
\r
80 result = parse(inputJexlExpression, jexl, jc);
\r
81 } catch (Throwable t) {
\r
82 System.err.println("ERROR: " + t);
\r
85 //System.out.println("---- REPLACE.uri: "+result+"\r\n");
\r
89 private static String parse(String in, JexlEngine jexl, JexlContext jc) {
\r
90 StringBuffer result = new StringBuffer();
\r
93 int start, end, len;
\r
98 while (start < len) {
\r
99 end = in.indexOf("}", start);
\r
100 start = in.indexOf("${", start);
\r
102 String tail = in.substring(cursor);
\r
103 result.append(tail);
\r
107 return "ERROR: unbalanced ${} braces";
\r
109 front = in.substring(cursor, start);
\r
110 result.append(front);
\r
111 cursor = end + 1; //bump past close brace
\r
112 var = in.substring(start + 2, end); //+2 bump past open brace ${ and then "end" is indexed just before the close brace }
\r
113 //s = s.substring(end+1); //bump past close brace
\r
116 Expression expr = jexl.createExpression(var);
\r
117 Object resultObj = expr.evaluate(jc);
\r
119 if (null == resultObj){
\r
120 //debug: System.out.println("null found while evaluationg variable: '"+var+"' Jexl context: "+dumpContext(jc));
\r
121 resultStr = "${"+var+"}";
\r
123 resultStr = resultObj.toString();
\r
126 result.append(resultStr);
\r
128 return result.toString();
\r
131 protected static String dumpContext(JexlContext jc){
\r
132 String result = "";
\r
133 if (jc instanceof MapContextWKeys){
\r
134 Set keys = ((MapContextWKeys)jc).getKeys();
\r
135 result = keys.toString();
\r
137 result = jc.toString();
\r
142 public static class MapContextWKeys extends MapContext implements JexlContext {
\r
143 private Map<String,Object> map = new HashMap();
\r
144 public Set getKeys(){
\r
145 return this.map.keySet();
\r