1 package org.ocltf.translation.library;
2
3 import java.net.URL;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 import org.apache.commons.lang.builder.ToStringBuilder;
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.ocltf.common.ComponentContainer;
11 import org.ocltf.common.Plugin;
12 import org.ocltf.common.XmlObjectFactory;
13 import org.ocltf.templateobject.TemplateObject;
14 import org.ocltf.translation.Translator;
15 import org.ocltf.utils.ClassUtils;
16 import org.ocltf.utils.ExceptionUtils;
17
18 /***
19 * This class contains the translation library information
20 * loaded from a translation-library.xml file.
21 *
22 * @author Chad Brandon
23 */
24 public class Library implements Plugin {
25
26 private static Log logger = LogFactory.getLog(Library.class);
27
28 private Map libraryTranslations = new HashMap();
29
30 private String templateEngineClass;
31
32 private String name;
33
34 private URL resource;
35
36 private Map templateObjects = new HashMap();
37
38 /***
39 * Returns a new configured instance of this Library
40 * from the libraryXml URL.
41 *
42 * @param libraryXml the URI to the library XML configuration document.
43 * @return Library the configured Library instance.
44 */
45 public static Library getInstance(URL libraryXml) {
46 String methodName = "getInstance";
47 ExceptionUtils.checkNull(methodName, "libraryXml", libraryXml);
48 Library library = (Library)XmlObjectFactory.getInstance(Library.class, libraryXml);
49 library.resource = libraryXml;
50 return library;
51 }
52
53 /***
54 * Returns the name of this Library.
55 *
56 * @return String
57 */
58 public String getName() {
59 return name;
60 }
61
62 /***
63 * Sets the name of this Library.
64 *
65 * @param name
66 */
67 public void setName(String name) {
68 this.name = name;
69 }
70
71 /***
72 * The resource URI from which this Library was configured.
73 *
74 * @return URL the URI from which this Library was configured.
75 */
76 public URL getResource() {
77 return this.resource;
78 }
79
80 /***
81 * Adds a new LibraryTranslation.
82 *
83 * @param libraryTranslation
84 */
85 public void addLibraryTranslation(LibraryTranslation libraryTranslation) {
86 String methodName = "addLibraryTranslation";
87 ExceptionUtils.checkNull(methodName, "libraryTranslation", libraryTranslation);
88 libraryTranslation.setLibrary(this);
89 this.libraryTranslations.put(libraryTranslation.getName(), libraryTranslation);
90 }
91
92 /***
93 * Gets the LibraryTranslation instances (keyed by name)
94 * which are part of this Library.
95 *
96 * @return Map
97 */
98 public Map getLibraryTranslations() {
99 return this.libraryTranslations;
100 }
101
102 /***
103 * Retrieves the LibraryTranslation with the specified name.
104 *
105 * @param name
106 * @return LibraryTranslation the LibraryTranslation corresponding
107 * to the <code>name</code>.
108 */
109 public LibraryTranslation getLibraryTranslation(String name) {
110 String methodName = "getLibraryTranslation";
111 ExceptionUtils.checkEmpty(methodName, "name", name);
112 return (LibraryTranslation)this.libraryTranslations.get(name);
113 }
114
115 /***
116 * Sets the TemplateEngine class that will perform
117 * processing of the translation file.
118 *
119 * @param templateEngineClass the Class to use for the TemplateEngine
120 */
121 public void setTemplateEngine(String templateEngineClass) {
122 this.templateEngineClass = templateEngineClass;
123 }
124
125 /***
126 * Sets the Translator class that will perform
127 * the translation processing.
128 *
129 * @param translatorClass the Class for the Translator implementation.
130 */
131 public void setTranslator(String translatorClass) {
132 String methodName = "setTemplateEngine";
133 try {
134 ComponentContainer.instance().registerDefaultComponent(
135 Translator.class,
136 ClassUtils.loadClass(translatorClass));
137 } catch (Throwable th) {
138 String errMsg = "Error performing " + methodName;
139 logger.error(errMsg, th);
140 throw new LibraryException(errMsg, th);
141 }
142 }
143
144 /***
145 * Adds a TransformerConfig object which will make a
146 * Transformer object available to this Library.
147 *
148 * @param transformerConfig the new TemplateObjectConfig object.
149 */
150 public void addTemplateObject(TemplateObject templateObject) {
151 String methodName = "addTemplateObjects";
152 ExceptionUtils.checkNull(methodName, "templateObject", templateObject);
153 templateObject.setOwner(this);
154 this.templateObjects.put(
155 templateObject.getName(),
156 templateObject.getInstance());
157 }
158
159 /***
160 * Returns all the TemplateObject objects that
161 * are available to this Cartridge.
162 * @return Map the templateObjects keyed by name.
163 */
164 public Map getTemplateObjects() {
165 return this.templateObjects;
166 }
167
168 /***
169 * @see org.ocltf.common.Plugin#getResourceName()
170 */
171 public String getResourceName() {
172 return "META-INF/translation-library.xml";
173 }
174
175 /***
176 * @see java.lang.Object#toString()
177 */
178 public String toString() {
179 return ToStringBuilder.reflectionToString(this);
180 }
181
182 }