Author: kmorin Date: 2009-05-15 13:59:00 +0000 (Fri, 15 May 2009) New Revision: 1402 Added: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/databinding/ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/databinding/BindingUtils.java guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/Generator.java guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/JavaArgument.java guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/JavaField.java guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/JavaFile.java guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/JavaFileGenerator.java guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/JavaMethod.java Removed: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/AbstractClassGenerator.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 guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/MainClassGenerator.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 guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/GuixGenerator.java guix/trunk/guix-compiler/src/test/java/org/nuiton/guix/GuixLauncherTest.java guix/trunk/guix-compiler/src/test/java/org/nuiton/guix/compiler/GuixCompilerTest.java Log: G?\195?\169n?\195?\169ration des classes 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-15 13:55:29 UTC (rev 1401) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/GuixLauncher.java 2009-05-15 13:59:00 UTC (rev 1402) @@ -4,26 +4,21 @@ 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; import java.util.HashMap; import java.util.List; import java.util.Map; 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.generator.MainClassGenerator; +import org.nuiton.guix.generator.Generator; +import org.nuiton.guix.generator.GuixGenerator; import org.nuiton.guix.model.ClassDescriptor; import org.nuiton.guix.model.GuixModelObject; import org.nuiton.guix.model.Rule; import org.nuiton.guix.model.Selector; import org.nuiton.guix.model.StyleSheet; -import org.xmlpull.v1.XmlPullParserFactory; import org.xmlpull.v1.XmlSerializer; /** @@ -45,8 +40,8 @@ protected List<String> guixFileClassNames = new ArrayList<String>(); /** Maps the root GuixModelObjects being compiled to the compiler instance * handling the compilation. */ - protected Map<GuixModelObject, GuixCompiler> rootModelObjects = - new HashMap<GuixModelObject, GuixCompiler>(); + protected Map<GuixModelObject, Long> rootModelObjects = + new HashMap<GuixModelObject, Long>(); /** CLassDescriptor met during the compilation */ protected List<ClassDescriptor> classDescriptors = new ArrayList<ClassDescriptor>(); @@ -55,6 +50,7 @@ private File targetDirectory; private String rootPackage; private String mainClass; + private Class generatorClass; /** * Constructor @@ -62,7 +58,7 @@ * @param files the files to compile */ public GuixLauncher(File[] files, File targetDirectory, String rootPackage, - File baseDir, String mainClass) { + File baseDir, String mainClass, Class generatorClass) { // Set up a simple configuration that logs on the console. this.files = files; this.targetDirectory = targetDirectory; @@ -94,6 +90,7 @@ else { mainClass = ""; } + this.generatorClass = generatorClass; } /** @@ -153,7 +150,7 @@ GuixCompiler compiler = new GuixCompiler(file, this, classPackage); GuixModelObject rootModelObject = compiler.compile(); - rootModelObjects.put(rootModelObject, compiler); + rootModelObjects.put(rootModelObject, compiler.getLastModification()); if (compiler.isFailed()) { success = false; @@ -177,225 +174,123 @@ } } } - if (success || true) { - XmlPullParserFactory factory = - XmlPullParserFactory.newInstance( - System.getProperty(XmlPullParserFactory.PROPERTY_NAME), - null); - XmlSerializer serializer = factory.newSerializer(); - File config = new File(destDir,"config.xml"); - - serializer.setOutput(new PrintWriter(config)); - serializer.startDocument("UTF-8", null); - serializer.setProperty("http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n"); - serializer.setProperty("http://xmlpull.org/v1/doc/properties.html#serializer-indentation", "\t"); - serializer.text("\n"); - serializer.docdecl(" beans SYSTEM \"http://www.springframework.org/dtd/spring-beans.dtd\""); - serializer.text("\n"); - serializer.startTag("", "beans"); - + if (generatorClass != null) { for (GuixModelObject mo : rootModelObjects.keySet()) { - String moClassName = mo.getClassDescriptor().getName(); - //File f = new File(destDir, mo.getClassDescriptor().getName() + "ModelTree.xml"); - File out = new File(destDir, moClassName + ".java"); - File outAbstract = new File(destDir, moClassName + "Abstract.java"); - File outImpl = new File(destDir, moClassName + "Impl.java"); - - if (log.isInfoEnabled()) { - log.info(moClassName + " last modification : " + new Date(rootModelObjects.get(mo).getLastModification())); + try { + Generator gen = (GuixGenerator) generatorClass.newInstance(); + gen.setDestDir(destDir); + gen.setGmo(mo); + gen.setLastModification(rootModelObjects.get(mo)); + gen.setMainClass((mo.getClassDescriptor().getPackageName() + "." + mo.getClassDescriptor().getName()).equals(mainClass)); + gen.generate(); + /*= new SwingGenerator(mo,destDir, + (mo.getClassDescriptor().getPackageName() + "." + mo.getClassDescriptor().getName()).equals(mainClass), + rootModelObjects.get(mo), serializer); + }*/ } - - if (rootModelObjects.get(mo).getLastModification() > out.lastModified()) { - if (log.isInfoEnabled()) { - log.info("Generation of " + mo.getClassDescriptor().getName()); - } - if (!out.exists()) { - out.createNewFile(); - } - if (!outAbstract.exists()) { - outAbstract.createNewFile(); - } - if (!outImpl.exists()) { - outImpl.createNewFile(); - } - /*XmlSerializer serializer = factory.newSerializer(); - XmlSerializer cssSerializer = factory.newSerializer(); - - serializer.setOutput(new PrintWriter(f)); - serializer.setProperty("http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n"); - serializer.setProperty("http://xmlpull.org/v1/doc/properties.html#serializer-indentation", "\t"); - - cssSerializer.setProperty("http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n"); - cssSerializer.setProperty("http://xmlpull.org/v1/doc/properties.html#serializer-indentation", "\t"); - - writeModelTree(serializer, mo, cssSerializer, destDir); - - 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); - - serializer.startTag("", "bean"); - serializer.attribute("", "id", moClassName.replace(moClassName.charAt(0), - Character.toLowerCase(moClassName.charAt(0)))); - serializer.attribute("", "class", - mo.getClassDescriptor().getPackageName() + "." + moClassName + "Impl"); - serializer.attribute("", "singleton", "false"); - serializer.endTag("", "bean"); - - if ((mo.getClassDescriptor().getPackageName() + "." + mo.getClassDescriptor().getName()).equals(mainClass)) { - MainClassGenerator mcg = new MainClassGenerator(mo, destDir); - FileWriter fw = null; - try { - fw = new FileWriter(new File(destDir, "Main.java")); - fw.write(mcg.getClassContent()); - fw.close(); - } - catch (IOException ex) { - if (log.isErrorEnabled()) { - log.error(ex); - } - } - finally { - try { - fw.close(); - } - catch (IOException ex) { - log.error(ex); - } - } - - serializer.startTag("", "bean"); - serializer.attribute("", "id", "main"); - serializer.attribute("", "class", mo.getClassDescriptor().getPackageName() + ".Main"); - serializer.startTag("", "property"); - serializer.attribute("", "name", moClassName.replace(moClassName.charAt(0), - Character.toLowerCase(moClassName.charAt(0)))); - serializer.startTag("", "ref"); - serializer.attribute("", "bean", moClassName.replace(moClassName.charAt(0), - Character.toLowerCase(moClassName.charAt(0)))); - serializer.endTag("", "ref"); - serializer.endTag("", "property"); - serializer.endTag("", "bean"); - } - /*<bean id = "articlesDao" class - - ="istia.st.articles.dao.ArticlesDaoPlainJdbc"> - <constructor-arg index="0"> - <value>org.firebirdsql.jdbc.FBDriver</value> - </constructor-arg> - <constructor-arg index="1"> - <value>jdbc:firebirdsql:localhost/3050:d:/databases/dbarticles.gdb</value> - </constructor-arg> - <constructor-arg index="2"> - <value>someone</value> - </constructor-arg> - <constructor-arg index="3"> - <value>somepassword</value> - </constructor-arg> - </bean>*/ - - + catch (InstantiationException ex) { + log.error(ex); } - else if (log.isWarnEnabled()) { - log.warn(mo.getClassDescriptor().getName() + " has already been generated and is up to date."); + catch (IllegalAccessException ex) { + log.error(ex); } + catch (NullPointerException ex) { + log.error(ex); + } } - serializer.endTag("", "beans"); - serializer.endDocument(); - - /*// pass 2 - if (!nextStep(LifeCycle.compile_second_pass, success)) { - return false; } - - assert guixFiles.size() == guixFileClassNames.size(); - List<File> jaxxFilesClone = new ArrayList<File>(guixFiles); - for (String className : guixFileClassNames) { - JAXXCompiler compiler = getCompiler(className, "Internal error: could not find compiler for " + className + " during second pass"); - addProfileTime(compiler, currentPass.name() + "_start"); - if (log.isDebugEnabled()) { - log.debug("runInitializers for " + className); + else { + if(log.isWarnEnabled()) { + log.warn("No generation language given"); + } } - if (!compiler.isFailed()) { - compiler.runInitializers(); - } - if (log.isDebugEnabled()) { - log.debug("compile second pass for " + className); - } - compiler.compileSecondPass(); - addProfileTime(compiler, currentPass.name() + "_end"); - if (log.isDebugEnabled()) { - log.debug("done with result [" + !compiler.isFailed() + "] for " + className); - } - if (compiler.isFailed()) { - success = false; - } - } - if (!jaxxFilesClone.equals(guixFiles)) { - throw new AssertionError("Internal error: compilation set altered during pass 2 (was " + jaxxFilesClone + ", modified to " + guixFiles + ")"); - } - // stylesheet application - if (!nextStep(LifeCycle.stylesheet_pass, success)) { - return false; - } - assert guixFiles.size() == guixFileClassNames.size(); - for (String className : guixFileClassNames) { - JAXXCompiler compiler = getCompiler(className, "Internal error: could not find compiler for " + className + " during stylesheet application"); - addProfileTime(compiler, currentPass.name() + "_start"); - compiler.applyStylesheets(); - addProfileTime(compiler, currentPass.name() + "_end"); - if (compiler.isFailed()) { - success = false; - } - } + /*// pass 2 + if (!nextStep(LifeCycle.compile_second_pass, success)) { + return false; + } - // code generation - if (!nextStep(LifeCycle.generate_pass, success)) { - return false; - } - assert guixFiles.size() == guixFileClassNames.size(); - List<Generator> generators = new ArrayList<Generator>(); - for (Generator generator : ServiceLoader.load(Generator.class)) { - generators.add(generator); - } - for (String className : guixFileClassNames) { - JAXXCompiler compiler = getCompiler(className, "Internal error: could not find compiler for " + className + " during code generation"); - addProfileTime(compiler, currentPass.name() + "_start"); - compiler.generateCode(generators); - addProfileTime(compiler, currentPass.name() + "_end"); - //compiler.generateCode(); - if (compiler.isFailed()) { - success = false; - } - } + assert guixFiles.size() == guixFileClassNames.size(); + List<File> jaxxFilesClone = new ArrayList<File>(guixFiles); + for (String className : guixFileClassNames) { + JAXXCompiler compiler = getCompiler(className, "Internal error: could not find compiler for " + className + " during second pass"); + addProfileTime(compiler, currentPass.name() + "_start"); + if (log.isDebugEnabled()) { + log.debug("runInitializers for " + className); + } + if (!compiler.isFailed()) { + compiler.runInitializers(); + } + if (log.isDebugEnabled()) { + log.debug("compile second pass for " + className); + } + compiler.compileSecondPass(); + addProfileTime(compiler, currentPass.name() + "_end"); + if (log.isDebugEnabled()) { + log.debug("done with result [" + !compiler.isFailed() + "] for " + className); + } + if (compiler.isFailed()) { + success = false; + } + } + if (!jaxxFilesClone.equals(guixFiles)) { + throw new AssertionError("Internal error: compilation set altered during pass 2 (was " + jaxxFilesClone + ", modified to " + guixFiles + ")"); + } - if (options.isProfile()) { - // profile pass (only if succes compile) - if (!nextStep(LifeCycle.profile_pass, success)) { - return false; - } - StringBuilder buffer = profiler.computeProfileReport(); - log.info(buffer.toString()); - } + // stylesheet application + if (!nextStep(LifeCycle.stylesheet_pass, success)) { + return false; + } + assert guixFiles.size() == guixFileClassNames.size(); + for (String className : guixFileClassNames) { + JAXXCompiler compiler = getCompiler(className, "Internal error: could not find compiler for " + className + " during stylesheet application"); + addProfileTime(compiler, currentPass.name() + "_start"); + compiler.applyStylesheets(); + addProfileTime(compiler, currentPass.name() + "_end"); + if (compiler.isFailed()) { + success = false; + } + } - return report(success); + // code generation + if (!nextStep(LifeCycle.generate_pass, success)) { + return false; + } + assert guixFiles.size() == guixFileClassNames.size(); + List<Generator> generators = new ArrayList<Generator>(); + for (Generator generator : ServiceLoader.load(Generator.class)) { + generators.add(generator); + } + for (String className : guixFileClassNames) { + JAXXCompiler compiler = getCompiler(className, "Internal error: could not find compiler for " + className + " during code generation"); + addProfileTime(compiler, currentPass.name() + "_start"); + compiler.generateCode(generators); + addProfileTime(compiler, currentPass.name() + "_end"); + //compiler.generateCode(); + if (compiler.isFailed()) { + success = false; + } + } - //FIXME : deal better the exception treatment... - } catch (CompilerException e) { - System.err.println(e.getMessage()); - e.printStackTrace(); - return false;*/ - } + if (options.isProfile()) { + // profile pass (only if succes compile) + if (!nextStep(LifeCycle.profile_pass, success)) { + return false; } + StringBuilder buffer = profiler.computeProfileReport(); + log.info(buffer.toString()); + } + + return report(success); + + //FIXME : deal better the exception treatment... + } catch (CompilerException e) { + System.err.println(e.getMessage()); + e.printStackTrace(); + return false; + }*/ + } catch (Throwable e) { e.printStackTrace(); return false; @@ -514,9 +409,9 @@ * @return false if another ClassDescriptor with the same classname * and package has a different script or superclass */ - public boolean registerClassDescriptor(ClassDescriptor classDescriptor) { + public ClassDescriptor registerClassDescriptor(ClassDescriptor classDescriptor) { if (classDescriptor == null || classDescriptor.getName() == null) { - return false; + return null; } 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())))) { @@ -528,8 +423,9 @@ if (log.isDebugEnabled()) { log.debug("new ClassDescriptor " + classDescriptor + " inserted"); } - return true; + return classDescriptor; } + classDescriptors.get(i).setPackageName(classDescriptor.getPackageName()); if (classDescriptor.getScript() != null) { if (classDescriptors.get(i).getScript() == null) { classDescriptors.get(i).setScript(classDescriptor.getScript()); @@ -541,7 +437,7 @@ if (log.isDebugEnabled()) { log.error("ClassDescriptor " + classDescriptor + " script already defined and different !"); } - return false; + return null; } } if (classDescriptor.getSuperClass() != null) { @@ -555,9 +451,9 @@ if (log.isErrorEnabled()) { log.error("ClassDescriptor " + classDescriptor + " superclass already defined and different !"); } - return false; + return null; } } - return true; + return classDescriptors.get(i); } } 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-15 13:55:29 UTC (rev 1401) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/compiler/GuixCompiler.java 2009-05-15 13:59:00 UTC (rev 1402) @@ -143,7 +143,10 @@ // the class name is the name of the file minus the extension String className = src.getName().substring(0, src.getName().lastIndexOf('.')); - rootMO.setClassDescriptor(new ClassDescriptor(className, srcPackage)); + // register the ClassDescriptor + ClassDescriptor cd = launcher.registerClassDescriptor(new ClassDescriptor(className, srcPackage)); + rootMO.setClassDescriptor(cd); + failed = cd == null; rootMO.getClassDescriptor().setSuperClass(new ClassDescriptor(tagName, tagPackageName)); rootMO.setAttributeDescriptors(getAttributes(xpp)); rootMO.setChildren(new ArrayList<GuixModelObject>()); @@ -184,9 +187,6 @@ // all the script tags have been recorded rootMO.getClassDescriptor().setScript(script.toString()); - // register the root ClassDescriptor - failed = !launcher.registerClassDescriptor(rootMO.getClassDescriptor()); - return rootMO; } } catch (XmlPullParserException ex) { @@ -247,7 +247,7 @@ // the parent is still the same prev = previousMO; } // if the tag is a script tag - else if ((xpp.getName() != null) && xpp.getName().equals("script")) { + else if ((xpp.getName() != null) && xpp.getName().equals("script")) { File scriptFile = null; // the name of the file to load @@ -268,7 +268,7 @@ // the parent is still the same prev = previousMO; } // if the tag is a class tag - else { + else { String tagNameSpace = xpp.getNamespace(); String tagPackageName = resolvePackageName(tagNameSpace, xpp.getName()); String tagName; @@ -288,17 +288,18 @@ GuixModelObject mo = new GuixModelObject(id, javaDoc, xpp.getAttributeValue("", "styleClass")); mo.setParent(previousMO); - mo.setClassDescriptor(new ClassDescriptor(tagName, tagPackageName)); + mo.setAttributeDescriptors(getAttributes(xpp)); mo.setChildren(new ArrayList<GuixModelObject>()); // add this GuixModelObject to the children of the parent previousMO.getChildren().add(mo); // register the ClassDescriptor - failed = !launcher.registerClassDescriptor(mo.getClassDescriptor()); + ClassDescriptor cd = launcher.registerClassDescriptor(new ClassDescriptor(tagName, tagPackageName)); + mo.setClassDescriptor(cd); + failed = cd == null; if (!failed) { - // parse the children of the tag prev = mo; } else { Added: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/databinding/BindingUtils.java =================================================================== --- guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/databinding/BindingUtils.java (rev 0) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/databinding/BindingUtils.java 2009-05-15 13:59:00 UTC (rev 1402) @@ -0,0 +1,155 @@ +package org.nuiton.guix.databinding; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import javax.swing.JToggleButton; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * + * @author kevin + */ +public class BindingUtils { + /** left brace matcher */ + protected static Matcher leftBraceMatcher = Pattern.compile("^(\\{)|[^\\\\](\\{)").matcher(""); + /** right brace matcher */ + protected static Matcher rightBraceMatcher = Pattern.compile("^(\\})|[^\\\\](\\})").matcher(""); + /** log */ + private static Log log = LogFactory.getLog(BindingUtils.class); + + /** + * Examine an attribute value for data binding expressions. Returns a 'cooked' expression which + * can be used to determine the resulting value. It is expected that this expression will be used + * as the source expression in a call to {@link #registerDataBinding}. + * If the attribute value does not invoke data binding, this method returns <code>null</code> + * + * @param stringValue the string value of the property from the XML + * @param type the type of the property, from the <code>JAXXPropertyDescriptor</code> + * @return a processed version of the expression + * @throws jaxx.CompilerException ? + */ + public static String processDataBindings(String stringValue) { + int pos = getNextLeftBrace(stringValue, 0); + if (pos != -1) { + StringBuffer expression = new StringBuffer(); + int lastPos = 0; + while (pos != -1 && pos < stringValue.length()) { + if (pos > lastPos) { + if (expression.length() > 0) { + expression.append(" + "); + } + expression.append('"'); + expression.append(escapeJavaString(stringValue.substring(lastPos, pos))); + expression.append('"'); + } + + if (expression.length() > 0) { + expression.append(" + "); + } + int pos2 = getNextRightBrace(stringValue, pos + 1); + if (pos2 == -1) { + if(log.isErrorEnabled()) + log.error("unmatched '{' in expression: " + stringValue); + return ""; + } + expression.append(stringValue.substring(pos + 1, pos2)); + pos2++; + if (pos2 < stringValue.length()) { + pos = getNextLeftBrace(stringValue, pos2); + lastPos = pos2; + } else { + pos = stringValue.length(); + lastPos = pos; + } + } + if (lastPos < stringValue.length()) { + if (expression.length() > 0) { + expression.append(" + "); + } + expression.append('"'); + expression.append(escapeJavaString(stringValue.substring(lastPos))); + expression.append('"'); + } + + StringBuffer el = new StringBuffer(); + Pattern p = Pattern .compile("\\.(get|is)([A-Z])([a-zA-Z0-9]*)\\(\\)"); + Matcher m = p.matcher(expression.toString()); + int mEnd = 0; + if(m.find()) { + do { + el.append(expression.toString().substring(mEnd, m.start())) + .append("."); + mEnd = m.end(); + el.append(m.group(2).toLowerCase()) + .append(m.group(3)); + } while (m.find()); + } + else { + el.append(expression.toString()); + } + return el.toString().length() > 0 ? el.toString().substring(0, el.toString().length()) : el.toString(); + } + return null; + } + + protected static int getNextLeftBrace(String string, int pos) { + leftBraceMatcher.reset(string); + return leftBraceMatcher.find(pos) ? Math.max(leftBraceMatcher.start(1), leftBraceMatcher.start(2)) : -1; + } + + protected static int getNextRightBrace(String string, int pos) { + leftBraceMatcher.reset(string); + rightBraceMatcher.reset(string); + int openCount = 1; + int rightPos; + while (openCount > 0) { + pos++; + int leftPos = leftBraceMatcher.find(pos) ? Math.max(leftBraceMatcher.start(1), leftBraceMatcher.start(2)) : -1; + rightPos = rightBraceMatcher.find(pos) ? Math.max(rightBraceMatcher.start(1), rightBraceMatcher.start(2)) : -1; + assert leftPos == -1 || leftPos >= pos; + assert rightPos == -1 || rightPos >= pos; + if (leftPos != -1 && leftPos < rightPos) { + pos = leftPos; + openCount++; + } else if (rightPos != -1) { + pos = rightPos; + openCount--; + } else { + openCount = 0; + } + } + return pos; + } + + /** + * Escapes a string using standard Java escape sequences, generally in preparation to including it in a string literal + * in a compiled Java file. + * + * @param raw the raw string to be escape + * @return a string in which all 'dangerous' characters have been replaced by equivalent Java escape sequences + */ + protected static String escapeJavaString(String raw) { + StringBuffer out = new StringBuffer(raw); + for (int i = 0; i < out.length(); i++) { + char c = out.charAt(i); + if (c == '\\' || c == '"') { + out.insert(i, '\\'); + i++; + } else if (c == '\n') { + out.replace(i, i + 1, "\\n"); + i++; + } else if (c == '\r') { + out.replace(i, i + 1, "\\r"); + i++; + } else if (c < 32 || c > 127) { + String value = Integer.toString((int) c, 16); + while (value.length() < 4) { + value = "0" + value; + } + out.replace(i, i + 1, "\\u" + value); + i += 5; + } + } + return out.toString(); + } +} Deleted: 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 2009-05-15 13:55:29 UTC (rev 1401) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/AbstractClassGenerator.java 2009-05-15 13:59:00 UTC (rev 1402) @@ -1,87 +0,0 @@ -package org.nuiton.guix.generator; - -//~--- non-JDK imports -------------------------------------------------------- - -import org.nuiton.guix.model.GuixModelObject; - -//~--- JDK imports ------------------------------------------------------------ - -import java.io.File; - -/** - * - * @author morin - */ -public class AbstractClassGenerator extends GuixGenerator { - public AbstractClassGenerator(GuixModelObject gmo) { - super(gmo); - } - - @Override - public void generate(File out) { - super.generate(); - - if (!gmo.getJavadoc().equals("")) { - sb.append("\n/**\n* "); - sb.append(gmo.getJavadoc()); - sb.append("\n*/"); - } - - sb.append("\npublic abstract class "); - sb.append(gmo.getClassDescriptor().getName()); - sb.append("Abstract implements "); - sb.append(gmo.getClassDescriptor().getName()); - sb.append(" {\n"); - addAttributes(gmo); - sb.append("\n\n\t//Getters and Setters"); - addGettersAndSetters(gmo); - sb.append("\n\npublic abstract void test();\n\n}"); - saveFile(out); - } - - @Override - protected void addGettersAndSetters(GuixModelObject gmo) { - for (GuixModelObject child : gmo.getChildren()) { - sb.append("\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("\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()) { - if (!child.getJavadoc().equals("")) { - sb.append("\n\t/**\n\t * "); - sb.append(child.getJavadoc()); - sb.append("\n\t */"); - } - - sb.append("\n\tprivate "); - sb.append(child.getClassDescriptor().getName()); - sb.append(" "); - sb.append(child.getId()); - sb.append(";\n"); - addAttributes(child); - } - } -} - Copied: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/Generator.java (from rev 1395, guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/GuixGenerator.java) =================================================================== --- guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/Generator.java (rev 0) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/Generator.java 2009-05-15 13:59:00 UTC (rev 1402) @@ -0,0 +1,30 @@ +package org.nuiton.guix.generator; +import java.io.File; +import org.nuiton.guix.model.GuixModelObject; +import org.xmlpull.v1.XmlSerializer; + +/** + * + * @author kevin + */ +public interface Generator { + + public void generate(); + + public File getDestDir(); + + public void setDestDir(File destDir); + + public GuixModelObject getGmo(); + + public void setGmo(GuixModelObject gmo); + + public Long getLastModification(); + + public void setLastModification(Long lastModification); + + public boolean isMainClass(); + + public void setMainClass(boolean mainClass); + +} Property changes on: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/Generator.java ___________________________________________________________________ Name: svn:mergeinfo + Modified: 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 2009-05-15 13:55:29 UTC (rev 1401) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/GuixGenerator.java 2009-05-15 13:59:00 UTC (rev 1402) @@ -1,81 +1,56 @@ -package org.nuiton.guix.generator; +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ -//~--- non-JDK imports -------------------------------------------------------- - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - +package org.nuiton.guix.generator; +import java.io.File; import org.nuiton.guix.model.GuixModelObject; +import org.xmlpull.v1.XmlSerializer; -//~--- JDK imports ------------------------------------------------------------ - -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; - -import java.util.ArrayList; -import java.util.List; - /** - * GuixGenerator * + * @author kevin */ -public abstract class GuixGenerator { +public abstract class GuixGenerator implements Generator { - /** log */ - protected Log log = LogFactory.getLog(GuixGenerator.class); - protected StringBuffer sb = new StringBuffer(); - protected List<String> classes = new ArrayList<String>(); - protected GuixModelObject gmo; + protected GuixModelObject gmo = null; + protected File destDir = null; + protected boolean mainClass = false; + protected Long lastModification = null; - public GuixGenerator(GuixModelObject gmo) { - this.gmo = gmo; + public abstract void generate(); + + public File getDestDir() { + return destDir; } - public abstract void generate(File out); + public void setDestDir(File destDir) { + this.destDir = destDir; + } - protected void generate() { - sb.append("package "); - sb.append(gmo.getClassDescriptor().getPackageName()); - sb.append(";\n\n"); - addImports(gmo); + public GuixModelObject getGmo() { + return gmo; } - protected abstract void addGettersAndSetters(GuixModelObject gmo); + public void setGmo(GuixModelObject gmo) { + this.gmo = gmo; + } - protected void addImports(GuixModelObject gmo) { - for (GuixModelObject child : gmo.getChildren()) { - String fqn = child.getClassDescriptor().getPackageName() + "." + child.getClassDescriptor().getName(); + public Long getLastModification() { + return lastModification; + } - if (!classes.contains(fqn)) { - classes.add(fqn); - sb.append("import " + fqn + ";\n"); - } + public void setLastModification(Long lastModification) { + this.lastModification = lastModification; + } - addImports(child); - } + public boolean isMainClass() { + return mainClass; } - 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) { - log.error(ex); - } - } + public void setMainClass(boolean mainClass) { + this.mainClass = mainClass; } -} - -//~ Formatted by Jindent --- http://www.jindent.com +} Deleted: 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 2009-05-15 13:55:29 UTC (rev 1401) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/ImplementationGenerator.java 2009-05-15 13:59:00 UTC (rev 1402) @@ -1,51 +0,0 @@ - -/* -* To change this template, choose Tools | Templates -* and open the template in the editor. - */ -package org.nuiton.guix.generator; - -//~--- non-JDK imports -------------------------------------------------------- - -import org.nuiton.guix.model.GuixModelObject; - -//~--- JDK imports ------------------------------------------------------------ - -import java.io.File; -import java.io.IOException; - -/** - * - * @author morin - */ -public class ImplementationGenerator extends GuixGenerator { - public ImplementationGenerator(GuixModelObject gmo) { - super(gmo); - } - - @Override - public void generate(File out) { - super.generate(); - - if (!gmo.getJavadoc().equals("")) { - sb.append("\n/**\n * "); - sb.append(gmo.getJavadoc()); - sb.append("\n */"); - } - - sb.append("\npublic class "); - sb.append(gmo.getClassDescriptor().getName()); - sb.append("Impl extends "); - sb.append(gmo.getClassDescriptor().getName()); - sb.append("Abstract {\n"); - sb.append("public void test() {System.out.println(\"ca marche\");}"); - sb.append("\n\n}"); - saveFile(out); - } - - @Override - protected void addGettersAndSetters(GuixModelObject gmo) {} -} - - -//~ Formatted by Jindent --- http://www.jindent.com Deleted: 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 2009-05-15 13:55:29 UTC (rev 1401) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/InterfaceGenerator.java 2009-05-15 13:59:00 UTC (rev 1402) @@ -1,62 +0,0 @@ -package org.nuiton.guix.generator; - -//~--- non-JDK imports -------------------------------------------------------- - -import org.nuiton.guix.model.GuixModelObject; - -//~--- JDK imports ------------------------------------------------------------ - -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; - -/** - * 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"); - sb.append("\n\t//Getters and Setters"); - addGettersAndSetters(gmo); - sb.append("\n\npublic void test();\n\n}"); - saveFile(out); - } - - @Override - protected void addGettersAndSetters(GuixModelObject gmo) { - for (GuixModelObject child : gmo.getChildren()) { - sb.append("\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("\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); - } - } -} - - -//~ Formatted by Jindent --- http://www.jindent.com Added: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/JavaArgument.java =================================================================== --- guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/JavaArgument.java (rev 0) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/JavaArgument.java 2009-05-15 13:59:00 UTC (rev 1402) @@ -0,0 +1,86 @@ +/* + * Copyright 2006 Ethan Nicholas. All rights reserved. + * Use is subject to license terms. + */ +package org.nuiton.guix.generator; + +/** + * Represents an argument to a <code>JavaMethod</code>. + * + * @see JavaMethod + */ +public class JavaArgument { + private String name; + private String type; + private boolean isFinal; + + + /** + * Creates a new <code>JavaArgument</code> with the specified name and type. For example, the method <code>main()</code> + * might have a <code>JavaArgument</code> with a name of <code>"arg"</code> and a type of <code>"java.lang.String[]"</code>. + * + * @param type the argument's type, as it would appear in Java source code + * @param name the argument's name + */ + public JavaArgument(String type, String name) { + this(type, name, false); + } + + + /** + * Creates a new <code>JavaArgument</code> with the specified name, type, and finality. For example, the method <code>main()</code> + * might have a <code>JavaArgument</code> with a name of <code>"arg"</code> and a type of <code>"java.lang.String[]"</code>. The + * <code>isFinal</code> parameter allows the presence of the <code>final</code> keyword on the argument to be controlled. + * + * @param type the argument's type, as it would appear in Java source code + * @param name the argument's name + * @param isFinal <code>true</code> if the argument should be marked final + */ + public JavaArgument(String type, String name, boolean isFinal) { + this.type = type; + this.name = name; + this.isFinal = isFinal; + } + + + /** + * Returns the argument's name. + * + * @return the name of the argument + */ + public String getName() { + return name; + } + + + /** + * Returns the argument's type as it would be represented in Java source code. + * + * @return the argument's type + */ + public String getType() { + return type; + } + + + /** + * Returns <code>true</code> if the <code>final</code> keyword should appear before the argument. + * + * @return <code>true</code> if the argument is final + */ + public boolean isFinal() { + return isFinal; + } + + + /** + * Returns the Java source code for this argument. + * + * @return the Java source code for this argument + */ + @Override + public String toString() { + String result = type + ' ' + name; + return isFinal ? "final " + result : result; + } +} \ No newline at end of file Added: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/JavaField.java =================================================================== --- guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/JavaField.java (rev 0) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/JavaField.java 2009-05-15 13:59:00 UTC (rev 1402) @@ -0,0 +1,187 @@ +/* + * Copyright 2006 Ethan Nicholas. All rights reserved. + * Use is subject to license terms. + */ +package org.nuiton.guix.generator; + +import java.lang.reflect.Modifier; +import java.util.Comparator; + +/** + * Represents a field in a Java source file being generated for output. <code>JavaFields</code> are created + * and added to a {@link JavaFile}, which can then output Java source code. + */ +public class JavaField implements Comparable<JavaField> { + + private int modifiers; + private String type; + private String name; + private String initializer; + + + /** + * Constructs a new <code>JavaField</code>. The <code>modifiers</code> parameter is a bit mask of the + * constants from {@link java.lang.reflect.Modifier}, and the <code>type</code> of the field should be + * represented as it would appear in Java source code. + * + * @param modifiers the modifier keywords that should appear as part of the field's declaration + * @param type the type of the field as it would appear in Java source code + * @param name the field's name + */ + public JavaField(int modifiers, String type, String name) { + this(modifiers, type, name, null); + } + + + /** + * Constructs a new <code>JavaField</code>. The <code>modifiers</code> parameter is a bit mask of the + * constants from <code>java.lang.reflect.Modifier</code>, and the <code>type</code> of the field should be + * represented as it would appear in Java source code. The <code>initializer</code> is the initial + * value of the field as it would appear in Java source code, or <code>null</code> to leave it at the + * default value. + * + * @param modifiers the modifier keywords that should appear as part of the field's declaration + * @param type the type of the field as it would appear in Java source code + * @param name the field's name + * @param initializer the initial value of the field, as it would appear in Java source code + */ + public JavaField(int modifiers, String type, String name, String initializer) { + this.modifiers = modifiers; + this.type = type; + this.name = name; + this.initializer = initializer; + } + + + /** + * Returns a bit mask describing the modifier keywords which should appear as part of this field's + * declaration. See <code>java.lang.reflect.Modifier</code> for more information on decoding this + * field. + * + * @return the modifier bit mask + */ + public int getModifiers() { + return modifiers; + } + + + /** + * Returns the field's name. + * + * @return the field's name + */ + public String getName() { + return name; + } + + + /** + * Returns the field's type, as it would be represented in Java source code. + * + * @return the field's type + */ + public String getType() { + return type; + } + + + /** + * Returns the Java source code for this field. + * + * @param lineSeparator line separator + * @return the Java source code for this field + */ + public String toString(String lineSeparator) { + StringBuffer result = new StringBuffer(); + result.append(JavaFile.getModifiersText(modifiers)); + result.append(type).append(' ').append(name); + if (initializer != null) { + result.append(" = ").append(initializer); + } + result.append(';').append(lineSeparator); + return result.toString(); + } + + public int compareTo(JavaField o) { + return COMPARATOR.compare(this, o); + } + + public static final Comparator<JavaField> COMPARATOR = new Comparator<JavaField>() { + + public int compare(JavaField o1, JavaField o2) { + + int result; + if ((result = compareStatic(o1, o2)) != 0) { + return result; + } + + // data sources must be on the last after all other fields + if ((result = compareDataSource(o1, o2)) != 0) { + return result; + } + + // same static + if ((result = compareVisibility(o1, o2)) != 0) { + return result; + } + // same visibility, test name + return o1.name.compareTo(o2.name); + } + + public int compareStatic(JavaField o1, JavaField o2) { + // first comparator modifiers : static always before none static + if (Modifier.isStatic(o1.modifiers) && !Modifier.isStatic(o2.modifiers)) { + return -1; + } + if (!Modifier.isStatic(o1.modifiers) && Modifier.isStatic(o2.modifiers)) { + return 1; + } + return 0; + } + + public int compareDataSource(JavaField o1, JavaField o2) { + // first comparator modifiers : static always before none static + if (o1.name.startsWith("$DataSource") && !o2.name.startsWith("$DataSource")) { + return 1; + } + if (!o1.name.startsWith("$DataSource") && o2.name.startsWith("$DataSource")) { + return -1; + } + return 0; + } + + public int compareVisibility(JavaField o1, JavaField o2) { + // first comparator modifiers : static always before none static + if (!Modifier.isPublic(o1.modifiers) && Modifier.isPublic(o2.modifiers)) { + return 1; + } + + if (Modifier.isPublic(o1.modifiers) && !Modifier.isPublic(o2.modifiers)) { + return -1; + } + + if (Modifier.isProtected(o1.modifiers) && !Modifier.isProtected(o2.modifiers)) { + return -1; + } + if (!Modifier.isProtected(o1.modifiers) && Modifier.isProtected(o2.modifiers)) { + return 1; + } + + if (Modifier.isPrivate(o1.modifiers) && !Modifier.isPrivate(o2.modifiers)) { + return -1; + } + if (!Modifier.isPrivate(o1.modifiers) && Modifier.isPrivate(o2.modifiers)) { + return 1; + } + return 0; + } + }; + + public static JavaField newField(int modifiers, String returnType, String name) { + return newField(modifiers, returnType, name, null); + } + + public static JavaField newField(int modifiers, String returnType, String name, String initializer) { + return new JavaField(modifiers, returnType, name, initializer); + } +} \ No newline at end of file Added: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/JavaFile.java =================================================================== --- guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/JavaFile.java (rev 0) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/JavaFile.java 2009-05-15 13:59:00 UTC (rev 1402) @@ -0,0 +1,337 @@ +package org.nuiton.guix.generator; + +import org.nuiton.guix.generator.JavaMethod.MethodOrder; + +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.EnumMap; +import java.util.List; +import java.util.Map.Entry; + +/** + * A Java source file being generated for output. Once the class is completely initialized, use the + * {@link #toString} method to generate source code for it. + */ +public class JavaFile { + + protected static final String GETTER_PATTERN = "return %1$s;"; + + protected static final String BOOLEAN_GETTER_PATTERN = "return %1$s !=null && %1$s;"; + + protected static final String SETTER_PATTERN = "this.%2$s = newValue;"; + + private int modifiers; + private String packageName; + private String className; + private List<String> imports = new ArrayList<String>(); + private List<JavaField> fields = new ArrayList<JavaField>(); + private List<JavaMethod> methods = new ArrayList<JavaMethod>(); + private List<JavaFile> innerClasses = new ArrayList<JavaFile>(); + private String superClass; + private List<String> interfaces; + private StringBuffer rawBodyCode = new StringBuffer(); + private boolean superclassIsGuixObject; + private String genericType; + private String superGenericType; + + + public JavaFile() { + } + + + public JavaFile(String packageName, int modifiers, String className, String superClass) { + this(packageName, modifiers, className, superClass, null); + } + + + public JavaFile(String packageName, int modifiers, String className, String superClass, List<String> interfaces) { + this.packageName = packageName; + this.modifiers = modifiers; + this.className = className; + this.superClass = superClass; + this.interfaces = interfaces; + } + + + public void addImport(String importString) { + imports.add(importString); + } + + public void addImport(Class importString) { + imports.add(importString.getName()); + } + + + public String[] getImports() { + return imports.toArray(new String[imports.size()]); + } + + public int getModifiers() { + return modifiers; + } + + + public void setModifiers(int modifiers) { + this.modifiers = modifiers; + } + + public String getPackageName() { + return packageName; + } + + public void setPackageName(String packageName) { + this.packageName = packageName; + } + + public String getClassName() { + return className; + } + + + public void setClassName(String className) { + this.className = className; + } + + + public String getSuperClass() { + return superClass; + } + + + public void setSuperClass(String superClass) { + this.superClass = superClass; + } + + + public List<String> getInterfaces() { + if (interfaces == null) { + interfaces = new ArrayList<String>(); + } + return interfaces; + } + + + public void setInterfaces(List<String> interfaces) { + this.interfaces = interfaces; + } + + public void setGenericType(String genericType) { + this.genericType = genericType; + } + + public void addMethod(JavaMethod method) { + methods.add(method); + } + + + public JavaMethod[] getMethods() { + return methods.toArray(new JavaMethod[methods.size()]); + } + + public void addField(JavaField field) { + addField(field, false); + } + + public void addField(JavaField field, boolean javaBean) { + addSimpleField(field); + String id = field.getName(); + String capitalizedName = (id.length()>0)? Character.toUpperCase(id.charAt(0))+id.substring(1) : id; + // add getter file + String content = String.format(GETTER_PATTERN, id); + addMethod(new JavaMethod(Modifier.PUBLIC, field.getType(), + "get" + capitalizedName, null, null, content)); + + if (javaBean) { + // add full javabean support + if (Boolean.class.getName().equals(field.getType())) { + content = String.format(BOOLEAN_GETTER_PATTERN, id); + addMethod(new JavaMethod(Modifier.PUBLIC, field.getType(), "is" + capitalizedName, null, null, content)); + } + content = String.format(SETTER_PATTERN, field.getType(), id); + JavaArgument arg = new JavaArgument(field.getType(), "newValue"); + addMethod(new JavaMethod(Modifier.PUBLIC, "void", "set" + capitalizedName, new JavaArgument[]{arg}, null, content)); + } + } + + public void addSimpleField(JavaField field) { + fields.add(field); + } + + + public JavaField[] getFields() { + return fields.toArray(new JavaField[fields.size()]); + } + + + public static String addIndentation(String source, int indentation, String lineSeparator) { + return indent(source, indentation, false, lineSeparator); + } + + + public static String setIndentation(String source, int indentation, String lineSeparator) { + return indent(source, indentation, true, lineSeparator); + } + + + public static String indent(String source, int indentation, boolean trim, String lineSeparator) { + if (trim) { + source = source.trim(); + } + char[] spaces = new char[indentation]; + Arrays.fill(spaces, ' '); + StringBuffer result = new StringBuffer(); + String[] lines = source.split(System.getProperty("line.separator") + "|\n"); + for (int i = 0; i < lines.length; i++) { + if (i > 0) { + result.append(lineSeparator); + } + result.append(spaces); + result.append(trim ? lines[i].trim() : lines[i]); + } + return result.toString(); + } + + + public void addBodyCode(String bodyCode) { + rawBodyCode.append(bodyCode); + } + + + public String getClassBody(String lineSeparator) { + StringBuffer result = new StringBuffer(); + if (fields.size() > 0) { + java.util.Collections.sort(fields); // sort fields + + for (JavaField field : fields) { + result.append(addIndentation(field.toString(lineSeparator), 4, lineSeparator)); + result.append(lineSeparator); + } + + result.append(lineSeparator); + } + + if (rawBodyCode.length() > 0) { + result.append(addIndentation("/* begin raw body code */\n", 4, lineSeparator)); + String s = rawBodyCode.toString(); + if (!s.startsWith(lineSeparator)) { + result.append(lineSeparator); + } + result.append(addIndentation(s, 4, lineSeparator)); + result.append(lineSeparator); + result.append(addIndentation("/* end raw body code */", 4, lineSeparator)); + result.append(lineSeparator); + } + + for (JavaFile innerClass : innerClasses) { + result.append(addIndentation(innerClass.toString(), 4, lineSeparator)); + result.append(lineSeparator).append(lineSeparator); + } + + EnumMap<MethodOrder, List<JavaMethod>> map = JavaMethod.getSortedMethods(methods); + for (Entry<MethodOrder, List<JavaMethod>> entry : map.entrySet()) { + List<JavaMethod> list = entry.getValue(); + entry.getKey().generate(result, list, lineSeparator); + list.clear(); + } + map.clear(); + + return result.toString(); + } + + + public String getClassDefinition(String lineSeparator) { + StringBuffer result = new StringBuffer(); + result.append(getModifiersText(modifiers)) + .append("class ") + .append(className.substring(className.lastIndexOf(".") + 1)); + if (genericType != null) { + result.append('<').append(genericType).append('>'); + } + if(superClass != null) { + result.append(" extends "); + result.append(superClass); + if (superGenericType != null) { + result.append('<').append(superGenericType).append('>'); + } + } + if (interfaces != null && !interfaces.isEmpty()) { + result.append(" implements ").append(interfaces.get(0)); + for (int i = 1; i < interfaces.size(); i++) { + result.append(", ").append(interfaces.get(i)); + } + } + result.append(" {"); + result.append(lineSeparator); + result.append(getClassBody(lineSeparator)); + result.append("}"); + return result.toString(); + } + + + public static String getModifiersText(int modifiers) { + if (modifiers == 0) { + return ""; + } else { + return Modifier.toString(modifiers) + ' '; + } + } + + + /** + * Returns the Java source code for this class. + * + * @param lineSeparator line separator + * @return a complete Java file for this class + */ + public String toString(String lineSeparator) { + StringBuffer result = new StringBuffer(); + if (packageName != null && !packageName.equals("")) { + result.append("package ").append(packageName).append(";"); + result.append(lineSeparator); + result.append(lineSeparator); + } + + if (imports.size() > 0) { + for (String anImport : imports) { + result.append("import "); + result.append(anImport); + result.append(';'); + result.append(lineSeparator); + } + result.append(lineSeparator); + } + + result.append(getClassDefinition(lineSeparator)); + return result.toString(); + } + + public void addInterface(String canonicalName) { + if (interfaces == null || !interfaces.contains(canonicalName)) { + getInterfaces().add(canonicalName); + } + } + + public void addInterfaces(String[] canonicalNames) { + if (canonicalNames == null) { + return; + } + for (String canonicalName : canonicalNames) { + if (interfaces == null || !interfaces.contains(canonicalName)) { + getInterfaces().add(canonicalName); + } + } + } + + public boolean isSuperclassIsGuixObject() { + return superclassIsGuixObject; + } + + public void setSuperclassIsGuixObject(boolean superclassIsGuixObject) { + this.superclassIsGuixObject = superclassIsGuixObject; + } + + public void setSuperGenericType(String superGenericType) { + this.superGenericType = superGenericType; + } +} \ No newline at end of file Added: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/JavaFileGenerator.java =================================================================== --- guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/JavaFileGenerator.java (rev 0) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/JavaFileGenerator.java 2009-05-15 13:59:00 UTC (rev 1402) @@ -0,0 +1,75 @@ +package org.nuiton.guix.generator; + +//~--- non-JDK imports -------------------------------------------------------- + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import org.nuiton.guix.model.GuixModelObject; + +//~--- JDK imports ------------------------------------------------------------ + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * GuixGenerator + * + */ +public abstract class JavaFileGenerator { + + /** log */ + protected Log log = LogFactory.getLog(JavaFileGenerator.class); + protected StringBuffer sb = new StringBuffer(); + protected List<String> classes = new ArrayList<String>(); + protected GuixModelObject gmo; + protected JavaFile jf; + + public JavaFileGenerator(GuixModelObject gmo) { + this.gmo = gmo; + } + + public abstract void generate(File out); + + protected void generate() { + addImports(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); + jf.addImport(fqn); + } + + addImports(child); + } + } + + protected void saveFile(File out) { + FileWriter fw = null; + try { + fw = new FileWriter(out); + fw.write(jf.toString("\n")); + fw.close(); + } catch (IOException ex) { + if (log.isErrorEnabled()) { + log.error(ex); + } + } finally { + try { + fw.close(); + } catch (IOException ex) { + log.error(ex); + } + } + } +} + + +//~ Formatted by Jindent --- http://www.jindent.com Added: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/JavaMethod.java =================================================================== --- guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/JavaMethod.java (rev 0) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/JavaMethod.java 2009-05-15 13:59:00 UTC (rev 1402) @@ -0,0 +1,504 @@ +/* + * Copyright 2006 Ethan Nicholas. All rights reserved. + * Use is subject to license terms. + */ +package org.nuiton.guix.generator; + +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.EnumMap; +import java.util.EnumSet; +import java.util.Iterator; +import java.util.List; + +/** + * Represents a method in a Java source file being generated for output. <code>JavaMethods</code> are created + * and added to a {@link JavaFile}, which can then output Java source code. In addition to normal methods, a + * <code>JavaMethod</code> can represent a constructor -- constructors should be named after their containing + * classes and have a return type of <code>null</code>. + */ +public class JavaMethod implements Comparable<JavaMethod> { + private int modifiers; + private String returnType; + private String name; + private JavaArgument[] arguments; + private String[] exceptions; + private StringBuffer bodyCode; + + + /** + * Constructs a new no-argument <code>JavaMethod</code> which throws no checked exceptions. The + * <code>modifiers</code> parameter is a bit mask of the constants from {@link java.lang.reflect.Modifier}, + * and the <code>returnType</code> of the method should be represented as it would appear in Java source + * code (<code>null</code> for a constructor). The method body is initially empty. + * + * @param modifiers the modifier keywords that should appear as part of the method's declaration + * @param returnType the return type of the method as it would appear in Java source code + * @param name the method's name + * @see #appendBodyCode + */ + //public JavaMethod(int modifiers, String returnType, String name) { + // this(modifiers, returnType, name, null); + //} + + + /** + * Constructs a new <code>JavaMethod</code> which throws no checked exceptions. The <code>modifiers</code> + * parameter is a bit mask of the constants from {@link java.lang.reflect.Modifier}, and the + * <code>returnType</code> of the method should be represented as it would appear in Java source code + * (<code>null</code> for a constructor). The method body is initially empty. + * + * @param modifiers the modifier keywords that should appear as part of the method's declaration + * @param returnType the return type of the method as it would appear in Java source code + * @param name the method's name + * @param arguments the method's arguments + * @see #appendBodyCode + */ + //public JavaMethod(int modifiers, String returnType, String name, JavaArgument[] arguments) { + // this(modifiers, returnType, name, arguments, null); + //} + + + /** + * Constructs a new <code>JavaMethod</code>. The <code>modifiers</code> parameter is a bit mask of the + * constants from {@link java.lang.reflect.Modifier}, and the <code>returnType</code> and <code>exceptions</code> + * of the method should be represented as they would appear in Java source code (<code>null</code> for a + * constructor). The method body is initially empty. + * + * @param modifiers the modifier keywords that should appear as part of the method's declaration + * @param returnType the return type of the method as it would appear in Java source code + * @param name the method's name + * @param arguments the method's arguments + * @param exceptions a list of exceptions the methods can throw, as they would be represented in Java source code + * @see #appendBodyCode + */ + //public JavaMethod(int modifiers, String returnType, String name, JavaArgument[] arguments, String[] exceptions) { + // this(modifiers, returnType, name, arguments, exceptions, null); + //} + + + /** + * Constructs a new <code>JavaMethod</code> containing the specified body code. The <code>modifiers</code> parameter + * is a bit mask of the constants from {@link java.lang.reflect.Modifier}, and the <code>returnType</code> and + * <code>exceptions</code> of the method should be represented as they would appear in Java source code (<code>null</code> + * for a constructor). The method body is initially empty. + * + * @param modifiers the modifier keywords that should appear as part of the method's declaration + * @param returnType the return type of the method as it would appear in Java source code + * @param name the method's name + * @param arguments the method's arguments + * @param exceptions a list of exceptions the methods can throw, as they would be represented in Java source code + * @param bodyCode Java source code which should appear in the method body + */ + public JavaMethod(int modifiers, String returnType, String name, JavaArgument[] arguments, String[] exceptions, String bodyCode) { + this.modifiers = modifiers; + this.returnType = returnType; + this.name = name; + this.arguments = arguments; + this.exceptions = exceptions; + this.bodyCode = bodyCode != null ? new StringBuffer(bodyCode) : null; + } + + + /** + * Returns a bit mask describing the modifier keywords which should appear as part of this method's + * declaration. See <code>java.lang.reflect.Modifier</code> for more information on decoding this + * field. + * + * @return the modifier bit mask + */ + public int getModifiers() { + return modifiers; + } + + + /** + * Returns the method's return type, as it would be represented in Java source code. + * + * @return the method's return type + */ + public String getReturnType() { + return returnType; + } + + + /** + * Returns the method's name. + * + * @return the method's name + */ + public String getName() { + return name; + } + + + /** + * Returns a list of the method's arguments. + * + * @return the method's arguments + */ + public JavaArgument[] getArguments() { + return arguments; + } + + + /** + * Returns a list of exceptions the method can throw. + * + * @return the method's exceptions + */ + public String[] getExceptions() { + return exceptions; + } + + + /** + * Returns the Java source code for the method's body. + * + * @return the method's body code + */ + public String getBodyCode() { + return bodyCode.toString(); + } + + + /** + * Appends additional code to the method's body. + * + * @param extraCode Java source code to append to the method's body + * @param lineSeparator line separator + */ + public void appendBodyCode(String extraCode, String lineSeparator) { + if (extraCode.length() == 0) { + return; + } + if (bodyCode.length() > 0 && !bodyCode.toString().endsWith(lineSeparator)) { + bodyCode.append(lineSeparator); + } + bodyCode.append(extraCode); + } + + + /** + * Returns the Java source code for this method. + * + * @param lineSeparator line separator + * @return the Java source code for this method + */ + public String toString(String lineSeparator) { + StringBuffer result = new StringBuffer(); + result.append(JavaFile.getModifiersText(modifiers)); + if (returnType != null) { + result.append(returnType); + result.append(' '); + } + result.append(name); + result.append('('); + if (arguments != null) { + for (int i = 0; i < arguments.length; i++) { + if (i > 0) { + result.append(", "); + } + result.append(arguments[i].toString()); + } + } + result.append(")"); + + if (bodyCode != null) { + result.append(" {"); + result.append(lineSeparator); + String formattedBodyCode = JavaFile.addIndentation(bodyCode.toString().trim(), 4, lineSeparator); + if (formattedBodyCode.length() > 0) { + result.append(formattedBodyCode); + result.append(lineSeparator); + } + result.append("}"); + } + else { + result.append(";"); + } + return result.toString(); + } + + public int compareTo(JavaMethod o) { + return COMPARATOR.compare(this, o); + } + + public static JavaMethod newMethod(int modifiers, String returnType, String name, String initializer, String[] exceptions, JavaArgument... arguments) { + return new JavaMethod(modifiers, returnType, name, arguments, exceptions, initializer); + } + + public static JavaMethod newMethod(int modifiers, String returnType, String name, String initializer, JavaArgument... arguments) { + return newMethod(modifiers, returnType, name, initializer, new String[0], arguments); + } + + public enum MethodOrder { + + statics(Modifier.STATIC, "/*---------------------------------------------------------------------------------*/\n" + + "/*-- Statics methods --------------------------------------------------------------*/\n" + + "/*---------------------------------------------------------------------------------*/") { + }, + + constructors(Modifier.PUBLIC, "/*---------------------------------------------------------------------------------*/\n" + + "/*-- Constructors -----------------------------------------------------------------*/\n" + + "/*---------------------------------------------------------------------------------*/") { + public boolean accept(JavaMethod method) { + return method.returnType == null; + } + }, + + JAXXObject(Modifier.PUBLIC, "/*---------------------------------------------------------------------------------*/\n" + + "/*-- JAXXObject implementation ----------------------------------------------------*/\n" + + "/*---------------------------------------------------------------------------------*/") { + private List<String> methods = Arrays.asList("applyDataBinding", "firePropertyChange", "getObjectById", "get$objectMap", "processDataBinding", "removeDataBinding"); + + public boolean accept(JavaMethod method) { + return methods.contains(method.name); + } + }, + + JAXXContext(Modifier.PUBLIC, "/*---------------------------------------------------------------------------------*/\n" + + "/*-- JAXXContext implementation ---------------------------------------------------*/\n" + + "/*---------------------------------------------------------------------------------*/") { + private List<String> methods = Arrays.asList("getContextValue", "getDelegateContext", "getParentContainer", "removeContextValue", "setContextValue"); + public boolean accept(JavaMethod method) { + return methods.contains(method.name); + } + }, + + JAXXValidation(Modifier.PUBLIC, "/*---------------------------------------------------------------------------------*/\n" + + "/*-- JAXXValidation implementation ------------------------------------------------*/\n" + + "/*---------------------------------------------------------------------------------*/") { + private List<String> methods = Arrays.asList("getValidator", "getValidatorIds"); + + public boolean accept(JavaMethod method) { + return methods.contains(method.name); + } + }, + + events(Modifier.PUBLIC, "/*---------------------------------------------------------------------------------*/\n" + + "/*-- Event methods ----------------------------------------------------------------*/\n" + + "/*---------------------------------------------------------------------------------*/") { + public boolean accept(JavaMethod method) { + return (method.name.startsWith("do") && method.name.indexOf("__") > -1); + } + }, + + publicGetters(Modifier.PUBLIC, "/*---------------------------------------------------------------------------------*/\n" + + "/*-- public acessor methods -------------------------------------------------------*/\n" + + "/*---------------------------------------------------------------------------------*/") { + public boolean accept(JavaMethod method) { + return (method.name.startsWith("get") || method.name.startsWith("is")); + } + }, + + publicSetters(Modifier.PUBLIC, "/*---------------------------------------------------------------------------------*/\n" + + "/*-- public mutator methods -------------------------------------------------------*/\n" + + "/*---------------------------------------------------------------------------------*/") { + public boolean accept(JavaMethod method) { + return (method.name.startsWith("set")); + } + }, + + otherPublic(Modifier.PUBLIC, "/*---------------------------------------------------------------------------------*/\n" + + "/*-- public mutator methods -------------------------------------------------------*/\n" + + "/*---------------------------------------------------------------------------------*/") { + @Override + public boolean accept(int mod) { + return super.accept(mod) && !Modifier.isStatic(mod); + } + }, + + protectedGetters(Modifier.PROTECTED, "/*---------------------------------------------------------------------------------*/\n" + + "/*-- protected acessors methods ---------------------------------------------------*/\n" + + "/*---------------------------------------------------------------------------------*/") { + public boolean accept(JavaMethod method) { + return (method.name.startsWith("get") || method.name.startsWith("is")); + } + }, + + createMethod(Modifier.PROTECTED | Modifier.PRIVATE, "/*---------------------------------------------------------------------------------*/\n" + + "/*-- ui creation methods ----------------------------------------------------------*/\n" + + "/*---------------------------------------------------------------------------------*/") { + public boolean accept(JavaMethod method) { + return method.name.startsWith("create") || method.name.startsWith("add") || + method.name.equals("$completeSetup") || + method.name.equals("$initialize"); + } + }, + + protecteds(Modifier.PROTECTED, "/*---------------------------------------------------------------------------------*/\n" + + "/*-- Other protected methods ------------------------------------------------------*/\n" + + "/*---------------------------------------------------------------------------------*/") { + }, + + packageLocal(0, "/*---------------------------------------------------------------------------------*/\n" + + "/*-- Package methods --------------------------------------------------------------*/\n" + + "/*---------------------------------------------------------------------------------*/") { + @Override + public boolean accept(int mod) { + return !Modifier.isStatic(mod) && !Modifier.isPublic(mod) && !Modifier.isProtected(mod); + }}, + + privates(Modifier.PRIVATE, "/*---------------------------------------------------------------------------------*/\n" + + "/*-- Private methods --------------------------------------------------------------*/\n" + + "/*---------------------------------------------------------------------------------*/") { + }; + + private final String header; + + private int modifier; + + MethodOrder(int modifier, String header) { + this.header = header; + this.modifier = modifier; + } + + public String getHeader() { + return header; + } + + public boolean accept(JavaMethod method) { + return true; + } + + public boolean accept(int mod) { + return (mod & modifier) != 0; + } + + public boolean accept(int mod, JavaMethod method) { + return accept(mod) && accept(method); + } + + public static MethodOrder valueOf(JavaMethod method, int scope) { + for (MethodOrder o : values()) { + if (o.accept(scope, method)) { + return o; + } + } + throw new IllegalArgumentException("could not find a " + MethodOrder.class + " for method " + method); + } + + public void generate(StringBuffer buffer, List<JavaMethod> methods, String lineSeparator) { + if (methods.isEmpty()) { + return; + } + buffer.append(JavaFile.addIndentation(header, 4, lineSeparator)); + buffer.append(lineSeparator).append(lineSeparator); + for (JavaMethod method : methods) { + buffer.append(JavaFile.addIndentation(method.toString(lineSeparator), 4, lineSeparator)); + buffer.append(lineSeparator).append(lineSeparator); + } + } + } + + public static EnumMap<MethodOrder, List<JavaMethod>> getSortedMethods(List<JavaMethod> methods) { + + EnumMap<MethodOrder, List<JavaMethod>> result = new EnumMap<MethodOrder, List<JavaMethod>>(MethodOrder.class); + for (MethodOrder methodOrder : MethodOrder.values()) { + result.put(methodOrder, new ArrayList<JavaMethod>()); + } + + EnumSet<MethodOrder> allConstants = EnumSet.allOf(MethodOrder.class); + List<JavaMethod> allMethods = new ArrayList<JavaMethod>(methods); + int[] scopes = new int[]{Modifier.STATIC, Modifier.PUBLIC, Modifier.PROTECTED, Modifier.PRIVATE}; + for (int scope : scopes) { + EnumSet<MethodOrder> constants = getMethodOrderScope(allConstants, scope); + + Iterator<JavaMethod> itMethods = allMethods.iterator(); + while (itMethods.hasNext()) { + JavaMethod method = itMethods.next(); + for (MethodOrder constant : constants) { + if (constant.accept(method.getModifiers(), method)) { + result.get(constant).add(method); + itMethods.remove(); + break; + } + } + } + constants.clear(); + } + + if (!allMethods.isEmpty()) { + throw new IllegalArgumentException("could not find a " + MethodOrder.class + " for method " + allMethods); + } + + for (MethodOrder methodOrder : MethodOrder.values()) { + // sort methods + java.util.Collections.sort(result.get(methodOrder)); + } + return result; + } + + public static EnumSet<MethodOrder> getMethodOrderScope(EnumSet<MethodOrder> allConstants, int scope) { + EnumSet<MethodOrder> constants = EnumSet.noneOf(MethodOrder.class); + for (MethodOrder order : allConstants) { + if (order.accept(scope)) { + constants.add(order); + } + } + return constants; + } + + public static final Comparator<JavaMethod> COMPARATOR = new Comparator<JavaMethod>() { + + public int compare(JavaMethod o1, JavaMethod o2) { + + /*int result; + if ((result = compareStatic(o1, o2)) != 0) { + return result; + } + if ((result = compareConstructor(o1, o2)) != 0) { + return result; + } + if ((result = compareVisibility(o1, o2)) != 0) { + return result; + }*/ + return o1.name.compareTo(o2.name); + } + + public int compareStatic(JavaMethod o1, JavaMethod o2) { + if (Modifier.isStatic(o1.modifiers) && !Modifier.isStatic(o2.modifiers)) { + return -1; + } + if (!Modifier.isStatic(o1.modifiers) && Modifier.isStatic(o2.modifiers)) { + return 1; + } + return 0; + } + + public int compareConstructor(JavaMethod o1, JavaMethod o2) { + if (o1.returnType == null && o2.returnType != null) { + return -1; + } + if (o1.returnType != null && o2.returnType == null) { + return 1; + } + return 0; + } + + public int compareVisibility(JavaMethod o1, JavaMethod o2) { + if (Modifier.isPublic(o1.modifiers) && !Modifier.isPublic(o2.modifiers)) { + return -1; + } + if (!Modifier.isPublic(o1.modifiers) && Modifier.isPublic(o2.modifiers)) { + return 1; + } + if (Modifier.isProtected(o1.modifiers) && !Modifier.isProtected(o2.modifiers)) { + return -1; + } + if (!Modifier.isProtected(o1.modifiers) && Modifier.isProtected(o2.modifiers)) { + return 1; + } + if (Modifier.isPrivate(o1.modifiers) && !Modifier.isPrivate(o2.modifiers)) { + return -1; + } + if (!Modifier.isPrivate(o1.modifiers) && Modifier.isPrivate(o2.modifiers)) { + return 1; + } + return 0; + } + }; +} \ No newline at end of file Deleted: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/MainClassGenerator.java =================================================================== --- guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/MainClassGenerator.java 2009-05-15 13:55:29 UTC (rev 1401) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/MainClassGenerator.java 2009-05-15 13:59:00 UTC (rev 1402) @@ -1,60 +0,0 @@ -package org.nuiton.guix.generator; - -//~--- non-JDK imports -------------------------------------------------------- - -import org.nuiton.guix.model.GuixModelObject; - -//~--- JDK imports ------------------------------------------------------------ - -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; - -/** - * Generator of interfaces - * - * @author morin - */ -public class MainClassGenerator { - private StringBuffer clazz = new StringBuffer(); - - public MainClassGenerator(GuixModelObject gmo, File destDir) { - String gmoClassName = gmo.getClassDescriptor().getName(); - String gmoAttributeName = gmoClassName.replace(gmoClassName.charAt(0), - Character.toLowerCase(gmoClassName.charAt(0))); - - clazz.append("package "); - clazz.append(gmo.getClassDescriptor().getPackageName()); - clazz.append(";\n\nimport java.io.File;\n" - + "import org.springframework.beans.factory.ListableBeanFactory;\n" - + "import org.springframework.beans.factory.xml.XmlBeanFactory;\n" - + "import org.springframework.core.io.FileSystemResource;\n" - + "\npublic class Main {" - + "\n\n\tpublic static void main(String[] args) {\n" + "\t\t//usine à beans\n" - + "\t\tListableBeanFactory bf = new XmlBeanFactory(" + - "new FileSystemResource(new File(\""); - clazz.append(destDir.getAbsolutePath()); - clazz.append("/config.xml\")));\n\t\t"); - clazz.append(gmoClassName); - clazz.append(" "); - clazz.append(gmoAttributeName); - clazz.append(" = ("); - clazz.append(gmoClassName); - clazz.append(") bf.getBean(\""); - clazz.append(gmoAttributeName); - clazz.append("\");\n\t\t"); - clazz.append(gmoAttributeName); - clazz.append(".test();" - + "\n\t}\n\n}"); - } - - public String getClassContent() { - return clazz.toString(); - } -} - Modified: guix/trunk/guix-compiler/src/test/java/org/nuiton/guix/GuixLauncherTest.java =================================================================== --- guix/trunk/guix-compiler/src/test/java/org/nuiton/guix/GuixLauncherTest.java 2009-05-15 13:55:29 UTC (rev 1401) +++ guix/trunk/guix-compiler/src/test/java/org/nuiton/guix/GuixLauncherTest.java 2009-05-15 13:59:00 UTC (rev 1402) @@ -22,7 +22,7 @@ public void compileTest() { GuixInitializer.initialize(); - GuixLauncher gl = new GuixLauncher(null,null,null,null,null); + GuixLauncher gl = new GuixLauncher(null,null,null,null,null,null); assertTrue(gl.files == null || gl.classNames.length == gl.files.length); assertTrue(gl.compile()); assertTrue(gl.files == null || gl.rootModelObjects.size() == gl.files.length); @@ -31,7 +31,7 @@ File f2 = new File(f.getAbsolutePath()); File dir = new File("target/test"); File dir2 = new File(dir.getAbsolutePath()); - gl = new GuixLauncher(new File[]{f2},dir2,"org.nuiton.guix",f2.getParentFile(), null); + gl = new GuixLauncher(new File[]{f2},dir2,"org.nuiton.guix",f2.getParentFile(), null,null); assertTrue(gl.files == null || gl.classNames.length == gl.files.length); assertTrue(gl.compile()); } @@ -41,36 +41,36 @@ */ @Test public void registerClassDescriptorTest() { - GuixLauncher gl = new GuixLauncher(null,null,null,null,null); - assertFalse(gl.registerClassDescriptor(null)); - assertFalse(gl.registerClassDescriptor(new ClassDescriptor(null, null))); + GuixLauncher gl = new GuixLauncher(null,null,null,null,null,null); + assertNull(gl.registerClassDescriptor(null)); + assertNull(gl.registerClassDescriptor(new ClassDescriptor(null, null))); ClassDescriptor cd = new ClassDescriptor("name", "package"); - assertTrue(gl.registerClassDescriptor(cd)); + assertNotNull(gl.registerClassDescriptor(cd)); cd.setSuperClass(new ClassDescriptor("super", "package")); - assertTrue(gl.registerClassDescriptor(cd) + assertTrue(gl.registerClassDescriptor(cd) != null && gl.classDescriptors.get(0).getSuperClass() != null); - assertTrue(gl.registerClassDescriptor(cd) + assertTrue(gl.registerClassDescriptor(cd) != null && gl.classDescriptors.size() == 1); ClassDescriptor cd2 = new ClassDescriptor("name", "package"); cd2.setSuperClass(new ClassDescriptor("super2", "package")); - assertFalse(gl.registerClassDescriptor(cd2)); + assertFalse(gl.registerClassDescriptor(cd2) != null); ClassDescriptor cd3 = new ClassDescriptor("name", "package"); cd3.setScript("script"); - assertTrue(gl.registerClassDescriptor(cd3) + assertTrue(gl.registerClassDescriptor(cd3) != null && gl.classDescriptors.size() == 1); ClassDescriptor cd4 = new ClassDescriptor("name", "package"); cd4.setScript("script2"); - assertFalse(gl.registerClassDescriptor(cd4)); + assertNull(gl.registerClassDescriptor(cd4)); - assertTrue(gl.registerClassDescriptor(new ClassDescriptor("name", null)) + assertTrue(gl.registerClassDescriptor(new ClassDescriptor("name", null)) != null && gl.classDescriptors.size() == 1); - assertTrue(gl.registerClassDescriptor(new ClassDescriptor("test", null)) + assertTrue(gl.registerClassDescriptor(new ClassDescriptor("test", null)) != null && gl.classDescriptors.size() == 2); - assertTrue(gl.registerClassDescriptor(new ClassDescriptor("test", null)) + assertTrue(gl.registerClassDescriptor(new ClassDescriptor("test", null)) != null && gl.classDescriptors.size() == 2); } } Modified: guix/trunk/guix-compiler/src/test/java/org/nuiton/guix/compiler/GuixCompilerTest.java =================================================================== --- guix/trunk/guix-compiler/src/test/java/org/nuiton/guix/compiler/GuixCompilerTest.java 2009-05-15 13:55:29 UTC (rev 1401) +++ guix/trunk/guix-compiler/src/test/java/org/nuiton/guix/compiler/GuixCompilerTest.java 2009-05-15 13:59:00 UTC (rev 1402) @@ -17,7 +17,7 @@ File f = new File("src/test/test.guix"); File f2 = new File(f.getAbsolutePath()); GuixLauncher gl = new GuixLauncher(new File[]{f2}, null, - "org.nuiton.guix",f2.getParentFile(),null); + "org.nuiton.guix",f2.getParentFile(),null,null); GuixCompiler gc = new GuixCompiler(null, null,null); assertNull(gc.compile());