KickJava   Java API By Example, From Geeks To Geeks.

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


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 Service Descriptor (CCSD) XML DTD.</p>
36  **/

37 public class CCSDParser
38 extends org.xml.sax.helpers.DefaultHandler JavaDoc
39 {
40     // ccsd
41
static final private String JavaDoc _class_name = "CCSDParser";
42     private ORBService _orbservice;
43
44     // state machine
45
private boolean _serviceid_found;
46     private boolean _reqcorbaservice_found;
47     private boolean _corbaserviceref_found;
48
49     // built fields
50
private String JavaDoc _service_id;
51     private org.omg.CORBA.Object JavaDoc _csref_ref;
52     private String JavaDoc _csref_type;
53     private String JavaDoc _csref_regname;
54
55     // default constructor
56
public
57     CCSDParser(ORBService orbs)
58     {
59         // ccsd
60
_orbservice = orbs;
61
62         // state machine
63
_serviceid_found = false;
64         _reqcorbaservice_found = false;
65         _corbaserviceref_found = false;
66
67         // built fields
68
_service_id = null;
69         _csref_ref = null;
70         _csref_type = null;
71         _csref_regname = null;
72     }
73
74     //
75
// internal operations
76
//
77

78     static private void
79     parseAsSAXDoc(String JavaDoc fname, String JavaDoc dtdloc,
80                   org.xml.sax.helpers.DefaultHandler JavaDoc handler)
81     {
82         final String JavaDoc opname = "parseAsSAXDoc";
83         // create SAX input source
84
org.xml.sax.InputSource JavaDoc isource = null;
85         try {
86             isource = new org.xml.sax.InputSource JavaDoc(new java.io.FileInputStream JavaDoc(fname));
87         } catch (java.io.FileNotFoundException JavaDoc ex) {
88             final String JavaDoc msg = "FAILED (file not found: "+fname+")";
89             TheLogger.error(_class_name, opname, msg, ex);
90             return ;
91         }
92
93         // change location of DTD in document
94
String JavaDoc loc = isource.getSystemId();
95         isource.setSystemId(dtdloc+"/"+loc);
96
97         // obtain new SAX parser factory
98
// NOTE: disable namespaces and validation
99
javax.xml.parsers.SAXParserFactory JavaDoc fact = javax.xml.parsers.SAXParserFactory.newInstance();
100         fact.setNamespaceAware(false);
101         fact.setValidating(false);
102
103         // create SAX parser and parse XML file
104
try {
105             javax.xml.parsers.SAXParser JavaDoc parser = fact.newSAXParser();
106             parser.parse(isource, handler);
107         } catch (Exception JavaDoc ex) {
108             TheLogger.error(_class_name, opname, "FAILED", ex);
109             return ;
110         }
111     }
112
113     //
114
// static public operations
115
//
116

117     static public CCSDParser
118     parse(String JavaDoc fname, String JavaDoc dtdloc, ORBService orbs)
119     {
120         CCSDParser parser = new CCSDParser(orbs);
121         parseAsSAXDoc(fname, dtdloc, parser);
122
123         return parser;
124     }
125
126     //
127
// public operations
128
//
129

130     final public String JavaDoc
131     getServiceId()
132     {
133         return _service_id;
134     }
135
136     final public org.coach.ECM.CORBAServiceRef
137     getCORBAServiceRef()
138     {
139         final String JavaDoc opname = "getCORBAServiceRef";
140         if (_csref_ref==null) {
141             final String JavaDoc msg = "no CORBAServiceRef element defined";
142             TheLogger.debug(_class_name, opname, msg);
143             return null;
144         }
145
146         org.coach.ECM.CORBAServiceRef csref = new org.coach.ECM.CORBAServiceRef();
147         if (_csref_type==null) {
148             final String JavaDoc msg = "type attribute of requirescorbaservice element must be specified";
149             TheLogger.error(_class_name, opname, msg);
150         }
151
152         if (_csref_regname==null) {
153             final String JavaDoc msg = "registerwithorb element must be specified";
154             TheLogger.error(_class_name, opname, msg);
155         }
156
157         csref.corba_type = _csref_type;
158         csref.orb_registration_name = _csref_regname;
159         csref.initial_reference = _csref_ref;
160
161         return csref;
162     }
163
164     //
165
// org.xml.sax.ContentHandler
166
//
167

168     final public void
169     startDocument()
170     throws org.xml.sax.SAXException JavaDoc
171     {
172         //final String opname = "startDocument";
173
//TheLogger.debug(_class_name, opname, "starting document");
174
}
175
176     final public void
177     endDocument()
178     throws org.xml.sax.SAXException JavaDoc
179     {
180         //final String opname = "endDocument";
181
//TheLogger.debug(_class_name, opname, "ending document");
182
}
183
184     final public void
185     startElement(String JavaDoc nsuri, String JavaDoc lname, String JavaDoc qname,
186                  org.xml.sax.Attributes JavaDoc attrs)
187     throws org.xml.sax.SAXException JavaDoc
188     {
189         // equiv XPath req: "corbacomponentservice/serviceid"
190
if (qname.equals("serviceid")) {
191             _serviceid_found = true;
192             return ;
193         }
194
195         // equiv XPath req: "corbacomponentservice/requirescorbaservice"
196
// equiv XPath req: "corbacomponentservice/requirescorbaservice/@type"
197
if (qname.equals("requirescorbaservice")) {
198             _reqcorbaservice_found = true;
199             _csref_type = attrs.getValue("type");
200             return ;
201         }
202
203         // equiv XPath req: "corbacomponentservice/requirescorbaservice/corbaserviceref"
204
if (_reqcorbaservice_found && (qname.equals("corbaserviceref"))) {
205             _corbaserviceref_found = true;
206             return ;
207         }
208
209         // equiv XPath req: "corbacomponentservice/requirescorbaservice/registerwithorb/@name"
210
if (_reqcorbaservice_found && (qname.equals("registerwithorb"))) {
211             _csref_regname = attrs.getValue("name");
212             return ;
213         }
214
215         // equiv XPath req: "corbacomponentservice/requirescorbaservice/corbaserviceref/objref/@string"
216
if (_corbaserviceref_found && (qname.equals("objref"))) {
217             String JavaDoc ior = attrs.getValue("string");
218             _csref_ref = _orbservice.string_to_object(ior);
219             return ;
220         }
221         // equiv XPath req: "corbacomponentservice/requirescorbaservice/corbaserviceref/link/@href"
222
if (_corbaserviceref_found && (qname.equals("link"))) {
223             // TODO
224
return ;
225         }
226         // equiv XPath req: "corbacomponentservice/requirescorbaservice/corbaserviceref/ins/@name"
227
if (_corbaserviceref_found && (qname.equals("ins"))) {
228             String JavaDoc insname = attrs.getValue("name");
229             RegistrationService regs = org.objectweb.corba.runtime.Runtime.getRegistrationService();
230             INSRegistrationScheme scheme = (INSRegistrationScheme)regs.get_scheme(INSRegistrationScheme.SCHEME_ID);
231
232             _csref_ref = scheme.lookup(insname, _orbservice);
233             return ;
234         }
235
236     }
237
238     final public void
239     endElement(String JavaDoc nsuri, String JavaDoc lname, String JavaDoc qname)
240     throws org.xml.sax.SAXException JavaDoc
241     {
242         // equiv XPath req: "corbacomponentservice/serviceid"
243
if (qname.equals("serviceid")) {
244             _serviceid_found = false;
245             return ;
246         }
247
248         // equiv XPath req: "corbacomponentservice/requirescorbaservice"
249
if (qname.equals("requirescorbaservice")) {
250             _reqcorbaservice_found = false;
251             return ;
252         }
253
254         // equiv XPath req: "corbacomponentservice/requirescorbaservice/corbaserviceref"
255
if (_reqcorbaservice_found && (qname.equals("corbaserviceref"))) {
256             _corbaserviceref_found = false;
257             return ;
258         }
259     }
260
261     final public void
262     characters(char[] ch, int start, int length)
263     throws org.xml.sax.SAXException JavaDoc
264     {
265         // equiv XPath req: "corbacomponentservice/serviceid/text()"
266
if (_serviceid_found) {
267             _service_id = new String JavaDoc(ch, start, length);
268             //final String opname = "characters";
269
//final String msg = "code entrypoint found: "+code_entrypoint;
270
//TheLogger.debug(_class_name, opname, msg);
271
return ;
272         }
273     }
274 }
275
Popular Tags