KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > arooa > ArooaFactory


1 /*
2  * (c) Rob Gordon 2005
3  */

4 package org.oddjob.arooa;
5
6 import java.io.ByteArrayInputStream JavaDoc;
7 import java.io.File JavaDoc;
8 import java.io.FileInputStream JavaDoc;
9 import java.io.FileNotFoundException JavaDoc;
10 import java.io.InputStream JavaDoc;
11
12 import org.apache.commons.beanutils.BeanUtilsBean;
13 import org.apache.log4j.Logger;
14 import org.oddjob.OddjobException;
15 import org.oddjob.arooa.handlers.DefaultComponentHandler;
16 import org.oddjob.arooa.handlers.DocumentStartHandler;
17 import org.oddjob.arooa.handlers.MainHandler;
18 import org.oddjob.arooa.handlers.MappedPropertyHandler;
19 import org.oddjob.arooa.handlers.NestedElementHandler;
20 import org.oddjob.arooa.handlers.NestedPropertyHandler;
21 import org.oddjob.arooa.registry.ComponentRegistry;
22 import org.oddjob.arooa.xml.XMLDefinitionHelper;
23 import org.xml.sax.InputSource JavaDoc;
24
25 /**
26  * Builds an object from XML. This highly configurable factory will
27  * either configure a component provided as the objectToConfigure or
28  * create a component using the class attribute or tag name of the
29  * document root.
30  * <p>
31  * If a documentStartHandler is provided the factory assumes the
32  * root object will also be provided.
33  */

