1 6 7 package SOFA.Connector.EEG; 8 9 import org.w3c.dom.Document ; 10 import org.w3c.dom.Element ; 11 import org.w3c.dom.Node ; 12 13 18 public class EEGenerator { 19 20 protected java.util.LinkedList plugin; 21 22 protected javax.xml.parsers.DocumentBuilderFactory documentBuilderFactory; 23 protected javax.xml.parsers.DocumentBuilder documentBuilder; 24 25 protected Document configDoc; 26 27 static public String getTypesPackage() { 28 return "SOFA.Connector.EEG.Types"; 29 } 30 31 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 (getEEGConfigRoot()+"/"+System.getProperty("SOFA.Connector.EEG.Config","conf.xml"))); 38 39 Element elParent=configDoc.getDocumentElement(); 40 Node elPlugins, elPlugin, elProp; 41 42 plugin=new java.util.LinkedList (); 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 props=new java.util.HashMap (); 53 elProp=elPlugin.getFirstChild(); 54 while (elProp!=null) { 55 if (elProp.getNodeType()==Node.ELEMENT_NODE) { 56 Element el=(Element )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 )elPlugin).getAttribute("class")+"):"); 65 66 Class cls=Class.forName(((Element )elPlugin).getAttribute("class")); 67 Class [] coPar=new Class [1]; 68 coPar[0]=Class.forName("java.util.HashMap"); 69 java.lang.reflect.Constructor co=cls.getConstructor(coPar); 70 71 Object [] coInst=new Object [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 )elPlugin).getAttribute("class")+") loaded."); 77 } 78 elPlugin=elPlugin.getNextSibling(); 79 } 80 } 81 elPlugins=elPlugins.getNextSibling(); 82 } 83 84 } catch (Exception e) { 85 throw new EEGeneratorException("Can't parse configuration",e); 86 } 87 } 88 89 public ElementsTechnologyDescriptor canGenerate(ElementQuery query) { 90 java.util.Iterator iter; 91 EEGPluginInterface plgObject; 92 ElementsTechnologyDescriptor etDescr=new ElementsTechnologyDescriptor(); 93 java.util.LinkedList tDescr=new java.util.LinkedList (); 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 e) { 104 } 105 106 int i; 107 Object 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 iter; 118 EEGPluginInterface plgObject; 119 120 int colonIndex=elDescr.implementation.indexOf(':'); 121 String 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 e) { 146 throw new EEGeneratorException("Can't generate element.",e); 147 } 148 } 149 150 static public String getEEGRoot() throws EEGeneratorException { 151 try { 152 return System.getProperty("SOFA.Connector.Root","SOFA/Connector")+"/EEG"; 153 } catch (Exception e) { 154 throw new EEGeneratorException("Can't access property SOFA.Connector.Root",e); 155 } 156 } 157 158 static public String getEEGConfigRoot() throws EEGeneratorException { 159 String configDir; 160 try { 161 configDir=System.getProperty("SOFA.Connector.Config"); 162 } catch (Exception 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 |