KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > metainfo > lib > MetaInfoPrinter


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

23 package org.objectweb.jorm.metainfo.lib;
24
25 import org.objectweb.jorm.metainfo.api.Class;
26 import org.objectweb.jorm.metainfo.api.ClassMapping;
27 import org.objectweb.jorm.metainfo.api.ClassRef;
28 import org.objectweb.jorm.metainfo.api.CompositeName;
29 import org.objectweb.jorm.metainfo.api.GenClassRef;
30 import org.objectweb.jorm.metainfo.api.Manager;
31 import org.objectweb.jorm.metainfo.api.NameDef;
32 import org.objectweb.jorm.metainfo.api.NameRef;
33 import org.objectweb.jorm.metainfo.api.PrimitiveElement;
34 import org.objectweb.jorm.metainfo.api.Reference;
35 import org.objectweb.jorm.metainfo.api.Package;
36 import org.objectweb.jorm.metainfo.api.TypedElement;
37 import org.objectweb.jorm.metainfo.api.PrimitiveElementMapping;
38 import org.objectweb.jorm.metainfo.api.MetaObject;
39 import org.objectweb.jorm.metainfo.api.MappingPrinter;
40 import org.objectweb.jorm.metainfo.api.GenClassMapping;
41 import org.objectweb.jorm.metainfo.api.Mapping;
42 import org.objectweb.jorm.metainfo.api.ClassProject;
43 import org.objectweb.jorm.metainfo.api.MappingFactory;
44 import org.objectweb.jorm.type.api.PType;
45 import org.objectweb.util.monolog.api.Logger;
46 import org.objectweb.util.monolog.wrapper.printwriter.PrintStreamImpl;
47
48 import java.io.PrintStream JavaDoc;
49 import java.util.ArrayList JavaDoc;
50 import java.util.Iterator JavaDoc;
51 import java.util.Map JavaDoc;
52 import java.util.Collection JavaDoc;
53
54 /**
55  * This class permits to print the jorm meta information in a PrintStream
56  * (System.out for example).
57  *
58  * @author S.Chassande-Barrioz
59  */

