001package org.andromda.core.translation.library;
002
003import java.util.LinkedHashMap;
004import java.util.Map;
005import org.andromda.core.common.ComponentContainer;
006import org.andromda.core.common.ExceptionUtils;
007import org.apache.log4j.Logger;
008
009/**
010 * Finds LibraryTranslations by <code>translation</code> (i.e. library and name).
011 *
012 * @author Chad Brandon
013 */
014public class LibraryTranslationFinder
015{
016    /**
017     * The logger instance.
018     */
019    private static final Logger logger = Logger.getLogger(LibraryTranslationFinder.class);
020
021    /**
022     * Stores the found library translations.
023     */
024    protected static final Map libraryTranslations = new LinkedHashMap();
025
026    /**
027     * Finds the library with the specified libraryName.
028     *
029     * @param libraryName
030     * @return the Library found or null if none is found.
031     */
032    protected static Library findLibrary(final String libraryName)
033    {
034        return (Library)ComponentContainer.instance().findComponentByNamespace(libraryName, Library.class);
035    }
036
037    /**
038     * Finds the LibraryTranslation with the specified translationName.
039     *
040     * @param translation the name of the translation to find.
041     * @return the LibraryTranslation found or null if none is found.
042     */
043    public static LibraryTranslation findLibraryTranslation(final String translation)
044    {
045        ExceptionUtils.checkEmpty("translation", translation);
046
047        LibraryTranslation libraryTranslation = (LibraryTranslation)libraryTranslations.get(translation);
048
049        if (libraryTranslation == null)
050        {
051            char libSeparator = '.';
052            int index = translation.indexOf(libSeparator);
053            if (index == -1)
054            {
055                throw new IllegalArgumentException(
056                    "libraryTranslation '" + translation + "' must contain the character '" +
057                    libSeparator + "' in order to separate the library name from the translation" +
058                    " name (must be in the form: <library name>.<translation name>)");
059            }
060            final String libraryName = translation.substring(0, index);
061            final Library library = findLibrary(libraryName);
062            final int translationLength = translation.length();
063
064            final String translationName = translation.substring(index + 1, translationLength);
065
066            if (library != null)
067            {
068                libraryTranslation = library.getLibraryTranslation(translationName);
069                if (libraryTranslation == null)
070                {
071                    logger.error(
072                        "ERROR! no translation '" + translationName + "' found within library --> '" + libraryName +
073                                '\'');
074                }
075            }
076        }
077        return libraryTranslation;
078    }
079}