]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
fde549e783f1a51fd1dcc7b5316c61aea1e4a219
[tmp/jakarta-migration.git] /
1 /**
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:
5  *
6  * http://www.collectionspace.org
7  * http://wiki.collectionspace.org
8  *
9  * Copyright (c) 2009 Regents of the University of California
10  *
11  * Licensed under the Educational Community License (ECL), Version 2.0.
12  * You may not use this file except in compliance with this License.
13  *
14  * You may obtain a copy of the ECL 2.0 License at
15  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  *
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.
22  */
23 package org.collectionspace.services.IntegrationTests.xmlreplay;
24
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;
29
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Set;
34
35 /**
36  * User: laramie
37  * $LastChangedRevision:  $
38  * $LastChangedDate:  $
39  */
40 public class XmlReplayEval {
41     public Map<String, ServiceResult> serviceResultsMap;
42     public JexlEngine jexl;
43     public JexlContext jc;
44
45     /**
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/"
58      */
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);
61         String result;
62         try {
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());
68             }
69             if (vars!=null){
70                 for (Map.Entry<String,String> entry: vars.entrySet()) {
71                     String value = entry.getValue();
72                     String key = entry.getKey();
73                     try {
74                         value = parse(value, jexl, jc);
75                         vars.put(key, value); //replace template value with actual value.
76                     } catch (Exception e){
77                         value = "ERROR: "+e;
78                     }
79                     jc.set(key, value);
80                 }
81             }
82             result = parse(inputJexlExpression, jexl, jc);
83         } catch (Throwable t) {
84             System.err.println("ERROR: " + t);
85             result = "ERROR";
86         }
87         //System.out.println("---- REPLACE.uri:        "+result+"\r\n");
88         return result;
89     }
90
91     private static String parse(String in, JexlEngine jexl, JexlContext jc) {
92         StringBuffer result = new StringBuffer();
93         String s = in;
94         String var = "";
95         int start, end, len;
96         len = in.length();
97         start = 0;
98         int cursor = 0;
99         String front = "";
100         while (start < len) {
101             end = in.indexOf("}", start);
102             start = in.indexOf("${", start);
103             if (start < 0) {
104                 String tail = in.substring(cursor);
105                 result.append(tail);
106                 break;
107             }
108             if (end < 0) {
109                 return "ERROR: unbalanced ${} braces";
110             }
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
116             start = cursor;
117
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.
120             String resultStr;
121             if (null == resultObj){
122                 //debug: System.out.println("null found while evaluationg variable: '"+var+"' Jexl context: "+dumpContext(jc));
123                 resultStr = "${"+var+"}";
124             } else {
125                 resultStr = resultObj.toString();
126
127             }
128             result.append(resultStr);
129         }
130         return result.toString();
131     }
132
133     protected static String dumpContext(JexlContext jc){
134         String result = "";
135         if (jc instanceof MapContextWKeys){
136             Set keys = ((MapContextWKeys)jc).getKeys();
137             result = keys.toString();
138         }  else {
139             result = jc.toString();
140         }
141         return result;
142     }
143
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();
148         }
149     }
150
151
152 }