KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ccm > runtime > CCDParser


1 // ====================================================================
2
//
3
// ECM: The Extensible Container Model
4
// Copyright (C) 2004 THALES
5
// Contact: openccm-ecm@objectweb.org
6
//
7
// This library is free software; you can redistribute it and/or
8
// modify it under the terms of the GNU Lesser General Public
9
// License as published by the Free Software Foundation; either
10
// version 2.1 of the License, or any later version.
11
//
12
// This library is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
// Lesser General Public License for more details.
16
//
17
// You should have received a copy of the GNU Lesser General Public
18
// License along with this library; if not, write to the Free Software
19
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20
// USA
21
//
22
// Initial developer(s): Mathieu Vadet.
23
// Initial Funding: IST COACH European project (IST-2001-34445)
24
// http://www.ist-coach.org
25
//
26
// ====================================================================
27

28
29
30 package org.objectweb.ccm.runtime;
31
32 import org.objectweb.corba.runtime.*;
33
34 /**
35  ** <p>Default DOM parser for the CORBA Component Descriptor (CCD) XML DTD.</p>
36  **/

37 public class CCDParser
38 {
39     // ccd
40
static final private String JavaDoc _class_name = "CCDParser";
41     private org.w3c.dom.Document JavaDoc _doc;
42     private java.util.HashMap JavaDoc _repid2sname;
43     private java.util.HashMap JavaDoc _sid2uuid;
44     private ExtComponentInstallationImpl _extci;
45
46     // public fields set during the parsing
47
// and accessible from the outside
48
public String JavaDoc home_repid;
49     public String JavaDoc component_repid;
50     public java.util.HashMap JavaDoc sname2policies;
51
52     // default constructor
53
public
54     CCDParser(org.w3c.dom.Document JavaDoc doc,
55               ExtComponentInstallationImpl extci)
56     {
57         // ccd
58
_doc = doc;
59         _repid2sname = new java.util.HashMap JavaDoc();
60         _sid2uuid = new java.util.HashMap JavaDoc();
61         _extci = extci;
62
63         // public fields
64
home_repid = null;
65         component_repid = null;
66         sname2policies = new java.util.HashMap JavaDoc();
67     }
68
69     //
70
// internal operations
71
//
72

73     static private org.w3c.dom.Document JavaDoc
74     openAsDocument(String JavaDoc fname, String JavaDoc dtdloc)
75     {
76         final String JavaDoc opname = "openAsDocument";
77         // create SAX input source
78
org.xml.sax.InputSource JavaDoc isource = null;
79         try {
80             isource = new org.xml.sax.InputSource JavaDoc(new java.io.FileInputStream JavaDoc(fname));
81         } catch (java.io.FileNotFoundException JavaDoc ex) {
82             final String JavaDoc msg = "FAILED (file not found: "+fname+")";
83             TheLogger.error(_class_name, opname, msg, ex);
84             return null;
85         }
86
87         // change location of DTD in document
88
String JavaDoc loc = isource.getSystemId();
89         isource.setSystemId(dtdloc+"/"+loc);
90
91         // obtain new DOM parser factory
92
// NOTE: disable namespaces and validation
93
javax.xml.parsers.DocumentBuilderFactory JavaDoc fact = javax.xml.parsers.DocumentBuilderFactory.newInstance();
94         fact.setNamespaceAware(false);
95         fact.setValidating(false);
96
97         // create DOM parser and parse XML file
98
try {
99             javax.xml.parsers.DocumentBuilder JavaDoc parser = fact.newDocumentBuilder();
100             return parser.parse(isource);
101         } catch (Exception JavaDoc 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 JavaDoc root = _doc.getDocumentElement();
111         org.w3c.dom.NodeList JavaDoc childs = root.getChildNodes();
112         org.w3c.dom.Node JavaDoc 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 JavaDoc element = (org.w3c.dom.Element JavaDoc)node;
118                 String JavaDoc tagname = element.getTagName();
119
120                 if (tagname.equals("usedservices")) {
121                     // equiv XPath: "corbacomponent/usedservices"
122
parseUsedServices(element);
123                 } else if (tagname.equals("policies")) {
124                     // equiv XPath: "corbacomponent/policies"
125
parsePolicies(element, "");
126                 } else if (tagname.equals("homefeatures")) {
127                     // equiv XPath: "corbacomponent/homefeatures"
128
parseHome(element);
129                 } else if (tagname.equals("componentfeatures")) {
130                     // equiv XPath: "corbacomponent/componentfeatures"
131
parseComponent(element);
132                 } else if (tagname.equals("interface")) {
133                     // equiv XPath: "corbacomponent/interface"
134
parseInterface(element);
135                 } else if (tagname.equals("componentrepid")) {
136                     // equiv XPath: "corbacomponent/componentrepid"
137
component_repid = element.getAttribute("repid");
138                 } else if (tagname.equals("homerepid")) {
139                     // equiv XPath: "corbacomponent/homerepid"
140
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 JavaDoc uservices)
152     {
153         org.w3c.dom.NodeList JavaDoc services = uservices.getElementsByTagName("usedservice");
154         org.w3c.dom.Element JavaDoc service = null;
155         org.w3c.dom.Element JavaDoc implref = null;
156         String JavaDoc serviceid = null;
157         String JavaDoc uuid = null;
158
159         for (int i=0;i<services.getLength();i++) {
160             // equiv XPath: "corbacomponent/usedservices/usedservice[@id=X]"
161
service = (org.w3c.dom.Element JavaDoc)services.item(i);
162             // equiv XPath: "corbacomponent/usedservices/usedservice[@id=X]/@id"
163
serviceid = service.getAttribute("id");
164             // equiv XPath: "corbacomponent/usedservices/usedservice[@id=X]/implementationref"
165
implref = (org.w3c.dom.Element JavaDoc)service.getElementsByTagName("implementationref").item(0);
166             // equiv XPath: "corbacomponent/usedservices/usedservice[@id=X]/implementationref/@idref"
167
uuid = implref.getAttribute("idref");
168
169             // store
170
_sid2uuid.put(serviceid, uuid);
171         }
172     }
173
174     final private void
175     parsePolicies(org.w3c.dom.Element JavaDoc pols, String JavaDoc sname)
176     {
177         final String JavaDoc opname = "parsePolicies";
178         if (pols==null) {
179             final String JavaDoc 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 JavaDoc targets = pols.getElementsByTagName("target");
187         org.w3c.dom.Element JavaDoc target = null;
188         org.w3c.dom.NodeList JavaDoc servicerefs = null;
189         org.w3c.dom.Element JavaDoc serviceref = null;
190         String JavaDoc targetname = null;
191
192         for (int i=0;i<targets.getLength();i++) {
193             target = (org.w3c.dom.Element JavaDoc)targets.item(i);
194             // equiv XPath: "policies/target/@name"
195
targetname = target.getAttribute("name");
196
197             // equiv XPath: "policies/target/serviceref"
198
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 JavaDoc idref = null;
203             String JavaDoc policyref = null;
204             String JavaDoc policyvalue = null;
205             String JavaDoc uuid = null;
206
207             for (int j=0;j<servicerefs.getLength();j++) {
208                 serviceref = (org.w3c.dom.Element JavaDoc)servicerefs.item(j);
209                 // equiv XPath: "policies/target/serviceref[@idref=X]/@idref"
210
idref = serviceref.getAttribute("idref");
211                 // equiv XPath: "policies/target/serviceref[@idref=X]/@policyref"
212
policyref = serviceref.getAttribute("policyref");
213                 // equiv XPath: "policies/target/serviceref[@idref=X]/@policyvalue"
214
policyvalue = serviceref.getAttribute("policyvalue");
215
216                 uuid = (String JavaDoc)_sid2uuid.get(idref);
217                 sarchive = _extci.get_service_archive(uuid);
218
219                 //String msg = "policy found";
220
//TheLogger.debug(_class_name, opname, msg);
221
//msg=" target: "+targetname;
222
//TheLogger.debug(_class_name, opname, msg);
223
//msg=" idref: "+idref;
224
//TheLogger.debug(_class_name, opname, msg);
225
//msg=" policyref: "+policyref;
226
//TheLogger.debug(_class_name, opname, msg);
227
//msg=" policyvalue:"+policyvalue;
228
//TheLogger.debug(_class_name, opname, msg);
229

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             // store
238
sname2policies.put(sname+"::"+targetname, prefs);
239         }
240     }
241
242     final private void
243     parseEventPolicies(org.w3c.dom.Element JavaDoc pols, String JavaDoc sname)
244     {
245         // NOTE: for event ports, the <serviceref> element is a child of the
246
// <policies> element
247

248         final String JavaDoc opname = "parseEventPolicies";
249         if (pols==null) {
250             final String JavaDoc 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 JavaDoc servicerefs = null;
258         org.w3c.dom.Element JavaDoc serviceref = null;
259
260         // equiv XPath: "policies/serviceref"
261
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 JavaDoc idref = null;
266         String JavaDoc policyref = null;
267         String JavaDoc policyvalue = null;
268         String JavaDoc uuid = null;
269
270         for (int j=0;j<servicerefs.getLength();j++) {
271             serviceref = (org.w3c.dom.Element JavaDoc)servicerefs.item(j);
272             // equiv XPath: "policies/serviceref[@idref=X]/@idref"
273
idref = serviceref.getAttribute("idref");
274             // equiv XPath: "policies/serviceref[@idref=X]/@policyref"
275
policyref = serviceref.getAttribute("policyref");
276             // equiv XPath: "policies/serviceref[@idref=X]/@policyvalue"
277
policyvalue = serviceref.getAttribute("policyvalue");
278
279             uuid = (String JavaDoc)_sid2uuid.get(idref);
280             sarchive = _extci.get_service_archive(uuid);
281
282             //String msg = "policy found";
283
//TheLogger.debug(_class_name, opname, msg);
284
//msg=" idref: "+idref;
285
//TheLogger.debug(_class_name, opname, msg);
286
//msg=" policyref: "+policyref;
287
//TheLogger.debug(_class_name, opname, msg);
288
//msg=" policyvalue:"+policyvalue;
289
//TheLogger.debug(_class_name, opname, msg);
290

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         // store
299
sname2policies.put(sname, prefs);
300     }
301
302     final private void
303     parseHome(org.w3c.dom.Element JavaDoc home)
304     {
305         // equiv XPath: "corbacomponent/homefeatures[@name=X]/@name"
306
String JavaDoc name = home.getAttribute("name");
307         // equiv XPath: "corbacomponent/homefeatures[@name=X]/@repid"
308
String JavaDoc repid = home.getAttribute("repid");
309         // store
310
_repid2sname.put(repid, "::"+name);
311
312         // check home attributes/operations/finders/factories
313
// equiv XPath: "corbacomponent/homefeatures[@name=X]/policies"
314
org.w3c.dom.Element JavaDoc pols = (org.w3c.dom.Element JavaDoc)home.getElementsByTagName("policies").item(0);
315         parsePolicies(pols, "::"+name);
316
317         // check inherited home
318
// TODO
319
}
320
321     final private void
322     parseComponent(org.w3c.dom.Element JavaDoc component)
323     {
324         // equiv XPath: "corbacomponent/componentfeatures[@name=X]/@name"
325
String JavaDoc name = component.getAttribute("name");
326         // equiv XPath: "corbacomponent/componentfeatures[@name=X]/@repid"
327
String JavaDoc repid = component.getAttribute("repid");
328         // store
329
_repid2sname.put(repid, "::"+name);
330
331         // check component attributes/operations/ports
332
// equiv XPath: "corbacomponent/componentfeatures[@name=X]/policies"
333
// NOTE: CAUTION: the "getElementsByTagName" operation returns all the children, including grand-children
334
// so if we call "getElementsByTagName" directly on the component element, we'll obtain the policies elements
335
// defined in the ports element also
336
// Therefore we use the "getChildNodes" operation instead
337
org.w3c.dom.NodeList JavaDoc 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 JavaDoc pols = (org.w3c.dom.Element JavaDoc)childs.item(i);
341                 parsePolicies(pols, "::"+name);
342             }
343         }
344
345         // check inherited component
346
// TODO
347

348         // check supported interfaces
349
// TODO
350

351         // check ports
352
// check receptacles
353
parsePort(component, "::"+name, "uses", "usesname");
354         // check facets
355
parsePort(component, "::"+name, "provides", "providesname");
356         // check event sources
357
parseEventPort(component, "::"+name, "emits", "emitsname");
358         parseEventPort(component, "::"+name, "publishes", "publishesname");
359         // check event sink
360
parseEventPort(component, "::"+name, "consumes", "consumesname");
361     }
362
363     final private void
364     parsePort(org.w3c.dom.Element JavaDoc component, String JavaDoc cname,
365               String JavaDoc ptype, String JavaDoc pattr)
366     {
367         org.w3c.dom.NodeList JavaDoc ports = null;
368         org.w3c.dom.Element JavaDoc port = null;
369         org.w3c.dom.Element JavaDoc pols = null;
370         String JavaDoc pname = null;
371         String JavaDoc prepid = null;
372         // equiv XPath: "corbacomponent/componentfeatures[@name=X]/ports/<ptype>"
373
ports = component.getElementsByTagName(ptype);
374         for (int i=0;i<ports.getLength();i++) {
375             port = (org.w3c.dom.Element JavaDoc)ports.item(i);
376             // equiv XPath: "corbacomponent/componentfeatures[@name=X]/ports/<ptype>[@<pattr>=X]/@<pattr>"
377
pname = port.getAttribute(pattr);
378             // equiv XPath: "corbacomponent/componentfeatures[@name=X]/ports/<ptype>[@<pattr>=X]/@repid"
379
prepid = port.getAttribute("repid");
380             // check if interface pointed by "repid" has policies
381
// TODO
382

383             // check attributes/operations on port
384
pols = (org.w3c.dom.Element JavaDoc)port.getElementsByTagName("policies").item(0);
385             parsePolicies(pols, cname+"::"+pname);
386         }
387     }
388
389     final private void
390     parseEventPort(org.w3c.dom.Element JavaDoc component, String JavaDoc cname,
391                    String JavaDoc ptype, String JavaDoc pattr)
392     {
393         org.w3c.dom.NodeList JavaDoc ports = null;
394         org.w3c.dom.Element JavaDoc port = null;
395         org.w3c.dom.Element JavaDoc pols = null;
396         String JavaDoc pname = null;
397         String JavaDoc pevt = null;
398         // equiv XPath: "corbacomponent/componentfeatures[@name=X]/ports/<ptype>"
399
ports = component.getElementsByTagName(ptype);
400         for (int i=0;i<ports.getLength();i++) {
401             port = (org.w3c.dom.Element JavaDoc)ports.item(i);
402             // equiv XPath: "corbacomponent/componentfeatures[@name=X]/ports/<ptype>[@<pattr>=X]/@<pattr>"
403
pname = port.getAttribute(pattr);
404             // equiv XPath: "corbacomponent/componentfeatures[@name=X]/ports/<ptype>[@<pattr>=X]/@repid"
405
pevt = port.getAttribute("eventtype");
406             // NOTE: TODO: is there a XML element that describe the eventtype ?
407
// if yes, than we should check this element for policies
408

409             // check policies on port
410
// NOTE: for event ports, policies are directly set on the port (and not on the operation
411
// of the equivalent interface)
412
pols = (org.w3c.dom.Element JavaDoc)port.getElementsByTagName("policies").item(0);
413             parseEventPolicies(pols, cname+"::"+pname);
414         }
415     }
416
417     final private void
418     parseInterface(org.w3c.dom.Element JavaDoc itf)
419     {
420         // TODO
421
}
422
423     //
424
// static public operations
425
//
426

427     static public CCDParser
428     parse(String JavaDoc fname, String JavaDoc dtdloc, ExtComponentInstallationImpl extci)
429     {
430         CCDParser parser = null;
431         try {
432             org.w3c.dom.Document JavaDoc doc = openAsDocument(fname, dtdloc);
433             parser = new CCDParser(doc, extci);
434             parser.parseDocument();
435         } catch (Exception JavaDoc ex) {
436             final String JavaDoc opname = "parse";
437             TheLogger.error(_class_name, opname, "FAILED", ex);
438             return null;
439         }
440
441         return parser;
442     }
443 }
444
Popular Tags