]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
5723f5b27848bf4b9819cfdab9569ef7feaf5a61
[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, JexlEngine jexl, JexlContext jc) {\r
56         //System.out.println("\r\n---- REPLACE.init-uri:        "+inputJexlExpression);\r
57         String result;\r
58         try {\r
59             for (ServiceResult postResult : serviceResultsMap.values()) {\r
60                 jc.set(postResult.testID, postResult);\r
61                 //System.out.println("eval :: "+postResult.testID+"==>"+postResult);\r
62             }\r
63             result = parse(inputJexlExpression, jexl, jc);\r
64         } catch (Throwable t) {\r
65             System.err.println("ERROR: " + t);\r
66             result = "ERROR";\r
67         }\r
68         //System.out.println("---- REPLACE.uri:        "+result+"\r\n");\r
69         return result;\r
70     }\r
71 \r
72     private static String parse(String in, JexlEngine jexl, JexlContext jc) {\r
73         StringBuffer result = new StringBuffer();\r
74         String s = in;\r
75         String var = "";\r
76         int start, end, len;\r
77         len = in.length();\r
78         start = 0;\r
79         int cursor = 0;\r
80         String front = "";\r
81         while (start < len) {\r
82             end = in.indexOf("}", start);\r
83             start = in.indexOf("${", start);\r
84             if (start < 0) {\r
85                 String tail = in.substring(cursor);\r
86                 result.append(tail);\r
87                 break;\r
88             }\r
89             if (end < 0) {\r
90                 return "ERROR: unbalanced ${} braces";\r
91             }\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
97             start = cursor;\r
98 \r
99             Expression expr = jexl.createExpression(var);\r
100             Object resultObj = expr.evaluate(jc);\r
101             String resultStr;\r
102             if (null == resultObj){\r
103                 resultStr = "ERROR";\r
104                 System.out.println("Jexl context: "+jc.toString());\r
105             } else {\r
106                 resultStr = resultObj.toString();\r
107 \r
108             }\r
109             result.append(resultStr);\r
110         }\r
111         return result.toString();\r
112     }\r
113 \r
114 }\r