KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > corba > xmiio > exporter > XMIExport


1 /**
2  * copyright 2002 2003 Laboratoire d'Informatique Paris 6 (LIP6)
3  *
4  * This file is part of ModFact.
5  *
6  * ModFact is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * at your option) any later version.
10  *
11  * ModFact is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with ModFact; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */

20 package org.objectweb.modfact.corba.xmiio.exporter;
21
22 import org.objectweb.modfact.corba.generator.TagGenerator;
23 import org.objectweb.modfact.corba.helper.CORBACommon;
24 import org.objectweb.modfact.corba.logging.Level;
25 import org.objectweb.modfact.corba.logging.ModFactLogger;
26 import org.objectweb.modfact.corba.provider.CORBAProvider;
27 import org.omg.mof.Model.ModelElement;
28
29 /**
30  * An abstract implementation of an XMI Import.
31  * This class provides common functions for all XMI importers,
32  * and generic functions for parsing files thanks to the XMIImportUtils inheritance
33  */

34 public abstract class XMIExport extends TagGenerator {
35
36     /** The ORB. */
37     protected org.omg.CORBA.ORB JavaDoc _orb;
38
39     /** The CORBA Helper. */
40     protected CORBACommon corbaHelper;
41
42     /** The DTD URI. */
43     protected String JavaDoc _dtd;
44
45     /** The XML Namespace. */
46     protected String JavaDoc _namespace;
47
48     /** The model name. */
49     protected String JavaDoc _model;
50
51     /** The Meta-Model name. */
52     protected String JavaDoc _metamodel;
53
54     /** The different element exported into XMI with their keys (ref_mof_id / xmi_id). */
55     protected java.util.Hashtable JavaDoc _elements = new java.util.Hashtable JavaDoc();
56
57     /** The index for the xmi_id of the elements. */
58     protected int _element_index = 1;
59
60     /** The different TypeCode exported into XMI (TypeCode / xmi_id). */
61     protected java.util.Hashtable JavaDoc _type_codes = new java.util.Hashtable JavaDoc();
62
63     /** The index for the xmi_id of the TypeCodes. */
64     protected int _type_code_index = 1;
65
66     /** The Logger. */
67     protected ModFactLogger _logger;
68
69     /**
70      * Default XMI Export Constructor.
71      * @param dtd The DTD File.
72      * @param namespace The XML Namespace.
73      * @param model The model name.
74      * @param metamodel The Meta-Model name.
75      */

76     protected XMIExport(String JavaDoc dtd, String JavaDoc namespace, String JavaDoc model, String JavaDoc metamodel) {
77         super();
78         corbaHelper = new CORBAProvider();
79         _dtd = dtd;
80         _namespace = namespace;
81         _model = model;
82         _metamodel = metamodel;
83     }
84
85     /**
86      * Set the ORB.
87      * @param orb The new ORB.
88      */

89     public void setORB(org.omg.CORBA.ORB JavaDoc orb) {
90         _orb = orb;
91     }
92
93     /**
94      * Parse the package. The package is retrieve from its name in the Name Service.
95      * @param name The name in the Name Service.
96      * @param printIOR TRUE to display the IOR on the console.
97      */

98     public void parseName(String JavaDoc name, boolean printIOR)
99         throws
100             org.omg.CORBA.ORBPackage.InvalidName JavaDoc,
101             org.omg.CosNaming.NamingContextPackage.InvalidName JavaDoc,
102             org.omg.CosNaming.NamingContextPackage.NotFound JavaDoc,
103             org.omg.CosNaming.NamingContextPackage.CannotProceed JavaDoc,
104             org.omg.mof.Reflective.MofError {
105         // Get the root naming context
106
org.omg.CORBA.Object JavaDoc objRef = _orb.resolve_initial_references("NameService");
107         // Use NamingContextExt instead of NamingContext. This is part of the Interoperable naming Service.
108
org.omg.CosNaming.NamingContextExt JavaDoc ncRef = org.omg.CosNaming.NamingContextExtHelper.narrow(objRef);
109         // Resolve the Object Reference in Naming
110
org.omg.CORBA.Object JavaDoc object = ncRef.resolve_str(name);
111         parse(object);
112         if (printIOR)
113             corbaHelper.printIOR(_orb, object, System.out);
114     }
115
116     /**
117      * Parse the package. The package is retrieve from an IOR file.
118      * @param iorFile The IOR filename.
119      * @param printIOR TRUE to display the IOR on the console.
120      */

121     public void parseIOR(String JavaDoc filename, boolean printIOR) throws org.omg.mof.Reflective.MofError {
122         org.omg.CORBA.Object JavaDoc object = corbaHelper.readIOR(_orb, filename);
123         parse(object);
124         if (printIOR)
125             corbaHelper.printIOR(_orb, object, System.out);
126     }
127
128     /**
129      * Parse the package.
130      * @param root_package The Root Package.
131      */

132     public void parse(org.omg.CORBA.Object JavaDoc root_package) throws org.omg.mof.Reflective.MofError {
133         outputln("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
134         if (_dtd.length() > 0) {
135             try {
136                 java.net.URL JavaDoc url = new java.net.URL JavaDoc(_dtd);
137                 outputln("<!DOCTYPE XMI SYSTEM \"" + url.toString() + "\">");
138             } catch (java.net.MalformedURLException JavaDoc murle) {
139                 murle.printStackTrace();
140             }
141         }
142         if (_namespace.length() > 0)
143             outputln("<XMI verified=\"false\" xmi.version=\"1.1\" xmlns:Model=\"" + _namespace + "\">");
144         else
145             outputln("<XMI verifier=\"false\" xmi.version=\"1.1\">");
146         xmiHeaderTemplate();
147         xmiContentTemplate(root_package);
148         outputln("</XMI>");
149         flushFile();
150     }
151
152     /**
153      * Generate the header of the XMI File.
154      */

155     public void xmiHeaderTemplate() {
156         _logger.log(Level.INFO, "XMI Header generation ...");
157         outputln("<XMI.header>");
158         outputln("<XMI.model xmi.name=\"" + _model + "\"/>");
159         outputln("<XMI.metamodel xmi.name=\"" + _metamodel + "\" xmi.version=\"1.3\"/>");
160         outputln("<XMI.documentation>");
161         outputln("<XMI.exporter>Generated by the ModFact XMI exporter</XMI.exporter>");
162         outputln("</XMI.documentation>");
163         outputln("</XMI.header>");
164     }
165
166     /**
167      * Generate the content of the XMI File.
168      * @param _root_package The Root Package.
169      */

170     public void xmiContentTemplate(org.omg.CORBA.Object JavaDoc _root_package) throws org.omg.mof.Reflective.MofError {
171         _logger.log(Level.INFO, "XMI Content generation ...");
172         outputln("<XMI.content>");
173         rootPackageTemplate(_root_package);
174         outputln("</XMI.content>");
175     }
176
177     /**
178      * The root package processing.
179      * @param _root_package The Root Package.
180      */

181     public abstract void rootPackageTemplate(org.omg.CORBA.Object JavaDoc _root_package) throws org.omg.mof.Reflective.MofError;
182
183     // ==================================================================
184
// Common Functions
185
// ==================================================================
186

187     /**
188      * Get the XMI id for an object which is exported to an XMI file.
189      * @param object The object to export.
190      */

191     protected String JavaDoc getXMIId(org.omg.mof.Reflective.RefBaseObjectOperations object) {
192         String JavaDoc mof_id = object.ref_mof_id();
193         if (_elements.containsKey(mof_id))
194             return (String JavaDoc) _elements.get(mof_id);
195         String JavaDoc key = "a" + _element_index;
196         _element_index++;
197         _elements.put(mof_id, key);
198         return key;
199     }
200
201     /**
202      * Get the XMI id for a CORBA TypeCode which is exported to an XMI file.
203      * @param type_code The TypeCode to export.
204      */

205     protected String JavaDoc getTypeCodeId(org.omg.CORBA.TypeCode JavaDoc type_code) {
206         if (_type_codes.containsKey(type_code))
207             return (String JavaDoc) _type_codes.get(type_code);
208         String JavaDoc key = "tc" + _type_code_index;
209         _type_code_index++;
210         _type_codes.put(type_code, key);
211         return key;
212     }
213
214     /**
215      * Pass from CORBA Type Code to XML.
216      * @param element The XML Element.
217      * @param repositoryID The repository ID.
218      * @return The corresponding TypeCode.
219      */

220     public String JavaDoc typeCode2XML(org.omg.CORBA.TypeCode JavaDoc typeCode) throws org.omg.CORBA.BAD_TYPECODE JavaDoc {
221         int tc_value = typeCode.kind().value();
222         switch (tc_value) {
223             case org.omg.CORBA.TCKind._tk_boolean :
224                 return "XMI.CorbaTcBoolean";
225             case org.omg.CORBA.TCKind._tk_octet :
226                 return "XMI.CorbaTcOctet";
227             case org.omg.CORBA.TCKind._tk_char :
228                 return "XMI.CorbaTcChar";
229             case org.omg.CORBA.TCKind._tk_wchar :
230                 return "XMI.CorbaTcWChar";
231             case org.omg.CORBA.TCKind._tk_short :
232                 return "XMI.CorbaTcShort";
233             case org.omg.CORBA.TCKind._tk_ushort :
234                 return "XMI.CorbaTcUshort";
235             case org.omg.CORBA.TCKind._tk_long :
236                 return "XMI.CorbaTcLong";
237             case org.omg.CORBA.TCKind._tk_ulong :
238                 return "XMI.CorbaTcUlong";
239             case org.omg.CORBA.TCKind._tk_longlong :
240                 return "XMI.CorbaTcLongLong";
241             case org.omg.CORBA.TCKind._tk_double :
242                 return "XMI.CorbaTcDouble";
243             case org.omg.CORBA.TCKind._tk_float :
244                 return "XMI.CorbaTcFloat";
245             case org.omg.CORBA.TCKind._tk_string :
246                 return "XMI.CorbaTcString";
247             case org.omg.CORBA.TCKind._tk_TypeCode :
248                 return "XMI.CorbaTcTypeCode";
249             case org.omg.CORBA.TCKind._tk_Principal :
250                 return "XMI.CorbaTcPrincipal";
251             case org.omg.CORBA.TCKind._tk_any :
252             default :
253                 return "XMI.CorbaTcAny";
254         }
255     }
256
257     /* (non-Javadoc)
258      * @see org.objectweb.modfact.corba.generator.Generator#generate()
259      */

260     public void generate() throws Exception JavaDoc {
261         // TODO Auto-generated method stub
262

263     }
264
265     /* (non-Javadoc)
266      * @see org.objectweb.modfact.corba.generator.Generator#setInput(org.omg.mof.Model.ModelElement[])
267      */

268     public void setInput(ModelElement[] elt) throws Exception JavaDoc {
269         // TODO Auto-generated method stub
270

271     }
272
273     /* (non-Javadoc)
274      * @see org.objectweb.modfact.corba.generator.Generator#setLogger(org.objectweb.modfact.corba.logging.ModFactLogger)
275      */

276     public void setLogger(ModFactLogger log) throws Exception JavaDoc {
277         _logger = log;
278
279     }
280
281 }
282
Popular Tags