]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
2662413b2fb089d6ac00c5053138f90e02aa4009
[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             //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
67             }\r
68             if (vars!=null){\r
69                 for (Map.Entry<String,String> entry: vars.entrySet()) {\r
70                     String value = entry.getValue();\r
71                     String key = entry.getKey();\r
72                     try {\r
73                         value = parse(value, jexl, jc);\r
74                     } catch (Exception e){\r
75                         value = "ERROR: "+e;\r
76                     }\r
77                     jc.set(key, value);\r
78                 }\r
79             }\r
80             result = parse(inputJexlExpression, jexl, jc);\r
81         } catch (Throwable t) {\r
82             System.err.println("ERROR: " + t);\r
83             result = "ERROR";\r
84         }\r
85         //System.out.println("---- REPLACE.uri:        "+result+"\r\n");\r
86         return result;\r
87     }\r
88 \r
89     private static String parse(String in, JexlEngine jexl, JexlContext jc) {\r
90         StringBuffer result = new StringBuffer();\r
91         String s = in;\r
92         String var = "";\r
93         int start, end, len;\r
94         len = in.length();\r
95         start = 0;\r
96         int cursor = 0;\r
97         String front = "";\r
98         while (start < len) {\r
99             end = in.indexOf("}", start);\r
100             start = in.indexOf("${", start);\r
101             if (start < 0) {\r
102                 String tail = in.substring(cursor);\r
103                 result.append(tail);\r
104                 break;\r
105             }\r
106             if (end < 0) {\r
107                 return "ERROR: unbalanced ${} braces";\r
108             }\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
114             start = cursor;\r
115 \r
116             Expression expr = jexl.createExpression(var);\r
117             Object resultObj = expr.evaluate(jc);\r
118             String resultStr;\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
122             } else {\r
123                 resultStr = resultObj.toString();\r
124 \r
125             }\r
126             result.append(resultStr);\r
127         }\r
128         return result.toString();\r
129     }\r
130 \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
136         }  else {\r
137             result = jc.toString();\r
138         }\r
139         return result;\r
140     }\r
141 \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
146         }\r
147     }\r
148 \r
149 \r
150 }\r