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
29 import java.util.Map;
\r
33 * $LastChangedRevision: $
\r
34 * $LastChangedDate: $
\r
36 public class XmlReplayEval {
\r
37 public Map<String, ServiceResult> serviceResultsMap;
\r
38 public JexlEngine jexl;
\r
39 public JexlContext jc;
\r
42 * You may pass in a Jexl 2 expression, e.g. ${foo.bar} and it will be eval'd for you.
\r
43 * We are looking at some URI like so: ${newOrgAuthority.CSID}
\r
44 * The idea here is that the XML control file may bind to this namespace, and
\r
45 * this module may find those values and any future extensions, specifically
\r
46 * when someone says "I want to bind to ${CSID} and ${SUBRESOURCE.CSID}
\r
47 * The code here is easy to extend, but the test cases build up, so you don't
\r
48 * want to break all the config files by not being backward compatible. Binding
\r
49 * to context variables like this makes it easy.
\r
50 * EXAMPLE USAGE: <br />
\r
51 * String uri = "/cspace-services/orgauthorities/${OrgAuth1.CSID}/items/${Org1.CSID}"; <br />
\r
52 * uri = eval(uri, serviceResultsMap, jexl, jc); <br />
\r
53 * RESULT: "/cspace-services/orgauthorities/43a2739c-4f40-49c8-a6d5/items/"
\r
55 public static String eval(String inputJexlExpression, Map<String, ServiceResult> serviceResultsMap, JexlEngine jexl, JexlContext jc) {
\r
56 //System.out.println("\r\n---- REPLACE.init-uri: "+inputJexlExpression);
\r
59 for (ServiceResult postResult : serviceResultsMap.values()) {
\r
60 jc.set(postResult.testID, postResult);
\r
61 //System.out.println("eval :: "+postResult.testID+"==>"+postResult);
\r
63 result = parse(inputJexlExpression, jexl, jc);
\r
64 } catch (Throwable t) {
\r
65 System.err.println("ERROR: " + t);
\r
68 //System.out.println("---- REPLACE.uri: "+result+"\r\n");
\r
72 private static String parse(String in, JexlEngine jexl, JexlContext jc) {
\r
73 StringBuffer result = new StringBuffer();
\r
76 int start, end, len;
\r
81 while (start < len) {
\r
82 end = in.indexOf("}", start);
\r
83 start = in.indexOf("${", start);
\r
85 String tail = in.substring(cursor);
\r
86 result.append(tail);
\r
90 return "ERROR: unbalanced ${} braces";
\r
92 front = in.substring(cursor, start);
\r
93 result.append(front);
\r
94 cursor = end + 1; //bump past close brace
\r
95 var = in.substring(start + 2, end); //+2 bump past open brace ${ and then "end" is indexed just before the close brace }
\r
96 //s = s.substring(end+1); //bump past close brace
\r
99 Expression expr = jexl.createExpression(var);
\r
100 Object resultObj = expr.evaluate(jc);
\r
102 if (null == resultObj){
\r
103 resultStr = "ERROR";
\r
104 System.out.println("Jexl context: "+jc.toString());
\r
106 resultStr = resultObj.toString();
\r
109 result.append(resultStr);
\r
111 return result.toString();
\r