60 public class MetaInfoPrinter {
61
62     ArrayList JavaDoc mappingprinters = null;
63     public final static String JavaDoc TAB = " ";
64
65     public static void printMO(MetaObject mo) {
66         printMO("", mo, System.out);
67     }
68     
69     public static void printMO(String JavaDoc prefix, MetaObject mo, PrintStream JavaDoc out) {
70         new MetaInfoPrinter(mo).print(prefix, mo, out);
71     }
72
73     public MetaInfoPrinter() {
74         mappingprinters = new ArrayList JavaDoc();
75     }
76
77     public MetaInfoPrinter(MetaObject mo) {
78         this(((BasicMetaObject) mo).getManager());
79     }
80
81     public MetaInfoPrinter(Manager m) {
82         Collection JavaDoc c = m.getMappingFactories();
83         mappingprinters = new ArrayList JavaDoc(c.size());
84         Iterator JavaDoc it = c.iterator();
85         while (it.hasNext()) {
86             mappingprinters.add(
87                     ((MappingFactory) it.next()).createMappingPrinter());
88         }
89     }
90
91     public void addMappingPrinter(MappingPrinter vmp) {
92         mappingprinters.add(vmp);
93     }
94
95     public void print(String JavaDoc p, MetaObject mo, Logger logger) {
96         print(p, mo, new PrintStreamImpl(logger));
97     }
98
99     public void print(String JavaDoc p, MetaObject mo, PrintStream JavaDoc out) {
100         if (mo instanceof Package JavaDoc)
101             print(p, (Package JavaDoc) mo, out);
102         else if (mo instanceof Class JavaDoc)
103             print(p, (Class JavaDoc) mo, out);
104         else if (mo instanceof CompositeName)
105             print(p, (CompositeName) mo, out);
106         else if (mo instanceof TypedElement)
107             print(p, (TypedElement) mo, out);
108         else if (mo instanceof NameDef)
109             print(p, (NameDef) mo, out);
110         else if (mo instanceof NameRef)
111             print(p, (NameRef) mo, out);
112         else if (mo instanceof ClassMapping)
113             print(p, (ClassMapping) mo, out);
114         else if (mo instanceof GenClassMapping)
115             print(p, (GenClassMapping) mo, out);
116         else if (mo instanceof PrimitiveElementMapping)
117             print(p, (PrimitiveElementMapping) mo, out);
118         else
119             out.print("Unknown meta object: " + mo);
120     }
121
122     public void print(String JavaDoc p, Manager mgr, PrintStream JavaDoc out) {
123         out.println("Print the meta information of the mnanager: " + mgr);
124         for (Iterator JavaDoc it = mgr.getPackages().iterator(); it.hasNext();) {
125             print(p, (Package JavaDoc) it.next(), out);
126         }
127     }
128
129     public void print(String JavaDoc p, Package JavaDoc sc, PrintStream JavaDoc out) {
130         for (Iterator JavaDoc it = sc.getClasses().iterator(); it.hasNext();) {
131             print(p, (Class JavaDoc) it.next(), out);
132         }
133         for (Iterator JavaDoc it = sc.iterateCompositeName(); it.hasNext();) {
134             print(p, (CompositeName) it.next(), out);
135         }
136     }
137
138     public void print(String JavaDoc p, CompositeName cn, PrintStream JavaDoc out) {
139         out.println(p + "* CompositeName " + cn.getFQName());
140         Iterator JavaDoc it = cn.getAllFields().iterator();
141         while (it.hasNext()) {
142             print(p, ((TypedElement) it.next()), out);
143         }
144     }
145
146     public void print(String JavaDoc p, Class JavaDoc clazz, PrintStream JavaDoc out) {
147         out.println(p + "* Class " + clazz.getFQName());
148         Iterator JavaDoc it = clazz.getSuperClasses().iterator();
149         while (it.hasNext()) {
150             out.println(p + TAB + "extends " + ((Class JavaDoc) it.next()).getFQName());
151         }
152         out.println(p + "Persistent fields");
153
154         it = clazz.getAllFields().iterator();
155         while (it.hasNext()) {
156             print(p + TAB, (TypedElement) it.next(), out);
157         }
158         it = clazz.getAllHiddenFields().iterator();
159         out.println(p + "Hidden persistent fields");
160         while (it.hasNext()) {
161             print(p + TAB, (TypedElement) it.next(), out);
162         }
163         it = clazz.getNameDefs().iterator();
164         out.println(p + "NameDef of the class " + clazz.getName());
165         while (it.hasNext()) {
166             print(p + TAB, (NameDef) it.next(), out);
167         }
168         it = clazz.getClassProjects().iterator();
169         while (it.hasNext()) {
170             ClassProject cp = (ClassProject) it.next();
171             String JavaDoc msg = p + "Project: " + cp.getProjectName() + ",";
172             Iterator JavaDoc mit = cp.getMappings().iterator();
173             if (!mit.hasNext()) {
174                 out.println(msg + " no mapping defined ");
175             }
176             while (mit.hasNext()) {
177                 Mapping m = (Mapping) mit.next();
178                 out.println(msg + " Mapper: " + m.getMapperName());
179                 print(p, m.getClassMapping(), out);
180                 Iterator JavaDoc gcmit = m.getGenClassMappings().iterator();
181                 while (gcmit.hasNext()) {
182                     print(p + TAB, (GenClassMapping) gcmit.next(), out);
183                 }
184             }
185         }
186     }
187
188     public void print(String JavaDoc p, TypedElement te, PrintStream JavaDoc out) {
189         if (te == null) {
190             out.print("ERROR: null field");
191             return;
192         }
193         PType type = te.getType();
194         String JavaDoc strtype;
195         if (type == null)
196             strtype = " type is null";
197         else
198             strtype = te.getType().getJormName();
199         out.print(p + "- Field " + strtype + " " + te.getName());
200         if (te instanceof Reference)
201             print(p, (Reference) te, out);
202         else if (te instanceof PrimitiveElement)
203             print(p, (PrimitiveElement) te, out);
204         else
205             out.print("ERROR: Unknon typedlement" + te);
206     }
207
208     public void print(String JavaDoc p, PrimitiveElement pe, PrintStream JavaDoc out) {
209         if (pe.isScalar()) {
210             out.println(" (scalar)");
211         } else {
212             out.println();
213         }
214     }
215
216     public void print(String JavaDoc p, Reference ref, PrintStream JavaDoc out) {
217         if (ref instanceof ClassRef)
218             print((ClassRef) ref, out);
219         else if (ref instanceof GenClassRef)
220             print(p, (GenClassRef) ref, out);
221         out.println(p + "NameDef of the reference " + ref.getName());
222         for (Iterator JavaDoc it = ref.getRefNameDef().iterator(); it.hasNext();) {
223             print(p + TAB, (NameDef) it.next(), out);
224         }
225     }
226
227     public void print(String JavaDoc p, NameDef nd, PrintStream JavaDoc out) {
228         if (nd.isFieldName())
229             out.println(p + "fieldName: " + nd.getFieldName());
230         else if (nd.isNameRef()) {
231             print(p, nd.getNameRef(), out);
232         }
233     }
234
235     public void print(String JavaDoc p, NameRef nr, PrintStream JavaDoc out) {
236         out.println(p + "NameRef name: " + nr.getName() + " / Compsosite name: " + nr.getCompositeName());
237         out.println(p + TAB + "Projection: " + nr.getCompositeName());
238         Map JavaDoc proj = nr.getProjection();
239         for (Iterator JavaDoc it = proj.entrySet().iterator(); it.hasNext();) {
240             Map.Entry JavaDoc me = (Map.Entry JavaDoc) it.next();
241             out.println(p + TAB + "composite field name: " + me.getKey()
242                         + " / class field Name: " + me.getValue());
243         }
244     }
245
246     public void print(ClassRef cr, PrintStream JavaDoc out) {
247         out.println("ClassRef: " + cr.getClassName());
248     }
249
250     public void print(String JavaDoc p, GenClassRef gcr, PrintStream JavaDoc out) {
251         out.println(p + "GenClass " + gcr.getName() + "hidden persistent fields");
252         for (Iterator JavaDoc it = gcr.getHiddenFields().iterator(); it.hasNext();) {
253             print(p + TAB, (TypedElement) it.next(), out);
254         }
255         out.println(p + "NameDef of the GenClass " + gcr.getName());
256         for (Iterator JavaDoc it = gcr.getIdNameDef().iterator(); it.hasNext();) {
257             print(p + TAB, (NameDef) it.next(), out);
258         }
259         out.println(p + "=====Gen class " + gcr.getName() + " element: begin");
260         if (gcr.isPrimitive()) {
261             out.println(p + "GenClassRef " + gcr.getName() + " has a PrimitveElement");
262             print(p + TAB, (TypedElement) gcr.getPrimitiveElement(), out);
263         } else if (gcr.isClassRef()) {
264             out.println(p + "GenClassRef " + gcr.getName() + " has a PrimitveElement");
265             print(p + TAB, (TypedElement) gcr.getClassRef(), out);
266         } else if (gcr.isGenClassRef()) {
267             out.println(p + "GenClassRef " + gcr.getName() + " has a GenClassRef");
268             print(p + TAB, (TypedElement) gcr.getGenClassRef(), out);
269         } else
270             out.print("ERROR: unknown inner element type of the gen class: " + gcr.getName());
271         out.println(p + "=====Gen class " + gcr.getName() + " element: end");
272
273         out.println(p + "ClassMapping of the genclass " + gcr.getName());
274     }
275
276     public void print(String JavaDoc p, PrimitiveElementMapping vm, PrintStream JavaDoc out) {
277         for (Iterator JavaDoc it = mappingprinters.iterator(); it.hasNext();) {
278             MappingPrinter mp = (MappingPrinter) it.next();
279             if (mp.canPrint(vm)) {
280                 mp.print(p + TAB, vm, out);
281                 return;
282             }
283         }
284         out.println(p + "WARN: Unable to print the value mapping: " + vm);
285     }
286
287     public void print(String JavaDoc p, ClassMapping cm, PrintStream JavaDoc out) {
288         for (Iterator JavaDoc it = mappingprinters.iterator(); it.hasNext();) {
289             MappingPrinter mp = (MappingPrinter) it.next();
290             if (mp.canPrint(cm)) {
291                 mp.print(p + TAB, cm, out);
292                 return;
293             }
294         }
295         out.println(p + "WARN: Unable to print the class mapping: " + cm);
296     }
297
298     public void print(String JavaDoc p, GenClassMapping gcm, PrintStream JavaDoc out) {
299         for (Iterator JavaDoc it = mappingprinters.iterator(); it.hasNext();) {
300             MappingPrinter mp = (MappingPrinter) it.next();
301             if (mp.canPrint(gcm)) {
302                 mp.print(p + TAB, gcm, out);
303                 return;
304             }
305         }
306         out.println(p + "WARN: Unable to print the genclass mapping: " + gcm);
307     }
308
309 }
310
Popular Tags