001package org.andromda.metafacades.uml;
002
003import java.text.Normalizer;
004
005import org.andromda.utils.StringUtilsHelper;
006import org.apache.commons.lang.StringUtils;
007
008/**
009 * Provides the ability to <code>mask</code> names in a standard manner.
010 *
011 * @author Chad Brandon
012 */
013public class NameMasker
014{
015    /**
016     * The <code>uppercase</code> mask.
017     */
018    public static final String UPPERCASE = "uppercase";
019
020    /**
021     * The <code>underscore</code> mask.
022     */
023    public static final String UNDERSCORE = "underscore";
024
025    /**
026     * The <code>upperunderscore</code> mask.
027     */
028    public static final String UPPERUNDERSCORE = "upperunderscore";
029
030    /**
031     * The <code>lowercase</code> mask.
032     */
033    public static final String LOWERCASE = "lowercase";
034
035    /**
036     * The <code>lowerunderscore</code> mask.
037     */
038    public static final String LOWERUNDERSCORE = "lowerunderscore";
039
040    /**
041     * The <code>uppercamelcase</code> mask.
042     */
043    public static final String UPPERCAMELCASE = "uppercamelcase";
044
045    /**
046     * The <code>lowercamelcase</code> mask.
047     */
048    public static final String LOWERCAMELCASE = "lowercamelcase";
049
050    /**
051     * The <code>nospace</code> mask.
052     */
053    public static final String NOSPACE = "nospace";
054
055    /**
056     * The <code>noaccent</code> mask.
057     */
058    public static final String NOACCENT = "noaccent";
059
060    /**
061     * The <code>none</code> mask.
062     */
063    public static final String NONE = "none";
064
065    /**
066     * Returns the name with the appropriate <code>mask</code> applied. The mask, must match one of the valid mask
067     * properties or will be ignored.
068     *
069     * @param name the name to be masked
070     * @param mask the mask to apply
071     * @return the masked name.
072     */
073    public static String mask(String name, String mask)
074    {
075        mask = StringUtils.trimToEmpty(mask);
076        name = StringUtils.trimToEmpty(name);
077        if (!mask.equalsIgnoreCase(NONE))
078        {
079            if (mask.equalsIgnoreCase(UPPERCASE))
080            {
081                name = name.toUpperCase();
082            }
083            else if (mask.equalsIgnoreCase(UNDERSCORE))
084            {
085                name = StringUtilsHelper.separate(name, "_");
086            }
087            else if (mask.equalsIgnoreCase(UPPERUNDERSCORE))
088            {
089                name = StringUtilsHelper.separate(name, "_").toUpperCase();
090            }
091            else if (mask.equalsIgnoreCase(LOWERCASE))
092            {
093                name = name.toLowerCase();
094            }
095            else if (mask.equalsIgnoreCase(LOWERUNDERSCORE))
096            {
097                name = StringUtilsHelper.separate(name, "_").toLowerCase();
098            }
099            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}