34 public class ArooaFactory {
35     private static final Logger logger = Logger.getLogger(ArooaFactory.class);
36     
37     private String JavaDoc documentTag;
38     private ObjectFactory componentFactory;
39     private ObjectFactory valueFactory;
40     private PropertyProxyResolver propertProxyResolver;
41     private ArooaHandler documentStartHandler;
42     private ArooaHandler componentHandler;
43     private ArooaHandler propertyHandler;
44     private ArooaHandler mappedHandler;
45     private ArooaHandler elementHandler;
46     private ComponentRegistry componentRegistry;
47     private String JavaDoc substitutionPolicy;
48     private Object JavaDoc objectToConfigure;
49     private BeanUtilsBean beanUtilsBean;
50     private ComponentProxyResolver componentProxyResolver;
51     
52     /**
53      * Build from a file.
54      *
55      * @param file A file.
56      * @return The built object. May be null if a custom start handler was
57      * provided.
58      */

59     public Object JavaDoc build(File JavaDoc file) {
60         
61         InputSource JavaDoc inputSource = null;
62         try {
63             InputStream JavaDoc inputStream = new FileInputStream JavaDoc(file);
64             inputSource = new InputSource JavaDoc(inputStream);
65         } catch (FileNotFoundException JavaDoc exc) {
66             throw new OddjobException(exc);
67         }
68         inputSource.setSystemId(new File JavaDoc(file.getAbsolutePath()).toURI().toString());
69         return build(inputSource);
70     }
71
72     /**
73      * Build from a string xml message.
74      * @param xml The xml.
75      * @return The built object. May be null if a custom start handler was
76      * provided.
77      */

78     public Object JavaDoc build(String JavaDoc xml) {
79         return build(new ByteArrayInputStream JavaDoc(xml.getBytes()));
80     }
81     
82     /**
83      * Build from an input stream of xml.
84      *
85      * @param in The input stream.
86      * @return The built object. May be null if a custom start handler was
87      * provided.
88      */

89     public Object JavaDoc build(InputStream JavaDoc in) {
90         InputSource JavaDoc inputSource = new InputSource JavaDoc(in);
91         inputSource.setSystemId("urn:stream");
92         return build(inputSource);
93     }
94     
95     /**
96      * Build from an input source.
97      *
98      * @param inputSource An input source.
99      *
100      * @return The built ojbect. May be null if a custom start handler was
101      * provided.
102      */

103     public Object JavaDoc build(InputSource JavaDoc inputSource) {
104         
105         ArooaContext context = new ArooaContext();
106         context.set(ArooaConstants.COMPONENT_FACTORY, getComponentFactory());
107         context.set(ArooaConstants.VALUE_FACTORY, getValueFactory());
108         context.set(ArooaConstants.PROPERTY_PROXY_RESOLVER, getPropertProxyResolver());
109         context.set(ArooaConstants.PROPERTY_HANDLER, getPropertyHandler());
110         context.set(ArooaConstants.MAPPED_HANDLER, getMappedHandler());
111         context.set(ArooaConstants.ELEMENT_HANDLER, getElementHandler());
112         context.set(ArooaConstants.SUBSTITUTION_POLICY, getSubstitutionPolicy());
113         context.set(ArooaConstants.COMPONENT_REGISTRY, getComponentRegistry());
114         context.set(ArooaConstants.BEAN_UTILS_BEAN, getBeanUtilsBean());
115         if (componentProxyResolver != null) {
116             context.set(ArooaConstants.COMPONENT_PROXY_RESOLVER,
117                     componentProxyResolver);
118         }
119         
120         if (componentHandler == null) {
121             componentHandler = new DefaultComponentHandler();
122         }
123         context.set(ArooaConstants.COMPONENT_HANDLER, componentHandler);
124
125         Object JavaDoc rootComponent = objectToConfigure;
126         ArooaHandler startHandler = documentStartHandler;
127         RuntimeConfiguration rtc = null;
128         
129         if (startHandler == null) {
130             if (rootComponent == null) {
131                 startHandler = componentHandler;
132                 rootComponent = new ComponentRoot();
133             }
134             else {
135                 startHandler = new DocumentStartHandler(objectToConfigure);
136             }
137             rtc = new ArooaRuntime(rootComponent,
138                     documentTag == null ? "documentTag" : documentTag, context);
139             context.set(ArooaConstants.CURRENTLY_CONFIGURING, rtc);
140         }
141                 
142         XMLDefinitionHelper ph = new XMLDefinitionHelper(context);
143         
144         if (documentTag == null) {
145             ph.parse(inputSource, new MainHandler(startHandler));
146         }
147         else {
148             ph.parse(inputSource, new MainHandler(documentTag, startHandler));
149         }
150
151         // if the component kept it's rtc this will do nothing.
152
rtc = (RuntimeConfiguration) context.get(ArooaConstants.CURRENTLY_CONFIGURING);
153         if (rtc != null) {
154             rtc.configure();
155         }
156         if (objectToConfigure != null) {
157             return objectToConfigure;
158         }
159         if (rootComponent == null) {
160             return null;
161         }
162         return ((ComponentRoot)rootComponent).component;
163     }
164     
165     public class ComponentRoot {
166         Object JavaDoc component;
167         public void addComponent(Object JavaDoc component) {
168             this.component = component;
169         }
170     }
171     
172     /**
173      * @param componentFactory The componentFactory to set.
174      */

175     public void setComponentFactory(ObjectFactory componentFactory) {
176         this.componentFactory = componentFactory;
177     }
178     /**
179      * @param componentHandler The componentHandler to set.
180      */

181     public void setComponentHandler(ArooaHandler componentHandler) {
182         this.componentHandler = componentHandler;
183     }
184     /**
185      * @param componentRegistry The componentRegistry to set.
186      */

187     public void setComponentRegistry(ComponentRegistry componentRegistry) {
188         this.componentRegistry = componentRegistry;
189     }
190     /**
191      * @param documentTag The documentTag to set.
192      */

193     public void setDocumentTag(String JavaDoc documentTag) {
194         this.documentTag = documentTag;
195     }
196     /**
197      * @param elementHandler The elementHandler to set.
198      */

199     public void setElementHandler(ArooaHandler elementHandler) {
200         this.elementHandler = elementHandler;
201     }
202     
203     /**
204      * @param substitutionPolicy The substitutionPolicy to set.
205      */

206     public void setSubstitutionPolicy(String JavaDoc substitutionPolicy) {
207         this.substitutionPolicy = substitutionPolicy;
208     }
209     /**
210      * @param typeManager The typeManager to set.
211      */

212     public void setPropertProxyResolver(PropertyProxyResolver typeManager) {
213         this.propertProxyResolver = typeManager;
214     }
215     /**
216      * @param valueFactory The valueFactory to set.
217      */

218     public void setValueFactory(ObjectFactory valueFactory) {
219         this.valueFactory = valueFactory;
220     }
221     /**
222      * @param valueHandler The valueHandler to set.
223      */

224     public void setPropertyHandler(ArooaHandler valueHandler) {
225         this.propertyHandler = valueHandler;
226     }
227     /**
228      * @param objectToConfigure The objectToConfigure to set.
229      */

230     public void setObjectToConfigure(Object JavaDoc objectToConfigure) {
231         this.objectToConfigure = objectToConfigure;
232     }
233     /**
234      * @param mappedHandler The mappedHandler to set.
235      */

236     public void setMappedHandler(ArooaHandler mappedHandler) {
237         this.mappedHandler = mappedHandler;
238     }
239     
240     /**
241      * @return Returns the bub.
242      */

243     public BeanUtilsBean getBeanUtilsBean() {
244         if (beanUtilsBean == null) {
245             beanUtilsBean = BeanUtilsBean.getInstance();
246         }
247         return beanUtilsBean;
248     }
249     /**
250      * @param bub The bub to set.
251      */

252     public void setBeanUtilsBean(BeanUtilsBean bub) {
253         this.beanUtilsBean = bub;
254     }
255     /**
256      * @return Returns the componentFactory.
257      */

258     public ObjectFactory getComponentFactory() {
259         if (componentFactory == null) {
260             componentFactory = new SimpleObjectFactory();
261         }
262         return componentFactory;
263     }
264     
265     /**
266      * @return Returns the componentHandler.
267      */

268     public ArooaHandler getComponentHandler() {
269         return componentHandler;
270     }
271     /**
272      * @return Returns the componentRegistry.
273      */

274     public ComponentRegistry getComponentRegistry() {
275         if (componentRegistry == null) {
276             logger.debug("Creating default ComponentRegistry.");
277             componentRegistry = new ComponentRegistry();
278         }
279         return componentRegistry;
280     }
281     /**
282      * @return Returns the documentTag.
283      */

284     public String JavaDoc getDocumentTag() {
285         return documentTag;
286     }
287     
288     /**
289      * @return Returns the elementHandler.
290      */

291     public ArooaHandler getElementHandler() {
292          if (elementHandler == null) {
293             elementHandler = new NestedElementHandler();
294          }
295         return elementHandler;
296     }
297     /**
298      * @return Returns the mappedHandler.
299      */

300     public ArooaHandler getMappedHandler() {
301         if (mappedHandler == null) {
302              mappedHandler = new MappedPropertyHandler();
303         }
304         return mappedHandler;
305     }
306     
307     /**
308      * @return Returns the propertyHandler.
309      */

310     public ArooaHandler getPropertyHandler() {
311         if (propertyHandler == null) {
312             propertyHandler = new NestedPropertyHandler();
313         }
314         return propertyHandler;
315     }
316     
317     /**
318      * @return Returns the substitutionPolicy.
319      */

320     public String JavaDoc getSubstitutionPolicy() {
321         return substitutionPolicy;
322     }
323     /**
324      * @return Returns the typeManager.
325      */

326     public PropertyProxyResolver getPropertProxyResolver() {
327         if (propertProxyResolver == null) {
328              propertProxyResolver = new PropertyProxyResolver();
329         }
330         return propertProxyResolver;
331     }
332     /**
333      * @return Returns the valueFactory.
334      */

335     public ObjectFactory getValueFactory() {
336         if (valueFactory == null) {
337             valueFactory = new SimpleObjectFactory();
338         }
339         return valueFactory;
340     }
341     /**
342      * Set the document start handler. This will receive a onStartElement
343      * event with the doucment root tag and attributes. If a
344      * documentStartHandler is provided it is optional if an
345      * objectToConfigure is also be provided. If it isn't then none will
346      * be created.
347      *
348      * @param documentStartHandler The documentStartHandler to set.
349      */

350     public void setDocumentStartHandler(ArooaHandler documentStartHandler) {
351         this.documentStartHandler = documentStartHandler;
352     }
353
354     /**
355      * Retrieve the document start handler. This will
356      * @return Returns the documentStartHandler.
357      */

358     public ArooaHandler getDocumentStartHandler() {
359         return documentStartHandler;
360     }
361
362     public ComponentProxyResolver getComponentProxyResolver() {
363         return componentProxyResolver;
364     }
365
366     public void setComponentProxyResolver(
367             ComponentProxyResolver componentProxyResolver) {
368         this.componentProxyResolver = componentProxyResolver;
369     }
370     
371 }
372
Popular Tags