View Javadoc

1   package org.appfuse.tool;
2   
3   import java.util.regex.Pattern;
4   
5   /**
6    * This class contains methods than can be used in
7    * Freemarker templates for String manipulation.
8    */
9   public class StringUtils {
10      private static final Pattern ES = Pattern.compile("^.*(sh|ss|ch|o|i)$");
11      private static final Pattern ICES = Pattern.compile("^.*(ex|ix)$");
12      private static final Pattern NOT_VOWEL_Y = Pattern.compile("^.*[^aeiou]y$");
13  
14      public String getPluralForWord(final String word) {
15          String plural = null;
16  
17          if (StringUtils.isNotBlank(word)) {
18              plural = word + "s";
19  
20              if (ES.matcher(word).matches()) {
21                  plural = word + "es";
22              } else if (NOT_VOWEL_Y.matcher(word).matches()) {
23                  String stripY = word.substring(0, word.length() - 1);
24                  plural = stripY + "ies";
25              } else if (ICES.matcher(word).matches()) {
26                  String strip_X = word.substring(0, word.length() - 2);
27                  plural = strip_X + "ices";
28              }
29          }
30  
31          return plural;
32      }
33  
34      private static boolean isNotBlank(String word) {
35          return word != null && word.trim().length() > 0;
36      }
37  }