Coverage Report - net.sf.jabref.util.CaseChanger
 
Classes in this File Line Coverage Branch Coverage Complexity
CaseChanger
26%
10/38
0%
0/19
3.667
 
 1  
 package net.sf.jabref.util;
 2  
 
 3  
 /* Mp3dings - manage mp3 meta-information
 4  
  * Copyright (C) 2003 Moritz Ringler
 5  
  * $Id: CaseChanger.java 2900 2009-02-24 17:29:11Z mortenalver $
 6  
  *
 7  
  * This program is free software; you can redistribute it and/or
 8  
  * modify it under the terms of the GNU General Public License
 9  
  * as published by the Free Software Foundation; either version 2
 10  
  * of the License, or (at your option) any later version.
 11  
  *
 12  
  * This program is distributed in the hope that it will be useful,
 13  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15  
  * GNU General Public License for more details.
 16  
  *
 17  
  * You should have received a copy of the GNU General Public License
 18  
  * along with this program; if not, write to the Free Software
 19  
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 20  
  */
 21  
 
 22  
 import net.sf.jabref.Util;
 23  
 
 24  
 import java.util.regex.Pattern;
 25  
 import java.util.regex.Matcher;
 26  
 import java.util.HashSet;
 27  
 
 28  
 /**
 29  
  * 
 30  
  * Class with static methods for changing the case of strings and arrays of
 31  
  * strings.
 32  
  * 
 33  
  * @author Moritz Ringler
 34  
  * 
 35  
  * @version $Revision: 2900 $ ($Date: 2009-02-24 12:29:11 -0500 (Tue, 24 Feb 2009) $)
 36  
  */
 37  677728484511
 public class CaseChanger {
 38  
 
 39  
         /** Lowercase */
 40  
         public final static int LOWER = 0;
 41  
 
 42  
         /** Uppercase */
 43  
         public final static int UPPER = 1;
 44  
 
 45  
         /** First letter of string uppercase */
 46  
         public final static int UPPER_FIRST = 2;
 47  
 
 48  
         /** First letter of each word uppercase */
 49  
         public final static int UPPER_EACH_FIRST = 3;
 50  
 
 51  
         /**
 52  
          * I don't think it is thread-safe to use the same matcher at the same time for all calls.
 53  
          */
 54  669552521515
         private final static Pattern UF_PATTERN = Pattern.compile("\\b\\w");
 55  
 
 56  
 
 57  
         // private final static Matcher UF_MATCHER =
 58  
         // // Pattern.compile("(?i)\\b\\w").matcher("");
 59  
         // Pattern.compile("\\b\\w").matcher("");
 60  
 
 61  
         /* you can add more modes here */
 62  
         private final static int numModes = 4;
 63  
 
 64  669552521515
         private final static String[] modeNames = { "lower", "UPPER", "Upper first", "Upper Each First" };
 65  
 
 66  669552521515
     private final static HashSet<String> notToCapitalize = new HashSet<String>();
 67  
 
 68  
     static {
 69  669552521515
         notToCapitalize.add("of");
 70  669552521515
         notToCapitalize.add("and");
 71  669552521515
         notToCapitalize.add("the");
 72  669552521515
     }
 73  
 
 74  
         /**
 75  
          * Gets the name of a case changing mode
 76  
          * 
 77  
          * @param mode
 78  
          *            by default one of LOWER, UPPER, UPPER_FIRST or
 79  
          *            UPPER_EACH_FIRST
 80  
          */
 81  
         public static String getModeName(int mode) {
 82  25543197196
                 return modeNames[mode];
 83  
         }
 84  
 
 85  
         /** Gets the names of all available case changing modes */
 86  
         public static String[] getModeNames() {
 87  0
                 return modeNames;
 88  
         }
 89  
 
 90  
         /** Gets the number of available case changing modes */
 91  
         public static int getNumModes() {
 92  6385799299
                 return numModes;
 93  
         }
 94  
 
 95  
         /**
 96  
          * Changes the case of the specified strings. wrapper for
 97  
          * {@link #changeCase(String input, int mode)}
 98  
          * 
 99  
          * @see #changeCase(String input, int mode)
 100  
          */
 101  
         public static String[] changeCase(String[] input, int mode) {
 102  0
                 int n = input.length;
 103  0
                 String[] output = new String[n];
 104  0
                 for (int i = 0; i < n; i++) {
 105  0
                         output[i] = changeCase(input[i], mode);
 106  
                 }
 107  0
                 return output;
 108  
         }
 109  
 
 110  
         /**
 111  
          * Changes the case of the specified string
 112  
          * 
 113  
          * @param input
 114  
          *            String to change
 115  
          * @param mode
 116  
          *            by default one of LOWER, UPPER, UPPER_FIRST or
 117  
          *            UPPER_EACH_FIRST
 118  
          * @return casechanged string
 119  
          */
 120  
         public static String changeCase(String input, int mode) {
 121  0
         return changeCase(input, mode, false);
 122  
     }
 123  
 
 124  
     /**
 125  
          * Changes the case of the specified string
 126  
          *
 127  
          * @param input
 128  
          *            String to change
 129  
          * @param mode
 130  
          *            by default one of LOWER, UPPER, UPPER_FIRST or
 131  
          *            UPPER_EACH_FIRST
 132  
      * @param skipSmallWords
 133  
      *            In UPPER_EACH_FIRST mode, do not capitalize words like of, the, and,
 134  
      *            unless at the start of the string.
 135  
          * @return casechanged string
 136  
          */
 137  
         public static String changeCase(String input, int mode, boolean skipSmallWords) {
 138  
 
 139  0
                 switch (mode) {
 140  
                 case UPPER:
 141  0
                         return input.toUpperCase();
 142  
                 case LOWER:
 143  0
                         return input.toLowerCase();
 144  
                 case UPPER_FIRST: {
 145  0
                         String s = input.toLowerCase();
 146  
 
 147  0
                         Matcher matcher = UF_PATTERN.matcher(s);
 148  
 
 149  0
                         if (matcher.find()) {
 150  0
                                 return matcher.replaceFirst(matcher.group(0).toUpperCase());
 151  
                         } else {
 152  0
                                 return input;
 153  
                         }
 154  
                 }
 155  
                 case UPPER_EACH_FIRST: {
 156  0
                         String s = input.toLowerCase();
 157  0
                         boolean found = false;
 158  0
             String[] words = s.split("\\s+");
 159  0
             StringBuilder sb = new StringBuilder();
 160  0
             for (int i = 0; i < words.length; i++) {
 161  0
                 String word = words[i];
 162  0
                 if ((i == 0) || !skipSmallWords || !notToCapitalize.contains(word))
 163  0
                     sb.append(Util.nCase(word));
 164  
                 else
 165  0
                     sb.append(word);
 166  0
                 if (i < words.length-1)
 167  0
                     sb.append(" ");
 168  
 
 169  
             }
 170  0
             return sb.toString();
 171  
                         
 172  
                 }
 173  
                 default:
 174  0
                         return input;
 175  
                 }
 176  
         }
 177  
 }