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