KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > jmi > xmi > Writer


1 package org.objectweb.modfact.jmi.xmi;
2
3 import java.io.*;
4 import java.util.*;
5 import javax.jmi.reflect.*;
6
7 public class Writer {
8
9
10     CharTranslator t = new CharTranslator();
11     PrintStream out;
12
13     Hashtable object_id = new Hashtable();
14     
15     Set writtenOuterMostComposites = new HashSet();
16     Set referencedObjects = new HashSet();
17
18     // metaObject -> qualified name
19
Hashtable qNameCach = new Hashtable();
20     //metaObjectOwner -> properties
21
Hashtable propCach = new Hashtable();
22     
23     XmiWriterImpl caller;
24
25     Writer(XmiWriterImpl owner, OutputStream out) {
26         this.caller = owner;
27         this.out = new PrintStream(out);
28     }
29
30     void writeHeader() {
31         out.println("<?xml version = '1.0' encoding = 'ISO-8859-1' ?>");
32         out.println("<XMI xmi.version = '1.2'>");
33         out.println("<XMI.header>");
34         out.println("<XMI.documentation>");
35         out.println("<XMI.exporter>ModFact</XMI.exporter>");
36         out.println("<XMI.exporterVersion>1.0</XMI.exporterVersion>");
37         out.println("</XMI.documentation>");
38         out.println("</XMI.header>");
39         out.println("<XMI.content>");
40     }
41
42     void writeEnd() {
43         out.println("</XMI.content>");
44         out.println("</XMI>");
45     }
46
47     void writePackage(RefPackage extent) {
48         Iterator it = extent.refAllClasses().iterator();
49         while (it.hasNext()) {
50             RefClass c = (RefClass) it.next();
51             Iterator it2 = c.refAllOfClass().iterator();
52             while (it2.hasNext()) {
53                 RefObject o = (RefObject) it2.next();
54                 writeOuterMostCompositeOf(o);
55             }
56         }
57         
58         it = extent.refAllAssociations().iterator();
59         while(it.hasNext()) {
60             RefAssociation a = (RefAssociation) it.next();
61             Iterator it2 = a.refAllLinks().iterator();
62             while(it2.hasNext()) {
63                 RefAssociationLink al = (RefAssociationLink) it2.next();
64                 writeOuterMostCompositeOf(al.refFirstEnd());
65                 writeOuterMostCompositeOf(al.refSecondEnd());
66             }
67         }
68         
69         // write subpackages
70
it = extent.refAllPackages().iterator();
71         while (it.hasNext()) {
72             RefPackage p = (RefPackage) it.next();
73             writePackage(p);
74         }
75     }
76     
77     
78     void writeOuterMostCompositeOf(RefObject o) {
79        RefObject outer = (RefObject) o.refOutermostComposite();
80        if(writtenOuterMostComposites.contains(outer)) {
81          return;
82        }
83        
84        writtenOuterMostComposites.add(outer);
85        write(outer);
86     }
87     
88     void writeReferencedObjects() {
89         while(!writtenOuterMostComposites.containsAll(referencedObjects)) {
90             RefObject[] objs = (RefObject[]) referencedObjects.toArray(new RefObject[0]);
91             for(int i=0; i<objs.length; i++) {
92                 writeOuterMostCompositeOf(objs[i]);
93             }
94         }
95     }
96     
97
98     void write(RefObject o) {
99         String JavaDoc qName = qName(o.refMetaObject());
100         out.print("<" + qName + " xmi.id='" + getId(o) + "'");
101         writePrimitiveValues(o);
102         out.println(">");
103         writeComplexValues(o);
104         out.println("</" + qName + ">");
105     }
106
107     int id_incrementer = 0; // use for generating xmi.id
108

109     String JavaDoc getId(RefObject o) {
110         String JavaDoc id = (String JavaDoc) object_id.get(o);
111         if (id != null)
112             return id;
113         id = String.valueOf(id_incrementer++);
114         object_id.put(o, id);
115         return id;
116     }
117
118     void writePrimitiveValues(RefObject o) {
119         Iterator it = allProperties(o.refMetaObject()).iterator();
120         while (it.hasNext()) {
121             RefObject p = (RefObject) it.next();
122             Object JavaDoc value = o.refGetValue(p);
123             if (value == null)
124                 continue;
125             if (isOfPrimitiveType(value)) {
126                 String JavaDoc vstring = value.toString();
127                 String JavaDoc encodedString = t.encode(vstring);
128                 out.print(" " + p.refGetValue("name") + "='" + encodedString + "'");
129             }
130         }
131     }
132
133     boolean isOfPrimitiveType(Object JavaDoc value) {
134         if (value instanceof RefObject)
135             return false;
136         if (value instanceof RefStruct)
137             return false;
138         if (value instanceof Collection)
139             return false;
140         return true;
141     }
142
143     void writeComplexValues(RefObject o) {
144         Iterator it = allProperties(o.refMetaObject()).iterator();
145         while (it.hasNext()) {
146             RefObject prop = (RefObject) it.next();
147             String JavaDoc qName = qName(prop);
148             Object JavaDoc value = o.refGetValue(prop);
149             if (value == null)
150                 continue;
151             // Multi-valued type
152
if ((value instanceof Collection)
153                 && !((Collection) value).isEmpty()) {
154                 Object JavaDoc[] values = ((Collection) value).toArray();
155                 if (isOfPrimitiveType(values[0])) {
156                     for (int i = 0; i < values.length; i++) {
157                         out.println(
158                             "<"
159                                 + qName
160                                 + ">"
161                                 + values[i].toString()
162                                 + "</"
163                                 + qName
164                                 + ">");
165                     }
166                 } else if (values[0] instanceof RefStruct) {
167                     for (int i = 0; i < values.length; i++) {
168                         out.println("<" + qName + ">");
169                         writeStructFields((RefStruct) values[i]);
170                         out.println("</" + qName + ">");
171                     }
172                 } else if (values[0] instanceof RefObject) {
173                     writeObjectValues(prop, (Collection)value);
174                     
175                 }
176                 // single-valued type
177
} else if (value instanceof RefObject) {
178                 writeObjectValue(prop, (RefObject)value);
179             
180             } else if (value instanceof RefStruct) {
181                 out.println("<" + qName + ">");
182                 writeStructFields((RefStruct) value);
183                 out.println("</" + qName + ">");
184             }
185         }
186     }
187     
188     
189     void writeObjectValues(RefObject prop, Collection c) {
190         String JavaDoc qName = qName(prop);
191         Iterator it = c.iterator();
192         if("Attribute".equals(prop.refMetaObject().refGetValue("name"))) {
193           out.println("<" + qName + ">");
194           while(it.hasNext()) {
195             RefObject v = (RefObject) it.next();
196             write(v);
197           }
198           out.println("</" + qName + ">");
199         } else {
200           RefObject end = (RefObject) prop.refGetValue("referencedEnd");
201           if( end.refGetValue("aggregation").toString().equals("composite") ) {
202             // do nothing
203
// this is impossible (a component cannot have many composites)
204
} else if( oppositeEnd(end).refGetValue("aggregation").toString().equals("composite") ) {
205             out.println("<" + qName + ">");
206             while(it.hasNext()) {
207               RefObject v = (RefObject) it.next();
208               write(v);
209             }
210             out.println("</" + qName + ">");
211           } else {
212             // association without by-value aggregation semantic
213
out.println("<" + qName + ">");
214             while(it.hasNext()) {
215               RefObject v = (RefObject) it.next();
216               writeObjectReference(v);
217             }
218             out.println("</" + qName + ">");
219           }
220         }
221     }
222     
223     
224     void writeObjectValue(RefObject prop, RefObject v) {
225         String JavaDoc qName = qName(prop);
226         if("Attribute".equals(prop.refMetaObject().refGetValue("name"))) {
227           out.println("<" + qName + ">");
228           write(v);
229           out.println("</" + qName + ">");
230         } else {
231           RefObject end = (RefObject) prop.refGetValue("referencedEnd");
232           if( end.refGetValue("aggregation").toString().equals("composite") ) {
233             // do nothing
234
} else if( oppositeEnd(end).refGetValue("aggregation").toString().equals("composite") ) {
235             out.println("<" + qName + ">");
236             write(v);
237             out.println("</" + qName + ">");
238           } else {
239             // association without by-value aggregation semantic
240
out.println("<" + qName + ">");
241             writeObjectReference(v);
242             out.println("</" + qName + ">");
243           }
244         }
245     }
246     
247     
248     RefObject oppositeEnd(RefObject end) {
249         RefObject asso = (RefObject) end.refGetValue("container");
250         Iterator it = ((List)asso.refGetValue("contents")).iterator();
251         while(it.hasNext()) {
252             RefObject o = (RefObject) it.next();
253             if("AssociationEnd".equals(o.refMetaObject().refGetValue("name"))
254               && !o.equals(end)
255             ) return o;
256         }
257         return null;
258     }
259     
260
261     void writeObjectReference(RefObject v) {
262         out.println(
263             "<"
264                 + qName(v.refMetaObject())
265                 + " xmi.idref='"
266                 + getId(v)
267                 + "'"
268                 + "/>");
269         referencedObjects.add(v.refOutermostComposite());
270     }
271
272     void writeStructFields(RefStruct struct) {
273         Iterator it = struct.refFieldNames().iterator();
274         while (it.hasNext()) {
275             Object JavaDoc value = struct.refGetValue((String JavaDoc) it.next());
276             out.print("<XMI.field>");
277             if (value instanceof RefObject) {
278                 writeObjectReference((RefObject) value);
279             } else if (value instanceof RefStruct) {
280                 writeStructFields((RefStruct) value);
281             } else {
282                 out.print(value);
283             }
284             out.println("</XMI.field>");
285         }
286     }
287
288     List allProperties(RefObject metaObjectOwner) {
289         List r = (List) propCach.get(metaObjectOwner);
290         if (r != null)
291             return r;
292         r = _allProperties(metaObjectOwner);
293         propCach.put(metaObjectOwner, r);
294         return r;
295     }
296
297     List _allProperties(RefObject metaObjectOwner) {
298         List r = new DistinctList();
299         Iterator it =
300             ((List) metaObjectOwner.refGetValue("supertypes")).iterator();
301         while (it.hasNext()) {
302             RefObject supertype = (RefObject) it.next();
303             r.addAll(allProperties(supertype));
304         }
305         it = ((List) metaObjectOwner.refGetValue("contents")).iterator();
306         while (it.hasNext()) {
307             RefObject content = (RefObject) it.next();
308             String JavaDoc type = (String JavaDoc) content.refMetaObject().refGetValue("name");
309             if ((type.equals("Attribute")
310                 && content.refGetValue("isDerived").equals(Boolean.FALSE))
311                 || type.equals("Reference")) {
312                 r.add(content);
313             }
314         }
315         return r;
316     }
317
318     String JavaDoc qName(RefObject meta) {
319         String JavaDoc r = (String JavaDoc) qNameCach.get(meta);
320         if (r != null)
321             return r;
322         r = _qName(meta);
323         qNameCach.put(meta, r);
324         return r;
325     }
326
327     private String JavaDoc _qName(RefObject meta) {
328         String JavaDoc name = (String JavaDoc) meta.refGetValue("name");
329         RefObject container = (RefObject) meta.refGetValue("container");
330         if (container == null)
331             return name;
332             
333         // write qualified property names
334
if( !caller.useFullyQualifiedName
335             && "Package".equals(container.refMetaObject().refGetValue("name")))
336             return name;
337         return _qName(container) + "." + name;
338     }
339
340 }
341
Popular Tags