View Javadoc

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