-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUtilities.java
919 lines (815 loc) · 23.8 KB
/
Utilities.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
/*
* xtc - The eXTensible Compiler
* Copyright (C) 2004-2011 Robert Grimm
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
package qimpp;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;
import qimpp.Constants;
/**
* Implementation of utilities for language processors, focusing on
* strings and I/O.
*
* @author Robert Grimm
* @version $Revision: 1.48 $
*/
public final class Utilities {
/** Hide constructor. */
private Utilities() { /* Nothing to do. */ }
/** The buffer size for pumping data. */
private static final int BUFFER_SIZE = 4096;
/** The buffer for pumping data. */
private static char[] charBuffer = null;
/** The regular expression pattern for white space. */
public static final Pattern WHITE_SPACE = Pattern.compile("\\s+");
/**
* The regular expression pattern for newlines surrounded by spaces.
*/
public static final Pattern SPACE_NEWLINE_SPACE =
Pattern.compile("[ \t\f]*(?:\r\n|\r|\n)[ \t\f]*");
/**
* The regular expression pattern for newlines surrounded by spaces
* and optionally followed by stars '<code>*</code>'.
*/
public static final Pattern COMMENT_NEWLINE =
Pattern.compile("[ \t\f]*(?:\r\n|\r|\n)[ \t\f]*\\**[ \t\f]*");
/**
* Flag for escaping the bell '<code>\a</code>' and vertical tab
* '<code>\v</code>' characters, which may be escaped in standard C
* but not in Java.
*/
public static final int ESCAPE_BELL_VT = 0x01;
/**
* Flag for using doubled escape sequences. Doubled escape
* sequences will still read as an escape sequence, even if they are
* included in a program source character or string.
*/
public static final int ESCAPE_DOUBLE = 0x02;
/**
* Flag for escaping the '<code>[</code>', '<code>-</code>', and
* '<code>]</code>' characters used in regular expressions.
*/
public static final int ESCAPE_REGEX = 0x04;
/**
* Flag for escaping all characters that neither are printable ASCII
* characters nor have named escapes using Unicode escapes.
*/
public static final int ESCAPE_UNICODE = 0x08;
/**
* Flag for escaping less-than '<code><</code>' and greater-than
* '<code>></code>' signs as HTML entities.
*/
public static final int ESCAPE_HTML = 0x10;
/** The escape flags for standard C escapes. */
public static final int C_ESCAPES = ESCAPE_BELL_VT | ESCAPE_UNICODE;
/** The escape flags for Java escapes. */
public static final int JAVA_ESCAPES = ESCAPE_UNICODE;
/** The escape flags for Java and HTML escapes. */
public static final int JAVA_HTML_ESCAPES = ESCAPE_UNICODE | ESCAPE_HTML;
/** The escape flags for Java and regex escapes. */
public static final int FULL_ESCAPES = ESCAPE_UNICODE | ESCAPE_REGEX;
/** The escape flags for Java, regex, and HTML escapes. */
public static final int FULL_HTML_ESCAPES = FULL_ESCAPES | ESCAPE_HTML;
// =======================================================================
/**
* Determine whether the specified name is qualified.
*
* @param name The name.
* @return <code>true</code> if the name is qualified.
*/
public static boolean isQualified(String name) {
final int length = name.length();
boolean opaque = false;
for (int i=0; i<length; i++) {
char c = name.charAt(i);
if (opaque) {
if (Constants.END_OPAQUE == c) opaque = false;
} else {
if (Constants.QUALIFIER.equals(c)) return true;
if (Constants.START_OPAQUE == c) opaque = true;
}
}
return false;
}
/**
* Extract the qualifying part from the specified qualified name.
* For example, the qualifying part for <code>xtc.parser.Rats</code>
* is <code>xtc.parser</code>.
*
* @param qname The qualified name.
* @return The qualifying part, or <code>null</code> if the
* specified name has no qualifying part.
*/
public static String getQualifier(String qname) {
boolean opaque = false;
for (int i=qname.length()-1; i>=0; i--) {
char c = qname.charAt(i);
if (opaque) {
if (Constants.START_OPAQUE == c) opaque = false;
} else {
if (Constants.QUALIFIER.equals(c)) return qname.substring(0, i);
if (Constants.END_OPAQUE == c) opaque = true;
}
}
return null;
}
/**
* Extract the unqualified name from the specified qualified name.
* For example, the unqualified name for
* <code>xtc.parser.Rats</code> is <code>Rats</code>.
*
* @param qname The qualified name.
* @return The unqualified name.
*/
public static String getName(String qname) {
boolean opaque = false;
for (int i=qname.length()-1; i>=0; i--) {
char c = qname.charAt(i);
if (opaque) {
if (Constants.START_OPAQUE == c) opaque = false;
} else {
if (Constants.QUALIFIER.equals(c)) return qname.substring(i+1);
if (Constants.END_OPAQUE == c) opaque = true;
}
}
return qname;
}
/**
* Construct an unqualified name. If the specified name is
* qualified, this method returns the unqualified name. Otherwise,
* it returns the specified name.
*
* @param name The name.
* @return The unqualified name.
*/
public static String unqualify(String name) {
return isQualified(name)? getName(name) : name;
}
/**
* Construct a qualified name. If the specified qualifier is
* <code>null</code>, this method returns the unqualified name
*
* @param qualifier The qualifier.
* @param name The unqualified name.
* @return The corresponding qualified name.
*/
public static String qualify(String qualifier, String name) {
return null == qualifier ? name : qualifier + Constants.QUALIFIER + name;
}
/**
* Convert the specified qualified name to its components.
*
* @param qname The qualified name.
* @return The corresponding identifier.
*/
public static String[] toComponents(String qname) {
final int length = qname.length();
int count = 1;
boolean opaque = false;
// Count the number of components.
for (int i=0; i<length; i++) {
char c = qname.charAt(i);
if (opaque) {
if (Constants.END_OPAQUE == c) opaque = false;
} else {
if (Constants.QUALIFIER.equals(c)) count++;
if (Constants.START_OPAQUE == c) opaque = true;
}
}
// Wrap up with the trivial case of no qualifiers.
if (1 == count) return new String[] { qname };
// Set up the component array.
String[] components = new String[count];
int start = -1;
count = 0;
// Fill in the component array.
for (int i=0; i<length; i++) {
char c = qname.charAt(i);
if (opaque) {
if (Constants.END_OPAQUE == c) opaque = false;
} else {
if (Constants.QUALIFIER.equals(c)) {
components[count] = qname.substring(start+1, i);
start = i;
count++;
} else if (Constants.START_OPAQUE == c) {
opaque = true;
}
}
}
// Don't forget the last component.
components[count] = qname.substring(start+1,length);
// Done.
return components;
}
/**
* Replace all occurrences of the qualifier character with the
* specified character.
*
* @param s The string.
* @param qual The new qualifier.
* @return The requalified string.
*/
private static String requalify(String s, char qual) {
final int length = s.length();
StringBuilder buf = new StringBuilder(length);
boolean opaque = false;
for (int i=0; i<length; i++) {
char c = s.charAt(i);
if (opaque) {
buf.append(c);
if (Constants.END_OPAQUE == c) opaque = false;
} else {
if (Constants.QUALIFIER.equals(c)) {
buf.append(qual);
} else {
buf.append(c);
if (Constants.START_OPAQUE == c) opaque = true;
}
}
}
return buf.toString();
}
/**
* Convert the specified qualified name into a programming language
* identifier.
*
* @param qname The qualified name.
* @return The corresponding identifier.
*/
public static String toIdentifier(String qname) {
return requalify(qname, '$');
}
/**
* Convert the specified qualified name into a file path.
*
* @param qname The qualified name.
* @return The corresponding file path.
*/
public static String toPath(String qname) {
return requalify(qname, File.separatorChar);
}
/**
* Convert the specified qualified name into a file path.
*
* @param qname The qualified name.
* @param ext The file's extension.
* @return The corresponding file path.
*/
public static String toPath(String qname, String ext) {
return toPath(qname) + "::" + ext;
}
// =======================================================================
/**
* Locate the specified file. This method searches for the
* specified file, relative to each root in the list of
* <code>java.io.File</code> objects.
*
* @param roots The list of file system roots to search.
* @param path The (relative) file path.
* @return The corresponding file.
* @throws FileNotFoundException
* Signals that the specified file could not be found.
*/
public static File locate(List<File> roots, String path)
throws FileNotFoundException {
for (File root : roots) {
File file = new File(root, path);
if (file.exists() && file.isFile()) {
return file;
}
}
throw new FileNotFoundException(path + " not found");
}
// =======================================================================
/**
* Pump all data from the specified reader to the specified writer.
*
* @param in The reader.
* @param out The writer.
* @throws IOException Signals an exceptinal condition during I/O.
*/
public static void pump(Reader in, Writer out) throws IOException {
if (null == charBuffer) charBuffer = new char[BUFFER_SIZE];
int number = in.read(charBuffer);
while (-1 != number) {
out.write(charBuffer, 0, number);
number = in.read(charBuffer);
}
}
// =======================================================================
/**
* Remove all whitespace from the specified string.
*
* @param s The string.
* @return The string without whitespace.
*/
public static String withoutSpace(String s) {
return WHITE_SPACE.matcher(s).replaceAll("");
}
// =======================================================================
/**
* Determine whether the escape bell and vertical tabs flag is set.
*
* @see #ESCAPE_BELL_VT
*
* @param flags The escape flags.
* @return <code>true</code> if the escape bell and vertical tabs
* flag is set.
*/
public static boolean useBellVTEscapes(int flags) {
return (0 != (ESCAPE_BELL_VT & flags));
}
/**
* Determine whether the escape double flag is set.
*
* @see #ESCAPE_DOUBLE
*
* @param flags The escape flags.
* @return <code>true</code> if the escape double flag is set.
*/
public static boolean useDoubleEscapes(int flags) {
return (0 != (ESCAPE_DOUBLE & flags));
}
/**
* Determine whether the escape regex flag is set.
*
* @see #ESCAPE_REGEX
*
* @param flags The escape flags.
* @return <code>true</code> if the escape regex flag is set.
*/
public static boolean useRegexEscapes(int flags) {
return (0 != (ESCAPE_REGEX & flags));
}
/**
* Determine whether the escape Unicode flag is set.
*
* @see #ESCAPE_UNICODE
*
* @param flags The escape flags.
* @return <code>true</code> if the escape Unicode flag is set.
*/
public static boolean useUnicodeEscapes(int flags) {
return (0 != (ESCAPE_UNICODE & flags));
}
/**
* Determine whether the escape HTML flag is set.
*
* @see #ESCAPE_HTML
*
* @param flags The escape flags.
* @return <code>true</code> if the escape HTML flag is set.
*/
public static boolean useHTMLEscapes(int flags) {
return (0 != (ESCAPE_HTML & flags));
}
// =======================================================================
/**
* Escape the specified character into the specified appendable.
*
* @param c The character.
* @param out The appendable.
* @param flags The escape flags.
* @throws IOException Signals an I/O error.
*/
public static void escape(char c, Appendable out, int flags)
throws IOException {
switch (c) {
case '\u0007':
if (useBellVTEscapes(flags)) {
if (useDoubleEscapes(flags)) {
out.append("\\\\a");
} else {
out.append("\\a");
}
}
// We still need to go through default processing.
break;
case '\b':
if (useDoubleEscapes(flags)) {
out.append("\\\\b");
} else {
out.append("\\b");
}
return;
case '\t':
if (useDoubleEscapes(flags)) {
out.append("\\\\t");
} else {
out.append("\\t");
}
return;
case '\n':
if (useDoubleEscapes(flags)) {
out.append("\\\\n");
} else {
out.append("\\n");
}
return;
case '\u000b':
if (useBellVTEscapes(flags)) {
if (useDoubleEscapes(flags)) {
out.append("\\\\v");
} else {
out.append("\\v");
}
}
// We still need to go through default processing.
break;
case '\f':
if (useDoubleEscapes(flags)) {
out.append("\\\\f");
} else {
out.append("\\f");
}
return;
case '\r':
if (useDoubleEscapes(flags)) {
out.append("\\\\r");
} else {
out.append("\\r");
}
return;
case '\"':
if (useDoubleEscapes(flags)) {
out.append("\\\\\\\"");
} else {
out.append("\\\"");
}
return;
case '\'':
if (useDoubleEscapes(flags)) {
out.append("\\\\\\\'");
} else {
out.append("\\\'");
}
return;
case '-':
if (useRegexEscapes(flags)) {
if (useDoubleEscapes(flags)) {
out.append("\\\\-");
} else {
out.append("\\-");
}
} else {
out.append('-');
}
return;
case '<':
if (useHTMLEscapes(flags)) {
out.append("<");
} else {
out.append('<');
}
return;
case '>':
if (useHTMLEscapes(flags)) {
out.append(">");
} else {
out.append('>');
}
return;
case '[':
if (useRegexEscapes(flags)) {
if (useDoubleEscapes(flags)) {
out.append("\\\\[");
} else {
out.append("\\[");
}
} else {
out.append('[');
}
return;
case '\\':
if (useDoubleEscapes(flags)) {
out.append("\\\\\\\\");
} else {
out.append("\\\\");
}
return;
case ']':
if (useRegexEscapes(flags)) {
if (useDoubleEscapes(flags)) {
out.append("\\\\]");
} else {
out.append("\\]");
}
} else {
out.append(']');
}
return;
default:
// The default case is handled below.
}
if (useUnicodeEscapes(flags) && ((' ' > c) || ('~' < c))) {
String hex = Integer.toHexString(c);
String padding;
switch (hex.length()) {
case 1:
padding = "000";
break;
case 2:
padding = "00";
break;
case 3:
padding = "0";
break;
default:
padding = "";
}
if (useDoubleEscapes(flags)) {
out.append("\\\\");
} else {
out.append("\\");
}
out.append('u');
out.append(padding);
out.append(hex);
} else {
out.append(c);
}
}
/**
* Escape the specified string into the specified appendable.
*
* @param s The string.
* @param out The appendable.
* @param flags The escape flags.
* @throws IOException Signals an I/O error.
*/
public static void escape(String s, Appendable out, int flags)
throws IOException {
final int length = s.length();
for (int i=0; i<length; i++) {
escape(s.charAt(i), out, flags);
}
}
/**
* Escape the specified character.
*
* @param c The character.
* @param flags The escape flags.
* @return The escape character as a string.
*/
public static String escape(char c, int flags) {
StringBuilder buf = new StringBuilder(1);
try {
escape(c, buf, flags);
} catch (IOException x) {
assert false;
}
return buf.toString();
}
/**
* Escape the specified string.
*
* @param s The string.
* @param flags The escape flags.
* @return The escaped string.
*/
public static String escape(String s, int flags) {
StringBuilder buf = new StringBuilder(s.length());
try {
escape(s, buf, flags);
} catch (IOException x) {
assert false;
}
return buf.toString();
}
/**
* Unescape the specified string. This method unescapes basic Java
* escapes ('<code>\b</code>', '<code>\f</code>', '<code>\n</code>',
* '<code>\r</code>', '<code>\t</code>', '<code>\"</code>',
* '<code>\'</code>', and '<code>\\</code>'), the additional
* standard C escapes ('<code>\a</code>', '<code>\v</code>', and
* '<code>\?</code>'), standard C's octal escapes, and standard C's
* and Java's Unicode escapes. To support regex-like character
* classes, it also unescapes '<code>\-</code>', '<code>\[</code>',
* and '<code>\]</code>'.
*
* @param s The string to unescape.
* @return The unescaped string.
* @throws IllegalArgumentException Signals a malformed string.
*/
public static String unescape(String s) {
if (-1 == s.indexOf('\\')) {
return s;
}
final int length = s.length();
StringBuilder buf = new StringBuilder(length);
for (int i=0; i<length; i++) {
char c = s.charAt(i);
if ('\\' != c) {
buf.append(c);
} else {
i++;
if (i >= length) {
throw new IllegalArgumentException("incomplete escape sequence");
}
c = s.charAt(i);
switch (c) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
{
int val = c;
if (i+1 < length) {
c = s.charAt(i+1);
if (('0' <= c) && (c <= '7')) {
val = (val * 8) + c;
i++;
if (i+1 <length) {
c = s.charAt(i+1);
if (('0' <= c) && (c <= '7')) {
val = (val * 8) + c;
i++;
}
}
}
}
buf.append((char)val);
}
break;
case '"':
buf.append('"');
break;
case '\'':
buf.append('\'');
break;
case '-':
buf.append('-');
break;
case '?':
buf.append('?');
break;
case '[':
buf.append('[');
break;
case '\\':
buf.append('\\');
break;
case ']':
buf.append(']');
break;
case 'a':
buf.append('\u0007');
break;
case 'b':
buf.append('\b');
break;
case 'f':
buf.append('\f');
break;
case 'n':
buf.append('\n');
break;
case 'r':
buf.append('\r');
break;
case 't':
buf.append('\t');
break;
case 'u':
i += 4;
int n;
if (i >= length) {
throw new IllegalArgumentException("incomplete universal character"+
" name " + s.substring(i-3));
}
try {
n = Integer.parseInt(s.substring(i-3, i+1), 16);
} catch (NumberFormatException x) {
throw new IllegalArgumentException("malformed universal character" +
" name " + s.substring(i-3,i+1));
}
buf.append((char)n);
break;
case 'v':
buf.append('\u000b');
break;
default:
throw new IllegalArgumentException("illegal escaped character \'\\"
+ c + "\'");
}
}
}
return buf.toString();
}
// =======================================================================
/**
* Convert the specified list to a human-readable representation.
* This method uses <code>toString()</code> for each element in the
* specified list to generate a human-readable representation.
*
* @param l The list.
* @return The human-readable representation.
*/
public static String format(List<?> l) {
final int length = l.size();
StringBuilder buf = new StringBuilder();
Iterator iter = l.iterator();
while (iter.hasNext()) {
String el = iter.next().toString();
if ((1 < length) && (! iter.hasNext())) {
buf.append("and ");
}
buf.append(el);
if ((2 == length) && (iter.hasNext())) {
buf.append(' ');
} else if (iter.hasNext()) {
buf.append(", ");
}
}
return buf.toString();
}
// =======================================================================
/**
* Split the specified identifier. This method splits identifiers
* using an upper case character for each word component into a
* string of lower case words separated by the specified separator.
*
* @param id The identifier.
* @param separator The separator.
* @return The split identifier.
*/
public static String split(String id, char separator) {
// Drop any suffixes.
int idx = id.indexOf('$');
if (-1 != idx) {
id = id.substring(0, idx);
}
// Count the number of upper case characters.
final int length = id.length();
boolean startsUpper = false;
int upperCount = 0;
for (int i=0; i<length; i++) {
if (Character.isUpperCase(id.charAt(i))) {
if (0 == i) startsUpper = true;
upperCount++;
}
}
// No conversion is necessary if all characters are either lower
// or upper case.
if ((0 == upperCount) || (length == upperCount)) {
return id;
}
// Do the actual conversion.
final int size = startsUpper ? length+upperCount-1 : length+upperCount;
StringBuilder buf = new StringBuilder(size);
for (int i=0; i<length; i++) {
char c = id.charAt(i);
if (Character.isUpperCase(c)) {
if (0 != i) {
buf.append(separator);
}
buf.append(Character.toLowerCase(c));
} else {
buf.append(c);
}
}
return buf.toString();
}
/**
* Get the appropriate indefinite article for the specified noun.
*
* @param noun The noun.
* @return The corresponding indefinite article.
*/
public static String toArticle(String noun) {
if (noun.startsWith("a") ||
noun.startsWith("e") ||
noun.startsWith("i") ||
noun.startsWith("o") ||
noun.startsWith("u")) {
return "an";
} else {
return "a";
}
}
}