001package org.andromda.core.translation.library;
002
003import java.util.LinkedHashMap;
004import java.util.Map;
005import org.andromda.core.common.ExceptionUtils;
006import org.apache.commons.lang.StringUtils;
007import org.apache.commons.lang.builder.ToStringBuilder;
008
009/**
010 * A Translation "fragment" of a translation file. This fragment belongs to a Translation object.
011 *
012 * @author Chad Brandon
013 * @see org.andromda.core.translation.library.Translation
014 */
015public class Fragment
016{
017    private String name;
018    private String handlerMethod;
019
020    /**
021     * The Translation to which this Fragment belongs.
022     */
023    private Translation translation;
024
025    /**
026     * The possible kinds available to this Fragment
027     */
028    private Map<String, String> kinds = new LinkedHashMap<String, String>();
029
030    /**
031     * It doesn't make any sense to instatiate this object explicitly. It intended to be instantiated as part of a
032     * Translation.
033     *
034     * @see org.andromda.core.translation.library.Translation
035     */
036    public Fragment()
037    {
038        // Here for documentation purposes
039    }
040
041    /**
042     * Gets the name of this fragment
043     *
044     * @return the name of this fragment.
045     */
046    public String getName()
047    {
048        return name;
049    }
050
051    /**
052     * Sets the name of this fragment.
053     *
054     * @param name the name to set.
055     */
056    public void setName(final String name)
057    {
058        this.name = StringUtils.trimToEmpty(name);
059    }
060
061    /**
062     * Returns the kinds contained within this translation fragment
063     *
064     * @return Map the Kinds keyed by name.
065     */
066    public Map<String, String> getKinds()
067    {
068        return this.kinds;
069    }
070
071    /**
072     * Returns the body for the fragment kind with the specified name.
073     *
074     * @param name the name of the kind to get.
075     * @return FragmentKind
076     */
077    public String getKind(String name)
078    {
079        // clean the name first
080        name = StringUtils.trimToEmpty(name);
081        ExceptionUtils.checkEmpty("name", name);
082        String kind = StringUtils.trimToEmpty(kinds.get(name));
083        if (kind == null)
084        {
085            throw new LibraryException(
086                "No kind '" + name + "' could be found for the translation fragment '" +
087                this.getName() + "' check the fragment '" + this.getName() + "' in translation template --> '" +
088                getTranslation().getLibraryTranslation().getTemplate() + '\'');
089        }
090        return kind;
091    }
092
093    /**
094     * Adds the specified kind having the specified name and body to the Fragment.
095     *
096     * @param name the name of the kind of expression.
097     * @param body the body of the kind of expression.
098     */
099    public void addKind(
100        final String name,
101        final String body)
102    {
103        kinds.put(
104            StringUtils.trimToEmpty(name),
105            body);
106    }
107
108    /**
109     * Returns the name of the handler method.
110     *
111     * @return Returns the handlerMethod.
112     */
113    public String getHandlerMethod()
114    {
115        return this.handlerMethod;
116    }
117
118    /**
119     * Sets the name of the handler method. This method is the method within the Translator that handles the processing
120     * of the fragment.
121     *
122     * @param handlerMethod The handlerMethod to set.
123     * @see org.andromda.core.translation.Translator
124     */
125    public void setHandlerMethod(final String handlerMethod)
126    {
127        this.handlerMethod = handlerMethod;
128    }
129
130    /**
131     * Gets the Translation to which this Fragment belongs.
132     *
133     * @return Translation
134     */
135    public Translation getTranslation()
136    {
137        final String methodName = "Fragment.getTranslation";
138
139        // should never happen, but it doesn't hurt to be safe
140        if (this.translation == null)
141        {
142            throw new LibraryException(methodName + " - translation can not be null");
143        }
144        return translation;
145    }
146
147    /**
148     * Sets the Translation to which this Fragment belongs.
149     *
150     * @param translation
151     */
152    public void setTranslation(final Translation translation)
153    {
154        this.translation = translation;
155    }
156
157    /**
158     * @see Object#toString()
159     */
160    public String toString()
161    {
162        return ToStringBuilder.reflectionToString(this);
163    }
164}