001 package net.sourceforge.mp3dings.utils;
002
003 /*
004 * Mp3dings - manage mp3 meta-information
005 * Copyright (C) 2003 Moritz Ringler
006 * $Id: CaseChanger.java,v 1.22 2007/01/17 17:47:30 ringler Exp $
007 *
008 * This program is free software; you can redistribute it and/or
009 * modify it under the terms of the GNU General Public License
010 * as published by the Free Software Foundation; either version 2
011 * of the License, or (at your option) any later version.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, write to the Free Software
020 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021 */
022 import java.util.regex.Matcher;
023 import java.util.regex.Pattern;
024 import net.sourceforge.mp3dings.Globals;
025
026 /**
027 * Static class that provides a method to change the case of a string.
028 *
029 *@author Moritz Ringler
030 *@version $Revision: 1.22 $ ($Date: 2007/01/17 17:47:30 $)
031 */
032 public class CaseChanger {
033 public final static int NO_CHANGE = -1;
034 /** Lowercase */
035 public final static int LOWER = 0;
036 /** Uppercase */
037 public final static int UPPER = 1;
038 /** First letter of string uppercase */
039 public final static int UPPER_FIRST = 2;
040 /** First letter of each word uppercase */
041 public final static int UPPER_EACH_FIRST = 3;
042 private final static Matcher UF_MATCHER =
043 Pattern.compile("(?i)\\b\\w").matcher("");
044
045 /*
046 * you can add more modes here
047 */
048 private final static int NUM_MODES = 4;
049 private final static String[] MODE_NAMES =
050 Globals.getStringArray("CASES");
051
052
053 /**
054 *@deprecated All methods are now static. You don't need to create
055 * instances of this class.
056 */
057 private CaseChanger() { }
058
059
060 /**
061 * Gets the name of a case changing mode
062 *
063 *@param mode by default one of LOWER, UPPER, UPPER_FIRST or
064 * UPPER_EACH_FIRST
065 *@return The modeName value
066 */
067 public static String getModeName(int mode) {
068 return (mode == NO_CHANGE)
069 ? Globals.getString("NO_CASE_CHANGE")
070 : MODE_NAMES[mode];
071 }
072
073
074 /**
075 * Gets the names of all available case changing modes.
076 *
077 *@return a new string array with the names of the case changing modes in
078 * the current locale
079 */
080 public static String[] getModeNames() {
081 String[] retval = new String[NUM_MODES];
082 System.arraycopy(MODE_NAMES, 0, retval, 0, NUM_MODES);
083 return retval;
084 }
085
086
087 /**
088 * Gets the number of available case changing modes
089 *
090 *@return The numModes value
091 */
092 public static int getNumModes() {
093 return NUM_MODES;
094 }
095
096
097 /**
098 * Changes the case of the specified strings. Equivalent to <code>changeCase(input, null, mode)</code>
099 * .
100 *
101 *@param input strings to process, my be <code>null</code>
102 *@param mode one of NO_CHANGE, LOWER, UPPER, UPPER_FIRST or UPPER_EACH_FIRST
103 *@return a new string array with the case-changed strings
104 *@see #changeCase(String[], String[], int)
105 */
106 public static String[] changeCase(String[] input, int mode) {
107 return changeCase(input, null, mode);
108 }
109
110
111 /**
112 * Changes the case of the specified strings.
113 *
114 *@param input strings to process, may be <code>null</code>
115 *@param output a string array to hold the case-changed strings that may
116 * be the same as <code>input</code>. If it is null or of incorrect
117 * length a new string array is allocated.
118 *@param mode one of NO_CHANGE, LOWER, UPPER, UPPER_FIRST or UPPER_EACH_FIRST
119 *@return the output array with the case-changed strings, non-null
120 * if input is non-null
121 *@see #changeCase(String input, int mode)
122 */
123 public static String[] changeCase(String[] input, String[] output, int mode) {
124 String[] result = null;
125 if (input != null) {
126 result = (output == null || output.length != input.length)
127 ? output = new String[input.length]
128 : output;
129 }
130 for (int i = 0; i < input.length; i++) {
131 result[i] = changeCase(input[i], mode);
132 }
133 output = result;
134 return result;
135 }
136
137
138 /**
139 * Changes the case of the specified string
140 *
141 *@param input string to process, may be <code>null</code>
142 *@param mode one of NO_CHANGE, LOWER, UPPER, UPPER_FIRST or UPPER_EACH_FIRST
143 *@return case-changed string
144 */
145 public static String changeCase(String input, int mode) {
146 String result = input;
147 if (input != null) {
148 switch (mode) {
149 case UPPER:
150 result = input.toUpperCase();
151 break;
152 case LOWER:
153 result = input.toLowerCase();
154 break;
155 case UPPER_FIRST:
156 String s = input.toLowerCase();
157 UF_MATCHER.reset(s);
158 if (UF_MATCHER.find()) {
159 result = UF_MATCHER.replaceFirst(
160 UF_MATCHER.group(0).toUpperCase());
161 }
162 break;
163 case UPPER_EACH_FIRST:
164 s = input.toLowerCase();
165 StringBuffer sb = new StringBuffer();
166 boolean found = false;
167 UF_MATCHER.reset(s);
168 while (UF_MATCHER.find()) {
169 UF_MATCHER.appendReplacement(sb,
170 UF_MATCHER.group(0).toUpperCase());
171 found = true;
172 }
173 if (found) {
174 UF_MATCHER.appendTail(sb);
175 result = sb.toString();
176 }
177 break;
178 default:
179 result = input;
180 //System.err.println("Warning in CaseChanger: Unknown mode "+ mode);
181 break;
182 }
183 }
184 return result;
185 }
186 }