]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
a9ece6858e781c5d965af1599c47f31dc1c1cba6
[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 \r
29 import java.util.Map;\r
30 \r
31 /**\r
32  * User: laramie\r
33  * $LastChangedRevision:  $\r
34  * $LastChangedDate:  $\r
35  */\r
36 public class XmlReplayEval {\r
37     public Map<String, ServiceResult> serviceResultsMap;\r
38     public JexlEngine jexl;\r
39     public JexlContext jc;\r
40 \r
41     /**\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
54      */\r
55      public static String eval(String inputJexlExpression, Map<String, ServiceResult> serviceResultsMap, Map<String,String> vars, JexlEngine jexl, JexlContext jc) {\r
56         //System.out.println("\r\n---- REPLACE.init-uri:        "+inputJexlExpression);\r
57         String result;\r
58         try {\r
59             //System.out.println("eval :: serviceResultsMap "+serviceResultsMap.size());\r
60             for (ServiceResult serviceResult : serviceResultsMap.values()) {\r
61                 jc.set(serviceResult.testID, serviceResult);\r
62                 //System.out.println("eval :: "+serviceResult.testID+"==>"+serviceResult.minimal());\r
63             }\r
64             if (vars!=null){\r
65                 for (Map.Entry<String,String> entry: vars.entrySet()) {\r
66                     String value = entry.getValue();\r
67                     String key = entry.getKey();\r
68                     try {\r
69                         value = parse(value, jexl, jc);\r
70                     } catch (Exception e){\r
71                         value = "ERROR: "+e;\r
72                     }\r
73                     jc.set(key, value);\r
74                 }\r
75             }\r
76             result = parse(inputJexlExpression, jexl, jc);\r
77         } catch (Throwable t) {\r
78             System.err.println("ERROR: " + t);\r
79             result = "ERROR";\r
80         }\r
81         //System.out.println("---- REPLACE.uri:        "+result+"\r\n");\r
82         return result;\r
83     }\r
84 \r
85     private static String parse(String in, JexlEngine jexl, JexlContext jc) {\r
86         StringBuffer result = new StringBuffer();\r
87         String s = in;\r
88         String var = "";\r
89         int start, end, len;\r
90         len = in.length();\r
91         start = 0;\r
92         int cursor = 0;\r
93         String front = "";\r
94         while (start < len) {\r
95             end = in.indexOf("}", start);\r
96             start = in.indexOf("${", start);\r
97             if (start < 0) {\r
98                 String tail = in.substring(cursor);\r
99                 result.append(tail);\r
100                 break;\r
101             }\r
102             if (end < 0) {\r
103                 return "ERROR: unbalanced ${} braces";\r
104             }\r
105             front = in.substring(cursor, start);\r
106             result.append(front);\r
107             cursor = end + 1;                   //bump past close brace\r
108             var = in.substring(start + 2, end);  //+2 bump past open brace ${ and then "end" is indexed just before the close brace }\r
109             //s   = s.substring(end+1);         //bump past close brace\r
110             start = cursor;\r
111 \r
112             Expression expr = jexl.createExpression(var);\r
113             Object resultObj = expr.evaluate(jc);\r
114             String resultStr;\r
115             if (null == resultObj){\r
116                 resultStr = "ERROR";\r
117                 System.out.println("Jexl context: "+jc.toString());\r
118             } else {\r
119                 resultStr = resultObj.toString();\r
120 \r
121             }\r
122             result.append(resultStr);\r
123         }\r
124         return result.toString();\r
125     }\r
126 \r
127 }\r