1 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 ; 49 import java.util.ArrayList ; 50 import java.util.Iterator ; 51 import java.util.Map ; 52 import java.util.Collection ; 53 54 60 public class MetaInfoPrinter { 61 62 ArrayList mappingprinters = null; 63 public final static String TAB = " "; 64 65 public static void printMO(MetaObject mo) { 66 printMO("", mo, System.out); 67 } 68 69 public static void printMO(String prefix, MetaObject mo, PrintStream out) { 70 new MetaInfoPrinter(mo).print(prefix, mo, out); 71 } 72 73 public MetaInfoPrinter() { 74 mappingprinters = new ArrayList (); 75 } 76 77 public MetaInfoPrinter(MetaObject mo) { 78 this(((BasicMetaObject) mo).getManager()); 79 } 80 81 public MetaInfoPrinter(Manager m) { 82 Collection c = m.getMappingFactories(); 83 mappingprinters = new ArrayList (c.size()); 84 Iterator 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 p, MetaObject mo, Logger logger) { 96 print(p, mo, new PrintStreamImpl(logger)); 97 } 98 99 public void print(String p, MetaObject mo, PrintStream out) { 100 if (mo instanceof Package ) 101 print(p, (Package ) mo, out); 102 else if (mo instanceof Class ) 103 print(p, (Class ) 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 p, Manager mgr, PrintStream out) { 123 out.println("Print the meta information of the mnanager: " + mgr); 124 for (Iterator it = mgr.getPackages().iterator(); it.hasNext();) { 125 print(p, (Package ) it.next(), out); 126 } 127 } 128 129 public void print(String p, Package sc, PrintStream out) { 130 for (Iterator it = sc.getClasses().iterator(); it.hasNext();) { 131 print(p, (Class ) it.next(), out); 132 } 133 for (Iterator it = sc.iterateCompositeName(); it.hasNext();) { 134 print(p, (CompositeName) it.next(), out); 135 } 136 } 137 138 public void print(String p, CompositeName cn, PrintStream out) { 139 out.println(p + "* CompositeName " + cn.getFQName()); 140 Iterator it = cn.getAllFields().iterator(); 141 while (it.hasNext()) { 142 print(p, ((TypedElement) it.next()), out); 143 } 144 } 145 146 public void print(String p, Class clazz, PrintStream out) { 147 out.println(p + "* Class " + clazz.getFQName()); 148 Iterator it = clazz.getSuperClasses().iterator(); 149 while (it.hasNext()) { 150 out.println(p + TAB + "extends " + ((Class ) 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 msg = p + "Project: " + cp.getProjectName() + ","; 172 Iterator 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 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 p, TypedElement te, PrintStream out) { 189 if (te == null) { 190 out.print("ERROR: null field"); 191 return; 192 } 193 PType type = te.getType(); 194 String 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 p, PrimitiveElement pe, PrintStream out) { 209 if (pe.isScalar()) { 210 out.println(" (scalar)"); 211 } else { 212 out.println(); 213 } 214 } 215 216 public void print(String p, Reference ref, PrintStream 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 it = ref.getRefNameDef().iterator(); it.hasNext();) { 223 print(p + TAB, (NameDef) it.next(), out); 224 } 225 } 226 227 public void print(String p, NameDef nd, PrintStream 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 p, NameRef nr, PrintStream out) { 236 out.println(p + "NameRef name: " + nr.getName() + " / Compsosite name: " + nr.getCompositeName()); 237 out.println(p + TAB + "Projection: " + nr.getCompositeName()); 238 Map proj = nr.getProjection(); 239 for (Iterator it = proj.entrySet().iterator(); it.hasNext();) { 240 Map.Entry me = (Map.Entry ) 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 out) { 247 out.println("ClassRef: " + cr.getClassName()); 248 } 249 250 public void print(String p, GenClassRef gcr, PrintStream out) { 251 out.println(p + "GenClass " + gcr.getName() + "hidden persistent fields"); 252 for (Iterator 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 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 p, PrimitiveElementMapping vm, PrintStream out) { 277 for (Iterator 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 p, ClassMapping cm, PrintStream out) { 288 for (Iterator 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 p, GenClassMapping gcm, PrintStream out) { 299 for (Iterator 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 |