KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > Connector > EEG > EEGenerator


1 /*
2  * EEGenerator.java
3  *
4  * Created on 2. duben 2002, 12:10
5  */

6
7 package SOFA.Connector.EEG;
8
9 import org.w3c.dom.Document JavaDoc;
10 import org.w3c.dom.Element JavaDoc;
11 import org.w3c.dom.Node JavaDoc;
12
13 /**
14  *
15  * @author ghort
16  * @version
17  */

18 public class EEGenerator {
19
20     protected java.util.LinkedList JavaDoc plugin; /* of EEGPluginInterface */
21     
22     protected javax.xml.parsers.DocumentBuilderFactory JavaDoc documentBuilderFactory;
23     protected javax.xml.parsers.DocumentBuilder JavaDoc documentBuilder;
24     
25     protected Document JavaDoc configDoc;
26
27     static public String JavaDoc getTypesPackage() {
28         return "SOFA.Connector.EEG.Types";
29     }
30     
31     /** Creates new EEGenerator */
32     public EEGenerator() throws EEGeneratorException {
33         try {
34             documentBuilderFactory=javax.xml.parsers.DocumentBuilderFactory.newInstance();
35             documentBuilder=documentBuilderFactory.newDocumentBuilder();
36         
37             configDoc=documentBuilder.parse(new java.io.File JavaDoc(getEEGConfigRoot()+"/"+System.getProperty("SOFA.Connector.EEG.Config","conf.xml")));
38             
39             Element JavaDoc elParent=configDoc.getDocumentElement();
40             Node JavaDoc elPlugins, elPlugin, elProp;
41             
42             plugin=new java.util.LinkedList JavaDoc();
43             
44             System.out.println("Loading EEG plugins:");
45             elPlugins=elParent.getFirstChild();
46             while (elPlugins!=null) {
47                 if (elPlugins.getNodeType()==Node.ELEMENT_NODE && elPlugins.getNodeName().equals("EEGPlugins")) {
48                     elPlugin=elPlugins.getFirstChild();
49                     while (elPlugin!=null) {
50                         if (elPlugin.getNodeType()==Node.ELEMENT_NODE && elPlugin.getNodeName().equals("EEGPlugin")) {
51                             
52                             java.util.HashMap JavaDoc props=new java.util.HashMap JavaDoc();
53                             elProp=elPlugin.getFirstChild();
54                             while (elProp!=null) {
55                                 if (elProp.getNodeType()==Node.ELEMENT_NODE) {
56                                     Element JavaDoc el=(Element JavaDoc)elProp;
57                                     if (el.getNodeName().equals("property") && el.hasAttribute("name") && el.hasAttribute("value")) {
58                                         props.put(el.getAttribute("name"),el.getAttribute("value"));
59                                     }
60                                 }
61                                 elProp=elProp.getNextSibling();
62                             }
63                             
64                             System.out.println(" Loading plugin ("+((Element JavaDoc)elPlugin).getAttribute("class")+"):");
65                                     
66                             Class JavaDoc cls=Class.forName(((Element JavaDoc)elPlugin).getAttribute("class"));
67                             Class JavaDoc[] coPar=new Class JavaDoc[1];
68                             coPar[0]=Class.forName("java.util.HashMap");
69                             java.lang.reflect.Constructor JavaDoc co=cls.getConstructor(coPar);
70                             
71                             Object JavaDoc[] coInst=new Object JavaDoc[1];
72                             coInst[0]=props;
73                             EEGPluginInterface plgObject=(EEGPluginInterface)co.newInstance(coInst);
74                             
75                             plugin.add(plgObject);
76                             System.out.println(" Plugin "+plgObject.getName()+" ("+((Element JavaDoc)elPlugin).getAttribute("class")+") loaded.");
77                         }
78                         elPlugin=elPlugin.getNextSibling();
79                     }
80                 }
81                 elPlugins=elPlugins.getNextSibling();
82             }
83                         
84         } catch (Exception JavaDoc e) {
85             throw new EEGeneratorException("Can't parse configuration",e);
86         }
87     }
88
89     public ElementsTechnologyDescriptor canGenerate(ElementQuery query) {
90         java.util.Iterator JavaDoc iter;
91         EEGPluginInterface plgObject;
92         ElementsTechnologyDescriptor etDescr=new ElementsTechnologyDescriptor();
93         java.util.LinkedList JavaDoc tDescr=new java.util.LinkedList JavaDoc();
94         
95         etDescr.type=query.type;
96         etDescr.instanceName=query.instanceName;
97         iter=plugin.iterator();
98         try {
99             for (;;) {
100                 plgObject=(EEGPluginInterface)iter.next();
101                 tDescr.addAll(java.util.Arrays.asList(plgObject.canGenerate(query)));
102             }
103         } catch (java.util.NoSuchElementException JavaDoc e) {
104         }
105         
106         int i;
107         Object JavaDoc arr[];
108         etDescr.technologies=new ElementTechnologyDescriptor[(arr=tDescr.toArray()).length];
109         for (i=0;i<arr.length;i++) {
110             etDescr.technologies[i]=(ElementTechnologyDescriptor)arr[i];
111         }
112
113         return etDescr;
114     }
115     
116     public ElementOutputDescriptor generate(ElementInputDescriptor elDescr) throws EEGeneratorException {
117         java.util.Iterator JavaDoc iter;
118         EEGPluginInterface plgObject;
119         
120         int colonIndex=elDescr.implementation.indexOf(':');
121         String JavaDoc pluginName;
122         
123         if (colonIndex==-1) {
124             pluginName=elDescr.implementation;
125         } else {
126             pluginName=elDescr.implementation.substring(0,colonIndex);
127         }
128         
129         System.out.println(" Generating element '"+elDescr.implementation+"'");
130         
131         iter=plugin.iterator();
132         try {
133             for (;;) {
134                 plgObject=(EEGPluginInterface)iter.next();
135                 if (pluginName.equals(plgObject.getName())) {
136                     try {
137                         ElementOutputDescriptor ret;
138                         ret=plgObject.generate(elDescr);
139                         return ret;
140                     } catch (EEGPluginException e) {
141                         throw new EEGeneratorException("Plugin failed",e);
142                     }
143                 }
144             }
145         } catch (java.util.NoSuchElementException JavaDoc e) {
146             throw new EEGeneratorException("Can't generate element.",e);
147         }
148     }
149
150     static public String JavaDoc getEEGRoot() throws EEGeneratorException {
151         try {
152             return System.getProperty("SOFA.Connector.Root","SOFA/Connector")+"/EEG";
153         } catch (Exception JavaDoc e) {
154             throw new EEGeneratorException("Can't access property SOFA.Connector.Root",e);
155         }
156     }
157
158     static public String JavaDoc getEEGConfigRoot() throws EEGeneratorException {
159         String JavaDoc configDir;
160         try {
161             configDir=System.getProperty("SOFA.Connector.Config");
162         } catch (Exception JavaDoc e) {
163             throw new EEGeneratorException("Can't access property SOFA.Connector.Config",e);
164         }
165         if (configDir==null) {
166             configDir=getEEGRoot();
167         } else {
168             configDir+="/EEG";
169         }
170         return configDir;
171     }
172 }
173
Popular Tags