001package org.andromda.core.translation.library;
002
003import java.util.LinkedHashMap;
004import java.util.Map;
005import org.andromda.core.common.BasePlugin;
006import org.andromda.core.common.ClassUtils;
007import org.andromda.core.common.ComponentContainer;
008import org.andromda.core.common.ExceptionUtils;
009import org.andromda.core.translation.Translator;
010
011/**
012 * The AndroMDA Translation Library implementation of the Plugin. Library instances are configured from
013 * <code>META-INF/andromda-translation-library.xml</code> files discovered on the classpath.
014 *
015 * @author Chad Brandon
016 */
017public class Library
018    extends BasePlugin
019{
020    private final Map<String, LibraryTranslation> libraryTranslations = new LinkedHashMap<String, LibraryTranslation>();
021
022    /**
023     * The default Library constructor.
024     */
025    public Library()
026    {
027        super();
028    }
029
030    /**
031     * Adds a new LibraryTranslation.
032     *
033     * @param libraryTranslation
034     */
035    public void addLibraryTranslation(final LibraryTranslation libraryTranslation)
036    {
037        ExceptionUtils.checkNull("libraryTranslation", libraryTranslation);
038        libraryTranslation.setLibrary(this);
039        this.libraryTranslations.put(
040            libraryTranslation.getName(),
041            libraryTranslation);
042    }
043
044    /**
045     * Gets the LibraryTranslation instances (keyed by name) which are part of this Library.
046     *
047     * @return Map
048     */
049    public Map<String, LibraryTranslation> getLibraryTranslations()
050    {
051        return this.libraryTranslations;
052    }
053
054    /**
055     * Retrieves the LibraryTranslation with the specified name.
056     *
057     * @param name
058     * @return LibraryTranslation the LibraryTranslation corresponding to the <code>name</code>.
059     */
060    public LibraryTranslation getLibraryTranslation(final String name)
061    {
062        ExceptionUtils.checkEmpty("name", name);
063        return this.libraryTranslations.get(name);
064    }
065
066    /**
067     * Sets the <code>translatorClass</code> that will perform the translation processing.
068     *
069     * @param translatorClass the Class for the Translator implementation.
070     */
071    public void setTranslator(final String translatorClass)
072    {
073        try
074        {
075            ComponentContainer.instance().registerDefaultComponent(
076                Translator.class,
077                ClassUtils.loadClass(translatorClass));
078        }
079        catch (final Throwable throwable)
080        {
081            throw new LibraryException(throwable);
082        }
083    }
084
085    /**
086     * @see org.andromda.core.common.BasePlugin#populateTemplateContext(java.util.Map)
087     */
088    public void populateTemplateContext(final Map<String, Object> templateContext)
089    {
090        super.populateTemplateContext(templateContext);
091    }
092}