KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > gulden > framework > amoda > generic > core > GenericApplicationEnvironmentFactory


1 /*
2  * Project: AMODA - Abstract Modeled Application
3  * Class: de.gulden.framework.amoda.generic.core.GenericApplicationEnvironmentFactory
4  * Version: snapshot-beautyj-1.1
5  *
6  * Date: 2004-09-29
7  *
8  * This is a snapshot version of the AMODA 0.2 development branch,
9  * it is not released as a seperate version.
10  * For AMODA, see http://amoda.berlios.de/.
11  *
12  * This is licensed under the GNU Lesser General Public License (LGPL)
13  * and comes with NO WARRANTY.
14  *
15  * Author: Jens Gulden
16  * Email: amoda@jensgulden.de
17  */

18
19 package de.gulden.framework.amoda.generic.core;
20
21 import de.gulden.framework.amoda.generic.metadata.*;
22 import de.gulden.framework.amoda.generic.option.*;
23 import de.gulden.framework.amoda.model.core.*;
24 import de.gulden.framework.amoda.model.core.ApplicationEnvironmentFactory;
25 import de.gulden.util.Toolbox;
26 import de.gulden.util.xml.*;
27 import de.gulden.util.xml.XMLToolbox;
28 import de.gulden.util.xml.serializer.*;
29 import java.lang.*;
30 import java.util.*;
31 import javax.xml.parsers.*;
32 import org.w3c.dom.*;
33
34 /**
35  * Class GenericApplicationEnvironmentFactory.
36  *
37  * @author Jens Gulden
38  * @version snapshot-beautyj-1.1
39  */

