View Javadoc
1   package org.andromda.core.translation.library;
2   
3   import java.util.LinkedHashMap;
4   import java.util.Map;
5   import org.andromda.core.common.ComponentContainer;
6   import org.andromda.core.common.ExceptionUtils;
7   import org.apache.log4j.Logger;
8   
9   /**
10   * Finds LibraryTranslations by <code>translation</code> (i.e. library and name).
11   *
12   * @author Chad Brandon
13   */
14  public class LibraryTranslationFinder
15  {
16      /**
17       * The logger instance.
18       */
19      private static final Logger logger = Logger.getLogger(LibraryTranslationFinder.class);
20  
21      /**
22       * Stores the found library translations.
23       */
24      protected static final Map libraryTranslations = new LinkedHashMap();
25  
26      /**
27       * Finds the library with the specified libraryName.
28       *
29       * @param libraryName
30       * @return the Library found or null if none is found.
31       */
32      protected static Library findLibrary(final String libraryName)
33      {
34          return (Library)ComponentContainer.instance().findComponentByNamespace(libraryName, Library.class);
35      }
36  
37      /**
38       * Finds the LibraryTranslation with the specified translationName.
39       *
40       * @param translation the name of the translation to find.
41       * @return the LibraryTranslation found or null if none is found.
42       */
43      public static LibraryTranslation findLibraryTranslation(final String translation)
44      {
45          ExceptionUtils.checkEmpty("translation", translation);
46  
47          LibraryTranslation libraryTranslation = (LibraryTranslation)libraryTranslations.get(translation);
48  
49          if (libraryTranslation == null)
50          {
51              char libSeparator = '.';
52              int index = translation.indexOf(libSeparator);
53              if (index == -1)
54              {
55                  throw new IllegalArgumentException(
56                      "libraryTranslation '" + translation + "' must contain the character '" +
57                      libSeparator + "' in order to separate the library name from the translation" +
58                      " name (must be in the form: <library name>.<translation name>)");
59              }
60              final String libraryName = translation.substring(0, index);
61              final Library library = findLibrary(libraryName);
62              final int translationLength = translation.length();
63  
64              final String translationName = translation.substring(index + 1, translationLength);
65  
66              if (library != null)
67              {
68                  libraryTranslation = library.getLibraryTranslation(translationName);
69                  if (libraryTranslation == null)
70                  {
71                      logger.error(
72                          "ERROR! no translation '" + translationName + "' found within library --> '" + libraryName +
73                                  '\'');
74                  }
75              }
76          }
77          return libraryTranslation;
78      }
79  }