KickJava   Java API By Example, From Geeks To Geeks.

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


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 Component Software Descriptor (CSD) XML DTD.</p>
36  **/

37 public class CSDParser
38 {
39     // csd
40
static final private String JavaDoc _class_name = "CSDParser";
41     private org.w3c.dom.Document JavaDoc _doc;
42     private String JavaDoc _uuid; // the target implementation
43

44     // public fields set during the parsing
45
// and accessible from the outside
46
public String JavaDoc descriptor_location; // location of the CCD file for this implementation
47
public String JavaDoc code_entrypoint; // entrypoint of the code for this implementation
48
public String JavaDoc code_location; // location of the archive for this implementation
49
public java.util.ArrayList JavaDoc valuetype_factories;
50
51     // default constructor
52
public
53     CSDParser(org.w3c.dom.Document JavaDoc doc,
54               String JavaDoc uuid)
55     {
56         // csd
57
_doc = doc;
58         _uuid = uuid;
59
60         // public fields
61
descriptor_location = null;
62         code_entrypoint = null;
63         code_location = null;
64         valuetype_factories = new java.util.ArrayList JavaDoc();
65     }
66
67     //
68
// internal operations
69
//
70

71     static private org.w3c.dom.Document JavaDoc
72     openAsDocument(String JavaDoc fname, String JavaDoc dtdloc)
73     {
74         final String JavaDoc opname = "openAsDocument";
75         // create SAX input source
76
org.xml.sax.InputSource JavaDoc isource = null;
77         try {
78             isource = new org.xml.sax.InputSource JavaDoc(new java.io.FileInputStream JavaDoc(fname));
79         } catch (java.io.FileNotFoundException JavaDoc ex) {
80             final String JavaDoc msg = "FAILED (file not found: "+fname+")";
81             TheLogger.error(_class_name, opname, msg, ex);
82             return null;
83         }
84
85         // change location of DTD in document
86
String JavaDoc loc = isource.getSystemId();
87         isource.setSystemId(dtdloc+"/"+loc);
88
89         // obtain new DOM parser factory
90
// NOTE: disable namespaces and validation
91
javax.xml.parsers.DocumentBuilderFactory JavaDoc fact = javax.xml.parsers.DocumentBuilderFactory.newInstance();
92         fact.setNamespaceAware(false);
93         fact.setValidating(false);
94
95         // create DOM parser and parse XML file
96
try {
97             javax.xml.parsers.DocumentBuilder JavaDoc parser = fact.newDocumentBuilder();
98             return parser.parse(isource);
99         } catch (Exception JavaDoc ex) {
100             TheLogger.error(_class_name, opname, "FAILED", ex);
101             return null;
102         }
103     }
104
105     final private void
106     parseDocument()
107     {
108         org.w3c.dom.Element JavaDoc root = _doc.getDocumentElement();
109         org.w3c.dom.NodeList JavaDoc childs = root.getChildNodes();
110         org.w3c.dom.Node JavaDoc node = null;
111         for (int i=0;i<childs.getLength();i++) {
112             node = childs.item(i);
113             switch (node.getNodeType()) {
114             case org.w3c.dom.Node.ELEMENT_NODE:
115                 org.w3c.dom.Element JavaDoc element = (org.w3c.dom.Element JavaDoc)node;
116                 String JavaDoc tagname = element.getTagName();
117
118                 if (tagname.equals("implementation")) {
119                     // equiv XPath: "softpkg/implementation[@id=uuid]"
120
String JavaDoc id = element.getAttribute("id");
121                     if (_uuid.equals(id)) {
122                         parseImplementation(element);
123                     }
124                 }
125
126                 break;
127             default:
128                 break;
129             }
130         }
131     }
132
133     final private void
134     parseImplementation(org.w3c.dom.Element JavaDoc impl)
135     {
136         final String JavaDoc opname = "parseImplementation";
137
138         // code
139
// equiv XPath: "softpkg/implementation[@id=uuid]/code"
140
org.w3c.dom.NodeList JavaDoc codes = impl.getElementsByTagName("code");
141         if (codes.getLength()==0) {
142             TheLogger.error(_class_name, opname, "FAILED (missing code element, UUID="+_uuid+")");
143         }
144
145         // NOTE: there's only one code element
146
org.w3c.dom.Element JavaDoc code = (org.w3c.dom.Element JavaDoc)codes.item(0);
147         parseCode(code);
148
149         // dependencies
150
// equiv XPath: "softpkg/implementation[@id=uuid]/dependency"
151
org.w3c.dom.NodeList JavaDoc dependencies = impl.getElementsByTagName("dependency");
152         org.w3c.dom.Element JavaDoc dep = null;
153         for (int i=0;i<dependencies.getLength();i++) {
154             dep = (org.w3c.dom.Element JavaDoc)dependencies.item(i);
155             parseDependency(dep);
156         }
157
158         // descriptor
159
// equiv XPath: "softpkg/implementation[@id=uuid]/descriptor"
160
org.w3c.dom.NodeList JavaDoc descriptors = impl.getElementsByTagName("descriptor");
161         if (descriptors.getLength()==0) {
162             TheLogger.error(_class_name, opname, "FAILED (missing descriptor element, UUID="+_uuid+")");
163         }
164
165         // NOTE: there's only one descriptor element
166
org.w3c.dom.Element JavaDoc desc = (org.w3c.dom.Element JavaDoc)descriptors.item(0);
167         parseDescriptor(desc);
168     }
169
170     final private void
171     parseCode(org.w3c.dom.Element JavaDoc code)
172     {
173         final String JavaDoc opname = "parseCode";
174
175         // entrypoint
176
// equiv XPath: "softpkg/implementation[@id=uuid]/code/entrypoint"
177
org.w3c.dom.NodeList JavaDoc entrypoints = code.getElementsByTagName("entrypoint");
178         if (entrypoints.getLength()==0) {
179             TheLogger.error(_class_name, opname, "FAILED (missing entrypoint element, UUID="+_uuid+")");
180         }
181
182         // NOTE: there's only one entrypoint element
183
org.w3c.dom.Element JavaDoc entrypoint = (org.w3c.dom.Element JavaDoc)entrypoints.item(0);
184         org.w3c.dom.NodeList JavaDoc datas = entrypoint.getChildNodes();
185         for (int i=0;i<datas.getLength();i++) {
186             org.w3c.dom.Node JavaDoc node = datas.item(i);
187             if ((node.getNodeName().equals("#cdata-section")) ||
188                 (node.getNodeName().equals("#text"))) {
189                 org.w3c.dom.CharacterData JavaDoc data = (org.w3c.dom.CharacterData JavaDoc)node;
190                 code_entrypoint = data.getData();
191             }
192         }
193
194         if (code_entrypoint==null) {
195             TheLogger.error(_class_name, opname, "FAILED (missing entrypoint value, UUID="+_uuid+")");
196         }
197
198         // code location
199
// NOTE: only support fileinarchive
200
// equiv XPath: "softpkg/implementation[@id=uuid]/code/fileinarchive"
201
org.w3c.dom.NodeList JavaDoc fileinarchives = code.getElementsByTagName("fileinarchive");
202         if (fileinarchives.getLength()==0) {
203             TheLogger.error(_class_name, opname, "FAILED (missing fileinarchive element, UUID="+_uuid+")");
204         }
205
206         // NOTE: there's only one fileinarchive element
207
org.w3c.dom.Element JavaDoc fileinarchive = (org.w3c.dom.Element JavaDoc)fileinarchives.item(0);
208         // equiv XPath: "softpkg/implementation[@id=uuid]/code/fileinarchive[@name]"
209
code_location = fileinarchive.getAttribute("name");
210     }
211
212     final private void
213     parseDependency(org.w3c.dom.Element JavaDoc dep)
214     {
215         final String JavaDoc opname = "parseDependency";
216
217         // NOTE: only parse valuetypefactory dependencies
218
// valuetypefactory
219
// equiv XPath: "softpkg/implementation[@id=uuid]/dependency/valuetypefactory"
220
org.w3c.dom.NodeList JavaDoc valuetypefactorys = dep.getElementsByTagName("valuetypefactory");
221         if (valuetypefactorys.getLength()==0) {
222             return ;
223         }
224
225         // NOTE: there's only one valuetypefactory element
226
org.w3c.dom.Element JavaDoc valuetypefactory = (org.w3c.dom.Element JavaDoc)valuetypefactorys.item(0);
227         org.coach.ECM.ValuetypeFactoryDependency vfdep = new org.coach.ECM.ValuetypeFactoryDependency();
228         vfdep.repid = valuetypefactory.getAttribute("repid");
229         vfdep.value_entrypoint = valuetypefactory.getAttribute("valueentrypoint");
230         vfdep.factory_entrypoint = valuetypefactory.getAttribute("factoryentrypoint");
231
232         // code location
233
// NOTE: only support fileinarchive
234
// equiv XPath: "softpkg/implementation[@id=uuid]/dependency/valuetypefactory/fileinarchive"
235
org.w3c.dom.NodeList JavaDoc fileinarchives = valuetypefactory.getElementsByTagName("fileinarchive");
236         if (fileinarchives.getLength()==0) {
237             TheLogger.error(_class_name, opname, "FAILED (missing fileinarchive element, UUID="+_uuid+")");
238         }
239
240         // NOTE: there's only one fileinarchive element
241
org.w3c.dom.Element JavaDoc fileinarchive = (org.w3c.dom.Element JavaDoc)fileinarchives.item(0);
242         vfdep.code_location = fileinarchive.getAttribute("name");
243
244         // store
245
valuetype_factories.add(vfdep);
246     }
247
248
249     final private void
250     parseDescriptor(org.w3c.dom.Element JavaDoc desc)
251     {
252         final String JavaDoc opname = "parseDescriptor";
253
254         // descriptor location
255
// NOTE: only support fileinarchive
256
// equiv XPath req: "softpkg/implementation[@id=_uuid]/descriptor/fileinarchive/@name"
257
org.w3c.dom.NodeList JavaDoc fileinarchives = desc.getElementsByTagName("fileinarchive");
258         if (fileinarchives.getLength()==0) {
259             TheLogger.error(_class_name, opname, "FAILED (missing fileinarchive element, UUID="+_uuid+")");
260         }
261
262         // NOTE: there's only one fileinarchive element
263
org.w3c.dom.Element JavaDoc fileinarchive = (org.w3c.dom.Element JavaDoc)fileinarchives.item(0);
264         descriptor_location = fileinarchive.getAttribute("name");
265     }
266
267     //
268
// static public operations
269
//
270

271     static public CSDParser
272     parse(String JavaDoc fname, String JavaDoc dtdloc, String JavaDoc uuid)
273     {
274         CSDParser parser = null;
275         try {
276             org.w3c.dom.Document JavaDoc doc = openAsDocument(fname, dtdloc);
277             parser = new CSDParser(doc, uuid);
278             parser.parseDocument();
279         } catch (Exception JavaDoc ex) {
280             final String JavaDoc opname = "parse";
281             TheLogger.error(_class_name, opname, "FAILED", ex);
282             return null;
283         }
284
285         return parser;
286     }
287 }
288
Popular Tags