40 public abstract class GenericApplicationEnvironmentFactory implements ApplicationEnvironmentFactory {
41
42     // ------------------------------------------------------------------------
43
// --- fields ---
44
// ------------------------------------------------------------------------
45

46     protected String JavaDoc[] args;
47
48     protected DocumentBuilder documentBuilder;
49
50
51     // ------------------------------------------------------------------------
52
// --- constructors ---
53
// ------------------------------------------------------------------------
54

55     public GenericApplicationEnvironmentFactory() {
56         try {
57             setDocumentBuilder(XMLToolbox.createDefaultDocumentBuilder(false,false,true,true,false,true));
58         } catch (Exception JavaDoc e) {
59             fatalError("cannot create DocumentBuilder",e);
60         }
61     }
62
63     public GenericApplicationEnvironmentFactory(String JavaDoc[] args) {
64         this();
65         setArgs(args);
66     }
67
68
69     // ------------------------------------------------------------------------
70
// --- methods ---
71
// ------------------------------------------------------------------------
72

73     public ApplicationEnvironment createApplicationEnvironment() {
74         try {
75             XMLSerializerFactory factory=getXMLSerializerFactory();//new AutoReflectionXMLSerializerFactory();
76
XMLSerializer ser=factory.createXMLSerializer();
77             GenericApplicationEnvironment env = (GenericApplicationEnvironment)(getApplicationEnvironmentClass().newInstance());
78             env.setFactory(this);
79             initFromXML(env, ser, this.getClass()); // recursively initialize
80
return env;
81         } catch (de.gulden.util.xml.XMLException e) {
82             fatalError("cannot parse application environment XML configuration", e);
83             return null;
84         } catch (Exception JavaDoc iae) { // IllegalAccessException, InstatiationException
85
fatalError("cannot create application environment", iae);
86             return null;
87         }
88     }
89
90     public String JavaDoc[] getArgs() {
91         return args;
92     }
93
94     public void setArgs(String JavaDoc[] _args) {
95         args = _args;
96     }
97
98     public DocumentBuilder getDocumentBuilder() {
99         return documentBuilder;
100     }
101
102     public void setDocumentBuilder(DocumentBuilder _documentBuilder) {
103         documentBuilder = _documentBuilder;
104     }
105
106     public XMLSerializerFactory getXMLSerializerFactory() {
107         de.gulden.util.xml.serializer.smart.SmartReflectionXMLSerializerFactory factory=new de.gulden.util.xml.serializer.smart.SmartReflectionXMLSerializerFactory();
108         factory.registerTagname("metadata",de.gulden.framework.amoda.generic.metadata.GenericMetadata.class); // inner elements of metadata parsed by GenericMetadata itself
109
factory.registerTagname("feature-group",de.gulden.framework.amoda.generic.core.GenericFeatureGroup.class);
110         factory.registerTagname("feature",de.gulden.framework.amoda.generic.core.GenericFeature.class);
111         factory.registerTagname("options",de.gulden.framework.amoda.generic.option.GenericOptions.class);
112         factory.registerTagname("option",de.gulden.framework.amoda.generic.option.GenericOptionEntry.class);
113         factory.registerTagname("option-choice",de.gulden.framework.amoda.generic.option.GenericOptionChoice.class);
114         factory.registerTagname("options-group",de.gulden.framework.amoda.generic.option.GenericOptions.class);
115         factory.registerTagname("command",de.gulden.framework.amoda.generic.behaviour.CommandWrapper.class);
116         factory.registerTagname("condition",de.gulden.framework.amoda.generic.behaviour.ConditionWrapper.class);
117         factory.registerTagname("message",de.gulden.framework.amoda.generic.interaction.GenericMessage.class);
118         factory.registerTagname("error-message",de.gulden.framework.amoda.generic.interaction.GenericErrorMessage.class);
119         factory.registerTagname("question",de.gulden.framework.amoda.generic.interaction.GenericQuestion.class);
120         factory.registerTagname("dialog",de.gulden.framework.amoda.generic.interaction.GenericDialog.class);
121         factory.registerTagname("wizard",de.gulden.framework.amoda.generic.interaction.GenericWizard.class);
122         factory.registerTagname("module",de.gulden.framework.amoda.model.core.ApplicationModule.class, true); // requires attribute "class="
123
return factory;
124     }
125
126     protected Document getEnvironmentConfigurationDocument(Class JavaDoc cls) {
127         try {
128             java.io.InputStream JavaDoc inputStream=GenericApplicationEnvironment.getXMLResourceForClass(cls).openStream();
129             Document d=getDocumentBuilder().parse(inputStream);
130             return d;
131         } catch (Exception JavaDoc e) {
132             fatalError("cannot initialize environment XML configuration",e);
133             return null;
134         }
135     }
136
137     protected abstract Class JavaDoc getApplicationEnvironmentClass();
138
139     protected void initFromXML(ApplicationEnvironment env, XMLSerializer ser, Class JavaDoc factoryClass) throws XMLException {
140         // first init by factory-superclass
141
Class JavaDoc superclass = factoryClass.getSuperclass();
142         if (ApplicationEnvironmentFactory.class.isAssignableFrom(superclass)) {
143             initFromXML(env, ser, superclass);
144         }
145         org.w3c.dom.Element JavaDoc applicationEnvironmentTag=getEnvironmentConfigurationDocument(factoryClass).getDocumentElement();
146         ser.xmlDeserialize(env, applicationEnvironmentTag);
147     }
148
149
150     // ------------------------------------------------------------------------
151
// --- static method ---
152
// ------------------------------------------------------------------------
153

154     public static void fatalError(String JavaDoc msg, Throwable JavaDoc t) {
155         System.out.println(msg);
156         System.out.println(t.getMessage());
157         t.printStackTrace(System.out);
158         if (t instanceof de.gulden.util.xml.XMLException) {
159             de.gulden.util.xml.XMLException exc=(de.gulden.util.xml.XMLException)t;
160             Throwable JavaDoc wrapped=exc.getWrappedThrowable();
161             if (wrapped!=null) {
162                 System.out.println("*** WRAPPED EXCEPTION ***");
163                 System.out.println(wrapped.getMessage());
164                 wrapped.printStackTrace(System.out);
165             }
166         }
167         System.exit(5);
168     }
169
170 } // end GenericApplicationEnvironmentFactory
171
Popular Tags