[Buix-commits] r1388 - in guix/trunk/guix-compiler/src/main/java/org/nuiton/guix: . compiler generator
Author: kmorin Date: 2009-05-06 07:29:23 +0000 (Wed, 06 May 2009) New Revision: 1388 Added: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/AbstractClassGenerator.java guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/GuixGenerator.java guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/ImplementationGenerator.java guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/InterfaceGenerator.java Modified: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/GuixLauncher.java guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/compiler/GuixCompiler.java Log: Site Modified: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/GuixLauncher.java =================================================================== --- guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/GuixLauncher.java 2009-05-06 07:28:41 UTC (rev 1387) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/GuixLauncher.java 2009-05-06 07:29:23 UTC (rev 1388) @@ -4,7 +4,6 @@ import org.nuiton.guix.compiler.GuixCompiler; import java.io.File; import java.io.FileWriter; -import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; @@ -14,6 +13,9 @@ import java.util.Map.Entry; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.nuiton.guix.generator.AbstractClassGenerator; +import org.nuiton.guix.generator.ImplementationGenerator; +import org.nuiton.guix.generator.InterfaceGenerator; import org.nuiton.guix.model.ClassDescriptor; import org.nuiton.guix.model.GuixModelObject; import org.nuiton.guix.model.Rule; @@ -61,19 +63,21 @@ // Set up a simple configuration that logs on the console. this.files = files; this.targetDirectory = targetDirectory; - if(rootPackage != null) + if (rootPackage != null) { this.rootPackage = rootPackage; - else + } else { this.rootPackage = ""; + } if (files != null) { classNames = new String[files.length]; for (int i = 0; i < files.length; i++) { String path = files[i].getAbsolutePath(); - if(baseDir != null) + if (baseDir != null) { classNames[i] = rootPackage + "." + path.substring(baseDir.getAbsolutePath().length() + 1, - path.lastIndexOf('.')).replace(File.separatorChar,'.'); - else - classNames[i] = path.substring(0,path.lastIndexOf('.')); + path.lastIndexOf('.')).replace(File.separatorChar, '.'); + } else { + classNames[i] = path.substring(0, path.lastIndexOf('.')); + } } } else { classNames = null; @@ -93,7 +97,7 @@ guixFiles.addAll(Arrays.asList(files)); guixFileClassNames.addAll(Arrays.asList(classNames)); boolean success = true; - + try { assert guixFiles.size() == guixFileClassNames.size(); @@ -126,7 +130,7 @@ } } else { destDir = file.getParentFile(); - classPackage = destDir.getAbsolutePath().replace(File.separatorChar,'.'); + classPackage = destDir.getAbsolutePath().replace(File.separatorChar, '.'); } //compile the file GuixCompiler compiler = new GuixCompiler(file, this, classPackage); @@ -156,24 +160,34 @@ } } if (success || true) { - XmlPullParserFactory factory = - XmlPullParserFactory.newInstance( - System.getProperty(XmlPullParserFactory.PROPERTY_NAME), - null); + /*XmlPullParserFactory factory = + XmlPullParserFactory.newInstance( + System.getProperty(XmlPullParserFactory.PROPERTY_NAME), + null);*/ for (GuixModelObject mo : rootModelObjects.keySet()) { - File f = new File(destDir, mo.getClassDescriptor().getName() + "ModelTree.xml"); + //File f = new File(destDir, mo.getClassDescriptor().getName() + "ModelTree.xml"); + File out = new File(destDir, mo.getClassDescriptor().getName() + ".java"); + File outAbstract = new File(destDir, mo.getClassDescriptor().getName() + "Abstract.java"); + File outImpl = new File(destDir, mo.getClassDescriptor().getName() + "Impl.java"); + if (log.isInfoEnabled()) { log.info(mo.getClassDescriptor().getName() + " last modification : " + new Date(rootModelObjects.get(mo).getLastModification())); } - if (rootModelObjects.get(mo).getLastModification() > f.lastModified()) { + if (rootModelObjects.get(mo).getLastModification() > out.lastModified()) { if (log.isInfoEnabled()) { log.info("Generation of " + mo.getClassDescriptor().getName()); } - if (!f.exists()) { - f.createNewFile(); + if (!out.exists()) { + out.createNewFile(); } - XmlSerializer serializer = factory.newSerializer(); + if (!outAbstract.exists()) { + outAbstract.createNewFile(); + } + if (!outImpl.exists()) { + outImpl.createNewFile(); + } + /*XmlSerializer serializer = factory.newSerializer(); XmlSerializer cssSerializer = factory.newSerializer(); serializer.setOutput(new PrintWriter(f)); @@ -185,7 +199,17 @@ writeModelTree(serializer, mo, cssSerializer, destDir); - serializer.endDocument(); + serializer.endDocument();*/ + + InterfaceGenerator ing = new InterfaceGenerator(mo); + ing.generate(out); + + AbstractClassGenerator acg = new AbstractClassGenerator(mo); + acg.generate(outAbstract); + + ImplementationGenerator img = new ImplementationGenerator(mo); + img.generate(outImpl); + } else if (log.isWarnEnabled()) { log.warn(mo.getClassDescriptor().getName() + " has already been generated and is up to date."); } @@ -393,14 +417,11 @@ * and package has a different script or superclass */ public boolean registerClassDescriptor(ClassDescriptor classDescriptor) { - if(classDescriptor == null || classDescriptor.getName() == null) + if (classDescriptor == null || classDescriptor.getName() == null) { return false; + } int i = 0; - while (i < classDescriptors.size() - && (!classDescriptors.get(i).getName().equals(classDescriptor.getName()) - || (classDescriptors.get(i).getPackageName() != null - && classDescriptor.getPackageName() != null - && !classDescriptors.get(i).getPackageName().equals(classDescriptor.getPackageName())))) { + while (i < classDescriptors.size() && (!classDescriptors.get(i).getName().equals(classDescriptor.getName()) || (classDescriptors.get(i).getPackageName() != null && classDescriptor.getPackageName() != null && !classDescriptors.get(i).getPackageName().equals(classDescriptor.getPackageName())))) { i++; } //if the ClassDescriptor does not exist Modified: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/compiler/GuixCompiler.java =================================================================== --- guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/compiler/GuixCompiler.java 2009-05-06 07:28:41 UTC (rev 1387) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/compiler/GuixCompiler.java 2009-05-06 07:29:23 UTC (rev 1388) @@ -41,8 +41,12 @@ private ScriptHandler scriptHandler = new ScriptHandler(); /** Root of the model */ private GuixModelObject rootMO; - /** */ + /** Long representing the date and time of the last modification of any file + * used during compilation */ private long lastModification; + /** Integer used to give an id to the objects which don't have one specified + * by the user */ + private int index = 1; /*------------------------------------------------------------------------*/ /*-- Constructor methods -------------------------------------------------*/ @@ -216,9 +220,12 @@ tagName = xpp.getName(); } + String id = (xpp.getAttributeValue("", "id") != null ? + xpp.getAttributeValue("", "id") + : "$" + tagName + index++); //create the GuixModelObject representing the tag - GuixModelObject mo = new GuixModelObject(xpp.getAttributeValue("", "id"), - javaDoc, xpp.getAttributeValue("", "styleClass")); + GuixModelObject mo = new GuixModelObject(id, javaDoc, + xpp.getAttributeValue("", "styleClass")); mo.setParent(previousMO); mo.setClassDescriptor(new ClassDescriptor(tagName, tagPackageName)); mo.setChildren(new ArrayList<GuixModelObject>()); Added: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/AbstractClassGenerator.java =================================================================== --- guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/AbstractClassGenerator.java (rev 0) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/AbstractClassGenerator.java 2009-05-06 07:29:23 UTC (rev 1388) @@ -0,0 +1,81 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package org.nuiton.guix.generator; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import org.nuiton.guix.model.GuixModelObject; + +/** + * + * @author morin + */ +public class AbstractClassGenerator extends GuixGenerator { + + public AbstractClassGenerator(GuixModelObject gmo) { + super(gmo); + } + + @Override + public void generate(File out) { + super.generate(); + + sb.append("\npublic abstract class "); + sb.append(gmo.getClassDescriptor().getName()); + sb.append("Abstract {\n"); + + addAttributes(gmo); + + addGettersAndSetters(gmo); + + sb.append("\n\n}"); + + saveFile(out); + } + + @Override + protected void addGettersAndSetters(GuixModelObject gmo) { + for (GuixModelObject child : gmo.getChildren()) { + sb.append(child.getId().startsWith("$") ? "\n\n\tprotected " : "\n\n\tpublic "); + sb.append(child.getClassDescriptor().getName()); + sb.append(" get"); + sb.append(child.getId().replace(child.getId().charAt(0), + Character.toUpperCase(child.getId().charAt(0)))); + sb.append("() {\n\t\treturn this."); + sb.append((child.getId() != null && !child.getId().equals("")) ? child.getId() : "test"); + sb.append(";\n\t}"); + + sb.append(child.getId().startsWith("$") ? "\n\n\tprotected " : "\n\n\tpublic "); + sb.append("void set"); + sb.append(child.getId().replace(child.getId().charAt(0), + Character.toUpperCase(child.getId().charAt(0)))); + sb.append("("); + sb.append(child.getClassDescriptor().getName()); + sb.append(" "); + sb.append(child.getId()); + sb.append("){\n\t\tthis."); + sb.append(child.getId()); + sb.append(" = "); + sb.append(child.getId()); + sb.append(";\n\t}"); + + addGettersAndSetters(child); + } + } + + private void addAttributes(GuixModelObject gmo) { + for (GuixModelObject child : gmo.getChildren()) { + sb.append("\tprivate "); + sb.append(child.getClassDescriptor().getName()); + sb.append(" "); + sb.append(child.getId()); + sb.append(";\n"); + addAttributes(child); + } + } + +} Added: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/GuixGenerator.java =================================================================== --- guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/GuixGenerator.java (rev 0) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/GuixGenerator.java 2009-05-06 07:29:23 UTC (rev 1388) @@ -0,0 +1,70 @@ +package org.nuiton.guix.generator; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.guix.model.GuixModelObject; + +/** + * GuixGenerator + * + */ +public abstract class GuixGenerator { + + /** log */ + protected Log log = LogFactory.getLog(GuixGenerator.class); + protected GuixModelObject gmo; + protected StringBuffer sb = new StringBuffer(); + protected List<String> classes = new ArrayList<String>(); + + public GuixGenerator(GuixModelObject gmo) { + this.gmo = gmo; + } + + public abstract void generate(File out); + + protected void generate() { + sb.append("package "); + sb.append(gmo.getClassDescriptor().getPackageName()); + sb.append(";\n\n"); + + addImports(gmo); + } + + protected abstract void addGettersAndSetters(GuixModelObject gmo); + + protected void addImports(GuixModelObject gmo) { + for (GuixModelObject child : gmo.getChildren()) { + String fqn = child.getClassDescriptor().getPackageName() + "." + child.getClassDescriptor().getName(); + if (!classes.contains(fqn)) { + classes.add(fqn); + sb.append("import " + fqn + ";\n"); + } + addImports(child); + } + } + + protected void saveFile(File out) { + FileWriter fw = null; + try { + fw = new FileWriter(out); + fw.write(sb.toString()); + fw.close(); + } catch (IOException ex) { + if(log.isErrorEnabled()) + log.error(ex); + } finally { + try { + fw.close(); + } catch (IOException ex) { + Logger.getLogger(GuixGenerator.class.getName()).log(Level.SEVERE, null, ex); + } + } + } +} Added: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/ImplementationGenerator.java =================================================================== --- guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/ImplementationGenerator.java (rev 0) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/ImplementationGenerator.java 2009-05-06 07:29:23 UTC (rev 1388) @@ -0,0 +1,41 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package org.nuiton.guix.generator; + +import java.io.File; +import java.io.IOException; +import org.nuiton.guix.model.GuixModelObject; + +/** + * + * @author morin + */ +public class ImplementationGenerator extends GuixGenerator { + + public ImplementationGenerator(GuixModelObject gmo) { + super(gmo); + } + + @Override + public void generate(File out) { + super.generate(); + + sb.append("\npublic class "); + sb.append(gmo.getClassDescriptor().getName()); + sb.append("Impl extends "); + sb.append(gmo.getClassDescriptor().getName()); + sb.append("Abstract {\n"); + + sb.append("\n\n}"); + + saveFile(out); + } + + @Override + protected void addGettersAndSetters(GuixModelObject gmo) { + } + +} Added: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/InterfaceGenerator.java =================================================================== --- guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/InterfaceGenerator.java (rev 0) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/InterfaceGenerator.java 2009-05-06 07:29:23 UTC (rev 1388) @@ -0,0 +1,62 @@ +package org.nuiton.guix.generator; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.nuiton.guix.model.GuixModelObject; + +/** + * Generator of interfaces + * + * @author morin + */ +public class InterfaceGenerator extends GuixGenerator { + + public InterfaceGenerator(GuixModelObject gmo) { + super(gmo); + } + + @Override + public void generate(File out) { + super.generate(); + + sb.append("\npublic interface "); + sb.append(gmo.getClassDescriptor().getName()); + sb.append(" {\n"); + + addGettersAndSetters(gmo); + + sb.append("\n\n}"); + + saveFile(out); + } + + @Override + protected void addGettersAndSetters(GuixModelObject gmo) { + for (GuixModelObject child : gmo.getChildren()) { + sb.append(child.getId().startsWith("$") ? "\n\n\tprotected " : "\n\n\tpublic "); + sb.append(child.getClassDescriptor().getName()); + sb.append(" get"); + sb.append(child.getId().replace(child.getId().charAt(0), + Character.toUpperCase(child.getId().charAt(0)))); + sb.append("();"); + + sb.append(child.getId().startsWith("$") ? "\n\n\tprotected " : "\n\n\tpublic "); + sb.append("void set"); + sb.append(child.getId().replace(child.getId().charAt(0), + Character.toUpperCase(child.getId().charAt(0)))); + sb.append("("); + sb.append(child.getClassDescriptor().getName()); + sb.append(" "); + sb.append(child.getId()); + sb.append(");"); + + addGettersAndSetters(child); + } + } + +}
participants (1)
-
kmorin@users.labs.libre-entreprise.org