1 2 package SOFA.SOFAnode.Made.Tools; 3 import java.io.File ; 4 import java.io.FileOutputStream ; 5 6 import javax.xml.transform.Transformer ; 7 import javax.xml.transform.TransformerConfigurationException ; 8 import javax.xml.transform.TransformerException ; 9 import javax.xml.transform.TransformerFactory ; 10 import javax.xml.transform.dom.DOMSource ; 11 import javax.xml.transform.stream.StreamResult ; 12 13 import org.w3c.dom.Document ; 14 import org.w3c.dom.Element ; 15 import org.w3c.dom.Node ; 16 import org.w3c.dom.NodeList ; 17 18 import SOFA.SOFAnode.Made.TIR.Repository; 19 import SOFA.SOFAnode.Made.TIR.Access.TIRAccessMethods; 20 import SOFA.SOFAnode.Run.SpecFile; 21 22 27 public class MakeAppl { 28 private static String trImplRoot; 29 30 public static void main(String [] argv) { 31 if (argv.length < 1) { 32 System.out.println("Too few arguments."); 33 System.out.println(" 1st argument - name of the specfile"); 34 System.exit(1); 35 } 36 37 SpecFile specFile = null; 38 try { 39 specFile = SpecFile.getSpecFileFromFile(argv[0]); 40 } catch (java.io.IOException e) { 41 System.out.println("IOException: "+e.getMessage()); 42 System.exit(1); 43 } catch (javax.xml.parsers.ParserConfigurationException e) { 44 System.out.println("ParserConfigurationException: "+e.getMessage() ); 45 System.exit(1); 46 } catch (org.xml.sax.SAXException e) { 47 System.out.println("SAXException: "+e.getMessage() ); 48 System.exit(1); 49 } 50 51 Element specFileElement = specFile.getRootElement(); 52 53 String trDir = System.getProperty("sofa.tr.dir", null); 54 if (trDir == null) { 55 System.out.println("Specify TR directrory (java property \"sofa.tr.dir\")"); 56 System.exit(1); 57 } 58 59 trImplRoot = trDir + File.separator + "impl" + File.separator; 60 61 62 Repository tir = null; 63 try { 64 tir = TIRAccessMethods.getRepository(); 65 } catch (java.net.MalformedURLException e) { 66 System.out.println("MalformedURLException: "+e.getMessage() ); 67 System.exit(1); 68 } catch (java.rmi.NotBoundException e) { 69 System.out.println("NotBoundException: "+e.getMessage() ); 70 System.exit(1); 71 } catch (java.rmi.RemoteException e) { 72 System.out.println("RemoteException: "+e.getMessage() ); 73 System.exit(1); 74 } 75 76 processSpecFileElement(tir, null, specFileElement); 77 78 try { 79 TransformerFactory tFactory = TransformerFactory.newInstance(); 80 Transformer transformer = tFactory.newTransformer(); 81 transformer.setOutputProperty("indent", "yes"); 82 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "5"); 83 DOMSource source = new DOMSource (specFileElement); 84 FileOutputStream out = new FileOutputStream ( trImplRoot + fullNameToFileName(SpecFile.getFullApplName(specFileElement), "run.dc")); 85 StreamResult result = new StreamResult (out); 86 transformer.transform(source, result); 87 out.close(); 88 } catch (TransformerConfigurationException e) { 89 System.out.println("TransformerConfigurationException: "+e.getMessage() ); 90 System.exit(1); 91 } catch (TransformerException e) { 92 System.out.println("TransformerException: "+e.getMessage() ); 93 System.exit(1); 94 } catch (java.io.IOException e) { 95 System.out.println("IOException: "+e.getMessage()); 96 System.exit(1); 97 } 98 99 } 100 101 private static void processSpecFileElement(Repository tir, SOFA.SOFAnode.Run.GenSpecFile.InfoArray riarray, Element specFileElem) { 102 Document specDoc = specFileElem.getOwnerDocument(); 103 String fullName = SpecFile.getFullApplName(specFileElem); 104 String archName = SpecFile.getFullArchName(specFileElem); 106 String inst = specFileElem.getAttribute("name"); 107 SOFA.SOFAnode.Run.GenSpecFile.InfoArray iarray = SOFA.SOFAnode.Run.GenSpecFile.getBindConnInfo(tir, archName); 108 Element archRootElement = null; 109 try { 110 archRootElement = InstallToTR.getDDElem(trImplRoot + fullNameToFileName(fullName, "index.dc") ); 111 } catch (java.io.IOException e) { 112 System.out.println("IOException: "+e.getMessage()); 113 System.exit(1); 114 } catch (javax.xml.parsers.ParserConfigurationException e) { 115 System.out.println("ParserConfigurationException: "+e.getMessage() ); 116 System.exit(1); 117 } catch (org.xml.sax.SAXException e) { 118 System.out.println("SAXException: "+e.getMessage() ); 119 System.exit(1); 120 } 121 java.util.ArrayList cons = getAllElements(specFileElem, "connector"); 122 for (int i=0; i<cons.size(); i++) { 123 processCon(riarray, inst, (Element ) cons.get(i)); 124 removeTextNodes((Element ) cons.get(i)); 125 } 126 String version = getElement(specFileElem, "version").getFirstChild().getNodeValue(); 127 Node specFileNextVerNode = getElement(specFileElem, "version").getNextSibling(); 128 Element cdlEnts = getElement(archRootElement, "cdl_entities"); 129 removeTextNodes(cdlEnts); 130 java.util.ArrayList impls = getAllElements(archRootElement, "implementation"); 131 specFileElem.insertBefore(specDoc.importNode(cdlEnts, true), specFileNextVerNode); 132 for (int i=0; i<impls.size(); i++) { 133 Element implEl = (Element ) specDoc.importNode((Element ) impls.get(i), true); 134 142 specFileElem.insertBefore(implEl, specFileNextVerNode); 143 } 144 java.util.ArrayList comps = getAllElements(specFileElem, "sofa_component"); 145 for (int i=0; i<comps.size(); i++) { 146 processSpecFileElement(tir, iarray, (Element ) comps.get(i)); 147 } 148 java.util.ArrayList units = getAllElements(specFileElem, "unit"); 149 for (int i=0; i<units.size(); i++) { 150 comps = getAllElements((Element ) units.get(i) , "sofa_component"); 151 for (int j=0; j<comps.size(); j++) { 152 processSpecFileElement(tir, iarray, (Element ) comps.get(j)); 153 } 154 } 155 } 156 157 private static Element addApplName(Element element) { 158 NodeList nl = element.getChildNodes(); 159 for (int i=0; i<nl.getLength(); i++) { 160 Node n = nl.item(i); 161 if ((n.getNodeType()==Node.ELEMENT_NODE) && (n.getNodeName().compareTo("architecture_ref")==0)) { 162 String ar = n.getFirstChild().getNodeValue(); 163 ar = ar.substring(ar.lastIndexOf(':')+1, ar.indexOf('?')); 164 element.setAttribute("name", ar); 165 } 166 } 167 return element; 168 } 169 170 private static String fullNameToFileName(String clname, String fname) { 171 StringBuffer sb = new StringBuffer (); 173 boolean s = true; 174 for (int i=0; i<clname.length(); i++) { 175 if (clname.charAt(i)==':') { 176 if (s) { 177 sb.append(":"); 178 s = false; 179 } 180 } else { 181 s = true; 182 sb.append(clname.charAt(i)); 183 } 184 } 185 clname = sb.toString().replace(':', File.separatorChar); 186 clname = clname.replace('[', File.separatorChar); 187 clname = clname.replace(']', File.separatorChar); 188 clname = clname + fname; 189 return clname; 190 } 191 192 private static Element getElement(Element element, String name) { 193 NodeList nl = element.getChildNodes(); 194 for (int i=0; i<nl.getLength(); i++) { 195 Node n = nl.item(i); 196 if ((n.getNodeType()==Node.ELEMENT_NODE) && (n.getNodeName().compareTo(name)==0)) { 197 return (Element ) n; 198 } 199 } 200 return null; 201 } 202 203 private static java.util.ArrayList getAllElements(Element element, String name) { 204 java.util.ArrayList ret = new java.util.ArrayList (); 205 NodeList nl = element.getChildNodes(); 206 for (int i=0; i<nl.getLength(); i++) { 207 Node n = nl.item(i); 208 if ((n.getNodeType()==Node.ELEMENT_NODE) && (n.getNodeName().compareTo(name)==0)) { 209 ret.add(n); 210 } 211 } 212 return ret; 213 } 214 215 private static final int CSR = 0; 216 private static final int CSP = 1; 217 private static final int EPR = 2; 218 private static final int EPP = 3; 219 private static final int DSR = 4; 220 private static final int DSP = 5; 221 222 private static void processCon(SOFA.SOFAnode.Run.GenSpecFile.InfoArray riarray, String inst, Element conEl) { 223 Document doc = conEl.getOwnerDocument(); 224 String connName = conEl.getAttribute("name"); 225 String connFrame = conEl.getAttribute("frame"); 226 228 int ckind = 0; 229 if (connName.startsWith("_delegate_")) { 230 conEl.removeAttribute("frame"); 231 conEl.setAttribute("impl", "SGenerator:CSProcCallImpl"); 232 conEl.setAttribute("unit", "Client"); 233 ckind = CSR; 234 } else if (connName.startsWith("_subsume_")) { 235 conEl.removeAttribute("frame"); 236 conEl.setAttribute("impl", "SGenerator:CSProcCallImpl"); 237 conEl.setAttribute("unit", "Server"); 238 ckind = CSP; 239 } else if (connFrame.compareTo("SGenerator:CSProcCallImpl")==0) { 240 conEl.removeAttribute("frame"); 241 conEl.setAttribute("impl", "SGenerator:CSProcCallImpl"); 242 SOFA.SOFAnode.Run.GenSpecFile.BindConnInfo bi = riarray.getInfo(inst, connName); 243 if (bi == null) { 244 System.out.println("Warning: Mismatch in TR and TIR. Cannot find connector for "+inst+":"+connName+"."); 245 System.out.println(" You will have to correct the resulting run.dc file by yourself."); 246 conEl.setAttribute("unit", "!!! set unit here !!!"); 248 } else { 249 if (bi.type == SOFA.SOFAnode.Run.GenSpecFile.BindConnInfo.PROV) { 250 conEl.setAttribute("unit", "Server"); 251 ckind = CSP; 252 } else { 253 conEl.setAttribute("unit", "Client"); 254 ckind = CSR; 255 } 256 } 257 } else if (connFrame.compareTo("SGenerator:EventPassingImpl")==0) { 258 conEl.removeAttribute("frame"); 259 conEl.setAttribute("impl", "SGenerator:EventPassingImpl"); 260 SOFA.SOFAnode.Run.GenSpecFile.BindConnInfo bi = riarray.getInfo(inst, connName); 261 if (bi == null) { 262 System.out.println("Warning: Mismatch in TR and TIR. Cannot find connector for "+inst+":"+connName+"."); 263 System.out.println(" You will have to correct the resulting run.dc file by yourself."); 264 conEl.setAttribute("unit", "!!! set unit here !!!"); 266 } else { 267 if (bi.type == SOFA.SOFAnode.Run.GenSpecFile.BindConnInfo.PROV) { 268 conEl.setAttribute("unit", "Consumer"); 269 ckind = EPP; 270 } else { 271 conEl.setAttribute("unit", "Supplier"); 272 ckind = EPR; 273 } 274 } 275 } else if (connFrame.compareTo("SGenerator:DataStreamImpl")==0) { 276 conEl.removeAttribute("frame"); 277 conEl.setAttribute("impl", "SGenerator:DataStreamImpl"); 278 SOFA.SOFAnode.Run.GenSpecFile.BindConnInfo bi = riarray.getInfo(inst, connName); 279 if (bi == null) { 280 System.out.println("Warning: Mismatch in TR and TIR. Cannot find connector for "+inst+":"+connName+"."); 281 System.out.println(" You will have to correct the resulting run.dc file by yourself."); 282 conEl.setAttribute("unit", "!!! set unit here !!!"); 284 } else { 285 if (bi.type == SOFA.SOFAnode.Run.GenSpecFile.BindConnInfo.PROV) { 286 conEl.setAttribute("unit", "Receiver"); 287 ckind = DSP; 288 conEl.setAttribute("requires","::SOFA::Connector::EEG::Types::DataStream::OutputStream?nenya.ms.mff.cuni.cz!0"); 289 conEl.setAttribute("provides","::SOFA::Connector::EEG::Types::DataStream::InputStream?nenya.ms.mff.cuni.cz!0"); 290 } else { 291 conEl.setAttribute("unit", "Sender"); 292 ckind = DSR; 293 conEl.setAttribute("provides","::SOFA::Connector::EEG::Types::DataStream::OutputStream?nenya.ms.mff.cuni.cz!0"); 294 conEl.setAttribute("requires","::SOFA::Connector::EEG::Types::DataStream::InputStream?nenya.ms.mff.cuni.cz!0"); 295 } 296 } 297 298 } else { 299 System.out.println("Unknown connector frame: "+connFrame); 300 System.exit(1); 301 } 302 Element newEl = null; 304 NodeList nl = conEl.getChildNodes(); 305 for (int i=0; i<nl.getLength(); i++) { 306 Node n = nl.item(i); 307 if (n.getNodeType()==Node.ELEMENT_NODE) { 308 if (n.getNodeName().compareTo("transport")==0) { 309 String type = ((Element )n).getAttribute("type"); 310 switch (ckind) { 311 case CSP: 312 conEl.removeChild(n); 313 conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "sRole", "impl", "EEM:SRoleImpl")); 314 if (type.compareTo("LOCAL")==0) { 315 conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "skeleton", "impl", "EEM:LocalSkeleton")); 316 } else if (type.compareTo("RMI")==0) { 317 conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "skeleton", "impl", "EEM:RMISkeleton")); 318 } else if (type.compareTo("CORBA")==0) { 319 conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "skeleton", "impl", "EEM:CORBASkeleton")); 320 } else { 321 System.out.println("Unknown transport type" + type); 322 System.exit(1); 323 } 324 break; 325 case CSR: 326 conEl.removeChild(n); 327 conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "cRole", "impl", "EEM:CRoleImpl")); 328 if (type.compareTo("LOCAL")==0) { 329 conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "stub", "impl", "EEM:LocalStub")); 330 } else if (type.compareTo("RMI")==0) { 331 conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "stub", "impl", "EEM:RMIStub")); 332 } else if (type.compareTo("CORBA")==0) { 333 conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "stub", "impl", "EEM:CORBAStub")); 334 } else { 335 System.out.println("Unknown transport type" + type); 336 System.exit(1); 337 } 338 break; 339 case EPR: 340 conEl.removeChild(n); 341 conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "sRole", "impl", "EEM:CRoleImpl")); 342 conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "distributor", "impl", "EEM:DistributorImpl")); 343 if (type.compareTo("LOCAL")==0) { 344 conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "stub", "impl", "EEM:LocalStub")); 345 } else if (type.compareTo("RMI")==0) { 346 conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "stub", "impl", "EEM:RMIStub")); 347 } else if (type.compareTo("CORBA")==0) { 348 conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "stub", "impl", "EEM:CORBAStub")); 349 } else { 350 System.out.println("Unknown transport type" + type); 351 System.exit(1); 352 } 353 break; 354 case EPP: 355 conEl.removeChild(n); 356 conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "cRole", "impl", "EEM:SRoleImpl")); 357 if (type.compareTo("LOCAL")==0) { 358 conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "skeleton", "impl", "EEM:LocalSkeleton")); 359 } else if (type.compareTo("RMI")==0) { 360 conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "skeleton", "impl", "EEM:RMISkeleton")); 361 } else if (type.compareTo("CORBA")==0) { 362 conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "skeleton", "impl", "EEM:CORBASkeleton")); 363 } else { 364 System.out.println("Unknown transport type" + type); 365 System.exit(1); 366 } 367 break; 368 case DSP: 369 conEl.removeChild(n); 370 conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "pushRRole", "impl", "EEM:SRoleImpl")); 371 if (type.compareTo("LOCAL")==0) { 372 conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "receiver", "impl", "EEM:DataStreamLocalReceiver")); 373 conEl.appendChild(newElemWithTwoAttribs(doc, "property", "name", "senderMode", "value", "push")); 374 conEl.appendChild(newElemWithTwoAttribs(doc, "property", "name", "receiverMode", "value", "push")); 375 } else { 376 System.out.println("DataStream can be only LOCAL."); 377 System.exit(1); 378 } 379 break; 380 case DSR: 381 conEl.removeChild(n); 382 conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "pushSRole", "impl", "EEM:CRoleImpl")); 383 if (type.compareTo("LOCAL")==0) { 384 conEl.appendChild(newElemWithTwoAttribs(doc, "element", "name", "sender", "impl", "EEM:DataStreamLocalSender")); 385 conEl.appendChild(newElemWithTwoAttribs(doc, "property", "name", "senderMode", "value", "push")); 386 conEl.appendChild(newElemWithTwoAttribs(doc, "property", "name", "receiverMode", "value", "push")); 387 } else { 388 System.out.println("DataStream can be only LOCAL."); 389 System.exit(1); 390 } 391 break; 392 } 393 } 394 } 395 } 396 397 } 398 399 private static Element newElemWithTwoAttribs(Document doc, String elName, String name1, String value1, String name2, String value2) { 400 Element newEl = doc.createElement(elName); 401 newEl.setAttribute(name1, value1); 402 newEl.setAttribute(name2, value2); 403 return newEl; 404 } 405 406 private static void removeTextNodes(Element el) { 407 NodeList nl = el.getChildNodes(); 408 for (int i=0; i<nl.getLength(); i++) { 409 Node n = nl.item(i); 410 if (n.getNodeType() == Node.TEXT_NODE) { 411 el.removeChild(n); 412 } 413 } 414 } 415 } 416 | Popular Tags |