KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * copyright 2002 2004 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.jmi.xmiio.exporter;
21
22 import javax.jmi.model.ModelElement;
23 import javax.jmi.reflect.RefObject;
24
25 import org.objectweb.modfact.jmi.generator.TagGenerator;
26 import org.objectweb.modfact.jmi.logging.Level;
27 import org.objectweb.modfact.jmi.logging.ModFactLogger;
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 DTD URI. */
37     protected String JavaDoc _dtd;
38
39     /** The model name. */
40     protected String JavaDoc _model;
41
42     /** The Meta-Model name. */
43     protected String JavaDoc _metamodel;
44
45     /** The different element exported into XMI with their keys (ref_mof_id / xmi_id). */
46     protected java.util.Hashtable JavaDoc _elements = new java.util.Hashtable JavaDoc();
47
48     /** The index for the xmi_id of the elements. */
49     protected int _element_index = 1;
50
51     /** The different TypeCode exported into XMI (TypeCode / xmi_id). */
52     protected java.util.Hashtable JavaDoc _type_codes = new java.util.Hashtable JavaDoc();
53
54     /** The index for the xmi_id of the TypeCodes. */
55     protected int _type_code_index = 1;
56
57     /** The Logger. */
58     protected ModFactLogger _logger;
59
60     /**
61      * Default XMI Export Constructor.
62      * @param dtd The DTD File.
63      * @param model The model name.
64      * @param metamodel The Meta-Model name.
65      */

66     protected XMIExport(String JavaDoc dtd, String JavaDoc model, String JavaDoc metamodel) {
67         super();
68         _dtd = dtd;
69         _model = model;
70         _metamodel = metamodel;
71     }
72     /**
73      * Parse the package. The package is retrieve from its name in the Name Service.
74      * @param name The name in the Name Service.
75      */

76     public void parseName(String JavaDoc name) {
77         System.err.println("parseName(" + name + ")");
78     }
79
80     /**
81      * Parse the package.
82      * @param root_package The Root Package.
83      */

84     public void parse(Object JavaDoc root_package) {
85         outputln("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
86         if (_dtd.length() > 0) {
87             try {
88                 java.net.URL JavaDoc url = new java.net.URL JavaDoc(_dtd);
89                 outputln("<!DOCTYPE XMI SYSTEM \"" + url.toString() + "\">");
90             } catch (java.net.MalformedURLException JavaDoc murle) {
91                 murle.printStackTrace();
92             }
93         }
94         outputln("<XMI verifier=\"false\" xmi.version=\"1.1\">");
95         xmiHeaderTemplate();
96         xmiContentTemplate(root_package);
97         outputln("</XMI>");
98         flushFile();
99     }
100
101     /**
102      * Generate the header of the XMI File.
103      */

104     public void xmiHeaderTemplate() {
105         _logger.log(Level.FINE, "XMI Header generation ...");
106         outputln("<XMI.header>");
107         outputln("<XMI.model xmi.name=\"" + _model + "\"/>");
108         outputln("<XMI.metamodel xmi.name=\"" + _metamodel + "\" xmi.version=\"1.3\"/>");
109         outputln("<XMI.documentation>");
110         outputln("<XMI.exporter>Generated by the ModFact XMI exporter</XMI.exporter>");
111         outputln("</XMI.documentation>");
112         outputln("</XMI.header>");
113     }
114
115     /**
116      * Generate the content of the XMI File.
117      * @param _root_package The Root Package.
118      */

119     public void xmiContentTemplate(Object JavaDoc _root_package) {
120         _logger.log(Level.FINE, "XMI Content generation ...");
121         outputln("<XMI.content>");
122         rootPackageTemplate(_root_package);
123         outputln("</XMI.content>");
124     }
125
126     /**
127      * The root package processing.
128      * @param _root_package The Root Package.
129      */

130     public abstract void rootPackageTemplate(Object JavaDoc _root_package);
131
132     // ==================================================================
133
// Common Functions
134
// ==================================================================
135

136     /**
137      * Get the XMI id for an object which is exported to an XMI file.
138      * @param object The object to export.
139      */

140     protected String JavaDoc getXMIId(Object JavaDoc object) {
141         RefObject refObject = (RefObject)object;
142         String JavaDoc mof_id = refObject.refMofId();
143         if (_elements.containsKey(mof_id))
144             return (String JavaDoc) _elements.get(mof_id);
145         String JavaDoc key = "a" + _element_index;
146         _element_index++;
147         _elements.put(mof_id, key);
148         return key;
149     }
150
151     /* (non-Javadoc)
152      * @see org.objectweb.modfact.corba.generator.Generator#generate()
153      */

154     public void generate() throws Exception JavaDoc {
155         // TODO Auto-generated method stub
156

157     }
158
159     /* (non-Javadoc)
160      * @see org.objectweb.modfact.corba.generator.Generator#setInput(org.omg.mof.Model.ModelElement[])
161      */

162     public void setInput(ModelElement[] elt) throws Exception JavaDoc {
163         // TODO Auto-generated method stub
164

165     }
166
167     /* (non-Javadoc)
168      * @see org.objectweb.modfact.corba.generator.Generator#setLogger(org.objectweb.modfact.corba.logging.ModFactLogger)
169      */

170     public void setLogger(ModFactLogger log) throws Exception JavaDoc {
171         _logger = log;
172
173     }
174
175 }
176
Popular Tags