]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-3332
authorLaramie Crocker <laramie@berkeley.edu>
Wed, 15 Dec 2010 21:09:58 +0000 (21:09 +0000)
committerLaramie Crocker <laramie@berkeley.edu>
Wed, 15 Dec 2010 21:09:58 +0000 (21:09 +0000)
services/common/src/main/java/org/collectionspace/services/common/Tools.java [new file with mode: 0755]

diff --git a/services/common/src/main/java/org/collectionspace/services/common/Tools.java b/services/common/src/main/java/org/collectionspace/services/common/Tools.java
new file mode 100755 (executable)
index 0000000..8073bfc
--- /dev/null
@@ -0,0 +1,126 @@
+/**\r
+ * This document is a part of the source code and related artifacts\r
+ * for CollectionSpace, an open source collections management system\r
+ * for museums and related institutions:\r
+ *\r
+ * http://www.collectionspace.org\r
+ * http://wiki.collectionspace.org\r
+ *\r
+ * Copyright (c) 2009 Regents of the University of California\r
+ *\r
+ * Licensed under the Educational Community License (ECL), Version 2.0.\r
+ * You may not use this file except in compliance with this License.\r
+ *\r
+ * You may obtain a copy of the ECL 2.0 License at\r
+ * https://source.collectionspace.org/collection-space/LICENSE.txt\r
+ *\r
+ *  Unless required by applicable law or agreed to in writing, software\r
+ *  distributed under the License is distributed on an "AS IS" BASIS,\r
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ *  See the License for the specific language governing permissions and\r
+ *  limitations under the License.\r
+ */\r
+\r
+package org.collectionspace.services.common;\r
+\r
+import org.apache.commons.jexl2.Expression;\r
+import org.apache.commons.jexl2.JexlContext;\r
+import org.apache.commons.jexl2.JexlEngine;\r
+import org.apache.commons.jexl2.MapContext;\r
+\r
+import java.io.File;\r
+import  java.util.regex.Pattern;\r
+import java.util.regex.Matcher;\r
+\r
+/** General utility methods.\r
+ *   @author Laramie Crocker\r
+ */\r
+public class Tools {\r
+    /** @return first glued to second with the separator string, at most one time - useful for appending paths.\r
+     */\r
+    public static String glue(String first, String separator, String second){\r
+        if (first==null) { first = ""; }\r
+        if (second==null) { second = ""; }\r
+        if (separator==null) { separator = ""; }\r
+        if (first.startsWith(separator) && second.startsWith(separator)){\r
+            return first.substring(0, first.length()-separator.length()) + second;\r
+        }\r
+        if (first.endsWith(separator) || second.startsWith(separator)){\r
+            return first+second;\r
+        }\r
+        return first+separator+second;\r
+    }\r
+\r
+    /** Handles null strings as empty.  */\r
+    public static boolean isEmpty(String str){\r
+        return !notEmpty(str);\r
+    }\r
+\r
+    /** Handles null strings as empty.  */\r
+        public static boolean notEmpty(String str){\r
+        if (str==null) return false;\r
+        if (str.length()==0) return false;\r
+        return true;\r
+    }\r
+\r
+    /** Handles null strings as false.  */\r
+    public static boolean isTrue(String test){\r
+        return notEmpty(test) && (new Boolean(test)).booleanValue();\r
+    }\r
+\r
+                    /*  Example usage of searchAndReplace:\r
+                        for (Map.Entry<String,String> entry : variablesMap.entrySet()){\r
+                            String key = entry.getKey();\r
+                            String replace = entry.getValue();\r
+                            String find = "\\$\\{"+key+"\\}";   //must add expression escapes\r
+                                                                //because $ and braces are "special", and we want to find "${object.CSID}"\r
+                            uri = Tools.searchAndReplace(uri, find, replace);\r
+                            System.out.println("---- REPLACE.uri:        "+initURI);\r
+                            System.out.println("---- REPLACE.find:       "+find);\r
+                            System.out.println("---- REPLACE.replace:    "+replace);\r
+                            System.out.println("---- REPLACE.uri result: "+uri);\r
+                        }\r
+                    */\r
+    public static String  searchAndReplace(String source, String find, String replace){\r
+        Pattern pattern = Pattern.compile(find);\r
+        Matcher matcher = pattern.matcher(source);\r
+        String output = matcher.replaceAll(replace);\r
+        return output;\r
+    }\r
+\r
+    static boolean m_fileSystemIsDOS = "\\".equals(File.separator);\r
+    static boolean m_fileSystemIsMac = ":".equals(File.separator);\r
+\r
+    public static boolean fileSystemIsDOS(){return m_fileSystemIsDOS;}\r
+    public static boolean fileSystemIsMac(){return m_fileSystemIsMac;}\r
+\r
+    public static String fixFilename(String filename){\r
+        if ( m_fileSystemIsDOS ) {\r
+            return filename.replace('/', '\\');\r
+        }\r
+        if ( m_fileSystemIsMac ) {\r
+            String t = filename.replace('/', ':');\r
+            t = t.replace('\\', ':');\r
+            return t;\r
+        }\r
+        return filename.replace('\\','/');\r
+    }\r
+\r
+    public static String join(String dir, String file){\r
+        if ( dir.length() == 0 ) {\r
+            return file;\r
+        }\r
+        dir = Tools.fixFilename(dir);\r
+        file = Tools.fixFilename(file);\r
+        if ( ! dir.endsWith(File.separator) ) {\r
+            dir += File.separator;\r
+        }\r
+        if ( file.startsWith(File.separator) ) {\r
+            file = file.substring(1);\r
+        }\r
+        return dir + file;\r
+    }\r
+\r
+\r
+\r
+}\r