]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
1beebe5536c72daa89e9ac178d0574669e695379
[tmp/jakarta-migration.git] /
1 /**\r
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
5  *\r
6  * http://www.collectionspace.org\r
7  * http://wiki.collectionspace.org\r
8  *\r
9  * Copyright (c) 2009 Regents of the University of California\r
10  *\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
13  *\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
16  *\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
22  */\r
23 package org.collectionspace.services.IntegrationTests.xmlreplay;\r
24 \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
29 \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
34 \r
35 /**\r
36  * User: laramie\r
37  * $LastChangedRevision:  $\r
38  * $LastChangedDate:  $\r
39  */\r
40 public class XmlReplayEval {\r
41     public Map<String, ServiceResult> serviceResultsMap;\r
42     public JexlEngine jexl;\r
43     public JexlContext jc;\r
44 \r
45     /**\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
58      */\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
61         String result;\r
62         try {\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
68             }\r
69             if (vars!=null){\r
70                 for (Map.Entry<String,String> entry: vars.entrySet()) {\r
71                     String value = entry.getValue();\r
72                     String key = entry.getKey();\r
73                     try {\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
78                     }\r
79                     jc.set(key, value);\r
80                 }\r
81             }\r
82             result = parse(inputJexlExpression, jexl, jc);\r
83         } catch (Throwable t) {\r
84             System.err.println("ERROR: " + t);\r
85             result = "ERROR";\r
86         }\r
87         //System.out.println("---- REPLACE.uri:        "+result+"\r\n");\r
88         return result;\r
89     }\r
90 \r
91     private static String parse(String in, JexlEngine jexl, JexlContext jc) {\r
92         StringBuffer result = new StringBuffer();\r
93         String s = in;\r
94         String var = "";\r
95         int start, end, len;\r
96         len = in.length();\r
97         start = 0;\r
98         int cursor = 0;\r
99         String front = "";\r
100         while (start < len) {\r
101             end = in.indexOf("}", start);\r
102             start = in.indexOf("${", start);\r
103             if (start < 0) {\r
104                 String tail = in.substring(cursor);\r
105                 result.append(tail);\r
106                 break;\r
107             }\r
108             if (end < 0) {\r
109                 return "ERROR: unbalanced ${} braces";\r
110             }\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
116             start = cursor;\r
117 \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
120             String resultStr;\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
124             } else {\r
125                 resultStr = resultObj.toString();\r
126 \r
127             }\r
128             result.append(resultStr);\r
129         }\r
130         return result.toString();\r
131     }\r
132 \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
138         }  else {\r
139             result = jc.toString();\r
140         }\r
141         return result;\r
142     }\r
143 \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
148         }\r
149     }\r
150 \r
151 \r
152 }\r