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.BasePlugin;
6   import org.andromda.core.common.ClassUtils;
7   import org.andromda.core.common.ComponentContainer;
8   import org.andromda.core.common.ExceptionUtils;
9   import org.andromda.core.translation.Translator;
10  
11  /**
12   * The AndroMDA Translation Library implementation of the Plugin. Library instances are configured from
13   * <code>META-INF/andromda-translation-library.xml</code> files discovered on the classpath.
14   *
15   * @author Chad Brandon
16   */
17  public class Library
18      extends BasePlugin
19  {
20      private final Map<String, LibraryTranslation> libraryTranslations = new LinkedHashMap<String, LibraryTranslation>();
21  
22      /**
23       * The default Library constructor.
24       */
25      public Library()
26      {
27          super();
28      }
29  
30      /**
31       * Adds a new LibraryTranslation.
32       *
33       * @param libraryTranslation
34       */
35      public void addLibraryTranslation(final LibraryTranslation libraryTranslation)
36      {
37          ExceptionUtils.checkNull("libraryTranslation", libraryTranslation);
38          libraryTranslation.setLibrary(this);
39          this.libraryTranslations.put(
40              libraryTranslation.getName(),
41              libraryTranslation);
42      }
43  
44      /**
45       * Gets the LibraryTranslation instances (keyed by name) which are part of this Library.
46       *
47       * @return Map
48       */
49      public Map<String, LibraryTranslation> getLibraryTranslations()
50      {
51          return this.libraryTranslations;
52      }
53  
54      /**
55       * Retrieves the LibraryTranslation with the specified name.
56       *
57       * @param name
58       * @return LibraryTranslation the LibraryTranslation corresponding to the <code>name</code>.
59       */
60      public LibraryTranslation getLibraryTranslation(final String name)
61      {
62          ExceptionUtils.checkEmpty("name", name);
63          return this.libraryTranslations.get(name);
64      }
65  
66      /**
67       * Sets the <code>translatorClass</code> that will perform the translation processing.
68       *
69       * @param translatorClass the Class for the Translator implementation.
70       */
71      public void setTranslator(final String translatorClass)
72      {
73          try
74          {
75              ComponentContainer.instance().registerDefaultComponent(
76                  Translator.class,
77                  ClassUtils.loadClass(translatorClass));
78          }
79          catch (final Throwable throwable)
80          {
81              throw new LibraryException(throwable);
82          }
83      }
84  
85      /**
86       * @see org.andromda.core.common.BasePlugin#populateTemplateContext(java.util.Map)
87       */
88      public void populateTemplateContext(final Map<String, Object> templateContext)
89      {
90          super.populateTemplateContext(templateContext);
91      }
92  }