DBVIEW
src/org/dbview/utils/Strings.java
00001 /*
00002         DbView - Graph Visualization
00003     Copyright (C) 2012  Denis BEURIVE
00004 
00005     This program is free software: you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation, either version 3 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 */
00018 
00019 /**
00020  * @author Denis Beurive
00021  */
00022 
00023 package org.dbview.utils;
00024 
00025 import java.util.Arrays;
00026 import java.util.List;
00027 import java.util.ArrayList;
00028 import java.util.regex.Matcher;
00029 import java.util.regex.Pattern;
00030 import java.util.Formatter;
00031 import java.security.MessageDigest;
00032 import java.security.NoSuchAlgorithmException;
00033 
00034 /**
00035  * This class contains tools used to manipulate strings.
00036  * @author Denis Beurive
00037  */
00038 public class Strings
00039 {
00040     /**
00041      * New line separator.
00042      */
00043         private static String __newLine = System.getProperty("line.separator");
00044 
00045         /**
00046          * This method joins all elements of a given list using new lines as separator.
00047          * @param in_list List to join.
00048          * @return The method returns a string.
00049          */
00050         public static String joinWithNewLines(List<String> in_list)
00051         {
00052                 if (in_list.size() > 0)
00053                 {
00054                         int index = 0;
00055                         String result = "";
00056                         for (index=0; index<in_list.size()-1; index++) { result =  result.concat(in_list.get(index) + Strings.__newLine); }
00057                         result = result.concat(in_list.get(index));
00058                         return result;
00059                 }
00060                 return "";
00061         }
00062 
00063         /**
00064          * This method joins all elements of a given list using a given separator.
00065          * @param in_list List to join.
00066          * @param in_separator Separator to use.
00067          * @return The method returns a string.
00068          */
00069         public static String join(List<String> in_list, String in_separator)
00070         {
00071                 if (in_list.size() > 0)
00072                 {
00073                         int index = 0;
00074                         String result = "";
00075                         for (index=0; index<in_list.size()-1; index++) { result =  result.concat(in_list.get(index) + in_separator); }
00076                         result = result.concat(in_list.get(index));
00077                         return result;
00078                 }
00079                 return "";
00080         }
00081 
00082         /**
00083          * This method adds a given prefix at the beginning of all lines in a given text.
00084          * @param in_prefix String to add at the beginning of all lines.
00085          * @param in_text Text to indent.
00086          * @return The method returns the indented text.
00087          */
00088         public static String indent(String in_prefix, String in_text)
00089         {
00090             ArrayList<String> r = new ArrayList<String>();
00091             String lines[] = in_text.split(Strings.__newLine);
00092             for (int i=0; i<lines.length; i++) { r.add(in_prefix.concat(lines[i])); }
00093             return Strings.joinWithNewLines(r);
00094         }
00095 
00096         /**
00097          * This method returns the size of the longest string in a list of strings.
00098          * @param in_list List of strings.
00099          * @return The method returns the size of the longest string in a list of strings.
00100          */
00101         public static int longest(ArrayList<String> in_list)
00102         {
00103                 int max = 0;
00104                 for (int i=0; i<in_list.size(); i++) { max = in_list.get(i).length() > max ? in_list.get(i).length() : max; }
00105                 return max;
00106         }
00107 
00108         /**
00109          * This method adds a given padding to the end of a given string.
00110          * @param in_string The string that will be padded.
00111          * @param in_length Length of the string at the end of the operation.
00112          * @param in_padding Padding to add.
00113          * @return The method returns the padded string.
00114          */
00115         public static String paddingRight(String in_string, int in_length, Character in_padding)
00116         {
00117             String new_string = in_string;
00118             int lenght        = new_string.length();
00119 
00120             for (int j=0; j<in_length-lenght; j++) { new_string += in_padding.toString(); }
00121             return new_string;
00122         }
00123 
00124         /**
00125          * This method adds spaces to the end of all elements of a given array.
00126          * @param in_list Array of elements.
00127          * @param in_length Minimum length for all elements, at the end of the operation.
00128          */
00129         public static void paddingRight(ArrayList<String> in_list, int in_length)
00130         {
00131                 for (int i=0; i<in_list.size(); i++)
00132                 {
00133                         String  new_string = in_list.get(i);
00134                         int             length = new_string.length();
00135 
00136                         for (int j=0; j<in_length-length; j++) { new_string += " "; }
00137                         in_list.set(i, new_string);
00138                 }
00139         }
00140 
00141         /**
00142          * This method adds a given padding to the end of all elements of a given array.
00143          * @param in_list Array of elements.
00144          * @param in_length Minimum length for all elements, at the end of the operation.
00145          * @param in_padding Padding to add.
00146          */
00147         public static void paddingRight(ArrayList<String> in_list, int in_length, Character in_padding)
00148         {
00149                 for (int i=0; i<in_list.size(); i++)
00150                 {
00151                         String  new_string = in_list.get(i);
00152                         int             length = new_string.length();
00153 
00154                         for (int j=0; j<in_length-length; j++) { new_string += in_padding.toString(); }
00155                         in_list.set(i, new_string);
00156                 }
00157         }
00158 
00159         /**
00160          * This method adds spaces to the end of all elements of a given array of strings.
00161          * At the end of the operation, all elements of the array have the same length.
00162          * The length of all elements is equal to the length of the largest element plus a given gap.
00163          * @param in_list Array of elements.
00164          * @param in_gap Size of the gap.
00165          */
00166         public static void margingRight(ArrayList<String> in_list, int in_gap)
00167         {
00168                 int max = Strings.longest(in_list);
00169                 Strings.paddingRight(in_list, max+in_gap);
00170         }
00171 
00172         /**
00173          * This method converts a string into an array list of strings.
00174          * @param in_source String to convert.
00175          * @return The method returns the array list of strings.
00176          */
00177         public static ArrayList<String> stringToArrayList(String in_source)
00178         {
00179             ArrayList<String> list_of_chars = new ArrayList<String>();
00180             for (char c: in_source.toCharArray()) { list_of_chars.add(Character.toString(c)); }
00181             return list_of_chars;
00182         }
00183 
00184         /**
00185          * This method returns a string that is the concatenation of an array list of strings.
00186          * @param in_array Array list of strings to convert.
00187          * @return The method returns the string that represents the concatenation of the given array.
00188          */
00189         public static String arrayListToString(ArrayList<String> in_array)
00190         {
00191             String res = new String();
00192             for (String s: in_array) { res = res.concat(s); }
00193             return res;
00194         }
00195 
00196         /**
00197          * This method converts a string that contains dashes into "Lower Camel Case".
00198          * Example: "a-bc-de" becomes "aBcDe".
00199          * @param in_source String to convert.
00200          * @return The method converts the given string into lower Camel case.
00201          */
00202         public static String dashToLowerCamelCase(String in_source)
00203         {
00204             ArrayList<String> list_of_chars = Strings.stringToArrayList(in_source);
00205 
00206             while (true)
00207             {
00208                 int dash_index = list_of_chars.indexOf("-");
00209                 if (-1 == dash_index) { break; }
00210                 if ((dash_index + 1) == list_of_chars.size()) { break; }
00211                 list_of_chars.set(dash_index+1, list_of_chars.get(dash_index+1).toUpperCase());
00212                 list_of_chars.remove(dash_index);
00213             }
00214 
00215             return Strings.arrayListToString(list_of_chars);
00216         }
00217 
00218     /**
00219      * This method converts a string that contains dashes into "Upper Camel Case".
00220      * Example: "a-bc-de" becomes "ABcDe".
00221      * @param in_source String to convert.
00222      * @return The method converts the given string into upper Camel case.
00223      */
00224     public static String dashToUpperCamelCase(String in_source)
00225     {
00226         return Strings.firstToUppercase(Strings.dashToLowerCamelCase(in_source));
00227     }
00228 
00229         /**
00230          * This method returns the dashed representation of a given "Lower Camel Case" string.
00231          * Example: "aBcDe" -> "a-bc-de"
00232          * @param in_source "Lower Camel Case" to convert.
00233          * @return The method returns the dashed representation of the given "Lower Camel Case" string.
00234          */
00235         public static String lowerCamelCaseToDash(String in_source)
00236         {
00237             Pattern p = Pattern.compile("^[a-zA-Z]$");
00238             ArrayList<String> list_of_chars = Strings.stringToArrayList(in_source);
00239             ArrayList<String> dash          = new ArrayList<String>();
00240 
00241         for (int i = 0; i < list_of_chars.size(); i++)
00242         {
00243             String c = list_of_chars.get(i);
00244             Matcher matcher = p.matcher(c);
00245 
00246             if (! matcher.matches()) { dash.add(c); continue; }
00247 
00248             if (0 == c.toUpperCase().compareTo(c))
00249             { dash.add("-" + c.toLowerCase()); }
00250             else { dash.add(c); }
00251         }
00252 
00253         return Strings.arrayListToString(dash);
00254         }
00255 
00256     /**
00257      * This method returns the dashed representation of a given "Upper Camel Case" string.
00258      * Example: "ABcDe" -> "a-bc-de"
00259      * @param in_source "Lower Camel Case" to convert.
00260      * @return The method returns the dashed representation of the given "Upper Camel Case" string.
00261      */
00262     public static String upperCamelCaseToDash(String in_source)
00263     {
00264         return Strings.lowerCamelCaseToDash(Strings.firstTolowercase(in_source));
00265     }
00266 
00267         /**
00268          * This method converts the first character of a given string into upper case.
00269          * @param in_string String to convert.
00270          * @return The method returns the converted string.
00271          */
00272         public static String firstToUppercase(String in_string)
00273         {
00274             if (0 == in_string.length()) { return ""; }
00275             if (1 == in_string.length()) { return in_string.substring(0, 1).toUpperCase(); }
00276             return in_string.substring(0, 1).toUpperCase() + in_string.substring(1);
00277         }
00278 
00279         /**
00280      * This method converts the first character of a given string into lower case.
00281      * @param in_string String to convert.
00282      * @return The method returns the converted string.
00283      */
00284     public static String firstTolowercase(String in_string)
00285     {
00286         if (0 == in_string.length()) { return ""; }
00287         if (1 == in_string.length()) { return in_string.substring(0, 1).toLowerCase(); }
00288         return in_string.substring(0, 1).toLowerCase() + in_string.substring(1);
00289     }
00290 
00291     /**
00292      * Test if a string is empty or null.
00293      * @param in_string String to test.
00294      * @return The method returns TRUE if the string is empty or null.
00295      *         Otherwise, it returns FALSE.
00296      */
00297     public static Boolean isEmpty(String in_string)
00298     {
00299         if (null == in_string) { return Boolean.TRUE; }
00300         if (0 == in_string.length()) { return Boolean.TRUE; }
00301         return Boolean.FALSE;
00302     }
00303     
00304     /**
00305      * Convert an array of bytes into a string that represents a hexadecimal value.
00306      * @param in_hash Array of bytes.
00307      * @return The method returns a string of hexadecimal characters that represents the given array of bytes.
00308      */
00309     private static String byteArray2Hex(final byte[] in_hash)
00310     {
00311         Formatter formatter = new Formatter();
00312         for (byte b : in_hash) { formatter.format("%02x", b); }
00313         return formatter.toString();
00314     }
00315 
00316     /**
00317      * Compute the SHA of a given array of bytes.
00318      * The returned value is a string of hexadecimal characters.
00319      * @param in_convertme Array of bytes to process.
00320      * @return The method returns a string that represents the SHA of the given array of bytes.
00321      * @note We ignore exception "NoSuchAlgorithmException" (since the Algorithm "SHA-1" exists)
00322      */
00323     public static String SHAsum(byte[] in_convertme)
00324     {
00325         try {
00326                 MessageDigest md = MessageDigest.getInstance("SHA-1");
00327                 return byteArray2Hex(md.digest(in_convertme));
00328             }
00329         catch (Exception e) { }
00330         return "SHA-1 Algorithm does not exist!";
00331     }
00332     
00333     /**
00334      * Compute the SHA of a given string.
00335      * The returned value is a string of hexadecimal characters.
00336      * @param in_convertme String to process.
00337      * @return The method returns a string that represents the SHA of the given string.
00338      */
00339     public static String SHAsum(String in_convertme)
00340     {
00341         return Strings.SHAsum(in_convertme.getBytes());
00342     }
00343 }