r1860 - in trunk: jaxx-compiler/src/main/java/jaxx/compiler jaxx-compiler/src/main/java/jaxx/compiler/css jaxx-compiler/src/main/java/jaxx/compiler/tags jaxx-widgets jaxx-widgets/src/main/java/jaxx/runtime/swing/editor jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin
Author: tchemit Date: 2010-04-29 16:01:01 +0200 (Thu, 29 Apr 2010) New Revision: 1860 Url: http://nuiton.org/repositories/revision/jaxx/1860 Log: - Evolution #558: Add Auto import of css mode in generate goal - clean some code - improve css usefull methods Modified: trunk/jaxx-compiler/src/main/java/jaxx/compiler/CompilerConfiguration.java trunk/jaxx-compiler/src/main/java/jaxx/compiler/DefaultCompilerConfiguration.java trunk/jaxx-compiler/src/main/java/jaxx/compiler/JAXXCompiler.java trunk/jaxx-compiler/src/main/java/jaxx/compiler/JAXXEngine.java trunk/jaxx-compiler/src/main/java/jaxx/compiler/css/StylesheetHelper.java trunk/jaxx-compiler/src/main/java/jaxx/compiler/tags/StyleHandler.java trunk/jaxx-widgets/pom.xml trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/TimeEditor.jaxx trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/ConfigCategoryUI.jaxx trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/ConfigUI.jaxx trunk/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/GenerateMojo.java Modified: trunk/jaxx-compiler/src/main/java/jaxx/compiler/CompilerConfiguration.java =================================================================== --- trunk/jaxx-compiler/src/main/java/jaxx/compiler/CompilerConfiguration.java 2010-04-29 08:54:33 UTC (rev 1859) +++ trunk/jaxx-compiler/src/main/java/jaxx/compiler/CompilerConfiguration.java 2010-04-29 14:01:01 UTC (rev 1860) @@ -29,7 +29,7 @@ import jaxx.runtime.JAXXContext; /** - * TODO javadoc. + * Configuration of a compiler task. * * @author tchemit <chemit@codelutin.com> * @since 2.0.0 @@ -87,6 +87,12 @@ boolean getOptimize(); /** + * Returns whether or not auto css should be used. + * @return {@code true} if a css file with same name as jaxx file should be included in jaxx file if found. + */ + boolean isAutoImportCss(); + + /** * Returns the target directory. * * @return the target directory Modified: trunk/jaxx-compiler/src/main/java/jaxx/compiler/DefaultCompilerConfiguration.java =================================================================== --- trunk/jaxx-compiler/src/main/java/jaxx/compiler/DefaultCompilerConfiguration.java 2010-04-29 08:54:33 UTC (rev 1859) +++ trunk/jaxx-compiler/src/main/java/jaxx/compiler/DefaultCompilerConfiguration.java 2010-04-29 14:01:01 UTC (rev 1860) @@ -114,6 +114,8 @@ * Encoding to use to write files */ private String encoding; + + private boolean autoImportCss; @Override public File getTargetDirectory() { @@ -215,6 +217,11 @@ } @Override + public boolean isAutoImportCss() { + return autoImportCss; + } + + @Override public String toString() { return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); } Modified: trunk/jaxx-compiler/src/main/java/jaxx/compiler/JAXXCompiler.java =================================================================== --- trunk/jaxx-compiler/src/main/java/jaxx/compiler/JAXXCompiler.java 2010-04-29 08:54:33 UTC (rev 1859) +++ trunk/jaxx-compiler/src/main/java/jaxx/compiler/JAXXCompiler.java 2010-04-29 14:01:01 UTC (rev 1860) @@ -47,6 +47,7 @@ import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.nuiton.util.FileUtil; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.*; @@ -254,6 +255,17 @@ protected final JAXXEngine engine; /** + * Flag to know if jaxx file ident css was found, otherwise add it + * when {@link CompilerConfiguration#isAutoImportCss()} is sets to {@code true} + * at the end of the second compile pass. + * + * @since 2.0.2 + */ + protected boolean identCssFound; + + public static final String[] EMPTY_STRING_ARRAY = new String[0]; + + /** * Creates a new JAXXCompiler. * * @param engine engine which use the compiler (could be null if not attach to any engine) @@ -263,16 +275,24 @@ * @param configuration configuration to pass to javac * @param defaultImports list of default imports to add to java files */ - public JAXXCompiler(JAXXEngine engine, File baseDir, File src, String outputClassName, CompilerConfiguration configuration, List<String> defaultImports) { + public JAXXCompiler(JAXXEngine engine, + File baseDir, + File src, + String outputClassName, + CompilerConfiguration configuration, + List<String> defaultImports) { this.engine = engine; this.baseDir = baseDir; this.src = src; - this.firstPassClassTagHandler = new DefaultObjectHandler(ClassDescriptorLoader.getClassDescriptor(Object.class)); + firstPassClassTagHandler = new DefaultObjectHandler( + ClassDescriptorLoader.getClassDescriptor(Object.class) + ); sourceFiles.push(src); this.outputClassName = outputClassName; this.configuration = configuration; if (outputClassName != null) { - addImport(outputClassName.substring(0, outputClassName.lastIndexOf(".") + 1) + "*"); + addImport(outputClassName.substring( + 0, outputClassName.lastIndexOf(".") + 1) + "*"); } if (defaultImports != null) { for (Object staticImport : defaultImports) { @@ -286,10 +306,14 @@ addImport(extraImport); } } - defaultDecorator = engine.getDecorator(configuration.getDefaultDecoratorClass()); + defaultDecorator = engine.getDecorator( + configuration.getDefaultDecoratorClass()); if (defaultDecorator == null) { - log.error("could not find default decorator : " + configuration.getDefaultDecoratorClass()); - throw new IllegalArgumentException("could not find default decorator : " + configuration.getDefaultDecoratorClass()); + log.error("could not find default decorator : " + + configuration.getDefaultDecoratorClass()); + throw new IllegalArgumentException( + "could not find default decorator : " + + configuration.getDefaultDecoratorClass()); } idHelper = new IDHelper(configuration.isOptimize()); } else { @@ -369,16 +393,16 @@ // add java bean support for this property String capitalizeName = StringUtils.capitalize(id); // add method - symbolTable.getScriptMethods().add(new MethodDescriptor("get" + capitalizeName, Modifier.PUBLIC, fullClassName, new String[0], classLoader)); + symbolTable.getScriptMethods().add(new MethodDescriptor("get" + capitalizeName, Modifier.PUBLIC, fullClassName, EMPTY_STRING_ARRAY, classLoader)); if (Boolean.class.getName().equals(fullClassName)) { - symbolTable.getScriptMethods().add(new MethodDescriptor("is" + capitalizeName, Modifier.PUBLIC, fullClassName, new String[0], classLoader)); + symbolTable.getScriptMethods().add(new MethodDescriptor("is" + capitalizeName, Modifier.PUBLIC, fullClassName, EMPTY_STRING_ARRAY, classLoader)); } symbolTable.getScriptMethods().add(new MethodDescriptor("set" + capitalizeName, Modifier.PUBLIC, "void", new String[]{fullClassName}, classLoader)); } else { // add simple get support String capitalizeName = StringUtils.capitalize(id); // add method - symbolTable.getScriptMethods().add(new MethodDescriptor("get" + capitalizeName, Modifier.PUBLIC, fullClassName, new String[0], classLoader)); + symbolTable.getScriptMethods().add(new MethodDescriptor("get" + capitalizeName, Modifier.PUBLIC, fullClassName, EMPTY_STRING_ARRAY, classLoader)); } } String interfacesStr = tag.getAttribute(DefaultObjectHandler.IMPLEMENTS_ATTRIBUTE); @@ -422,13 +446,15 @@ // (and we can't wait until they have been processed because of circular dependencies). So we don't do any processing // during the first pass which requires having a ClassDescriptor; here we determine whether we have a class tag or not // (class tag namespaces end in "*") and use a generic handler if so. The real handler is used during the second pass. - TagHandler handler = (namespace != null && namespace.endsWith("*")) ? firstPassClassTagHandler : TagManager.getTagHandler(tag.getNamespaceURI(), localName, namespacePrefix, this); - if (handler != firstPassClassTagHandler && handler instanceof DefaultObjectHandler) { + TagHandler handler = namespace != null && namespace.endsWith("*") ? + firstPassClassTagHandler : + TagManager.getTagHandler(tag.getNamespaceURI(), localName, namespacePrefix, this); + if (!firstPassClassTagHandler.equals(handler) && handler instanceof DefaultObjectHandler) { fullClassName = ((DefaultObjectHandler) handler).getBeanClass().getName(); //namespace = fullClassName.substring(0, fullClassName.lastIndexOf(".") + 1) + "*"; handler = firstPassClassTagHandler; } - if (handler == firstPassClassTagHandler) { + if (firstPassClassTagHandler.equals(handler)) { final String finalClassName = fullClassName; registerInitializer(new Runnable() { // register an initializer which will create the CompiledObject after pass 1 @@ -478,15 +504,16 @@ } protected void compileFirstPass() throws IOException { + InputStream in = new FileInputStream(src); try { - InputStream in = new FileInputStream(src); document = parseDocument(in); - in.close(); compileFirstPass(document.getDocumentElement()); } catch (SAXParseException e) { reportError(e.getLineNumber(), "Invalid XML: " + e.getMessage()); } catch (SAXException e) { reportError(null, "Error parsing XML document: " + e); + } finally { + in.close(); } } @@ -627,7 +654,7 @@ int index = 0; String result2; do { - result2 = result + "_" + (index++); + result2 = result + "_" + index++; } while (eventHandlerMethodNames.containsValue(result2)); result = result2; @@ -669,7 +696,10 @@ if (sourceFile != null) { File pop = sourceFiles.pop(); if (pop != sourceFile) { - throw new RuntimeException("leaving registerScript(), but " + sourceFile + " was not the top entry on the stack (found " + pop + " instead)"); + throw new RuntimeException( + "leaving registerScript(), but " + sourceFile + + " was not the top entry on the stack (found " + pop + + " instead)"); } } } @@ -681,6 +711,50 @@ /*------------------------------------------------------------------------*/ /*-- StyleSheet methods --------------------------------------------------*/ /*------------------------------------------------------------------------*/ + + public File getIdentCssFile() { + String extension = FileUtil.extension(src); + String jaxxFileName = src.getName(); + String identCssFilename = jaxxFileName.substring( + 0, + jaxxFileName.length() - extension.length()) + "css"; + File identCssFile = new File(src.getParentFile(), identCssFilename); + return identCssFile; + } + + public boolean isIdentCssFound() { + return identCssFound; + } + + public void registerStyleSheetFile(File styleFile) throws IOException { + if (!identCssFound && getConfiguration().isAutoImportCss()) { + + // detects if the given css file is ident to jaxx file + File identCssFile = getIdentCssFile(); + + if (identCssFile.exists()) { + + // ok found ident css file + identCssFound = true; + + reportWarning("autoImportCss mode is on, you can remove style declaration with source " + styleFile); +// if (getConfiguration().isVerbose()) { +// log.info("Ident css found : " + identCssFile); +// } + } + + } + String content = StylesheetHelper.loadCssFile(this, styleFile); + getSourceFiles().push(styleFile); + try { + Stylesheet stylesheet = StylesheetHelper.processStylesheet(content); + registerStylesheet(stylesheet); + } finally { + + // whatever could be result, must pop this source file + getSourceFiles().pop(); + } + } public void applyStylesheets() { for (Object o : new ArrayList<CompiledObject>(objects.values())) { CompiledObject object = (CompiledObject) o; @@ -728,7 +802,7 @@ StringBuilder buffer = new StringBuilder(); buffer.append(srcFile); if (lineNumber != null) { - buffer.append(":").append((sourceFiles.size() == 1) ? Integer.parseInt(lineNumber) + lineOffset : lineOffset + 1); + buffer.append(":").append(sourceFiles.size() == 1 ? Integer.parseInt(lineNumber) + lineOffset : lineOffset + 1); } buffer.append(getLineSeparator()).append(warning.trim()); if (engine != null) { @@ -752,7 +826,7 @@ public void reportError(String extraMessage, CompilerException ex) { String message = ex.getMessage(); - if (ex.getClass() == UnsupportedAttributeException.class || ex.getClass() == UnsupportedTagException.class) { + if (UnsupportedAttributeException.class.equals(ex.getClass()) || UnsupportedTagException.class.equals(ex.getClass())) { message = ex.getClass().getName().substring(ex.getClass().getName().lastIndexOf(".") + 1) + ": " + message; } int lineOffset; @@ -950,7 +1024,7 @@ int parentIndex = -1; if (parent != null) { for (int j = 0; j < i; j++) { - if (components[j] == parent) { + if (components[j].equals(parent)) { parentIndex = j; break; } @@ -1106,43 +1180,14 @@ return true; } -// public String getAutoId(ClassDescriptor objectClass) { -// String name = objectClass.getName(); -// name = name.substring(name.lastIndexOf(".") + 1); -// return getAutoId(name); -//// if (configuration.getOptimize()) { -//// return "$" + Integer.toString(autogenID++, 36); -//// } else { -//// String name = objectClass.getName(); -//// name = name.substring(name.lastIndexOf(".") + 1); -//// return "$" + name + autogenID++; -//// } -// } - public String getAutoId(String name) { return idHelper.nextId(name); -// if (configuration.getOptimize()) { -// return "$" + Integer.toString(autogenID++, 36); -// } else { -// name = name.substring(name.lastIndexOf(".") + 1); -// return "$" + name + autogenID++; -// } } public String getUniqueId(Object object) { return idHelper.getUniqueId(object); -// String result = uniqueIds.get(object); -// if (result == null) { -// result = "$u" + uniqueIds.size(); -// uniqueIds.put(object, result); -// } -// return result; } -// public void revertId(String name) { -// idHelper.revertId(name); -// } - public void setExtraInterfaces(String[] extraInterfaces) { this.extraInterfaces = extraInterfaces; } @@ -1246,19 +1291,11 @@ log.warn("could not touch file " + dest); } PrintWriter out = null; + out = new PrintWriter(new FileWriter(dest)); try { - out = new PrintWriter(new FileWriter(dest)); generator.generateFile(javaFile, out); - } catch (IOException e) { - // file could not be generated, so delete it... - if (!dest.delete()) { - log.warn("could not delete file " + dest); - } - throw e; } finally { - if (out != null) { - out.close(); - } + out.close(); } } @@ -1450,286 +1487,4 @@ ids.clear(); } -// /*------------------------------------------------------------------------*/ -// /*-- REMOVE CODE (Goto to idHelper and DataBindingHelper) ----------------*/ -// /*------------------------------------------------------------------------*/ -// /** -// * Sequence number used to create automatic variable names. -// */ -// protected int autogenID = 0; -// /** -// * data bindings detected -// */ -// protected List<DataBinding> dataBindings = new ArrayList<DataBinding>(); -// /** -// * Maps of uniqued id for objects used in compiler -// */ -// protected Map<Object, String> uniqueIds = new HashMap<Object, String>(); -// /** -// * left brace matcher -// */ -// protected Matcher leftBraceMatcher = Pattern.compile("^(\\{)|[^\\\\](\\{)").matcher(""); -// /** -// * right brace matcher -// */ -// protected Matcher rightBraceMatcher = Pattern.compile("^(\\})|[^\\\\](\\})").matcher(""); -// /** -// * Code to initialize data bindings. -// */ -// protected StringBuffer initDataBindings = new StringBuffer(); -// /** -// * Body of the applyDataBinding method. -// */ -// protected StringBuffer applyDataBinding = new StringBuffer(); -// /** -// * Body of the removeDataBinding method. -// */ -// protected StringBuffer removeDataBinding = new StringBuffer(); -// /** -// * Body of the processDataBinding method. -// */ -// protected StringBuffer processDataBinding = new StringBuffer(); -// public String getJavaCode(Object object) { -// String result = TypeManager.getJavaCode(object); -// return result; -// } -// public Object convertFromString(String string, Class<?> type) { -// Object result = TypeManager.convertFromString(string, type); -// return result; -// } -// public List<DataBinding> getDataBindings() { -// return dataBindings; -// } -// /** -// * 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 -// * @return a processed version of the expression -// * @throws CompilerException ? -// */ -// public String processDataBindings(String stringValue) throws CompilerException { -// 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('"'); -// } -// boolean multi = expression.length() > 0; -// if (multi) { -// expression.append(" + "); -// expression.append('('); -// } -// int pos2 = getNextRightBrace(stringValue, pos + 1); -// if (pos2 == -1) { -// reportError("unmatched '{' in expression: " + stringValue); -// return ""; -// } -// expression.append(stringValue.substring(pos + 1, pos2)); -// if (multi) { -// expression.append(')'); -// } -// 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('"'); -// } -// //TC-20091027 : developper must write extact databinding -// // the fact of adding the String boxed for String type binding is not -// // a good thing, since it add one more call to process in binding -// // and add nothing special more ? -//// return type == ClassDescriptorLoader.getClassDescriptor(String.class) ? "String.valueOf(" + expression + ")" : expression.toString(); -// return expression.toString(); -// } -// return null; -// } -// public void registerDataBinding(String src, String dest, String assignment) { -// try { -// src = checkJavaCode(src); -//// if (log.isDebugEnabled()) { -//// log.info(getRootObject().getId() + " src=" + src + ", dst=" + dest.trim()); -//// } -// dataBindings.add(new DataBinding(src, dest, assignment)); -//// dataBindings.add(new DataBinding(src, dest, assignment, this)); -// } catch (CompilerException e) { -// reportError("While parsing data binding for '" + dest.substring(dest.lastIndexOf(".") + 1) + "': " + e.getMessage()); -// } -// } -// public void setFailed(boolean failed) { -// this.failed = failed; -// } -// public StringBuffer getInitDataBindings() { -// return initDataBindings; -// } -// public StringBuffer getApplyDataBinding() { -// return applyDataBinding; -// } -// public StringBuffer getRemoveDataBinding() { -// return removeDataBinding; -// } -// public StringBuffer getProcessDataBinding() { -// return processDataBinding; -// } -// public void appendInitDataBindings(String code) { -// initDataBindings.append(code); -// } - -// public void appendProcessDataBinding(String code) { -// processDataBinding.append(code); -// } - -// public void appendApplyDataBinding(String code) { -// applyDataBinding.append(code); -// } - -// public void appendRemoveDataBinding(String code) { -// removeDataBinding.append(code); -// } - -// public boolean hasProcessDataBinding() { -// return processDataBinding.length() > 0; -// } - -// public boolean hasApplyDataBinding() { -// return applyDataBinding.length() > 0; -// } - - // public boolean hasRemoveDataBinding() { -// return removeDataBinding.length() > 0; -// } -// public int getNextLeftBrace(String string, int pos) { -// leftBraceMatcher.reset(string); -// return leftBraceMatcher.find(pos) ? Math.max(leftBraceMatcher.start(1), leftBraceMatcher.start(2)) : -1; -// } -// -// public 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; -// } -// -// public String[] parseParameterList(String parameters) throws CompilerException { -// List<String> result = new ArrayList<String>(); -// StringBuffer current = new StringBuffer(); -// int state = 0; // normal -// for (int i = 0; i < parameters.length(); i++) { -// char c = parameters.charAt(i); -// switch (state) { -// case 0: // normal -// switch (c) { -// case '"': -// current.append(c); -// state = 1; -// break; // in quoted string -// case '\\': -// current.append(c); -// state = 2; -// break; // immediately after backslash -// case ',': -// if (current.length() > 0) { -// result.add(current.toString()); -// current.setLength(0); -// break; -// } else { -// reportError("error parsing parameter list: " + parameters); -// break; -// } -// default: -// current.append(c); -// } -// break; -// case 1: // in quoted string -// switch (c) { -// case '"': -// current.append(c); -// state = 0; -// break; // normal -// case '\\': -// current.append(c); -// state = 3; -// break; // immediate after backslash in quoted string -// default: -// current.append(c); -// } -// break; -// case 2: // immediately after backslash -// current.append(c); -// state = 0; // normal -// break; -// case 3: // immediately after backslash in quoted string -// current.append(c); -// state = 1; // in quoted string -// break; -// } -// } -// if (current.length() > 0) { -// result.add(current.toString()); -// } -// return result.toArray(new String[result.size()]); -// } -// /** -// * Convertit un nom de variable en nom de constante. -// * -// * @param variableName le nom de variable a convertir -// * @return le nom de la constante à partir du nom de la variable -// */ -// public static String convertVariableNameToConstantName(String variableName) { -// StringBuilder buffer = new StringBuilder(); -// boolean lastCarIsUp = false; -// for (int i = 0, j = variableName.length(); i < j; i++) { -// char c = variableName.charAt(i); -// boolean carIsUp = Character.isUpperCase(c); -// if (i > 0 && !lastCarIsUp && carIsUp) { -// // ajout d'un _ -// buffer.append('_'); -// } -// if (carIsUp) { -// buffer.append(c); -// } else { -// buffer.append(Character.toUpperCase(c)); -// } -// lastCarIsUp = carIsUp; -// } -// return buffer.toString(); -// } - } Modified: trunk/jaxx-compiler/src/main/java/jaxx/compiler/JAXXEngine.java =================================================================== --- trunk/jaxx-compiler/src/main/java/jaxx/compiler/JAXXEngine.java 2010-04-29 08:54:33 UTC (rev 1859) +++ trunk/jaxx-compiler/src/main/java/jaxx/compiler/JAXXEngine.java 2010-04-29 14:01:01 UTC (rev 1860) @@ -255,8 +255,8 @@ do { compiled = false; assert engine.jaxxFiles.size() == engine.jaxxFileClassNames.size(); - java.util.Iterator<File> filesIterator = new ArrayList<File>(engine.jaxxFiles).iterator(); // clone it so it can safely be modified while we're iterating - java.util.Iterator<String> classNamesIterator = new ArrayList<String>(engine.jaxxFileClassNames).iterator(); + Iterator<File> filesIterator = new ArrayList<File>(engine.jaxxFiles).iterator(); // clone it so it can safely be modified while we're iterating + Iterator<String> classNamesIterator = new ArrayList<String>(engine.jaxxFileClassNames).iterator(); while (filesIterator.hasNext()) { File file = filesIterator.next(); String className = classNamesIterator.next(); @@ -284,6 +284,25 @@ addStartProfileTime(engine, compiler); engine.compilers.put(className, compiler); compiler.compileFirstPass(); + if (compiler.getConfiguration().isAutoImportCss() && !compiler.isIdentCssFound()) { + + // check if can add ident css file + + File cssFile = compiler.getIdentCssFile(); + if (log.isDebugEnabled()) { + log.debug("test ident css file " + cssFile+" : "+compiler.getConfiguration().isVerbose()); + } + if (cssFile.exists()) { + + compiler.identCssFound = true; + + if (compiler.getConfiguration().isVerbose()) { + log.info("Auto import of css " + cssFile); + } + // ok add it + compiler.registerStyleSheetFile(cssFile); + } + } addEndProfileTime(engine, compiler); assert !engine.symbolTables.values().contains(compiler.getSymbolTable()) : "symbolTable is already registered"; engine.symbolTables.put(file, compiler.getSymbolTable()); Modified: trunk/jaxx-compiler/src/main/java/jaxx/compiler/css/StylesheetHelper.java =================================================================== --- trunk/jaxx-compiler/src/main/java/jaxx/compiler/css/StylesheetHelper.java 2010-04-29 08:54:33 UTC (rev 1859) +++ trunk/jaxx-compiler/src/main/java/jaxx/compiler/css/StylesheetHelper.java 2010-04-29 14:01:01 UTC (rev 1860) @@ -31,6 +31,10 @@ import jaxx.compiler.binding.DataBinding; import jaxx.compiler.binding.DataBindingHelper; import jaxx.compiler.binding.PseudoClassDataBinding; +import jaxx.compiler.css.parser.CSSParser; +import jaxx.compiler.css.parser.CSSParserConstants; +import jaxx.compiler.css.parser.CSSParserTreeConstants; +import jaxx.compiler.css.parser.SimpleNode; import jaxx.compiler.reflect.ClassDescriptor; import jaxx.compiler.reflect.ClassDescriptorLoader; import jaxx.compiler.reflect.MethodDescriptor; @@ -43,6 +47,7 @@ import java.awt.event.MouseEvent; import java.awt.event.MouseListener; +import java.io.*; import java.util.*; /** @@ -55,6 +60,110 @@ */ public class StylesheetHelper { + public static String loadCssFile(JAXXCompiler compiler, + File styleFile) throws IOException { + FileReader in = new FileReader(styleFile); + try { + StringWriter styleBuffer = new StringWriter(); + char[] readBuffer = new char[2048]; + int c; + while ((c = in.read(readBuffer)) > 0) { + styleBuffer.write(readBuffer, 0, c); + } + return styleBuffer.toString(); + } catch (FileNotFoundException e) { + compiler.reportError("stylesheet file not found: " + styleFile); + return ""; + } finally { + in.close(); + } + } + + public static Stylesheet processStylesheet(String stylesheetText) throws CompilerException { + CSSParser p = new CSSParser(new StringReader(stylesheetText)); + SimpleNode node; + try { + node = p.Stylesheet(); + } catch (Error e) { + throw new CompilerException(e); + } + List<Rule> rules = new ArrayList<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(rules.toArray(new Rule[rules.size()])); + return stylesheet; + } + + public static Rule processRule(SimpleNode ruleNode) { + 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> selectors = new ArrayList<Selector>(); + for (int i = 0; i < selectorsNode.jjtGetNumChildren(); i++) { + SimpleNode selectorNode = selectorsNode.getChild(i); + selectors.add(processSelector(selectorNode)); + } + + Map<String, String> properties = new HashMap<String, String>(); + for (int i = 1; i < ruleNode.jjtGetNumChildren(); i++) { + SimpleNode declarationNode = ruleNode.getChild(i); + if (declarationNode.getId() == CSSParserTreeConstants.JJTDECLARATION) { + String key = declarationNode.getChild(0).getText(); + SimpleNode valueNode = declarationNode.getChild(1); + String value = valueNode.getText(); + if (valueNode.firstToken.kind == CSSParserConstants.STRING) { + value = value.substring(1, value.length() - 1); + } + properties.put(key, value); + } + } + Rule rule; + rule = new Rule(selectors.toArray(new Selector[selectors.size()]), properties); + return rule; + } + + public static Selector processSelector(SimpleNode selector) { + if (selector.getId() != CSSParserTreeConstants.JJTSELECTOR) { + throw new IllegalArgumentException("argument node is not a Selector"); + } + String javaClassName = null; + String styleClass = null; + String pseudoClass = null; + String id = null; + + for (int i = 0; i < selector.jjtGetNumChildren(); i++) { + SimpleNode child = selector.getChild(i); + switch (child.getId()) { + case CSSParserTreeConstants.JJTJAVACLASS: + if (!child.getText().trim().equals("*")) { + javaClassName = child.getText(); + } + break; + case CSSParserTreeConstants.JJTCLASS: + styleClass = child.getText().substring(1); + break; + case CSSParserTreeConstants.JJTPSEUDOCLASS: + pseudoClass = child.getText().substring(1); + break; + case CSSParserTreeConstants.JJTID: + id = child.getText().substring(1); + break; + + default: + throw new IllegalStateException("unexpected child of Selector node, type=" + child.getId()); + } + } + + return new Selector(javaClassName, styleClass, pseudoClass, id); + } + public enum MouseEventEnum { mouseover("mouseEntered", "mouseExited"), @@ -145,7 +254,10 @@ } } - public static void applyTo(CompiledObject object, JAXXCompiler compiler, Stylesheet stylesheet, Stylesheet overrides) throws CompilerException { + public static void applyTo(CompiledObject object, + JAXXCompiler compiler, + Stylesheet stylesheet, + Stylesheet overrides) throws CompilerException { Map<String, String> overriddenProperties; if (overrides != null) { overriddenProperties = getApplicableProperties(overrides, object); @@ -159,30 +271,37 @@ if (overriddenProperties != null) { properties.keySet().removeAll(overriddenProperties.keySet()); } - DefaultObjectHandler handler = TagManager.getTagHandler(object.getObjectClass()); + DefaultObjectHandler handler = + TagManager.getTagHandler(object.getObjectClass()); for (Map.Entry<String, String> e : properties.entrySet()) { String value = e.getValue(); - if (value.equals(Rule.INLINE_ATTRIBUTE) || value.equals(Rule.DATA_BINDING)) { + if (value.equals(Rule.INLINE_ATTRIBUTE) || + value.equals(Rule.DATA_BINDING)) { continue; } - handler.setAttribute(object, e.getKey(), e.getValue(), false, compiler); + handler.setAttribute(object, e.getKey(), e.getValue(), + false, compiler); } } Rule[] pseudoClasses = getApplicablePseudoClasses(stylesheet, object); if (pseudoClasses != null) { - Map<String, Map<String, String>> combinedPseudoClasses = new LinkedHashMap<String, Map<String, String>>(); + Map<String, Map<String, String>> combinedPseudoClasses = + new LinkedHashMap<String, Map<String, String>>(); for (Rule pseudoClass1 : pseudoClasses) { Selector[] selectors = pseudoClass1.getSelectors(); for (Selector selector : selectors) { - if (appliesTo(selector, object) == Selector.PSEUDOCLASS_APPLIES) { + if (appliesTo(selector, object) == + Selector.PSEUDOCLASS_APPLIES) { properties = pseudoClass1.getProperties(); String pseudoClass = selector.getPseudoClass(); // TODO: overrides by downstream pseudoclasses are not handled - Map<String, String> combinedProperties = combinedPseudoClasses.get(pseudoClass); + Map<String, String> combinedProperties = + combinedPseudoClasses.get(pseudoClass); if (combinedProperties == null) { combinedProperties = new HashMap<String, String>(); - combinedPseudoClasses.put(pseudoClass, combinedProperties); + combinedPseudoClasses.put(pseudoClass, + combinedProperties); } combinedProperties.putAll(properties); } @@ -190,8 +309,10 @@ } int count = 0; - for (Map.Entry<String, Map<String, String>> e : combinedPseudoClasses.entrySet()) { - applyPseudoClass(e.getKey(), e.getValue(), object, compiler, count++); + for (Map.Entry<String, Map<String, String>> e : + combinedPseudoClasses.entrySet()) { + applyPseudoClass(e.getKey(), e.getValue(), object, compiler, + count++); } } } @@ -327,28 +448,28 @@ // } public static String unwrap(ClassDescriptor type, String valueCode) { - if (type == ClassDescriptorLoader.getClassDescriptor(boolean.class)) { + if (ClassDescriptorLoader.getClassDescriptor(boolean.class).equals(type)) { return "((java.lang.Boolean) " + valueCode + ").booleanValue()"; } - if (type == ClassDescriptorLoader.getClassDescriptor(byte.class)) { + if (ClassDescriptorLoader.getClassDescriptor(byte.class).equals(type)) { return "((java.lang.Byte) " + valueCode + ").byteValue()"; } - if (type == ClassDescriptorLoader.getClassDescriptor(short.class)) { + if (ClassDescriptorLoader.getClassDescriptor(short.class).equals(type)) { return "((java.lang.Short) " + valueCode + ").shortValue()"; } - if (type == ClassDescriptorLoader.getClassDescriptor(int.class)) { + if (ClassDescriptorLoader.getClassDescriptor(int.class).equals(type)) { return "((java.lang.Integer) " + valueCode + ").intValue()"; } - if (type == ClassDescriptorLoader.getClassDescriptor(long.class)) { + if (ClassDescriptorLoader.getClassDescriptor(long.class).equals(type)) { return "((java.lang.Long) " + valueCode + ").longValue()"; } - if (type == ClassDescriptorLoader.getClassDescriptor(float.class)) { + if (ClassDescriptorLoader.getClassDescriptor(float.class).equals(type)) { return "((java.lang.Float) " + valueCode + ").floatValue()"; } - if (type == ClassDescriptorLoader.getClassDescriptor(double.class)) { + if (ClassDescriptorLoader.getClassDescriptor(double.class).equals(type)) { return "((java.lang.Double) " + valueCode + ").doubleValue()"; } - if (type == ClassDescriptorLoader.getClassDescriptor(char.class)) { + if (ClassDescriptorLoader.getClassDescriptor(char.class).equals(type)) { return "((java.lang.Character) " + valueCode + ").charValue()"; } return valueCode; @@ -375,10 +496,10 @@ public static void applyPseudoClass(String pseudoClass, Map<String, String> properties, CompiledObject object, JAXXCompiler compiler, int priority) throws CompilerException { - if (pseudoClass.indexOf("[") != -1) { + if (pseudoClass.contains("[")) { pseudoClass = pseudoClass.substring(0, pseudoClass.indexOf("[")); } - final StringBuffer buffer = new StringBuffer(); + StringBuffer buffer = new StringBuffer(); DefaultObjectHandler handler = TagManager.getTagHandler(object.getObjectClass()); boolean valueDeclared = false; @@ -564,7 +685,7 @@ String id = selector.getId(); while (parent != null) { - boolean classMatch = (javaClassName == null); + boolean classMatch = javaClassName == null; if (!classMatch) { ClassDescriptor javaClass = parent.getObjectClass(); do { @@ -577,11 +698,13 @@ } while (javaClass != null); } - boolean styleClassMatch = (styleClass == null || styleClass.equals(parent.getStyleClass())); + boolean styleClassMatch = styleClass == null || + styleClass.equals(parent.getStyleClass()); String objectId = parent.getId(); objectId = objectId.substring(objectId.lastIndexOf(".") + 1); - boolean idMatch = (id == null || (' ' + objectId + ' ').indexOf(' ' + id + ' ') > -1); + boolean idMatch = id == null || + (' ' + objectId + ' ').contains(' ' + id + ' '); if (classMatch && styleClassMatch && idMatch) { if (pseudoClass != null) { Modified: trunk/jaxx-compiler/src/main/java/jaxx/compiler/tags/StyleHandler.java =================================================================== --- trunk/jaxx-compiler/src/main/java/jaxx/compiler/tags/StyleHandler.java 2010-04-29 08:54:33 UTC (rev 1859) +++ trunk/jaxx-compiler/src/main/java/jaxx/compiler/tags/StyleHandler.java 2010-04-29 14:01:01 UTC (rev 1860) @@ -28,12 +28,7 @@ import jaxx.compiler.CompilerException; import jaxx.compiler.UnsupportedAttributeException; import jaxx.compiler.JAXXCompiler; -import jaxx.compiler.css.parser.CSSParser; -import jaxx.compiler.css.parser.CSSParserConstants; -import jaxx.compiler.css.parser.CSSParserTreeConstants; -import jaxx.compiler.css.parser.SimpleNode; -import jaxx.runtime.css.Rule; -import jaxx.runtime.css.Selector; +import jaxx.compiler.css.StylesheetHelper; import jaxx.runtime.css.Stylesheet; import org.w3c.dom.Attr; import org.w3c.dom.Element; @@ -43,15 +38,8 @@ import org.w3c.dom.Text; import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; import java.io.IOException; -import java.io.StringReader; import java.io.StringWriter; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; /** * Handles the <code><style></code> tag. @@ -72,20 +60,27 @@ if (name.equals(SOURCE_ATTRIBUTE)) { source = true; File styleFile = new File(compiler.getBaseDir(), attrValue.replace('/', File.separatorChar)); - StringWriter styleBuffer = new StringWriter(); - try { - FileReader in = new FileReader(styleFile); - char[] readBuffer = new char[2048]; - int c; - while ((c = in.read(readBuffer)) > 0) { - styleBuffer.write(readBuffer, 0, c); - } - } catch (FileNotFoundException e) { + if (!styleFile.exists()) { compiler.reportError("stylesheet file not found: " + styleFile); + } else { + compiler.registerStyleSheetFile(styleFile); } - compiler.getSourceFiles().push(styleFile); - compiler.registerStylesheet(processStylesheet(styleBuffer.toString())); - compiler.getSourceFiles().pop(); +// StringWriter styleBuffer = new StringWriter(); +// FileReader in = new FileReader(styleFile); +// try { +// char[] readBuffer = new char[2048]; +// int c; +// while ((c = in.read(readBuffer)) > 0) { +// styleBuffer.write(readBuffer, 0, c); +// } +// } catch (FileNotFoundException e) { +// compiler.reportError("stylesheet file not found: " + styleFile); +// } finally { +// in.close(); +// } +// compiler.getSourceFiles().push(styleFile); +// compiler.registerStylesheet(StylesheetHelper.processStylesheet(styleBuffer.toString())); +// compiler.getSourceFiles().pop(); } else if (!name.startsWith(XMLNS_ATTRIBUTE) && !JAXXCompiler.JAXX_INTERNAL_NAMESPACE.equals(attribute.getNamespaceURI())) { throw new UnsupportedAttributeException(name); @@ -110,7 +105,9 @@ if (source) { compiler.reportError("<style> tag has both a source attribute and an inline stylesheet"); } - compiler.registerStylesheet(processStylesheet(style.toString())); + Stylesheet stylesheet = + StylesheetHelper.processStylesheet(style.toString()); + compiler.registerStylesheet(stylesheet); } } @@ -118,88 +115,4 @@ public void compileSecondPass(Element tag, JAXXCompiler compiler) throws CompilerException, IOException { } - protected Selector processSelector(SimpleNode selector) { - if (selector.getId() != CSSParserTreeConstants.JJTSELECTOR) { - throw new IllegalArgumentException("argument node is not a Selector"); - } - String javaClassName = null; - String styleClass = null; - String pseudoClass = null; - String id = null; - - for (int i = 0; i < selector.jjtGetNumChildren(); i++) { - SimpleNode child = selector.getChild(i); - switch (child.getId()) { - case CSSParserTreeConstants.JJTJAVACLASS: - if (!child.getText().trim().equals("*")) { - javaClassName = child.getText(); - } - break; - case CSSParserTreeConstants.JJTCLASS: - styleClass = child.getText().substring(1); - break; - case CSSParserTreeConstants.JJTPSEUDOCLASS: - pseudoClass = child.getText().substring(1); - break; - case CSSParserTreeConstants.JJTID: - id = child.getText().substring(1); - break; - - default: - throw new IllegalStateException("unexpected child of Selector node, type=" + child.getId()); - } - } - - return new Selector(javaClassName, styleClass, pseudoClass, id); - } - - protected Rule processRule(SimpleNode ruleNode) { - 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> selectors = new ArrayList<Selector>(); - for (int i = 0; i < selectorsNode.jjtGetNumChildren(); i++) { - SimpleNode selectorNode = selectorsNode.getChild(i); - selectors.add(processSelector(selectorNode)); - } - - Map<String, String> properties = new HashMap<String, String>(); - for (int i = 1; i < ruleNode.jjtGetNumChildren(); i++) { - SimpleNode declarationNode = ruleNode.getChild(i); - if (declarationNode.getId() == CSSParserTreeConstants.JJTDECLARATION) { - String key = declarationNode.getChild(0).getText(); - SimpleNode valueNode = declarationNode.getChild(1); - String value = valueNode.getText(); - if (valueNode.firstToken.kind == CSSParserConstants.STRING) { - value = value.substring(1, value.length() - 1); - } - properties.put(key, value); - } - } - Rule rule; - rule = new Rule(selectors.toArray(new Selector[selectors.size()]), properties); - return rule; - } - - protected Stylesheet processStylesheet(String stylesheetText) throws CompilerException { - CSSParser p = new CSSParser(new StringReader(stylesheetText)); - SimpleNode node; - try { - node = p.Stylesheet(); - } catch (Error e) { - throw new CompilerException(e); - } - List<Rule> rules = new ArrayList<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(rules.toArray(new Rule[rules.size()])); - return stylesheet; - } } Modified: trunk/jaxx-widgets/pom.xml =================================================================== --- trunk/jaxx-widgets/pom.xml 2010-04-29 08:54:33 UTC (rev 1859) +++ trunk/jaxx-widgets/pom.xml 2010-04-29 14:01:01 UTC (rev 1860) @@ -91,6 +91,7 @@ <properties> <jaxx.addSourcesToClassPath>true</jaxx.addSourcesToClassPath> + <jaxx.autoImportCss>true</jaxx.autoImportCss> <!--jaxx.useUIManagerForIcon>true</jaxx.useUIManagerForIcon--> </properties> Modified: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/TimeEditor.jaxx =================================================================== --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/TimeEditor.jaxx 2010-04-29 08:54:33 UTC (rev 1859) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/TimeEditor.jaxx 2010-04-29 14:01:01 UTC (rev 1860) @@ -27,7 +27,7 @@ <JPanel layout='{new BorderLayout()}'> - <style source='TimeEditor.css'/> + <!--<style source='TimeEditor.css'/>--> <!-- bean property --> <String id='property' javaBean='""'/> Modified: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/ConfigCategoryUI.jaxx =================================================================== --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/ConfigCategoryUI.jaxx 2010-04-29 08:54:33 UTC (rev 1859) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/ConfigCategoryUI.jaxx 2010-04-29 14:01:01 UTC (rev 1860) @@ -25,7 +25,7 @@ --> <JPanel layout='{new BorderLayout()}'> - <style source='ConfigCategoryUI.css'/> + <!--<style source='ConfigCategoryUI.css'/>--> <script><![CDATA[ import jaxx.runtime.swing.editor.config.model.*; Modified: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/ConfigUI.jaxx =================================================================== --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/ConfigUI.jaxx 2010-04-29 08:54:33 UTC (rev 1859) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/ConfigUI.jaxx 2010-04-29 14:01:01 UTC (rev 1860) @@ -25,7 +25,7 @@ --> <JPanel layout='{new BorderLayout()}'> - <style source='ConfigUI.css'/> + <!--<style source='ConfigUI.css'/>--> <script><![CDATA[ import jaxx.runtime.swing.editor.config.model.ConfigUIModel; Modified: trunk/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/GenerateMojo.java =================================================================== --- trunk/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/GenerateMojo.java 2010-04-29 08:54:33 UTC (rev 1859) +++ trunk/maven-jaxx-plugin/src/main/java/org/nuiton/jaxx/plugin/GenerateMojo.java 2010-04-29 14:01:01 UTC (rev 1860) @@ -202,6 +202,14 @@ protected boolean optimize; /** + * To auto import css files for a jaxx file. + * + * @parameter expression="${jaxx.autoImportCss}" default-value="false" + * @since 2.0.2 + */ + protected boolean autoImportCss; + + /** * flag to add logger to each generated jaxx file. * <p/> * By default, always add it. @@ -514,6 +522,11 @@ } @Override + public boolean isAutoImportCss() { + return autoImportCss; + } + + @Override public boolean isI18nable() { return i18nable; }
participants (1)
-
tchemit@users.nuiton.org