1 2 package SOFA.SOFAnode.Run; 3 import java.util.Iterator ; 4 5 import javax.xml.transform.TransformerConfigurationException ; 6 import javax.xml.transform.TransformerException ; 7 8 import org.w3c.dom.Document ; 9 import org.w3c.dom.Element ; 10 import org.w3c.dom.Node ; 11 import org.w3c.dom.NodeList ; 12 13 import SOFA.SOFAnode.Made.TIR.ArchitectureDef; 14 import SOFA.SOFAnode.Made.TIR.ArrayDef; 15 import SOFA.SOFAnode.Made.TIR.BindDef; 16 import SOFA.SOFAnode.Made.TIR.BindMode; 17 import SOFA.SOFAnode.Made.TIR.BindOperKind; 18 import SOFA.SOFAnode.Made.TIR.BindOperSub; 19 import SOFA.SOFAnode.Made.TIR.BindType; 20 import SOFA.SOFAnode.Made.TIR.BindTypeArray; 21 import SOFA.SOFAnode.Made.TIR.BindTypeKind; 22 import SOFA.SOFAnode.Made.TIR.BindTypeNormal; 23 import SOFA.SOFAnode.Made.TIR.Contained; 24 import SOFA.SOFAnode.Made.TIR.CDLType; 25 import SOFA.SOFAnode.Made.TIR.DefinitionKind; 26 import SOFA.SOFAnode.Made.TIR.FrameDef; 27 import SOFA.SOFAnode.Made.TIR.InstDef; 28 import SOFA.SOFAnode.Made.TIR.ProvideDef; 29 import SOFA.SOFAnode.Made.TIR.Repository; 30 import SOFA.SOFAnode.Made.TIR.RequireDef; 31 import SOFA.SOFAnode.Made.TIR.TIRExceptLock; 32 import SOFA.SOFAnode.Made.TIR.TypedefDef; 33 import SOFA.SOFAnode.Made.TIR.Access.TIRAccessMethods; 34 35 39 public class GenSpecFile { 40 41 46 public static void main(String [] argv) { 47 if (argv.length < 2) { 48 System.out.println("Too few arguments."); 49 System.out.println(" 1st argument - full application name"); 50 System.out.println(" 2nd argument - name of the generated specfile"); 51 System.exit(1); 52 } 53 54 if (!argv[0].matches("^.+\\[.+\\]$")) { 55 System.out.println("Bad format of application name."); 56 System.exit(1); 57 } 58 59 Document depldesc = null; 60 try { 61 depldesc = TRAccess.getDeplDescrXMLLocal(argv[0], "index.dc"); 62 } catch (java.net.MalformedURLException e) { 63 System.out.println("MalformedURLException: "+e.getMessage()); 64 e.printStackTrace(); 65 System.exit(1); 66 } catch (java.io.IOException e) { 67 System.out.println("IOException: "+e.getMessage()); 68 System.exit(1); 69 } catch (javax.xml.parsers.ParserConfigurationException e) { 70 System.out.println("ParserConfigurationExceptioni: "+e.getMessage() ); 71 System.exit(1); 72 } catch (org.xml.sax.SAXException e) { 73 System.out.println("SAXException: "+e.getMessage() ); 74 System.exit(1); 75 } 76 77 Element applRootElement = depldesc.getDocumentElement(); 78 applRootElement.normalize(); 79 80 if (applRootElement.getTagName().compareTo("sofa_system") != 0) { 81 System.out.println(argv[0]+" is not system component."); 82 System.exit(1); 83 } 84 85 Repository tir = null; 86 try { 87 tir = TIRAccessMethods.getRepository(); 88 } catch (java.net.MalformedURLException e) { 89 System.out.println("MalformedURLException: "+e.getMessage() ); 90 System.exit(1); 91 } catch (java.rmi.NotBoundException e) { 92 System.out.println("NotBoundException: "+e.getMessage() ); 93 System.exit(1); 94 } catch (java.rmi.RemoteException e) { 95 System.out.println("RemoteException: "+e.getMessage() ); 96 System.exit(1); 97 } 98 99 System.out.print("Generating specfile..."); 100 101 applRootElement = processElement(tir, null, null, applRootElement); 102 applRootElement = addApplName(applRootElement); 103 104 105 SpecFile sf = SpecFile.getSpecFileFromDeplDescriptorXMLDoc(applRootElement); 106 try { 107 sf.toFile(argv[1]+".ssf"); 108 } catch (TransformerConfigurationException e) { 109 System.out.println("TransformerConfigurationException: "+e.getMessage() ); 110 System.exit(1); 111 } catch (TransformerException e) { 112 System.out.println("TransformerException: "+e.getMessage() ); 113 System.exit(1); 114 } catch (java.io.IOException e) { 115 System.out.println("IOException: "+e.getMessage()); 116 System.exit(1); 117 } 118 119 System.out.println("OK"); 120 } 121 122 private static Element processElement(Repository tir, String inst, InfoArray riarray, Element element) { 123 String archName = null; 124 InfoArray iarray = null; 125 NodeList nl = element.getChildNodes(); 126 for (int i=0; i<nl.getLength(); i++) { 127 Node n = nl.item(i); 128 if ((n.getNodeType()==Node.ELEMENT_NODE) && (n.getNodeName().compareTo("component_ref")==0)) { 129 String instName = ((Element )n).getAttribute("inst"); 130 String name = ((Element )n).getAttribute("arch"); 131 String version = ((Element )n).getAttribute("version"); 132 Element archRootElement = null; 133 try { 134 Document archDoc = TRAccess.getDeplDescrXMLLocal(name+"["+version+"]", "index.dc"); 135 archRootElement = archDoc.getDocumentElement(); 136 archRootElement.normalize(); 137 } catch (java.net.MalformedURLException e) { 138 System.out.println("MalformedURLException: "+e.getMessage()); 139 e.printStackTrace(); 140 System.exit(1); 141 } catch (java.io.IOException e) { 142 System.out.println("IOException: "+e.getMessage()); 143 System.exit(1); 144 } catch (javax.xml.parsers.ParserConfigurationException e) { 145 System.out.println("ParserConfigurationException: "+e.getMessage() ); 146 System.exit(1); 147 } catch (org.xml.sax.SAXException e) { 148 System.out.println("SAXException: "+e.getMessage() ); 149 System.exit(1); 150 } 151 archRootElement.setAttribute("name", instName); 152 archRootElement = processElement(tir, instName, iarray, archRootElement); 153 element.removeChild(n); 154 element.appendChild(element.getOwnerDocument().importNode(archRootElement, true)); 155 } else if ((n.getNodeType()==Node.ELEMENT_NODE) && (n.getNodeName().compareTo("architecture_ref")==0)) { 156 archName = n.getFirstChild().getNodeValue(); 157 iarray = getBindConnInfo(tir, archName); 158 } else if ((n.getNodeType()==Node.ELEMENT_NODE) && (n.getNodeName().compareTo("connector")==0)) { 159 String connName = ((Element )n).getAttribute("name"); 160 if (connName.startsWith("_delegate_") || connName.startsWith("_subsume_")) { 161 ((Element ) n).setAttribute("frame", "SGenerator:CSProcCallImpl"); 162 } else { 163 String usingConn = riarray.getConn(inst, connName); 164 if (usingConn == null) { 165 ((Element ) n).setAttribute("frame", "SGenerator:!Add connector type here!Impl"); 166 } else { 167 ((Element ) n).setAttribute("frame", "SGenerator:"+usingConn+"Impl"); 168 } 169 } 170 Element newEl = n.getOwnerDocument().createElement("transport"); 171 newEl.setAttribute("type", "LOCAL"); 172 n.appendChild(newEl); 173 } 174 } 175 176 return element; 177 } 178 179 private static Element addApplName(Element element) { 180 NodeList nl = element.getChildNodes(); 181 for (int i=0; i<nl.getLength(); i++) { 182 Node n = nl.item(i); 183 if ((n.getNodeType()==Node.ELEMENT_NODE) && (n.getNodeName().compareTo("architecture_ref")==0)) { 184 String ar = n.getFirstChild().getNodeValue(); 185 ar = ar.substring(ar.lastIndexOf(':')+1, ar.indexOf('?')); 186 element.setAttribute("name", ar); 187 } 188 } 189 return element; 190 } 191 192 public static class BindConnInfo { 193 public String inst; 194 public String prname; 195 public String con; 196 public int type; 197 public static final int PROV = 0; 198 public static final int REQ = 1; 199 public BindConnInfo(String i, String pr, String c, int t) { 200 inst = i; 201 prname = pr; 202 con = c; 203 type = t; 204 } 205 } 206 207 public static class InfoArray extends java.util.ArrayList { 208 public void addInfo(String inst, String pr, String using, int type) { 209 add(new BindConnInfo(inst, pr, using, type)); 210 } 211 212 public void replaceInfoUsingField(String inst, String pr, String using, int type) { 213 Iterator iter=iterator(); 214 215 while (iter.hasNext()) { 216 BindConnInfo iterElement; 217 iterElement = (BindConnInfo)iter.next(); 218 219 if (iterElement.inst.equals(inst) && iterElement.prname.equals(pr) && iterElement.type==type) { 220 iterElement.con=using; 221 break; 222 } 223 } 224 } 225 226 public BindConnInfo getInfo(String inst, String pr) { 227 BindConnInfo bi = null; 228 for (int i=0; i<size(); i++) { 229 bi = (BindConnInfo) get(i); 230 if ((bi.inst.compareTo(inst) == 0) && (bi.prname.compareTo(pr) == 0)) { 231 return bi; 232 } 233 } 234 return null; 235 } 236 237 public String getConn(String inst, String pr) { 238 BindConnInfo i = getInfo(inst, pr); 239 if (i != null) 240 return i.con; 241 else 242 return null; 243 } 244 } 245 246 public static InfoArray getBindConnInfo(Repository rep, String archFullName) { 247 try { 248 ArchitectureDef arch = (ArchitectureDef) TIRAccessMethods.lookupCDLContained(rep, archFullName); 249 if (arch != null) { 250 InfoArray iarray = new InfoArray(); 251 252 Contained[] componentInstances=arch.contents(rep.get_spec_def_kind(DefinitionKind.dk_Inst)); 253 for (int componentInstanceIdx=0; componentInstanceIdx<componentInstances.length; componentInstanceIdx++) { 254 InstDef componentInstance=(InstDef) componentInstances[componentInstanceIdx]; 255 256 CDLType instanceType=componentInstance.type(); 259 int instanceTypeDefKind; 260 FrameDef instanceFrame=null; 261 String instanceTypeDefKindChain=null; 262 263 while (true) { 264 instanceTypeDefKind=instanceType.get_def_kind().value(); 265 instanceTypeDefKindChain=(instanceTypeDefKindChain==null ? "" : instanceTypeDefKindChain + "-") + instanceTypeDefKind; 266 267 if (instanceTypeDefKind==DefinitionKind.dk_Frame) { instanceFrame=(FrameDef)instanceType; 269 break; 270 271 } else if (instanceTypeDefKind==DefinitionKind.dk_Array) { instanceType=((ArrayDef)instanceType).element_type(); 273 274 } else if (instanceTypeDefKind==DefinitionKind.dk_Typedef) { instanceType=((TypedefDef)instanceType).original_type(); 276 277 } else { 278 System.out.println("Unsupported instance type: "+instanceTypeDefKindChain); 279 System.exit(1); 280 } 281 } 282 283 Contained[] frameProvides=instanceFrame.contents(rep.get_spec_def_kind(DefinitionKind.dk_Provides)); 284 for (int frameProvideIdx=0; frameProvideIdx<frameProvides.length; frameProvideIdx++) { 285 ProvideDef frameProvide=(ProvideDef) frameProvides[frameProvideIdx]; 286 iarray.addInfo(componentInstance.get_identification().name(), frameProvide.get_identification().name(), null, BindConnInfo.PROV); 287 } 288 289 Contained[] frameRequires=instanceFrame.contents(rep.get_spec_def_kind(DefinitionKind.dk_Requires)); 290 for (int frameRequireIdx=0; frameRequireIdx<frameRequires.length; frameRequireIdx++) { 291 RequireDef frameRequire=(RequireDef) frameRequires[frameRequireIdx]; 292 iarray.addInfo(componentInstance.get_identification().name(), frameRequire.get_identification().name(), null, BindConnInfo.REQ); 293 } 294 } 295 296 Contained[] binds = arch.contents(rep.get_spec_def_kind(DefinitionKind.dk_Bind)); 297 for (int i=0; i<binds.length; i++) { 298 BindDef bind = (BindDef) binds[i]; 299 String using = bind.using(); 300 switch (bind.mode().value()) { 301 case BindMode.DELEGATE: 302 { 303 BindOperSub rhs = (BindOperSub) bind.rhsop(); 304 BindType[] relems = rhs.elements(); 305 BindType[] rsubs = rhs.subs(); 306 String rcompname; 307 if (rsubs[0].get_bt_kind().value() == BindTypeKind.btk_normal) { 308 rcompname = ((BindTypeNormal) rsubs[0]).name(); 309 } else { 310 rcompname = ((BindTypeArray) rsubs[0]).name(); 311 } 312 String relemname; 313 if (relems[0].get_bt_kind().value() == BindTypeKind.btk_normal) { 314 relemname = ((BindTypeNormal) relems[0]).name(); 315 } else { 316 relemname = ((BindTypeArray) relems[0]).name(); 317 } 318 iarray.replaceInfoUsingField(rcompname, relemname, using, BindConnInfo.PROV); 319 } 320 break; 321 case BindMode.SUBSUME: 322 { 323 BindOperSub lhs = (BindOperSub) bind.lhsop(); 324 BindType[] lelems = lhs.elements(); 325 BindType[] lsubs = lhs.subs(); 326 String lcompname; 327 if (lsubs[0].get_bt_kind().value() == BindTypeKind.btk_normal) { 328 lcompname = ((BindTypeNormal) lsubs[0]).name(); 329 } else { 330 lcompname = ((BindTypeArray) lsubs[0]).name(); 331 } 332 String lelemname; 333 if (lelems[0].get_bt_kind().value() == BindTypeKind.btk_normal) { 334 lelemname = ((BindTypeNormal) lelems[0]).name(); 335 } else { 336 lelemname = ((BindTypeArray) lelems[0]).name(); 337 } 338 iarray.replaceInfoUsingField(lcompname, lelemname, using, BindConnInfo.REQ); 339 } 340 break; 341 case BindMode.BIND: 342 if (bind.lhsop().get_bo_kind().value() == BindOperKind.bok_provreq) { 343 ; 344 } else { 345 BindOperSub lhs = (BindOperSub) bind.lhsop(); 346 BindOperSub rhs = (BindOperSub) bind.rhsop(); 347 BindType[] lelems = lhs.elements(); 348 BindType[] lsubs = lhs.subs(); 349 BindType[] relems = rhs.elements(); 350 BindType[] rsubs = rhs.subs(); 351 String lcompname; 352 String rcompname; 353 if (lsubs[0].get_bt_kind().value() == BindTypeKind.btk_normal) { 354 lcompname = ((BindTypeNormal) lsubs[0]).name(); 355 } else { 356 lcompname = ((BindTypeArray) lsubs[0]).name(); 357 } 358 if (rsubs[0].get_bt_kind().value() == BindTypeKind.btk_normal) { 359 rcompname = ((BindTypeNormal) rsubs[0]).name(); 360 } else { 361 rcompname = ((BindTypeArray) rsubs[0]).name(); 362 } 363 String lelemname; 364 String relemname; 365 if (lelems[0].get_bt_kind().value() == BindTypeKind.btk_normal) { 366 lelemname = ((BindTypeNormal) lelems[0]).name(); 367 } else { 368 lelemname = ((BindTypeArray) lelems[0]).name(); 369 } 370 if (relems[0].get_bt_kind().value() == BindTypeKind.btk_normal) { 371 relemname = ((BindTypeNormal) relems[0]).name(); 372 } else { 373 relemname = ((BindTypeArray) relems[0]).name(); 374 } 375 iarray.replaceInfoUsingField(lcompname, lelemname, using, BindConnInfo.REQ); 376 iarray.replaceInfoUsingField(rcompname, relemname, using, BindConnInfo.PROV); 377 } 378 break; 379 } 380 } 381 return iarray; 382 } else { 383 return null; 384 } 385 } catch (java.rmi.RemoteException e) { 386 System.out.println("RemoteException: "+e.getMessage()); 387 e.printStackTrace(); 388 System.exit(1); 389 } catch (TIRExceptLock e) { 390 System.out.println("TIRExceptLock: "+e.getMessage()); 391 e.printStackTrace(); 392 System.exit(1); 393 } 394 return null; 395 } 396 397 } 398 | Popular Tags |