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
6 * http://www.collectionspace.org
\r
7 * http://wiki.collectionspace.org
\r
9 * Copyright (c) 2009 Regents of the University of California
\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
14 * You may obtain a copy of the ECL 2.0 License at
\r
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
\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
24 package org.collectionspace.services.IntegrationTests.xmlreplay;
\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
31 import java.io.File;
\r
32 import java.util.regex.Pattern;
\r
33 import java.util.regex.Matcher;
\r
35 /** General utility methods.
\r
36 * @author Laramie Crocker
\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
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
48 if (first.endsWith(separator) || second.startsWith(separator)){
\r
49 return first+second;
\r
51 return first+separator+second;
\r
54 /** Handles null strings as empty. */
\r
55 public static boolean isEmpty(String str){
\r
56 return !notEmpty(str);
\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
66 /** Handles null strings as false. */
\r
67 public static boolean isTrue(String test){
\r
68 return notEmpty(test) && (new Boolean(test)).booleanValue();
\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
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
91 static boolean m_fileSystemIsDOS = "\\".equals(File.separator);
\r
92 static boolean m_fileSystemIsMac = ":".equals(File.separator);
\r
94 public static boolean fileSystemIsDOS(){return m_fileSystemIsDOS;}
\r
95 public static boolean fileSystemIsMac(){return m_fileSystemIsMac;}
\r
97 public static String fixFilename(String filename){
\r
98 if ( m_fileSystemIsDOS ) {
\r
99 return filename.replace('/', '\\');
\r
101 if ( m_fileSystemIsMac ) {
\r
102 String t = filename.replace('/', ':');
\r
103 t = t.replace('\\', ':');
\r
106 return filename.replace('\\','/');
\r
109 public static String join(String dir, String file){
\r
110 if ( dir.length() == 0 ) {
\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
118 if ( file.startsWith(File.separator) ) {
\r
119 file = file.substring(1);
\r