View Javadoc
1   package org.andromda.metafacades.uml;
2   
3   import java.text.Normalizer;
4   
5   import org.andromda.utils.StringUtilsHelper;
6   import org.apache.commons.lang.StringUtils;
7   
8   /**
9    * Provides the ability to <code>mask</code> names in a standard manner.
10   *
11   * @author Chad Brandon
12   */
13  public class NameMasker
14  {
15      /**
16       * The <code>uppercase</code> mask.
17       */
18      public static final String UPPERCASE = "uppercase";
19  
20      /**
21       * The <code>underscore</code> mask.
22       */
23      public static final String UNDERSCORE = "underscore";
24  
25      /**
26       * The <code>upperunderscore</code> mask.
27       */
28      public static final String UPPERUNDERSCORE = "upperunderscore";
29  
30      /**
31       * The <code>lowercase</code> mask.
32       */
33      public static final String LOWERCASE = "lowercase";
34  
35      /**
36       * The <code>lowerunderscore</code> mask.
37       */
38      public static final String LOWERUNDERSCORE = "lowerunderscore";
39  
40      /**
41       * The <code>uppercamelcase</code> mask.
42       */
43      public static final String UPPERCAMELCASE = "uppercamelcase";
44  
45      /**
46       * The <code>lowercamelcase</code> mask.
47       */
48      public static final String LOWERCAMELCASE = "lowercamelcase";
49  
50      /**
51       * The <code>nospace</code> mask.
52       */
53      public static final String NOSPACE = "nospace";
54  
55      /**
56       * The <code>noaccent</code> mask.
57       */
58      public static final String NOACCENT = "noaccent";
59  
60      /**
61       * The <code>none</code> mask.
62       */
63      public static final String NONE = "none";
64  
65      /**
66       * Returns the name with the appropriate <code>mask</code> applied. The mask, must match one of the valid mask
67       * properties or will be ignored.
68       *
69       * @param name the name to be masked
70       * @param mask the mask to apply
71       * @return the masked name.
72       */
73      public static String mask(String name, String mask)
74      {
75          mask = StringUtils.trimToEmpty(mask);
76          name = StringUtils.trimToEmpty(name);
77          if (!mask.equalsIgnoreCase(NONE))
78          {
79              if (mask.equalsIgnoreCase(UPPERCASE))
80              {
81                  name = name.toUpperCase();
82              }
83              else if (mask.equalsIgnoreCase(UNDERSCORE))
84              {
85                  name = StringUtilsHelper.separate(name, "_");
86              }
87              else if (mask.equalsIgnoreCase(UPPERUNDERSCORE))
88              {
89                  name = StringUtilsHelper.separate(name, "_").toUpperCase();
90              }
91              else if (mask.equalsIgnoreCase(LOWERCASE))
92              {
93                  name = name.toLowerCase();
94              }
95              else if (mask.equalsIgnoreCase(LOWERUNDERSCORE))
96              {
97                  name = StringUtilsHelper.separate(name, "_").toLowerCase();
98              }
99              else if (mask.equalsIgnoreCase(LOWERCAMELCASE))
100             {
101                 name = StringUtilsHelper.lowerCamelCaseName(name);
102             }
103             else if (mask.equalsIgnoreCase(UPPERCAMELCASE))
104             {
105                 name = StringUtilsHelper.upperCamelCaseName(name);
106             }
107             else if (mask.equalsIgnoreCase(NOSPACE))
108             {
109                 name = StringUtils.deleteWhitespace(name);
110             }
111             else if (mask.equalsIgnoreCase(NOACCENT))
112             {
113                 name = Normalizer.normalize(name, java.text.Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]","");
114             }
115         }
116         return name;
117     }
118 }