1 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 40 public abstract class GenericApplicationEnvironmentFactory implements ApplicationEnvironmentFactory { 41 42 46 protected String [] args; 47 48 protected DocumentBuilder documentBuilder; 49 50 51 55 public GenericApplicationEnvironmentFactory() { 56 try { 57 setDocumentBuilder(XMLToolbox.createDefaultDocumentBuilder(false,false,true,true,false,true)); 58 } catch (Exception e) { 59 fatalError("cannot create DocumentBuilder",e); 60 } 61 } 62 63 public GenericApplicationEnvironmentFactory(String [] args) { 64 this(); 65 setArgs(args); 66 } 67 68 69 73 public ApplicationEnvironment createApplicationEnvironment() { 74 try { 75 XMLSerializerFactory factory=getXMLSerializerFactory(); XMLSerializer ser=factory.createXMLSerializer(); 77 GenericApplicationEnvironment env = (GenericApplicationEnvironment)(getApplicationEnvironmentClass().newInstance()); 78 env.setFactory(this); 79 initFromXML(env, ser, this.getClass()); 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 iae) { fatalError("cannot create application environment", iae); 86 return null; 87 } 88 } 89 90 public String [] getArgs() { 91 return args; 92 } 93 94 public void setArgs(String [] _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); 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); return factory; 124 } 125 126 protected Document getEnvironmentConfigurationDocument(Class cls) { 127 try { 128 java.io.InputStream inputStream=GenericApplicationEnvironment.getXMLResourceForClass(cls).openStream(); 129 Document d=getDocumentBuilder().parse(inputStream); 130 return d; 131 } catch (Exception e) { 132 fatalError("cannot initialize environment XML configuration",e); 133 return null; 134 } 135 } 136 137 protected abstract Class getApplicationEnvironmentClass(); 138 139 protected void initFromXML(ApplicationEnvironment env, XMLSerializer ser, Class factoryClass) throws XMLException { 140 Class superclass = factoryClass.getSuperclass(); 142 if (ApplicationEnvironmentFactory.class.isAssignableFrom(superclass)) { 143 initFromXML(env, ser, superclass); 144 } 145 org.w3c.dom.Element applicationEnvironmentTag=getEnvironmentConfigurationDocument(factoryClass).getDocumentElement(); 146 ser.xmlDeserialize(env, applicationEnvironmentTag); 147 } 148 149 150 154 public static void fatalError(String msg, Throwable 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 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 } | Popular Tags |