1 27 28 package org.objectweb.speedo.generation.compiler; 29 30 import org.apache.tools.ant.BuildException; 31 import org.apache.tools.ant.Project; 32 import org.apache.tools.ant.taskdefs.Javac; 33 import org.apache.tools.ant.types.Path; 34 import org.objectweb.speedo.generation.lib.AbstractGeneratorComponent; 35 import org.objectweb.speedo.generation.lib.NamingRules; 36 import org.objectweb.speedo.metadata.SpeedoClass; 37 import org.objectweb.speedo.metadata.SpeedoPackage; 38 import org.objectweb.speedo.metadata.SpeedoXMLDescriptor; 39 import org.objectweb.speedo.metadata.SpeedoIdentity; 40 import org.objectweb.speedo.api.SpeedoException; 41 import org.objectweb.speedo.api.SpeedoProperties; 42 import org.objectweb.util.monolog.api.BasicLevel; 43 import org.objectweb.util.monolog.wrapper.ant.MonologBuildListener; 44 45 import java.io.File ; 46 import java.util.Iterator ; 47 48 53 public class Compiler extends AbstractGeneratorComponent { 54 55 public final static String LOGGER_NAME 56 = SpeedoProperties.LOGGER_NAME + ".generation.compiler"; 57 58 61 public final static char separator = File.separatorChar; 62 63 66 protected Javac compiler; 67 68 73 public boolean init() { 74 logger = scp.loggerFactory.getLogger(LOGGER_NAME); 75 if (scp.getXmldescriptor().isEmpty()) 76 return false; 77 78 compiler = new Javac(); 80 Project projectCompiler = new Project(); 81 if (scp.javac != null) { 82 String c = scp.javac.getProject().getProperty("build.compiler"); 83 if (c != null && c.length() >0) { 84 projectCompiler.setProperty("build.compiler", c); 85 if (c.indexOf("eclipse") != -1) { 86 projectCompiler.addBuildListener(new MonologBuildListener(logger)); 87 } 88 } 89 } 90 projectCompiler.init(); 91 compiler.setProject(projectCompiler); 92 93 compiler.setClasspath(scp.classpath); 95 96 File destDirs = new File (scp.output); 98 destDirs.mkdirs(); 99 compiler.setDebug(true); 100 compiler.setDestdir(destDirs); 102 boolean javacdebug = scp.loggerFactory.getLogger(LOGGER_NAME + ".javac") 103 .isLoggable(BasicLevel.DEBUG); 104 compiler.setVerbose(javacdebug); 105 compiler.setNowarn(!javacdebug); 106 compiler.setDeprecation(javacdebug); 107 compiler.setClasspath(scp.speedoclasspath); 108 109 Path srcCompiler = new Path(compiler.getProject()); 111 srcCompiler.setLocation(new File (scp.output)); 112 113 logger.log(BasicLevel.DEBUG, "input: " + scp.input); 114 logger.log(BasicLevel.DEBUG, "output: " + scp.output); 115 for (Iterator itDesc = scp.getXmldescriptor().values().iterator(); itDesc.hasNext();) { 116 SpeedoXMLDescriptor desc = (SpeedoXMLDescriptor) itDesc.next(); 117 for (Iterator itPack = desc.jdoPackage.values().iterator(); itPack.hasNext();) { 118 SpeedoPackage sp = (SpeedoPackage) itPack.next(); 119 120 for (Iterator itclass = sp.jdoClass.values().iterator(); itclass.hasNext();) { 121 SpeedoClass sc = (SpeedoClass) itclass.next(); 122 String className = sc.getFQName(); 123 124 String c = NamingRules.proxyName(className).replace('.', '/') + ".java"; 125 compiler.createInclude().setName(c); 126 logger.log(BasicLevel.DEBUG, c); 127 128 c = NamingRules.fieldsName(className).replace('.', '/') + ".java"; 129 compiler.createInclude().setName(c); 130 logger.log(BasicLevel.DEBUG, c); 131 132 c = NamingRules.homeName(className).replace('.', '/') + ".java"; 133 compiler.createInclude().setName(c); 134 logger.log(BasicLevel.DEBUG, c); 135 136 c = NamingRules.fpncName(className).replace('.', '/') + ".java"; 137 compiler.createInclude().setName(c); 138 logger.log(BasicLevel.DEBUG, c); 139 140 c = NamingRules.kfpncName(className).replace('.', '/') + ".java"; 141 compiler.createInclude().setName(c); 142 logger.log(BasicLevel.DEBUG, c); 143 144 c = NamingRules.fqMappingName(className).replace('.', '/') + ".java"; 145 compiler.createInclude().setName(c); 146 logger.log(BasicLevel.DEBUG, c); 147 148 if (sc.identityType == SpeedoIdentity.USER_ID 149 && sc.generateObjectId()) { 150 c = NamingRules.generatedObjectIdName(sc.getFQName()) 151 .replace('.', '/') + ".java"; 152 compiler.createInclude().setName(c); 153 logger.log(BasicLevel.DEBUG, c); 154 } 155 } 156 } 157 } 158 compiler.setSrcdir(srcCompiler); 159 return true; 160 } 161 162 167 public void process() throws SpeedoException { 168 if (scp.getXmldescriptor().isEmpty()) 169 return; 170 try { 171 compiler.execute(); 172 } catch (BuildException e) { 173 throw new SpeedoException("Error during the compilation", e); 174 } 175 } 176 } 177 | Popular Tags |