1 28 29 30 package org.objectweb.ccm.runtime; 31 32 import org.objectweb.corba.runtime.*; 33 34 37 public class CCDParser 38 { 39 static final private String _class_name = "CCDParser"; 41 private org.w3c.dom.Document _doc; 42 private java.util.HashMap _repid2sname; 43 private java.util.HashMap _sid2uuid; 44 private ExtComponentInstallationImpl _extci; 45 46 public String home_repid; 49 public String component_repid; 50 public java.util.HashMap sname2policies; 51 52 public 54 CCDParser(org.w3c.dom.Document doc, 55 ExtComponentInstallationImpl extci) 56 { 57 _doc = doc; 59 _repid2sname = new java.util.HashMap (); 60 _sid2uuid = new java.util.HashMap (); 61 _extci = extci; 62 63 home_repid = null; 65 component_repid = null; 66 sname2policies = new java.util.HashMap (); 67 } 68 69 73 static private org.w3c.dom.Document 74 openAsDocument(String fname, String dtdloc) 75 { 76 final String opname = "openAsDocument"; 77 org.xml.sax.InputSource isource = null; 79 try { 80 isource = new org.xml.sax.InputSource (new java.io.FileInputStream (fname)); 81 } catch (java.io.FileNotFoundException ex) { 82 final String msg = "FAILED (file not found: "+fname+")"; 83 TheLogger.error(_class_name, opname, msg, ex); 84 return null; 85 } 86 87 String loc = isource.getSystemId(); 89 isource.setSystemId(dtdloc+"/"+loc); 90 91 javax.xml.parsers.DocumentBuilderFactory fact = javax.xml.parsers.DocumentBuilderFactory.newInstance(); 94 fact.setNamespaceAware(false); 95 fact.setValidating(false); 96 97 try { 99 javax.xml.parsers.DocumentBuilder parser = fact.newDocumentBuilder(); 100 return parser.parse(isource); 101 } catch (Exception ex) { 102 TheLogger.error(_class_name, opname, "FAILED", ex); 103 return null; 104 } 105 } 106 107 final private void 108 parseDocument() 109 { 110 org.w3c.dom.Element root = _doc.getDocumentElement(); 111 org.w3c.dom.NodeList childs = root.getChildNodes(); 112 org.w3c.dom.Node node = null; 113 for (int i=0;i<childs.getLength();i++) { 114 node = childs.item(i); 115 switch (node.getNodeType()) { 116 case org.w3c.dom.Node.ELEMENT_NODE: 117 org.w3c.dom.Element element = (org.w3c.dom.Element )node; 118 String tagname = element.getTagName(); 119 120 if (tagname.equals("usedservices")) { 121 parseUsedServices(element); 123 } else if (tagname.equals("policies")) { 124 parsePolicies(element, ""); 126 } else if (tagname.equals("homefeatures")) { 127 parseHome(element); 129 } else if (tagname.equals("componentfeatures")) { 130 parseComponent(element); 132 } else if (tagname.equals("interface")) { 133 parseInterface(element); 135 } else if (tagname.equals("componentrepid")) { 136 component_repid = element.getAttribute("repid"); 138 } else if (tagname.equals("homerepid")) { 139 home_repid = element.getAttribute("repid"); 141 } 142 143 break; 144 default: 145 break; 146 } 147 } 148 } 149 150 final private void 151 parseUsedServices(org.w3c.dom.Element uservices) 152 { 153 org.w3c.dom.NodeList services = uservices.getElementsByTagName("usedservice"); 154 org.w3c.dom.Element service = null; 155 org.w3c.dom.Element implref = null; 156 String serviceid = null; 157 String uuid = null; 158 159 for (int i=0;i<services.getLength();i++) { 160 service = (org.w3c.dom.Element )services.item(i); 162 serviceid = service.getAttribute("id"); 164 implref = (org.w3c.dom.Element )service.getElementsByTagName("implementationref").item(0); 166 uuid = implref.getAttribute("idref"); 168 169 _sid2uuid.put(serviceid, uuid); 171 } 172 } 173 174 final private void 175 parsePolicies(org.w3c.dom.Element pols, String sname) 176 { 177 final String opname = "parsePolicies"; 178 if (pols==null) { 179 final String msg = "no policies element for target: "+sname; 180 TheLogger.debug(_class_name, opname, msg); 181 return ; 182 } 183 184 TheLogger.debug(_class_name, opname, "scoped name is: "+sname); 185 186 org.w3c.dom.NodeList targets = pols.getElementsByTagName("target"); 187 org.w3c.dom.Element target = null; 188 org.w3c.dom.NodeList servicerefs = null; 189 org.w3c.dom.Element serviceref = null; 190 String targetname = null; 191 192 for (int i=0;i<targets.getLength();i++) { 193 target = (org.w3c.dom.Element )targets.item(i); 194 targetname = target.getAttribute("name"); 196 197 servicerefs = target.getElementsByTagName("serviceref"); 199 200 org.coach.ECM.CCDPolicyRef[] prefs = new org.coach.ECM.CCDPolicyRef[servicerefs.getLength()]; 201 org.coach.ECM.ServiceArchive sarchive = null; 202 String idref = null; 203 String policyref = null; 204 String policyvalue = null; 205 String uuid = null; 206 207 for (int j=0;j<servicerefs.getLength();j++) { 208 serviceref = (org.w3c.dom.Element )servicerefs.item(j); 209 idref = serviceref.getAttribute("idref"); 211 policyref = serviceref.getAttribute("policyref"); 213 policyvalue = serviceref.getAttribute("policyvalue"); 215 216 uuid = (String )_sid2uuid.get(idref); 217 sarchive = _extci.get_service_archive(uuid); 218 219 230 prefs[j] = new org.coach.ECM.CCDPolicyRef(); 231 prefs[j].implementationref = uuid; 232 prefs[j].service_id = sarchive.service_id(); 233 prefs[j].policyref = policyref; 234 prefs[j].policyvalue = policyvalue; 235 } 236 237 sname2policies.put(sname+"::"+targetname, prefs); 239 } 240 } 241 242 final private void 243 parseEventPolicies(org.w3c.dom.Element pols, String sname) 244 { 245 248 final String opname = "parseEventPolicies"; 249 if (pols==null) { 250 final String msg = "no policies element for target: "+sname; 251 TheLogger.debug(_class_name, opname, msg); 252 return ; 253 } 254 255 TheLogger.debug(_class_name, opname, "scoped name is: "+sname); 256 257 org.w3c.dom.NodeList servicerefs = null; 258 org.w3c.dom.Element serviceref = null; 259 260 servicerefs = pols.getElementsByTagName("serviceref"); 262 263 org.coach.ECM.CCDPolicyRef[] prefs = new org.coach.ECM.CCDPolicyRef[servicerefs.getLength()]; 264 org.coach.ECM.ServiceArchive sarchive = null; 265 String idref = null; 266 String policyref = null; 267 String policyvalue = null; 268 String uuid = null; 269 270 for (int j=0;j<servicerefs.getLength();j++) { 271 serviceref = (org.w3c.dom.Element )servicerefs.item(j); 272 idref = serviceref.getAttribute("idref"); 274 policyref = serviceref.getAttribute("policyref"); 276 policyvalue = serviceref.getAttribute("policyvalue"); 278 279 uuid = (String )_sid2uuid.get(idref); 280 sarchive = _extci.get_service_archive(uuid); 281 282 291 prefs[j] = new org.coach.ECM.CCDPolicyRef(); 292 prefs[j].implementationref = uuid; 293 prefs[j].service_id = sarchive.service_id(); 294 prefs[j].policyref = policyref; 295 prefs[j].policyvalue = policyvalue; 296 } 297 298 sname2policies.put(sname, prefs); 300 } 301 302 final private void 303 parseHome(org.w3c.dom.Element home) 304 { 305 String name = home.getAttribute("name"); 307 String repid = home.getAttribute("repid"); 309 _repid2sname.put(repid, "::"+name); 311 312 org.w3c.dom.Element pols = (org.w3c.dom.Element )home.getElementsByTagName("policies").item(0); 315 parsePolicies(pols, "::"+name); 316 317 } 320 321 final private void 322 parseComponent(org.w3c.dom.Element component) 323 { 324 String name = component.getAttribute("name"); 326 String repid = component.getAttribute("repid"); 328 _repid2sname.put(repid, "::"+name); 330 331 org.w3c.dom.NodeList childs = component.getChildNodes(); 338 for (int i=0;i<childs.getLength();i++) { 339 if (childs.item(i).getNodeName().equals("policies")) { 340 org.w3c.dom.Element pols = (org.w3c.dom.Element )childs.item(i); 341 parsePolicies(pols, "::"+name); 342 } 343 } 344 345 348 351 parsePort(component, "::"+name, "uses", "usesname"); 354 parsePort(component, "::"+name, "provides", "providesname"); 356 parseEventPort(component, "::"+name, "emits", "emitsname"); 358 parseEventPort(component, "::"+name, "publishes", "publishesname"); 359 parseEventPort(component, "::"+name, "consumes", "consumesname"); 361 } 362 363 final private void 364 parsePort(org.w3c.dom.Element component, String cname, 365 String ptype, String pattr) 366 { 367 org.w3c.dom.NodeList ports = null; 368 org.w3c.dom.Element port = null; 369 org.w3c.dom.Element pols = null; 370 String pname = null; 371 String prepid = null; 372 ports = component.getElementsByTagName(ptype); 374 for (int i=0;i<ports.getLength();i++) { 375 port = (org.w3c.dom.Element )ports.item(i); 376 pname = port.getAttribute(pattr); 378 prepid = port.getAttribute("repid"); 380 383 pols = (org.w3c.dom.Element )port.getElementsByTagName("policies").item(0); 385 parsePolicies(pols, cname+"::"+pname); 386 } 387 } 388 389 final private void 390 parseEventPort(org.w3c.dom.Element component, String cname, 391 String ptype, String pattr) 392 { 393 org.w3c.dom.NodeList ports = null; 394 org.w3c.dom.Element port = null; 395 org.w3c.dom.Element pols = null; 396 String pname = null; 397 String pevt = null; 398 ports = component.getElementsByTagName(ptype); 400 for (int i=0;i<ports.getLength();i++) { 401 port = (org.w3c.dom.Element )ports.item(i); 402 pname = port.getAttribute(pattr); 404 pevt = port.getAttribute("eventtype"); 406 409 pols = (org.w3c.dom.Element )port.getElementsByTagName("policies").item(0); 413 parseEventPolicies(pols, cname+"::"+pname); 414 } 415 } 416 417 final private void 418 parseInterface(org.w3c.dom.Element itf) 419 { 420 } 422 423 427 static public CCDParser 428 parse(String fname, String dtdloc, ExtComponentInstallationImpl extci) 429 { 430 CCDParser parser = null; 431 try { 432 org.w3c.dom.Document doc = openAsDocument(fname, dtdloc); 433 parser = new CCDParser(doc, extci); 434 parser.parseDocument(); 435 } catch (Exception ex) { 436 final String opname = "parse"; 437 TheLogger.error(_class_name, opname, "FAILED", ex); 438 return null; 439 } 440 441 return parser; 442 } 443 } 444 | Popular Tags |