-
Notifications
You must be signed in to change notification settings - Fork 0
/
CaseFold.java
281 lines (256 loc) · 9.37 KB
/
CaseFold.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import org.apache.commons.cli.*;
import java.io.*;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
public class CaseFold
{
private static final String[] lowerWords = {"a", "an", "the", "as", "at", "by", "for", "in", "of", "off", "on",
"per", "to", "up", "via", "and", "but", "if", "nor", "or", "so", "yet"};
public static void main(String[] args)
{
Locale locale = Locale.getDefault();
// load resources from disk for current locale
ResourceBundle resources = null;
try {
resources = ResourceBundle.getBundle("casefold", locale);
}
// error for incomplete language
catch (MissingResourceException err)
{
String error404 = "This content is not available in your region.\n هذا المحتوى غير متوفر في منطقتك.\n " +
"Deze inhoud is niet beschikbaar in uw regio.\n Ce contenu n'est pas disponible dans votre région." +
"\n Dieser Inhalt ist in Ihrer Region nicht verfügbar.\n Questo contenuto non è disponibile nella " +
"tua regione.\n Este contenido no está disponible en su región.\n " +
"Этот контент недоступен в вашем регионе.\n このコンテンツはお住まいの地域ではご利用いただけません。";
System.err.println(error404);
System.exit(404);
}
// get resource strings
String upperString = resources.getString("upper");
String lowerString = resources.getString("lower");
String sentenceString = resources.getString("sentence");
String titleString = resources.getString("title");
String helpString = resources.getString("help");
String desc = resources.getString("description");
String error1 = resources.getString("err1");
String error2 = resources.getString("err2");
String error3 = resources.getString("err3");
String error4 = resources.getString("err4");
// configure command-line options
Options options = new Options();
options.addOption("u", "upper", false, upperString);
options.addOption("l", "lower", false,
lowerString);
options.addOption("s", "sentence", false,
sentenceString);
options.addOption("t", "title", false,
titleString);
options.addOption("h", "help", false, helpString);
// parse args using the above options parser
CommandLine input = null;
try
{
CommandLineParser parser = new DefaultParser();
input = parser.parse(options, args);
}
catch (ParseException err)
{
System.err.println(error2 + err.getMessage());
System.exit(2);
}
// check command option
String option = null;
if (input.hasOption("h"))
{
// automatically generate the help statement
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("CaseFold", desc, options, "", true);
System.exit(0);
}
else if (input.hasOption("u"))
{
// uppercase option
option = "u";
}
else if (input.hasOption("l"))
{
// lowercase option
option = "l";
}
else if (input.hasOption("s"))
{
// sentence case option
option = "s";
}
else if (input.hasOption("t"))
{
// title case option
option = "t";
}
else
{
System.err.println(error1);
System.exit(1);
}
// get remaining arguments
String[] inputArray = input.getArgs();
// Read text from the input file or user input.
StringBuilder text = new StringBuilder();
// input #1
if (inputArray.length == 1)
{
// get file name
String fileName = inputArray[0];
try
{
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line = reader.readLine();
text.append(line);
while ((line = reader.readLine()) != null)
{
text.append("\n").append(line);
}
reader.close();
}
catch(FileNotFoundException err)
{
System.err.println(error4);
System.exit(4);
}
catch (IOException err)
{
System.err.println("Error reading file. Cause: " + err.getMessage());
err.printStackTrace();
System.exit(3);
}
}
// input #2
else
{
// create reader over input
BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in));
// get file name
try
{
if (System.in.available() != 0)
{
BufferedReader reader = new BufferedReader(inputReader);
String line = reader.readLine();
text.append(line);
while ((line = reader.readLine()) != null)
{
text.append("\n").append(line);
}
reader.close();
}
else
{
// no file
System.err.println(error4);
System.exit(4);
}
}
catch (IOException err)
{
System.err.println(error3 + err.getMessage());
err.printStackTrace();
System.exit(3);
}
}
// Perform the selected case conversion
StringBuilder convertedString = new StringBuilder();
// check for empty file
if (text.toString().equals("null"))
{
System.exit(0);
}
switch (option)
{
// Upper case
case "u" -> convertedString.append(text.toString().toUpperCase());
// Lower case
case "l" -> convertedString.append(text.toString().toLowerCase());
// Title case
case "t" ->
{
boolean convertNextToUpper = true;
char[] charArray = text.toString().toCharArray();
// loop across all characters
for (int i = 0; i < charArray.length; i++)
{
char ch = charArray[i];
if (ch == ' ')
{
boolean lowerWord = false;
// check if non-capitalize word
for (String word : lowerWords)
{
// check if lower word already found
if (lowerWord)
{
// end check
break;
}
// continue checking
lowerWord = true;
for (int x = 0; x < word.length(); x++)
{
char letter = Character.toLowerCase(charArray[(i + 1) + x]);
char wordLetter = word.charAt(x);
if (letter != wordLetter)
{
lowerWord = false;
break;
}
}
}
if (!lowerWord)
{
convertNextToUpper = true;
}
}
else if (ch == '\n')
{
convertNextToUpper = true;
}
else if (convertNextToUpper)
{
ch = Character.toUpperCase(ch);
convertNextToUpper = false;
}
else
{
ch = Character.toLowerCase(ch);
}
convertedString.append(ch);
};
}
// Sentence case
case "s" ->
{
boolean isNewSentence = true;
for (char ch : text.toString().toCharArray())
{
if (isNewSentence && Character.isLetter(ch))
{
ch = Character.toUpperCase(ch);
isNewSentence = false;
}
else
{
ch = Character.toLowerCase(ch);
}
if (ch == '.' || ch == '?' || ch == '!')
{
isNewSentence = true;
}
convertedString.append(ch);
}
}
}
// return converted text
System.out.print(convertedString);
System.exit(0);
}
}