1 18 package org.objectweb.jorm.generator.lib; 19 20 import org.objectweb.jorm.metainfo.api.Class; 21 import org.objectweb.jorm.metainfo.api.Package; 22 import org.objectweb.jorm.metainfo.api.NameDef; 23 import org.objectweb.jorm.metainfo.api.ClassProject; 24 import org.objectweb.jorm.metainfo.api.Mapping; 25 import org.objectweb.jorm.metainfo.api.ClassMapping; 26 import org.objectweb.jorm.util.io.api.TargetHolder; 27 import org.objectweb.jorm.compiler.api.JormCompilerParameter; 28 import org.objectweb.jorm.compiler.api.PExceptionCompiler; 29 import org.objectweb.jorm.compiler.api.JormCompilerConfigurator; 30 import org.objectweb.jorm.api.PException; 31 import org.objectweb.jorm.api.PMapper; 32 import org.objectweb.jorm.generator.api.MOP; 33 import org.objectweb.util.monolog.api.BasicLevel; 34 import org.apache.velocity.context.Context; 35 import org.apache.velocity.VelocityContext; 36 37 import java.io.FileWriter ; 38 import java.io.File ; 39 import java.util.Collection ; 40 import java.util.Iterator ; 41 import java.util.ArrayList ; 42 import java.util.HashMap ; 43 44 49 public class AbstractMappingGenerator extends CommonGenerator { 50 51 56 protected HashMap mmops = new HashMap (); 57 58 61 public AbstractMappingGenerator(JormCompilerConfigurator jcc, 62 JormCompilerParameter cp) throws PException { 63 super(); 64 Iterator it = jcc.knownMappers(); 65 while (it.hasNext()) { 66 String mn = (String ) it.next(); 67 mmops.put(mn, jcc.getMOPFactory(mn).createMappingMOP(cp, mn)); 68 } 69 } 70 71 public String [][] getTemplateLibraries() { 72 String [][] res = new String [mmops.size()][]; 73 Iterator it = mmops.values().iterator(); 74 int i = 0; 75 while (it.hasNext()) { 76 res[i] = ((MOP) it.next()).getTemplateLibraries(); 77 i++; 78 } 79 return res; 80 } 81 82 public void generate(Class clazz, 83 TargetHolder holder, 84 JormCompilerParameter cp, 85 JormCompilerConfigurator jcc) throws PException { 86 if (!clazz.isAbstract()) { 87 return; 88 } 89 Collection nds = clazz.getNameDefs(); 90 Class classWithNd = clazz; 91 while(nds.size() < 1 && classWithNd.getInheritedClassNumber() > 0) { 92 classWithNd = (Class ) classWithNd.getSuperClasses().iterator().next(); 93 nds = classWithNd.getNameDefs(); 94 } 95 if (nds.size() < 1) { 96 logger.log(BasicLevel.WARN, "AbstractMapping: No NameDef defined for the class " + clazz.getFQName()); 97 return; 98 } 99 if (nds.size() == 1) { 100 generate(clazz, holder, cp, (NameDef) nds.iterator().next(), null); 101 } else { 102 Iterator it = nds.iterator(); 103 while(it.hasNext()) { 104 NameDef nd = (NameDef) it.next(); 105 generate(clazz, holder, cp, nd, 106 (nd.getName() != null && nd.getName().length() == 0 107 ? null 108 : nd.getName())); 109 } 110 } 111 112 } 113 public void generate(Class clazz, 114 TargetHolder holder, 115 JormCompilerParameter cp, 116 NameDef nd, 117 String ndName) throws PException { 118 119 String packName = ((Package ) clazz.getParent()).getName(); 120 String fileName = clazz.getName() + PMapper.PCLASSMAPPINGAPPENDER; 122 if (ndName != null) { 123 if (packName != null && packName.length() > 0) { 124 packName += "." + ndName; 125 } else { 126 packName = ndName; 127 } 128 } 129 if (packName.length() > 0) { 130 fileName = (packName + '.').replace('.', File.separatorChar) + fileName; 131 } 132 logger.log(BasicLevel.DEBUG, "Generate the " + fileName + " class"); 133 Context ctx = new VelocityContext(); 134 ctx.put("class", clazz); 135 if (packName.length() > 0) { 136 ctx.put("package", packName); 137 } 138 ctx.put("cparam", cp); 139 CommonHelper ch = new CommonHelper(); 140 ch.setLogger(logger); 141 ctx.put("tools", ch); 142 ctx.put("mappername", ""); 143 ctx.put("noBindingCreation", Boolean.TRUE); 144 ctx.put("abstractMapping", Boolean.TRUE); 145 ctx.put("header", GEN_TEMPLATE_DIR + "Header.vm"); 146 ctx.put("mappingTools", this); 147 ClassProject classproject = clazz.getClassProject(cp.getProjectName()); 148 ArrayList dependencies = new ArrayList (); 149 if (classproject != null) { 150 Collection ms = classproject.getMappings(); 151 if (!ms.isEmpty()) { 152 Iterator it = ms.iterator(); 153 while(it.hasNext()) { 154 Mapping m = (Mapping) it.next(); 155 ClassMapping cm = m.getClassMapping(); 156 if (cm != null) { 157 dependencies.addAll(cm.getDependencies()); 158 } 159 } 160 } 161 } 162 ctx.put("dependencies", dependencies); 164 try { 165 FileWriter fw = holder.getFileWriter(fileName + ".java"); 167 velocityEngine.getTemplate(GEN_TEMPLATE_DIR + "Mapping.vm").merge(ctx, fw); 168 fw.flush(); 169 fw.close(); 170 } catch (Exception e) { 171 throw new PExceptionCompiler(e, "Problem while writing Mapping java file: " + fileName + ".java"); 172 } 173 174 } 175 } 176 | Popular Tags |