Author: kmorin Date: 2009-04-27 16:06:15 +0000 (Mon, 27 Apr 2009) New Revision: 1359 Modified: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/CompilerException.java guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/GuixInitializer.java guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/GuixLauncher.java guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/UnsupportedAttributeException.java guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/UnsupportedTagException.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/model/GuixModelObject.java guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/model/Rule.java guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/model/Selector.java guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/model/StyleSheet.java guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/tags/ScriptHandler.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: - JavaDoc - v?\195?\169rification de date de derniere modification avant de g?\195?\169n?\195?\169rer une classe Modified: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/CompilerException.java =================================================================== --- guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/CompilerException.java 2009-04-24 15:39:17 UTC (rev 1358) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/CompilerException.java 2009-04-27 16:06:15 UTC (rev 1359) @@ -1,12 +1,7 @@ -/* - * Copyright 2006 Ethan Nicholas. All rights reserved. - * Use is subject to license terms. - */ package org.nuiton.guix; /** Thrown by the compiler when an error occurs. */ public class CompilerException extends RuntimeException { - private static final long serialVersionUID = -9099889519671482440L; /** Creates a new <code>ParseException</code>. */ public CompilerException() { Modified: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/GuixInitializer.java =================================================================== --- guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/GuixInitializer.java 2009-04-24 15:39:17 UTC (rev 1358) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/GuixInitializer.java 2009-04-27 16:06:15 UTC (rev 1359) @@ -1,13 +1,10 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ package org.nuiton.guix; import org.nuiton.guix.tags.TagManager; /** + * Initialize the application * * @author morin */ 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-24 15:39:17 UTC (rev 1358) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/GuixLauncher.java 2009-04-27 16:06:15 UTC (rev 1359) @@ -1,7 +1,3 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ package org.nuiton.guix; import java.io.IOException; @@ -11,13 +7,13 @@ import java.io.PrintWriter; import java.util.ArrayList; 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.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import org.apache.log4j.Logger; import org.nuiton.guix.model.ClassDescriptor; import org.nuiton.guix.model.GuixModelObject; import org.nuiton.guix.model.Rule; @@ -34,8 +30,7 @@ public class GuixLauncher { /** log */ - /* protected static final Log log = - LogFactory.getLog(GuixLauncher.class);*/ + Logger log = Logger.getLogger(GuixLauncher.class); /** original list of files to compile */ protected final File[] files; /** original list of classes to compile */ @@ -44,23 +39,30 @@ protected List<File> guixFiles = new ArrayList<File>(); /** Class names corresponding to the files in the guixFiles list. */ protected List<String> guixFileClassNames = new ArrayList<String>(); - /** Maps the names of classes being compiled to the compiler instance + /** Maps the root GuixModelObjects being compiled to the compiler instance * handling the compilation. */ - protected Map<String, GuixCompiler> compilers = - new HashMap<String, GuixCompiler>(); - /** ModelObjects which are root of files */ - protected List<GuixModelObject> rootModelObjects = new ArrayList<GuixModelObject>(); + protected Map<GuixModelObject, GuixCompiler> rootModelObjects = + new HashMap<GuixModelObject, GuixCompiler>(); /** CLassDescriptor met during the compilation */ protected List<ClassDescriptor> classDescriptors = new ArrayList<ClassDescriptor>(); private List<File> compiledFiles = new ArrayList<File>(); protected int compilerCount; - private File targetDirectory = new File("destination"); + private File targetDirectory; - public GuixLauncher() { - files = new File[]{new File("/home/morin/NetBeansProjects/guix/trunk/guix-test/src/main/java/org/nuiton/guix/demo/GuixDemo.guix")}; - classNames = new String[]{"GuixDemo"}; - compile(); + /** + * Constructor + * + * @param files the files to compile + */ + public GuixLauncher(File[] files, File targetDirectory) { + // 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('.')); + } } /** @@ -70,232 +72,240 @@ * <code>false</code> otherwise */ public synchronized boolean compile() { - //reset(); // just to be safe... compilerCount = 0; guixFiles.addAll(Arrays.asList(files)); guixFileClassNames.addAll(Arrays.asList(classNames)); + boolean success = true; + try { - boolean success = true; + assert guixFiles.size() == guixFileClassNames.size(); - // pass 1 - /*if (!nextStep(LifeCycle.compile_first_pass, success)) { - return false; - }*/ - boolean compiled; - do { - compiled = false; - assert guixFiles.size() == guixFileClassNames.size(); - // clone it so it can safely be modified while we're iterating - Iterator<File> filesIterator = - new ArrayList<File>(guixFiles).iterator(); - Iterator<String> classNamesIterator = - new ArrayList<String>(guixFileClassNames).iterator(); + for (int i = 0; i < guixFiles.size(); i++) { + File file = guixFiles.get(i); + String className = guixFileClassNames.get(i); - while (filesIterator.hasNext()) { + //if we have not compiled the file yet + if (!compiledFiles.contains(file)) { + log.info("Compiling class " + className); + compiledFiles.add(file); - File file = filesIterator.next(); - String className = classNamesIterator.next(); - /*if (log.isDebugEnabled()) { - log.debug("compile first pass for " + className); - }*/ - if (!compiledFiles.contains(file)) { - compiled = true; - 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)); - } - if (!destDir.exists() && !destDir.mkdirs()) { - //log.warn("couldn't create directory " + destDir); - System.out.println("couldn't create directory " + destDir); - continue; - } + File destDir = targetDirectory; + if (destDir != null) { + int dotPos = className.lastIndexOf("."); + if (dotPos != -1) { + destDir = new File(destDir, className.substring(0, dotPos).replace('.', File.separatorChar)); } else { - //destDir = file.getParentFile(); + destDir = new File(destDir, className); } - GuixCompiler compiler = new GuixCompiler( - file.getParentFile(), file, className, this); - //addProfileTime(compiler, currentPass.name() + "_start"); - compilers.put(className, compiler); - GuixModelObject rootModelObject = compiler.compile(); - rootModelObjects.add(rootModelObject); - //addProfileTime(compiler, currentPass.name() + "_end"); - //assert !symbolTables.values().contains(compiler.getSymbolTable()) : "symbolTable is already registered"; - //symbolTables.put(file, compiler.getSymbolTable()); - if (compiler.isFailed()) { - success = false; + if (!destDir.exists() && !destDir.mkdirs()) { + log.warn("couldn't create directory " + destDir); + continue; } + } else { + destDir = file.getParentFile(); } - } + //compile the file + GuixCompiler compiler = new GuixCompiler( + file.getParentFile(), file, className, this); - } while (compiled); + GuixModelObject rootModelObject = compiler.compile(); + rootModelObjects.put(rootModelObject, compiler); - XmlPullParserFactory factory = - XmlPullParserFactory.newInstance( - System.getProperty(XmlPullParserFactory.PROPERTY_NAME), - null); + if (compiler.isFailed()) { + success = false; + } + } else { + log.warn(file.getName() + " has already been compiled."); + } + } - for (GuixModelObject mo : rootModelObjects) { - - File f = new File(targetDirectory,mo.getClassDescriptor().getName() + "ModelTree.xml"); - if (!f.exists()) { - f.createNewFile(); + if (success) { + int i = 0; + while (success && i < classDescriptors.size()) { + success = (classDescriptors.get(i++).getPackageName() != null); } - System.out.println(f.getAbsolutePath()); + 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); - XmlSerializer serializer = factory.newSerializer(); - XmlSerializer cssSerializer = factory.newSerializer(); + 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(); + } + 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); - serializer.endDocument(); + serializer.endDocument(); + } + 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; - } - StringBuilder buffer = profiler.computeProfileReport(); - log.info(buffer.toString()); - } + return report(success); - return report(success); - - //FIXME : deal better the exception treatment... - } catch (CompilerException e) { - System.err.println(e.getMessage()); - e.printStackTrace(); - return false;*/ - + //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; - } finally { - compilerCount = compilers.size(); - //TC - 20081018 only reset when no error was detected - //if (options.isResetAfterCompile() && errorCount == 0) { - // reset(); - //} - return false; } + return success; } + /** + * Temporary + * Writes the model in an Xml file + * + * @param serializer + * @param mo + * @param cssSerializer + * @throws java.io.IOException + */ private void writeModelTree(XmlSerializer serializer, GuixModelObject mo, XmlSerializer cssSerializer) throws IOException { - if(!mo.getStyleSheets().isEmpty()) { + if (!mo.getStyleSheets().isEmpty()) { int i = 0; - for(StyleSheet styleSheet : mo.getStyleSheets()) { - File ss = new File(targetDirectory,mo.getClassDescriptor().getName() + i++ + "SS.xml"); + for (StyleSheet styleSheet : mo.getStyleSheets()) { + File ss = new File(targetDirectory, mo.getClassDescriptor().getName() + i++ + "SS.xml"); cssSerializer.setOutput(new FileWriter(ss)); cssSerializer.startTag("", "rules"); - for(Rule rule : styleSheet.getRules()) { + for (Rule rule : styleSheet.getRules()) { cssSerializer.startTag("", "rule"); cssSerializer.startTag("", "selectors"); - for(Selector selector : rule.getSelectors()) { + for (Selector selector : rule.getSelectors()) { cssSerializer.startTag("", "selector"); cssSerializer.startTag("", "javaClassName"); - if(selector.getJavaClassName() != null) + if (selector.getJavaClassName() != null) { cssSerializer.text(selector.getJavaClassName()); + } cssSerializer.endTag("", "javaClassName"); cssSerializer.startTag("", "styleClass"); - if(selector.getStyleClass() != null) + if (selector.getStyleClass() != null) { cssSerializer.text(selector.getStyleClass()); + } cssSerializer.endTag("", "styleClass"); cssSerializer.startTag("", "pseudoClass"); - if(selector.getPseudoClass() != null) + if (selector.getPseudoClass() != null) { cssSerializer.text(selector.getPseudoClass()); + } cssSerializer.endTag("", "pseudoClass"); cssSerializer.startTag("", "id"); - if(selector.getId() != null) + if (selector.getId() != null) { cssSerializer.text(selector.getId()); + } cssSerializer.endTag("", "id"); cssSerializer.endTag("", "selector"); } cssSerializer.endTag("", "selectors"); cssSerializer.startTag("", "properties"); - for(Entry entry : rule.getProperties().entrySet()) { - cssSerializer.startTag("", (String)entry.getKey()); - if(entry.getValue() != null) - cssSerializer.text((String)entry.getValue()); - cssSerializer.endTag("", (String)entry.getKey()); + for (Entry entry : rule.getProperties().entrySet()) { + cssSerializer.startTag("", (String) entry.getKey()); + if (entry.getValue() != null) { + cssSerializer.text((String) entry.getValue()); + } + cssSerializer.endTag("", (String) entry.getKey()); } cssSerializer.endTag("", "properties"); cssSerializer.endTag("", "rule"); @@ -342,6 +352,13 @@ } + /** + * Registers a ClassDescriptor + * + * @param classDescriptor the ClassDescripor to register + * @return false if another ClassDescriptor with the same classname + * and package has a different script or superclass + */ public boolean registerClassDescriptor(ClassDescriptor classDescriptor) { 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())))) { Modified: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/UnsupportedAttributeException.java =================================================================== --- guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/UnsupportedAttributeException.java 2009-04-24 15:39:17 UTC (rev 1358) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/UnsupportedAttributeException.java 2009-04-27 16:06:15 UTC (rev 1359) @@ -1,16 +1,9 @@ -/* - * Copyright 2006 Ethan Nicholas. All rights reserved. - * Use is subject to license terms. - */ package org.nuiton.guix; /** - * Thrown by <code>TagHandler</code> when an unsupported attribute is encountered. - * - * @see jaxx.tags.TagHandler + * Thrown when an unsupported attribute is encountered. */ public class UnsupportedAttributeException extends CompilerException { - private static final long serialVersionUID = -6919583037172920343L; /** Creates a new <code>UnsupportedAttributeException</code>. */ public UnsupportedAttributeException() { Modified: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/UnsupportedTagException.java =================================================================== --- guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/UnsupportedTagException.java 2009-04-24 15:39:17 UTC (rev 1358) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/UnsupportedTagException.java 2009-04-27 16:06:15 UTC (rev 1359) @@ -1,12 +1,7 @@ -/* - * Copyright 2006 Ethan Nicholas. All rights reserved. - * Use is subject to license terms. - */ package org.nuiton.guix; /** Thrown by the compiler when an unregistered tag is encountered. */ public class UnsupportedTagException extends CompilerException { - private static final long serialVersionUID = 3199732135804426699L; /** Creates a new <code>UnsupportedTagException</code>. */ public UnsupportedTagException() { 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-24 15:39:17 UTC (rev 1358) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/compiler/GuixCompiler.java 2009-04-27 16:06:15 UTC (rev 1359) @@ -1,14 +1,13 @@ package org.nuiton.guix.compiler; -import java.util.logging.Level; -import java.util.logging.Logger; import org.nuiton.guix.GuixLauncher; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; +import org.apache.log4j.Logger; import org.nuiton.guix.model.AttributeDescriptor; import org.nuiton.guix.model.ClassDescriptor; import org.nuiton.guix.model.GuixModelObject; @@ -41,6 +40,8 @@ private ScriptHandler scriptHandler = new ScriptHandler(); /** Root of the model */ private GuixModelObject rootMO; + /** */ + private long lastModification; /*------------------------------------------------------------------------*/ /*-- Constructor methods -------------------------------------------------*/ @@ -59,6 +60,7 @@ this.src = src; this.outputClassName = outputClassName; this.launcher = launcher; + lastModification = src.lastModified(); } /** @@ -96,12 +98,23 @@ 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 - StyleSheet ss = styleHandler.autoDetectStyleFile(baseDir.getPath(), outputClassName); - rootMO.getStyleSheets().add(ss); + 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()); + } + StringBuffer script = new StringBuffer(); //add the script in the .script file with the same name as the Guix file - script.append(scriptHandler.autoDetectScriptFile(baseDir.getPath(), outputClassName)); + File scriptFile = new File(baseDir, className + ".script"); + if(scriptFile.exists()) { + script.append(scriptHandler.loadScriptFile(scriptFile)); + lastModification = Math.max(lastModification, scriptFile.lastModified()); + } + //reach the next START_TAG do { xpp.nextToken(); @@ -119,9 +132,9 @@ } failed = true; } catch (XmlPullParserException ex) { - Logger.getLogger(GuixCompiler.class.getName()).log(Level.SEVERE, null, ex); + Logger.getLogger(GuixCompiler.class.getName()).error(ex); } catch (IOException ex) { - Logger.getLogger(GuixCompiler.class.getName()).log(Level.SEVERE, null, ex); + Logger.getLogger(GuixCompiler.class.getName()).error(ex); } return null; } @@ -146,17 +159,37 @@ try { //if the tag is a style tag if (xpp.getName() != null && xpp.getName().equals("style")) { + File styleFile = null; + //the name of the file to load + String source = xpp.getAttributeValue("", "source"); + //if the source attribute is specified + if (source != null) { + styleFile = new File(baseDir, source.replace('/', File.separatorChar)); + } //creates a new stylesheet - StyleSheet ss = styleHandler.compileStyle(xpp, baseDir.getPath()); - rootMO.getStyleSheets().add(ss); + StyleSheet ss = styleHandler.compileStyle(xpp, styleFile); + if(ss != null) + rootMO.getStyleSheets().add(ss); + 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 else if (xpp.getName() != null && xpp.getName().equals("script")) { + File scriptFile = null; + //the name of the file to load + String source = xpp.getAttributeValue("", "source"); + //if the source attribute is specified + if (source != null) { + scriptFile = new File(baseDir, source.replace('/', File.separatorChar)); + } //add the script to the result - result.append(scriptHandler.compileScript(xpp, baseDir.getPath())); + result.append(scriptHandler.compileScript(xpp, scriptFile)); + if (scriptFile != null && scriptFile.exists()) { + lastModification = Math.max(lastModification, scriptFile.lastModified()); + } //the parent is still the same prev = previousMO; } @@ -210,9 +243,9 @@ result.append(compile(xpp, prev, doc.toString())); } } catch (XmlPullParserException ex) { - Logger.getLogger(GuixCompiler.class.getName()).log(Level.SEVERE, null, ex); + Logger.getLogger(GuixCompiler.class.getName()).error(ex); } catch (IOException ex) { - Logger.getLogger(GuixCompiler.class.getName()).log(Level.SEVERE, null, ex); + Logger.getLogger(GuixCompiler.class.getName()).error(ex); } return result; } @@ -275,4 +308,8 @@ public boolean isFailed() { return failed; } + + public long getLastModification() { + return lastModification; + } } 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-24 15:39:17 UTC (rev 1358) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/model/AttributeDescriptor.java 2009-04-27 16:06:15 UTC (rev 1359) @@ -1,18 +1,23 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ - package org.nuiton.guix.model; /** + * Descriptor of tag attributes * * @author morin */ public class AttributeDescriptor { + + /** Name of the attribute */ private String name; + /** Value of the attribute */ private String value; + /** + * Constructor + * + * @param name name of the attribute + * @param value value of the attribute + */ public AttributeDescriptor(String name, String value) { this.name = name; this.value = 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-24 15:39:17 UTC (rev 1358) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/model/ClassDescriptor.java 2009-04-27 16:06:15 UTC (rev 1359) @@ -1,21 +1,29 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ - package org.nuiton.guix.model; /** + * Descriptor of a class found in the guix files. * * @author morin */ public class ClassDescriptor { + /** Name of the class */ private String name; + /** Package of the class (must ends with '.*')*/ private String packageName; + /** Script declared in the guix file. + * Available only for the root GuixModelObject class.*/ private String script; - private ClassDescriptor superClass; + /** Superclass of the class + * Available only for the root GuixModelObject class.*/ + private ClassDescriptor superClass; + /** + * Constructor + * + * @param name name of the class + * @param packageName name of the package + */ public ClassDescriptor(String name, String packageName){ this.name = name; this.packageName = packageName; Modified: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/model/GuixModelObject.java =================================================================== --- guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/model/GuixModelObject.java 2009-04-24 15:39:17 UTC (rev 1358) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/model/GuixModelObject.java 2009-04-27 16:06:15 UTC (rev 1359) @@ -1,4 +1,3 @@ - package org.nuiton.guix.model; import java.util.ArrayList; Modified: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/model/Rule.java =================================================================== --- guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/model/Rule.java 2009-04-24 15:39:17 UTC (rev 1358) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/model/Rule.java 2009-04-27 16:06:15 UTC (rev 1359) @@ -1,8 +1,3 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ - package org.nuiton.guix.model; import java.util.ArrayList; @@ -11,12 +6,16 @@ import java.util.List; /** + * Representation of a rule of a CSS file * * @author morin */ public class Rule { + /** Properties of the rule : + * Maps the name of the property to its value.*/ private Map<String,String> properties = new HashMap<String,String>(); + /** Selectors which the rule is applied to */ private List<Selector> selectors = new ArrayList<Selector>(); public Map<String, String> getProperties() { Modified: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/model/Selector.java =================================================================== --- guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/model/Selector.java 2009-04-24 15:39:17 UTC (rev 1358) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/model/Selector.java 2009-04-27 16:06:15 UTC (rev 1359) @@ -1,14 +1,10 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ - package org.nuiton.guix.model; import java.util.ArrayList; import java.util.List; /** + * Represents a selector of a CSS file * * @author morin */ @@ -19,12 +15,29 @@ public static final int ALWAYS_APPLIES_INHERIT_ONLY = 3; public static final int ALWAYS_APPLIES = 4; + /** Class which rules are applied to + * e.g. Label */ private String javaClassName; + /** Styleclass which rules are applied to + * e.g. .bold */ private String styleClass; + /** Pseudoclass which rules are applied to + * e.g. :hover */ private String pseudoClass; + /** Id of the object which rules are applied to + * e.g. #label5 */ private String id; + /** List of the rules applied to the selector */ private List<Rule> rules = new ArrayList<Rule>(); + /** + * Constructor + * + * @param javaClassName class which rules are applied to + * @param styleClass styleclass which rules are applied to + * @param pseudoClass pseudoclass which rules are applied to + * @param id id of the object which rules are applied to + */ public Selector(String javaClassName, String styleClass, String pseudoClass, String id) { this.javaClassName = javaClassName; this.styleClass = styleClass; Modified: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/model/StyleSheet.java =================================================================== --- guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/model/StyleSheet.java 2009-04-24 15:39:17 UTC (rev 1358) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/model/StyleSheet.java 2009-04-27 16:06:15 UTC (rev 1359) @@ -1,7 +1,3 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ package org.nuiton.guix.model; @@ -9,12 +5,16 @@ import java.util.List; /** + * Represantation of a stylesheet file * * @author morin */ public class StyleSheet { + /** Rules of the stylesheet */ private List<Rule> rules = new ArrayList<Rule>(); + + /** Selectors which at least one of the rules is applied to */ private List<Selector> selectors = new ArrayList<Selector>(); public List<Rule> getRules() { Modified: guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/tags/ScriptHandler.java =================================================================== --- guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/tags/ScriptHandler.java 2009-04-24 15:39:17 UTC (rev 1358) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/tags/ScriptHandler.java 2009-04-27 16:06:15 UTC (rev 1359) @@ -1,12 +1,9 @@ package org.nuiton.guix.tags; import java.io.File; -import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.StringWriter; -import java.util.logging.Level; -import java.util.logging.Logger; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; @@ -24,7 +21,7 @@ * @return the content of the script file * @throws java.io.IOException if an error occurs while reading scriptFile */ - private String loadScriptFile(File scriptFile) throws IOException { + public String loadScriptFile(File scriptFile) throws IOException { StringWriter scriptBuffer = new StringWriter(); FileReader in = new FileReader(scriptFile); char[] readBuffer = new char[2048]; @@ -36,23 +33,6 @@ } /** - * Loads the file with the name of the class - * and get the content of the file. - * - * @param baseDir the directory of the compiled guix file - * @param className the name of the file to load - * @return the content of the file - * @throws IOException if an error occurs while reading the file - * with the same name as the class - */ - public String autoDetectScriptFile(String baseDir, String className) - throws IOException { - File scriptFile = null; - scriptFile = new File(baseDir, className + ".script"); - return loadScriptFile(scriptFile); - } - - /** * Parse the <code>script</code> tag. * * @param xpp the parser referencing the style tag @@ -62,21 +42,12 @@ * @throws IOException * @throws XmlPullParserException */ - public String compileScript(XmlPullParser xpp, String baseDir) + public String compileScript(XmlPullParser xpp, File scriptFile) throws IOException, XmlPullParserException { StringBuffer script = new StringBuffer(); - File scriptFile = null; - //the name of the file to load - String source = xpp.getAttributeValue("", "source"); - - //if the source attribute is specified - if (source != null) { - scriptFile = new File(baseDir, source.replace('/', File.separatorChar)); - } - if (scriptFile != null && scriptFile.exists()) { + if(scriptFile != null && scriptFile.exists()) script.append(loadScriptFile(scriptFile)); - } xpp.nextToken(); if (xpp.getEventType() == XmlPullParser.CDSECT) { 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-24 15:39:17 UTC (rev 1358) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/tags/StyleHandler.java 2009-04-27 16:06:15 UTC (rev 1359) @@ -11,8 +11,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.logging.Level; -import java.util.logging.Logger; +import org.apache.log4j.Logger; import org.nuiton.guix.css.CSSParser; import org.nuiton.guix.css.CSSParserConstants; import org.nuiton.guix.css.CSSParserTreeConstants; @@ -65,9 +64,7 @@ * @throws IOException if an error occurs while reading the file * with the same name as the class */ - public StyleSheet autoDetectStyleFile(String baseDir, String className) throws FileNotFoundException, IOException { - File styleFile = null; - styleFile = new File(baseDir, className + ".css"); + public StyleSheet autoDetectStyleFile(File styleFile) throws IOException { return processStylesheet(loadStyleFile(styleFile)); } @@ -84,28 +81,19 @@ * @throws org.xmlpull.v1.XmlPullParserException if an error occurs * while parsing the guix file */ - public StyleSheet compileStyle(XmlPullParser xpp, String baseDir) throws IOException, XmlPullParserException { - File styleFile = null; + public StyleSheet compileStyle(XmlPullParser xpp, File styleFile) throws IOException, XmlPullParserException { StyleSheet result = null; - //the name of the file to load - String source = xpp.getAttributeValue("", "source"); - //if a file is specified - if (source != null) { - styleFile = new File(baseDir, source.replace('/', File.separatorChar)); - } StringBuffer style = new StringBuffer(); - - if (styleFile != null && styleFile.exists()) { + if(styleFile != null && styleFile.exists()) style.append(loadStyleFile(styleFile)); - } xpp.nextToken(); do { switch (xpp.getEventType()) { case XmlPullParser.START_TAG: - Logger.getLogger(StyleHandler.class.getName()).log(Level.WARNING, - "<style> tag may not contain child elements: " + xpp.getName()); + Logger.getLogger(StyleHandler.class) + .warn("<style> tag may not contain child elements: " + xpp.getName()); break; case XmlPullParser.TEXT: break;// fall through @@ -115,8 +103,11 @@ break; } } while ((xpp.getEventType() != XmlPullParser.END_TAG) && (!xpp.getName().equals("style"))); + + //analyze the content of the CSS result = processStylesheet(style.toString()); + //reaches next START_TAG do { xpp.nextToken(); } while (xpp.getEventType() != XmlPullParser.START_TAG && xpp.getEventType() != XmlPullParser.END_DOCUMENT); @@ -126,11 +117,14 @@ } /** + * Analyses a selector node * - * @param selector - * @return + * @param selector the selector node to analyse + * @return a Selector with the class, styleclass, pseudoclass and/or id + * which rules will be applied to */ protected Selector processSelector(SimpleNode selector) { + //chack if it is a selector if (selector.getId() != CSSParserTreeConstants.JJTSELECTOR) { throw new IllegalArgumentException("argument node is not a Selector"); } @@ -142,21 +136,25 @@ for (int i = 0; i < selector.jjtGetNumChildren(); i++) { SimpleNode child = selector.getChild(i); switch (child.getId()) { + //class case CSSParserTreeConstants.JJTJAVACLASS: if (!child.getText().trim().equals("*")) { javaClassName = child.getText(); } break; + //styleclass case CSSParserTreeConstants.JJTCLASS: styleClass = child.getText().substring(1); break; + //pseudoclass case CSSParserTreeConstants.JJTPSEUDOCLASS: pseudoClass = child.getText().substring(1); break; + //id case CSSParserTreeConstants.JJTID: id = child.getText().substring(1); break; - + //error if other default: throw new IllegalStateException("unexpected child of Selector node, type=" + child.getId()); } @@ -166,29 +164,35 @@ } /** + * Analyses a CSS rule * - * @param ruleNode - * @return + * @param ruleNode the CSSParser node containing the rule to analyse + * @return a Rule containing the properties and the selectors of the rule */ protected Rule processRule(SimpleNode ruleNode) { - + //checks if it is a rule if (ruleNode.getId() != CSSParserTreeConstants.JJTRULE) { throw new IllegalArgumentException("argument node is not a Rule"); } + SimpleNode selectorsNode = ruleNode.getChild(0); assert selectorsNode.getId() == CSSParserTreeConstants.JJTSELECTORS : "expected node to be of type Selectors"; List<Selector> ruleSelectors = new ArrayList<Selector>(); + //analyses each selector for (int i = 0; i < selectorsNode.jjtGetNumChildren(); i++) { SimpleNode selectorNode = selectorsNode.getChild(i); Selector selector = processSelector(selectorNode); ruleSelectors.add(selector); + //records the selector if it has not been recorded yet + //to add each selector only once in the StyleSheet if (!selectorRecorded(selector)) { selectors.add(selector); } } Map<String, String> properties = new HashMap<String, String>(); + //records the properties for (int i = 1; i < ruleNode.jjtGetNumChildren(); i++) { SimpleNode declarationNode = ruleNode.getChild(i); if (declarationNode.getId() == CSSParserTreeConstants.JJTDECLARATION) { @@ -201,8 +205,8 @@ properties.put(key, value); } } - Rule rule; - rule = new Rule(); + //creates the new rule + Rule rule = new Rule(); rule.setSelectors(ruleSelectors); for (Selector selector : ruleSelectors) { selector.getRules().add(rule); @@ -212,11 +216,13 @@ } /** + * Analyses the CSS * - * @param stylesheetText - * @return + * @param stylesheetText the content of the CSS file or the style tag + * @return the StyleSheet containing the rules of the CSS file or the style tag */ protected StyleSheet processStylesheet(String stylesheetText) { + //parses the CSS CSSParser p = new CSSParser(new StringReader(stylesheetText)); SimpleNode node; try { @@ -224,14 +230,16 @@ } catch (ParseException ex) { throw ex; } + List<Rule> rules = new ArrayList<Rule>(); + //analyses each rule for (int i = 0; i < node.jjtGetNumChildren(); i++) { SimpleNode ruleNode = node.getChild(i); Rule rule = processRule(ruleNode); rules.add(rule); } - StyleSheet styleSheet; - styleSheet = new StyleSheet(); + + StyleSheet styleSheet = new StyleSheet(); styleSheet.setRules(rules); styleSheet.setSelectors(selectors); return styleSheet; 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-24 15:39:17 UTC (rev 1358) +++ guix/trunk/guix-compiler/src/main/java/org/nuiton/guix/tags/TagManager.java 2009-04-27 16:06:15 UTC (rev 1359) @@ -1,7 +1,3 @@ -/* - * Copyright 2006 Ethan Nicholas. All rights reserved. - * Use is subject to license terms. - */ package org.nuiton.guix.tags; import java.util.logging.Level; @@ -19,13 +15,15 @@ import java.util.Map; import java.util.Set; -/** Manages TagHandlers, including automatically compiling .jaxx files corresponding to class tags. */ +/** + * Manages tags of the .guix files. + */ public class TagManager { /** log */ protected static final Log log = LogFactory.getLog(TagManager.class); /** - * Namespace for JAXX's non-class tags, such as <script;>. The namespace normally does not + * 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. */ public static final String GUIX_NAMESPACE = "org.nuiton.guix.*"; @@ -33,59 +31,13 @@ /** Maps simple tag names to their default namespaces (package names). */ private static Map<String, String> defaultNamespaces = new HashMap<String, String>(); - /** Maps qualified tag names to the TagHandlers responsible for processing them. */ - //private static Map<QName, TagHandler> registeredTags = new HashMap<QName, TagHandler>(); - /** Keeps track of whether or not named classes exist. */ 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>(); - /** - * Maps bean classes to their TagHandler classes. The mapping is to TagHandler classes, rather than to - * TagHandler instances, because subclasses of the bean class should be handled by the same TagHandler - * (assuming no more specific mappings exist), which requires creating a new instance of the TagHandler. - */ - //private static ClassMap<Class<? extends TagHandler>> registeredBeans = new ClassMap<Class<? extends TagHandler>>(); - // still targeting 1.4, so I can't use javax.xml.namespace.QName - private static class QName { - private String namespaceURI; - private String localPart; - - public QName(String namespaceURI, String localPart) { - if (localPart == null) - throw new NullPointerException(); - this.namespaceURI = namespaceURI; - this.localPart = localPart; - } - - - public String getNamespaceURI() { - return namespaceURI; - } - - - public String getLocalPart() { - return localPart; - } - - @Override - public boolean equals(Object o) { - if (o == null || !(o instanceof QName)) - return false; - QName qname = (QName) o; - return qname.getNamespaceURI().equals(getNamespaceURI()) && qname.getLocalPart().equals(getLocalPart()); - } - - @Override - public int hashCode() { - return (namespaceURI != null ? namespaceURI.hashCode() : 0) ^ getLocalPart().hashCode(); - } - } - - private TagManager() { /* not instantiable */ } @@ -253,7 +205,12 @@ } }*/ - + /** + * Tests if a class exists + * + * @param className name of the class we want to know if it exists + * @return true if the class exists + */ private static boolean classExists(String className) { if (classExistenceCache.containsKey(className)) { return classExistenceCache.get(className); @@ -276,40 +233,6 @@ return found; } - - /*private static String determinePackage(String simpleClassName, String defaultPackage) { - String namespace = null; - Set<String> classes = compiler.getImportedClasses(); - for (String className : classes) { // search class imports (e.g. import java.util.Date;) - if (className.equals(simpleClassName) || className.endsWith("." + simpleClassName)) { - namespace = className.substring(0, className.lastIndexOf(".") + 1) + "*"; - } - } - if (namespace == null) { // search package imports (e.g. import java.util.*;) - Set<String> searchList = compiler.getImportedPackages(); - if (defaultPackage != null) { - if (!defaultPackage.endsWith("*")) { - throw new IllegalArgumentException("defaultPackage must end in '*', found '" + defaultPackage + "'"); - } - if (classExists(defaultPackage.substring(0, defaultPackage.length() - 1) + simpleClassName)) { - return defaultPackage; - } - } - for (String currentPackage : searchList) { - String className = currentPackage + simpleClassName; - if (classExists(className)) { - if (namespace != null) { // we've already found the same name in another package - //compiler.reportError("symbol '" + simpleClassName + "' is ambiguous, found matching classes " + namespace.substring(0, namespace.length() - 1) + simpleClassName + " and " + currentPackage + simpleClassName + ". Use fully-qualified name to disambiguate."); - return null; - } - namespace = currentPackage + "*"; - } - } - } - - return namespace; - }*/ - /** * Resolves a simple class name (like <code>Object</code> or <code>String</code>) to its fully-qualified name. Inner * classes should be represented as they would appear in Java source code (e.g. JPopupMenu.Separator). Fully-qualified names, @@ -338,16 +261,4 @@ return null; } - - /*public static ClassDescriptor resolveClass(String className, JAXXCompiler compiler) { - try { - className = resolveClassName(className, compiler); - if (className == null) - return null; - return ClassDescriptorLoader.getClassDescriptor(className, compiler.getClassLoader()); - } - catch (ClassNotFoundException e) { - return null; - } - }*/ } \ No newline at end of file