[Buix-commits] r1365 - in guix/trunk/guix-compiler/src: main/java/org/nuiton/guix main/java/org/nuiton/guix/compiler main/java/org/nuiton/guix/model main/java/org/nuiton/guix/tags main/resources test test/java/org/nuiton/guix test/java/org/nuiton/guix/compiler test/java/org/nuiton/guix/tags
Author: kmorin Date: 2009-04-29 15:41:14 +0000 (Wed, 29 Apr 2009) New Revision: 1365 Added: guix/trunk/guix-compiler/src/main/resources/log4j.properties guix/trunk/guix-compiler/src/test/java/org/nuiton/guix/GuixLauncherTest.java guix/trunk/guix-compiler/src/test/java/org/nuiton/guix/compiler/ guix/trunk/guix-compiler/src/test/java/org/nuiton/guix/compiler/GuixCompilerTest.java guix/trunk/guix-compiler/src/test/java/org/nuiton/guix/tags/ guix/trunk/guix-compiler/src/test/java/org/nuiton/guix/tags/TagManagerTest.java guix/trunk/guix-compiler/src/test/test.guix 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/model/AttributeDescriptor.java guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/model/ClassDescriptor.java guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/tags/StyleHandler.java guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/tags/TagManager.java Log: log + test unitaires + corrections des bugs d?\195?\169couverts grace aux tests unitaires 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-04-29 08:06:47 UTC (rev 1364) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/GuixLauncher.java 2009-04-29 15:41:14 UTC (rev 1365) @@ -9,11 +9,11 @@ import java.util.Arrays; import java.util.Date; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import org.apache.log4j.Logger; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.nuiton.guix.model.ClassDescriptor; import org.nuiton.guix.model.GuixModelObject; import org.nuiton.guix.model.Rule; @@ -30,7 +30,7 @@ public class GuixLauncher { /** log */ - Logger log = Logger.getLogger(GuixLauncher.class); + protected static final Log log = LogFactory.getLog(GuixLauncher.class); /** original list of files to compile */ protected final File[] files; /** original list of classes to compile */ @@ -49,19 +49,34 @@ private List<File> compiledFiles = new ArrayList<File>(); protected int compilerCount; private File targetDirectory; + private String rootPackage; /** * Constructor * * @param files the files to compile */ - public GuixLauncher(File[] files, File targetDirectory) { + public GuixLauncher(File[] files, File targetDirectory, String rootPackage, + File baseDir) { // Set up a simple configuration that logs on the console. this.files = files; this.targetDirectory = targetDirectory; - classNames = new String[files.length]; - for (int i = 0; i < files.length; i++) { - classNames[i] = files[i].getName().substring(0, files[i].getName().lastIndexOf('.')); + if(rootPackage != null) + this.rootPackage = rootPackage; + 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) + classNames[i] = rootPackage + "." + path.substring(baseDir.getAbsolutePath().length() + 1, + path.lastIndexOf('.')).replace(File.separatorChar,'.'); + else + classNames[i] = path.substring(0,path.lastIndexOf('.')); + } + } else { + classNames = null; } } @@ -73,185 +88,204 @@ */ public synchronized boolean compile() { compilerCount = 0; - guixFiles.addAll(Arrays.asList(files)); - guixFileClassNames.addAll(Arrays.asList(classNames)); - boolean success = true; + File destDir = targetDirectory; + if (files != null) { + guixFiles.addAll(Arrays.asList(files)); + guixFileClassNames.addAll(Arrays.asList(classNames)); + boolean success = true; + + try { + assert guixFiles.size() == guixFileClassNames.size(); - try { - assert guixFiles.size() == guixFileClassNames.size(); + for (int i = 0; i < guixFiles.size(); i++) { + File file = guixFiles.get(i); + String className = guixFileClassNames.get(i); - for (int i = 0; i < guixFiles.size(); i++) { - File file = guixFiles.get(i); - String className = guixFileClassNames.get(i); + //if we have not compiled the file yet + if (!compiledFiles.contains(file)) { + if (log.isInfoEnabled()) { + log.info("Compiling class " + className); + } + compiledFiles.add(file); - //if we have not compiled the file yet - if (!compiledFiles.contains(file)) { - log.info("Compiling class " + className); - compiledFiles.add(file); - - File destDir = targetDirectory; - if (destDir != null) { - int dotPos = className.lastIndexOf("."); - if (dotPos != -1) { - destDir = new File(destDir, className.substring(0, dotPos).replace('.', File.separatorChar)); + String classPackage; + if (targetDirectory != null) { + int dotPos = className.lastIndexOf("."); + if (dotPos != -1) { + destDir = new File(targetDirectory, className.substring(0, dotPos).replace('.', File.separatorChar)); + classPackage = className.substring(0, dotPos); + } else { + destDir = new File(targetDirectory, className); + classPackage = className; + } + if (!destDir.exists() && !destDir.mkdirs()) { + if (log.isWarnEnabled()) { + log.warn("couldn't create directory " + destDir); + } + continue; + } } else { - destDir = new File(destDir, className); + destDir = file.getParentFile(); + classPackage = destDir.getAbsolutePath().replace(File.separatorChar,'.'); } - if (!destDir.exists() && !destDir.mkdirs()) { - log.warn("couldn't create directory " + destDir); - continue; + //compile the file + GuixCompiler compiler = new GuixCompiler(file, this, classPackage); + + GuixModelObject rootModelObject = compiler.compile(); + rootModelObjects.put(rootModelObject, compiler); + + if (compiler.isFailed()) { + success = false; } } else { - destDir = file.getParentFile(); + if (log.isWarnEnabled()) { + log.warn(file.getName() + " has already been compiled."); + } } - //compile the file - GuixCompiler compiler = new GuixCompiler( - file.getParentFile(), file, className, this); + } - GuixModelObject rootModelObject = compiler.compile(); - rootModelObjects.put(rootModelObject, compiler); - - if (compiler.isFailed()) { - success = false; + if (success) { + int i = 0; + while (success && i < classDescriptors.size()) { + success = (classDescriptors.get(i++).getPackageName() != null); } - } else { - log.warn(file.getName() + " has already been compiled."); + if (!success) { + if (log.isErrorEnabled()) { + log.error("The class '" + classDescriptors.get(--i).getName() + "' has no package."); + } + } } - } + if (success || true) { + XmlPullParserFactory factory = + XmlPullParserFactory.newInstance( + System.getProperty(XmlPullParserFactory.PROPERTY_NAME), + null); - if (success) { - int i = 0; - while (success && i < classDescriptors.size()) { - success = (classDescriptors.get(i++).getPackageName() != null); - } - if (!success) { - log.error("The class '" + classDescriptors.get(--i).getName() + "' has no package."); - } - } - if (success || true) { - XmlPullParserFactory factory = - XmlPullParserFactory.newInstance( - System.getProperty(XmlPullParserFactory.PROPERTY_NAME), - null); - - for (GuixModelObject mo : rootModelObjects.keySet()) { - File f = new File(targetDirectory, mo.getClassDescriptor().getName() + "ModelTree.xml"); - log.info(mo.getClassDescriptor().getName() + " last modification : " - + new Date(rootModelObjects.get(mo).getLastModification())); - - if (rootModelObjects.get(mo).getLastModification() > f.lastModified()) { - log.info("Generation of " + mo.getClassDescriptor().getName()); - if (!f.exists()) { - f.createNewFile(); + for (GuixModelObject mo : rootModelObjects.keySet()) { + File f = new File(destDir, mo.getClassDescriptor().getName() + "ModelTree.xml"); + if (log.isInfoEnabled()) { + log.info(mo.getClassDescriptor().getName() + " last modification : " + new Date(rootModelObjects.get(mo).getLastModification())); } - XmlSerializer serializer = factory.newSerializer(); - XmlSerializer cssSerializer = factory.newSerializer(); + if (rootModelObjects.get(mo).getLastModification() > f.lastModified()) { + if (log.isInfoEnabled()) { + log.info("Generation of " + mo.getClassDescriptor().getName()); + } + if (!f.exists()) { + f.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"); + 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"); + 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); + writeModelTree(serializer, mo, cssSerializer, destDir); - serializer.endDocument(); + serializer.endDocument(); + } else if (log.isWarnEnabled()) { + log.warn(mo.getClassDescriptor().getName() + " has already been generated and is up to date."); + } } - else { - log.warn(mo.getClassDescriptor().getName() + " has already been generated and is up to date."); - } + + /*// pass 2 + if (!nextStep(LifeCycle.compile_second_pass, success)) { + return false; } - /*// 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); + } + 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 + ")"); + } - 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 + ")"); - } + // 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; + } + } - // 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; - } - } + // 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; + } + } - // 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; - } - } + 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()); + } - if (options.isProfile()) { - // profile pass (only if succes compile) - if (!nextStep(LifeCycle.profile_pass, success)) { - return false; + 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; } - StringBuilder buffer = profiler.computeProfileReport(); - log.info(buffer.toString()); + return success; + } else { + if (log.isWarnEnabled()) { + log.warn("No file to compile"); } - - 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; + return true; } - return success; } /** @@ -264,11 +298,11 @@ * @throws java.io.IOException */ private void writeModelTree(XmlSerializer serializer, GuixModelObject mo, - XmlSerializer cssSerializer) throws IOException { + XmlSerializer cssSerializer, File destDir) throws IOException { if (!mo.getStyleSheets().isEmpty()) { int i = 0; for (StyleSheet styleSheet : mo.getStyleSheets()) { - File ss = new File(targetDirectory, mo.getClassDescriptor().getName() + i++ + "SS.xml"); + File ss = new File(destDir, mo.getClassDescriptor().getName() + i++ + "SS.xml"); cssSerializer.setOutput(new FileWriter(ss)); cssSerializer.startTag("", "rules"); for (Rule rule : styleSheet.getRules()) { @@ -338,16 +372,15 @@ serializer.endTag("", "script"); serializer.startTag("", "superclass"); if (mo.getClassDescriptor().getSuperClass() != null) { - serializer.text(mo.getClassDescriptor().getSuperClass().getPackageName() + "." + mo.getClassDescriptor().getSuperClass().getName()); + serializer.text(mo.getClassDescriptor().getSuperClass().toString()); } serializer.endTag("", "superclass"); serializer.endTag("", "class"); for (GuixModelObject child : mo.getChildren()) { - writeModelTree(serializer, child, cssSerializer); + writeModelTree(serializer, child, cssSerializer, destDir); } - serializer.endTag("", "Object"); } @@ -360,26 +393,47 @@ * and package has a different script or superclass */ public boolean registerClassDescriptor(ClassDescriptor classDescriptor) { + 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 if (i >= classDescriptors.size()) { classDescriptors.add(classDescriptor); + if (log.isDebugEnabled()) { + log.debug("new ClassDescriptor " + classDescriptor + " inserted"); + } return true; } if (classDescriptor.getScript() != null) { if (classDescriptors.get(i).getScript() == null) { classDescriptors.get(i).setScript(classDescriptor.getScript()); + if (log.isDebugEnabled()) { + log.debug("add script to ClassDescriptor " + classDescriptor); + } } else if (!classDescriptors.get(i).getScript().equals(classDescriptor.getScript())) { + if (log.isDebugEnabled()) { + log.error("ClassDescriptor " + classDescriptor + " script already defined and different !"); + } return false; } } if (classDescriptor.getSuperClass() != null) { if (classDescriptors.get(i).getSuperClass() == null) { classDescriptors.get(i).setSuperClass(classDescriptor.getSuperClass()); + if (log.isDebugEnabled()) { + log.debug("add superclass to ClassDescriptor " + classDescriptor); + } } else if (!classDescriptors.get(i).getSuperClass().equals(classDescriptor.getSuperClass())) { + if (log.isErrorEnabled()) { + log.error("ClassDescriptor " + classDescriptor + " superclass already defined and different !"); + } return false; } } 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-04-29 08:06:47 UTC (rev 1364) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/compiler/GuixCompiler.java 2009-04-29 15:41:14 UTC (rev 1365) @@ -1,4 +1,3 @@ - package org.nuiton.guix.compiler; import org.nuiton.guix.GuixLauncher; @@ -7,7 +6,8 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; -import org.apache.log4j.Logger; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.nuiton.guix.model.AttributeDescriptor; import org.nuiton.guix.model.ClassDescriptor; import org.nuiton.guix.model.GuixModelObject; @@ -26,14 +26,15 @@ */ public class GuixCompiler { + /** log */ + protected static final Log log = LogFactory.getLog(GuixCompiler.class); /** Flag to detec if an error occurs while compiling guix file */ protected boolean failed; /** Directory containing the guix files */ private File baseDir; /** Guix file to compile */ private File src; - /** Name of the class to produce */ - private String outputClassName; + private String srcPackage; /** GuixLauncher instance which launched this compiler */ private GuixLauncher launcher; private StyleHandler styleHandler = new StyleHandler(); @@ -49,18 +50,20 @@ /** * Creates a new GuixCompiler. * - * @param baseDir classpath location - * @param src location of file to compile - * @param outputClassName the out file name - * @param launcher the launcher of the compiler + * @param src location of file to compile + * @param launcher the launcher of the compiler */ - public GuixCompiler(File baseDir, File src, String outputClassName, - GuixLauncher launcher) { - this.baseDir = baseDir; + public GuixCompiler(File src, GuixLauncher launcher, String srcPackage) { this.src = src; - this.outputClassName = outputClassName; this.launcher = launcher; - lastModification = src.lastModified(); + if(srcPackage != null) + this.srcPackage = srcPackage; + else + this.srcPackage = ""; + if (src != null) { + baseDir = src.getParentFile(); + lastModification = src.lastModified(); + } } /** @@ -69,73 +72,81 @@ * @return the root of the model */ public GuixModelObject compile() { - try { - //Creation of the Xml parser - XmlPullParserFactory factory = XmlPullParserFactory.newInstance(System.getProperty(XmlPullParserFactory.PROPERTY_NAME), null); - factory.setNamespaceAware(true); - XmlPullParser xpp = factory.newPullParser(); - xpp.setInput(new FileReader(src)); - //Start parsing - if (xpp.getEventType() == XmlPullParser.START_DOCUMENT) { - //javaDoc of the class to create - StringBuffer doc = new StringBuffer(); - do { - xpp.nextToken(); - if (xpp.getEventType() == XmlPullParser.COMMENT) { - doc.append(xpp.getText()); + if (src != null && launcher != null) { + try { + //Creation of the Xml parser + XmlPullParserFactory factory = XmlPullParserFactory.newInstance(System.getProperty(XmlPullParserFactory.PROPERTY_NAME), null); + factory.setNamespaceAware(true); + XmlPullParser xpp = factory.newPullParser(); + xpp.setInput(new FileReader(src)); + //Start parsing + if (xpp.getEventType() == XmlPullParser.START_DOCUMENT) { + //javaDoc of the class to create + StringBuffer doc = new StringBuffer(); + do { + xpp.nextToken(); + if (xpp.getEventType() == XmlPullParser.COMMENT) { + doc.append(xpp.getText()); + } + } while (xpp.getEventType() != XmlPullParser.START_TAG); + //resolve the package of teh superclass + String tagNameSpace = xpp.getNamespace(); + String tagName; + if (xpp.getName().lastIndexOf('.') >= 0) { + tagName = xpp.getName().substring(xpp.getName().lastIndexOf('.') + 1); + } else { + tagName = xpp.getName(); } - } while (xpp.getEventType() != XmlPullParser.START_TAG); - //resolve the package of teh superclass - String tagNameSpace = xpp.getNamespace(); - String tagName = xpp.getName(); - String tagPackageName = resolvePackageName(tagNameSpace, tagName); - //creation of the root GuixModelObject - rootMO = new GuixModelObject(xpp.getAttributeValue("", "id"), doc.toString(), xpp.getAttributeValue("", "styleClass")); - //the class name is the name of the file minus the extension - String className = src.getName().substring(0, src.getName().lastIndexOf('.')); - String classPackageName = src.getPath().replace('/', '.').substring(src.getPath().lastIndexOf("src/main/java/") + 14, src.getPath().lastIndexOf('/')); - rootMO.setClassDescriptor(new ClassDescriptor(className, classPackageName)); - rootMO.getClassDescriptor().setSuperClass(new ClassDescriptor(tagName, tagPackageName)); - rootMO.setAttributeDescriptors(getAttributes(xpp)); - rootMO.setChildren(new ArrayList<GuixModelObject>()); + String tagPackageName = resolvePackageName(tagNameSpace, xpp.getName()); + //creation of the root GuixModelObject + rootMO = new GuixModelObject(xpp.getAttributeValue("", "id"), doc.toString(), xpp.getAttributeValue("", "styleClass")); + //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)); + rootMO.getClassDescriptor().setSuperClass(new ClassDescriptor(tagName, tagPackageName)); + rootMO.setAttributeDescriptors(getAttributes(xpp)); + rootMO.setChildren(new ArrayList<GuixModelObject>()); - //add the stylesheet of the CSS file with the same name as the Guix file - File styleFile = new File(baseDir, className + ".css"); - if(styleFile.exists()) { - StyleSheet ss = styleHandler.autoDetectStyleFile(styleFile); - rootMO.getStyleSheets().add(ss); - lastModification = Math.max(lastModification,styleFile.lastModified()); - } + //add the stylesheet of the CSS file with the same name as the Guix file + File styleFile = new File(src.getParentFile(), className + ".css"); + if (styleFile.exists()) { + StyleSheet ss = styleHandler.autoDetectStyleFile(styleFile); + rootMO.getStyleSheets().add(ss); + lastModification = Math.max(lastModification, styleFile.lastModified()); + } - StringBuffer script = new StringBuffer(); - //add the script in the .script file with the same name as the Guix file - File scriptFile = new File(baseDir, className + ".script"); - if(scriptFile.exists()) { - script.append(scriptHandler.loadScriptFile(scriptFile)); - lastModification = Math.max(lastModification, scriptFile.lastModified()); - } + StringBuffer script = new StringBuffer(); + //add the script in the .script file with the same name as the Guix file + File scriptFile = new File(src.getParentFile(), className + ".script"); + if (scriptFile.exists()) { + script.append(scriptHandler.loadScriptFile(scriptFile)); + lastModification = Math.max(lastModification, scriptFile.lastModified()); + } - //reach the next START_TAG - do { - xpp.nextToken(); - } while (xpp.getEventType() != XmlPullParser.START_TAG && xpp.getEventType() != XmlPullParser.END_DOCUMENT); - //if not eof - if (xpp.getEventType() == XmlPullParser.START_TAG) { - //compile the rest of the file - script.append(compile(xpp, rootMO, doc.toString())); + //reach the next START_TAG + do { + xpp.nextToken(); + } while (xpp.getEventType() != XmlPullParser.START_TAG && xpp.getEventType() != XmlPullParser.END_DOCUMENT); + //if not eof + if (xpp.getEventType() == XmlPullParser.START_TAG) { + //compile the rest of the file + script.append(compile(xpp, rootMO, doc.toString())); + } + //all the script tags have been recorded + rootMO.getClassDescriptor().setScript(script.toString()); + //register the root ClassDescriptor + failed = !launcher.registerClassDescriptor(rootMO.getClassDescriptor()); + return rootMO; } - //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) { + if(log.isErrorEnabled()) + log.error(ex); + } catch (IOException ex) { + if(log.isErrorEnabled()) + log.error(ex); } - failed = true; - } catch (XmlPullParserException ex) { - Logger.getLogger(GuixCompiler.class.getName()).error(ex); - } catch (IOException ex) { - Logger.getLogger(GuixCompiler.class.getName()).error(ex); } + failed = true; return null; } @@ -149,7 +160,7 @@ * @param javaDoc the comments above the tag, to add to its javaDoc * @return the scripts already discovered while parsing the file */ - private StringBuffer compile(final XmlPullParser xpp, GuixModelObject previousMO, + protected StringBuffer compile(final XmlPullParser xpp, GuixModelObject previousMO, String javaDoc) { StringBuffer result = new StringBuffer(); @@ -168,15 +179,16 @@ } //creates a new stylesheet StyleSheet ss = styleHandler.compileStyle(xpp, styleFile); - if(ss != null) + if (ss != null) { rootMO.getStyleSheets().add(ss); - if (styleFile != null && styleFile.exists()) + } + if (styleFile != null && styleFile.exists()) { lastModification = Math.max(lastModification, styleFile.lastModified()); - + } + //the parent is still the same prev = previousMO; - } - //if the tag is a script tag + } //if the tag is a script tag else if (xpp.getName() != null && xpp.getName().equals("script")) { File scriptFile = null; //the name of the file to load @@ -187,26 +199,23 @@ } //add the script to the result result.append(scriptHandler.compileScript(xpp, scriptFile)); - if (scriptFile != null && scriptFile.exists()) { + if (scriptFile != null && scriptFile.exists()) { lastModification = Math.max(lastModification, scriptFile.lastModified()); } //the parent is still the same prev = previousMO; - } - //if the tag is a class tag + } //if the tag is a class tag else { String tagNameSpace = xpp.getNamespace(); String tagPackageName = resolvePackageName(tagNameSpace, xpp.getName()); String tagName; //if the name of the tag is the fully-qualified class name - if(xpp.getName().lastIndexOf('.') >= 0) { + if (xpp.getName().lastIndexOf('.') >= 0) { tagName = xpp.getName().substring(xpp.getName().lastIndexOf('.') + 1); - } - else { + } else { tagName = xpp.getName(); } - tagNameSpace = tagPackageName + ".*"; //create the GuixModelObject representing the tag GuixModelObject mo = new GuixModelObject(xpp.getAttributeValue("", "id"), javaDoc, xpp.getAttributeValue("", "styleClass")); @@ -218,7 +227,6 @@ //register the ClassDescriptor failed = !launcher.registerClassDescriptor(mo.getClassDescriptor()); - if (!failed) { //parse the children of the tag prev = mo; @@ -231,21 +239,23 @@ //add the comment to the doc of the next tag doc.append(xpp.getText()); } - } while (xpp.getEventType() != XmlPullParser.START_TAG - && xpp.getEventType() != XmlPullParser.END_DOCUMENT); + } while (xpp.getEventType() != XmlPullParser.START_TAG && xpp.getEventType() != XmlPullParser.END_DOCUMENT); + } else { + return null; } - else - return null; } + //if not eof if (xpp.getEventType() == XmlPullParser.START_TAG) { //compile the rest of the document result.append(compile(xpp, prev, doc.toString())); } } catch (XmlPullParserException ex) { - Logger.getLogger(GuixCompiler.class.getName()).error(ex); + if(log.isErrorEnabled()) + log.error(ex); } catch (IOException ex) { - Logger.getLogger(GuixCompiler.class.getName()).error(ex); + if(log.isErrorEnabled()) + log.error(ex); } return result; } @@ -265,16 +275,15 @@ packageName = tagNameSpace.substring(0, tagNameSpace.length() - 2); } //if the name of the tag is the fully qualified class name - if(tagName.lastIndexOf('.') >= 0) { - if(packageName == null) { + if (tagName.lastIndexOf('.') >= 0) { + if (packageName == null) { packageName = tagName.substring(0, tagName.lastIndexOf('.')); - } - else { + } else { packageName += tagName.substring(0, tagName.lastIndexOf('.')); } } //if nor the namespace nor the fully qualified class name is defined - if(packageName == null) { + if (packageName == null) { String fullClassName = TagManager.resolveClassName(tagName); packageName = (fullClassName == null) ? null : fullClassName.substring(0, fullClassName.lastIndexOf('.')); } Modified: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/model/AttributeDescriptor.java =================================================================== --- guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/model/AttributeDescriptor.java 2009-04-29 08:06:47 UTC (rev 1364) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/model/AttributeDescriptor.java 2009-04-29 15:41:14 UTC (rev 1365) @@ -38,5 +38,9 @@ public void setValue(String value) { this.value = value; } + + public String toString() { + return name + " : " + value; + } } Modified: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/model/ClassDescriptor.java =================================================================== --- guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/model/ClassDescriptor.java 2009-04-29 08:06:47 UTC (rev 1364) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/model/ClassDescriptor.java 2009-04-29 15:41:14 UTC (rev 1365) @@ -60,4 +60,8 @@ public void setSuperClass(ClassDescriptor superClass) { this.superClass = superClass; } + + public String toString() { + return (packageName != null) ? packageName + "." + name : name; + } } Modified: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/tags/StyleHandler.java =================================================================== --- guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/tags/StyleHandler.java 2009-04-29 08:06:47 UTC (rev 1364) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/tags/StyleHandler.java 2009-04-29 15:41:14 UTC (rev 1365) @@ -11,7 +11,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import org.apache.log4j.Logger; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.nuiton.guix.css.CSSParser; import org.nuiton.guix.css.CSSParserConstants; import org.nuiton.guix.css.CSSParserTreeConstants; @@ -30,6 +31,8 @@ */ public class StyleHandler { + /** log */ + protected static final Log log = LogFactory.getLog(TagManager.class); /** List of the selectors */ private List<Selector> selectors = new ArrayList<Selector>(); @@ -92,8 +95,8 @@ do { switch (xpp.getEventType()) { case XmlPullParser.START_TAG: - Logger.getLogger(StyleHandler.class) - .warn("<style> tag may not contain child elements: " + xpp.getName()); + if(log.isWarnEnabled()) + log.warn("<style> tag may not contain child elements: " + xpp.getName()); break; case XmlPullParser.TEXT: break;// fall through Modified: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/tags/TagManager.java =================================================================== --- guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/tags/TagManager.java 2009-04-29 08:06:47 UTC (rev 1364) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/tags/TagManager.java 2009-04-29 15:41:14 UTC (rev 1365) @@ -1,19 +1,11 @@ package org.nuiton.guix.tags; -import java.util.logging.Level; -import java.util.logging.Logger; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import java.io.IOException; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; -import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Set; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; /** * Manages tags of the .guix files. @@ -21,7 +13,6 @@ public class TagManager { /** log */ protected static final Log log = LogFactory.getLog(TagManager.class); - /** * Namespace for Guix's non-class tags, such as <Button;>. The namespace normally does not * need to be specified but can be used to resolve ambiguities. @@ -35,7 +26,7 @@ private static Map<String, Boolean> classExistenceCache = new HashMap<String, Boolean>(); /** List of the Guix classes such as <Button;> */ - private static List<String> guixClasses = new ArrayList<String>(); + protected static List<String> guixClasses = new ArrayList<String>(); private TagManager() { /* not instantiable */ } @@ -249,7 +240,6 @@ return resolveClassName(name.substring(0, name.length() - 2)) + "[]"; if (name.indexOf("<") != -1) name = name.substring(0, name.indexOf("<")); // strip off generic types - name = name.intern(); if (name.equals("boolean") || name.equals("byte") || name.equals("short") || name.equals("int") || name.equals("long") || name.equals("float") || name.equals("double") || name.equals("char")) @@ -258,6 +248,10 @@ if (guixClasses.contains(name)) { return GUIX_NAMESPACE.substring(0, GUIX_NAMESPACE.lastIndexOf('*')) + name; } + //if the name of the tag is the fully qualified class name + if(name.lastIndexOf('.') >= 0) { + return name; + } return null; } Added: guix/trunk/guix-compiler/src/main/resources/log4j.properties =================================================================== --- guix/trunk/guix-compiler/src/main/resources/log4j.properties (rev 0) +++ guix/trunk/guix-compiler/src/main/resources/log4j.properties 2009-04-29 15:41:14 UTC (rev 1365) @@ -0,0 +1,9 @@ +# Global logging configuration +log4j.rootLogger=INFO, stdout +# Console output... +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=%5p [%F : %M] %m%n + +#log4j.logger.org.codelutin.i18n=ERROR +log4j.logger.org.nuiton.guix=DEBUG Added: guix/trunk/guix-compiler/src/test/java/org/nuiton/guix/GuixLauncherTest.java =================================================================== --- guix/trunk/guix-compiler/src/test/java/org/nuiton/guix/GuixLauncherTest.java (rev 0) +++ guix/trunk/guix-compiler/src/test/java/org/nuiton/guix/GuixLauncherTest.java 2009-04-29 15:41:14 UTC (rev 1365) @@ -0,0 +1,77 @@ +package org.nuiton.guix; + +import java.io.File; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import org.nuiton.guix.model.ClassDescriptor; + +/** + * Tests the methods of the GuixLauncher class + * + * @author morin + */ +public class GuixLauncherTest { + + /** + * Tests the method compile + */ + @Test + public void compileTest() { + GuixInitializer.initialize(); + + GuixLauncher gl = new GuixLauncher(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); + + File f = new File("src/test/test.guix"); + 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()); + assertTrue(gl.files == null || gl.classNames.length == gl.files.length); + assertTrue(gl.compile()); + } + + /** + * Tests the method registerClassDescriptor + */ + @Test + public void registerClassDescriptorTest() { + GuixLauncher gl = new GuixLauncher(null,null,null,null); + assertFalse(gl.registerClassDescriptor(null)); + assertFalse(gl.registerClassDescriptor(new ClassDescriptor(null, null))); + + ClassDescriptor cd = new ClassDescriptor("name", "package"); + assertTrue(gl.registerClassDescriptor(cd)); + cd.setSuperClass(new ClassDescriptor("super", "package")); + assertTrue(gl.registerClassDescriptor(cd) + && gl.classDescriptors.get(0).getSuperClass() != null); + assertTrue(gl.registerClassDescriptor(cd) + && gl.classDescriptors.size() == 1); + + ClassDescriptor cd2 = new ClassDescriptor("name", "package"); + cd2.setSuperClass(new ClassDescriptor("super2", "package")); + assertFalse(gl.registerClassDescriptor(cd2)); + + ClassDescriptor cd3 = new ClassDescriptor("name", "package"); + cd3.setScript("script"); + assertTrue(gl.registerClassDescriptor(cd3) + && gl.classDescriptors.size() == 1); + + ClassDescriptor cd4 = new ClassDescriptor("name", "package"); + cd4.setScript("script2"); + assertFalse(gl.registerClassDescriptor(cd4)); + + assertTrue(gl.registerClassDescriptor(new ClassDescriptor("name", null)) + && gl.classDescriptors.size() == 1); + assertTrue(gl.registerClassDescriptor(new ClassDescriptor("test", null)) + && gl.classDescriptors.size() == 2); + assertTrue(gl.registerClassDescriptor(new ClassDescriptor("test", null)) + && gl.classDescriptors.size() == 2); + } +} + Added: 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 (rev 0) +++ guix/trunk/guix-compiler/src/test/java/org/nuiton/guix/compiler/GuixCompilerTest.java 2009-04-29 15:41:14 UTC (rev 1365) @@ -0,0 +1,38 @@ +package org.nuiton.guix.compiler; + +import java.io.File; +import org.junit.Test; +import org.nuiton.guix.GuixLauncher; +import static org.junit.Assert.*; + +/** + * Tests the GuixCompiler class + * + * @author morin + */ +public class GuixCompilerTest { + + @Test + public void compileTest() { + 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()); + + GuixCompiler gc = new GuixCompiler(null, null,null); + assertNull(gc.compile()); + + gc = new GuixCompiler(null, gl, null); + assertNull(gc.compile()); + + gc = new GuixCompiler(f, null, null); + assertNull(gc.compile()); + + gc = new GuixCompiler(f2, gl, null); + assertNotNull(gc.compile()); + + gc = new GuixCompiler(f2, gl, "test.compiler"); + assertNotNull(gc.compile()); + } + +} Added: guix/trunk/guix-compiler/src/test/java/org/nuiton/guix/tags/TagManagerTest.java =================================================================== --- guix/trunk/guix-compiler/src/test/java/org/nuiton/guix/tags/TagManagerTest.java (rev 0) +++ guix/trunk/guix-compiler/src/test/java/org/nuiton/guix/tags/TagManagerTest.java 2009-04-29 15:41:14 UTC (rev 1365) @@ -0,0 +1,34 @@ +package org.nuiton.guix.tags; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import org.junit.Test; + +/** + * Tests the methods of the TagManager class + * + * @author morin + */ +public class TagManagerTest { + + /** + * Tests the method resolveClassName + */ + @Test + public void resolveClassNameTest() { + assertTrue(TagManager.resolveClassName("test.test.test[]").equals("test.test.test[]")); + assertNull(TagManager.resolveClassName("test")); + assertNotNull(TagManager.resolveClassName("boolean")); + assertNotNull(TagManager.resolveClassName("byte")); + assertNotNull(TagManager.resolveClassName("short")); + assertNotNull(TagManager.resolveClassName("int")); + assertNotNull(TagManager.resolveClassName("long")); + assertNotNull(TagManager.resolveClassName("float")); + assertNotNull(TagManager.resolveClassName("double")); + assertNotNull(TagManager.resolveClassName("char")); + for(String clazz : TagManager.guixClasses) { + assertNotNull(TagManager.resolveClassName(clazz)); + } + } +} Added: guix/trunk/guix-compiler/src/test/test.guix =================================================================== --- guix/trunk/guix-compiler/src/test/test.guix (rev 0) +++ guix/trunk/guix-compiler/src/test/test.guix 2009-04-29 15:41:14 UTC (rev 1365) @@ -0,0 +1,4 @@ +<?xml version='1.0' encoding='UTF-8' ?> +<org.nuiton.guix.Test> + <Panel /> +</org.nuiton.guix.Test>
participants (1)
-
kmorin@users.labs.libre-entreprise.org