]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
bfd7ebf63297a26326f42701b8516f84a6074f22
[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 \r
24 package org.collectionspace.services.IntegrationTests.xmlreplay;\r
25 \r
26 import org.apache.commons.jexl2.Expression;\r
27 import org.apache.commons.jexl2.JexlContext;\r
28 import org.apache.commons.jexl2.JexlEngine;\r
29 import org.apache.commons.jexl2.MapContext;\r
30 \r
31 import java.io.File;\r
32 import  java.util.regex.Pattern;\r
33 import java.util.regex.Matcher;\r
34 \r
35 /** General utility methods.\r
36  *   @author Laramie Crocker\r
37  */\r
38 public class Tools {\r
39     /** @return first glued to second with the separator string, at most one time - useful for appending paths.\r
40      */\r
41     public static String glue(String first, String separator, String second){\r
42         if (first==null) { first = ""; }\r
43         if (second==null) { second = ""; }\r
44         if (separator==null) { separator = ""; }\r
45         if (first.startsWith(separator) && second.startsWith(separator)){\r
46             return first.substring(0, first.length()-separator.length()) + second;\r
47         }\r
48         if (first.endsWith(separator) || second.startsWith(separator)){\r
49             return first+second;\r
50         }\r
51         return first+separator+second;\r
52     }\r
53 \r
54     /** Handles null strings as empty.  */\r
55     public static boolean isEmpty(String str){\r
56         return !notEmpty(str);\r
57     }\r
58 \r
59     /** Handles null strings as empty.  */\r
60         public static boolean notEmpty(String str){\r
61         if (str==null) return false;\r
62         if (str.length()==0) return false;\r
63         return true;\r
64     }\r
65 \r
66     /** Handles null strings as false.  */\r
67     public static boolean isTrue(String test){\r
68         return notEmpty(test) && (new Boolean(test)).booleanValue();\r
69     }\r
70 \r
71                     /*  Example usage of searchAndReplace:\r
72                         for (Map.Entry<String,String> entry : variablesMap.entrySet()){\r
73                             String key = entry.getKey();\r
74                             String replace = entry.getValue();\r
75                             String find = "\\$\\{"+key+"\\}";   //must add expression escapes\r
76                                                                 //because $ and braces are "special", and we want to find "${object.CSID}"\r
77                             uri = Tools.searchAndReplace(uri, find, replace);\r
78                             System.out.println("---- REPLACE.uri:        "+initURI);\r
79                             System.out.println("---- REPLACE.find:       "+find);\r
80                             System.out.println("---- REPLACE.replace:    "+replace);\r
81                             System.out.println("---- REPLACE.uri result: "+uri);\r
82                         }\r
83                     */\r
84     public static String  searchAndReplace(String source, String find, String replace){\r
85         Pattern pattern = Pattern.compile(find);\r
86         Matcher matcher = pattern.matcher(source);\r
87         String output = matcher.replaceAll(replace);\r
88         return output;\r
89     }\r
90 \r
91     static boolean m_fileSystemIsDOS = "\\".equals(File.separator);\r
92     static boolean m_fileSystemIsMac = ":".equals(File.separator);\r
93 \r
94     public static boolean fileSystemIsDOS(){return m_fileSystemIsDOS;}\r
95     public static boolean fileSystemIsMac(){return m_fileSystemIsMac;}\r
96 \r
97     public static String fixFilename(String filename){\r
98         if ( m_fileSystemIsDOS ) {\r
99             return filename.replace('/', '\\');\r
100         }\r
101         if ( m_fileSystemIsMac ) {\r
102             String t = filename.replace('/', ':');\r
103             t = t.replace('\\', ':');\r
104             return t;\r
105         }\r
106         return filename.replace('\\','/');\r
107     }\r
108 \r
109     public static String join(String dir, String file){\r
110         if ( dir.length() == 0 ) {\r
111             return file;\r
112         }\r
113         dir = Tools.fixFilename(dir);\r
114         file = Tools.fixFilename(file);\r
115         if ( ! dir.endsWith(File.separator) ) {\r
116             dir += File.separator;\r
117         }\r
118         if ( file.startsWith(File.separator) ) {\r
119             file = file.substring(1);\r
120         }\r
121         return dir + file;\r
122     }\r
123 \r
124 \r
125 \r
126 }\r