Author: kmorin Date: 2009-07-24 17:19:02 +0200 (Fri, 24 Jul 2009) New Revision: 1527 Modified: trunk/guix-compiler/src/main/java/org/nuiton/guix/GuixLauncher.java trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/GuixGenerator.java trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/JavaFileGenerator.java Log: Error handling + comments Modified: trunk/guix-compiler/src/main/java/org/nuiton/guix/GuixLauncher.java =================================================================== --- trunk/guix-compiler/src/main/java/org/nuiton/guix/GuixLauncher.java 2009-07-24 15:18:31 UTC (rev 1526) +++ trunk/guix-compiler/src/main/java/org/nuiton/guix/GuixLauncher.java 2009-07-24 15:19:02 UTC (rev 1527) @@ -262,96 +262,103 @@ //for each field which binds the value of others objects for (String field : gen.getBindingsToGenerate().keySet()) { for (String attr : gen.getBindingsToGenerate().get(field).keySet()) { - dbCreation.append(dbCreation.length() != 0 ? "else if(\"" : "if(\"").append(field).append(".").append(attr).append("\".equals(_binding)) {\n"); - dbDeletion.append(dbDeletion.length() != 0 ? "else if(\"" : "if(\"").append(field).append(".").append(attr).append("\".equals(_binding)) {\n"); - dbProcess.append(dbProcess.length() != 0 ? "else if(\"" : "if(\"").append(field).append(".").append(attr).append("\".equals(_binding)) {\n").append(field).append(".set").append(Character.toUpperCase(attr.charAt(0))).append(attr.substring(1)).append("("); - List<String[]> bindings = new ArrayList<String[]>(); - //the parser which decompose the binding into the different elements to bind - JavaParser p = new JavaParser(new StringReader(gen.getBindingsToGenerate().get(field).get(attr))); - //start parsing the binding value - while (!p.Line()) { - SimpleNode node = p.popNode(); - if (node != null) { - //get the different elements to bind - for (String s : browseNode(node)) { - //decompose the element s into attributes - List<String> l = new ArrayList<String>(); - //number of open brackets - int parOuvertes = 0; - StringBuffer read = new StringBuffer(); - for (char c : s.toCharArray()) { - if (c == '(') { - parOuvertes++; - read.append(c); - } - else if (c == ')') { - parOuvertes--; - read.append(c); - } - else if (c == '.') { - if (parOuvertes == 0) { - l.add(read.toString()); - read = new StringBuffer(); + try { + dbCreation.append(dbCreation.length() != 0 ? "else if(\"" : "if(\"").append(field).append(".").append(attr).append("\".equals(_binding)) {\n"); + dbDeletion.append(dbDeletion.length() != 0 ? "else if(\"" : "if(\"").append(field).append(".").append(attr).append("\".equals(_binding)) {\n"); + dbProcess.append(dbProcess.length() != 0 ? "else if(\"" : "if(\"").append(field).append(".").append(attr).append("\".equals(_binding)) {\n"); + List<String[]> bindings = new ArrayList<String[]>(); + //the parser which decompose the binding into the different elements to bind + JavaParser p = new JavaParser(new StringReader(gen.getBindingsToGenerate().get(field).get(attr))); + //start parsing the binding value + while (!p.Line()) { + SimpleNode node = p.popNode(); + if (node != null) { + //get the different elements to bind + for (String s : browseNode(node)) { + //decompose the element s into attributes + List<String> l = new ArrayList<String>(); + //number of open brackets + int parOuvertes = 0; + StringBuffer read = new StringBuffer(); + for (char c : s.toCharArray()) { + if (c == '(') { + parOuvertes++; + read.append(c); } + else if (c == ')') { + parOuvertes--; + read.append(c); + } + else if (c == '.') { + if (parOuvertes == 0) { + l.add(read.toString()); + read = new StringBuffer(); + } + else { + read.append(c); + } + } else { read.append(c); } } - else { - read.append(c); - } + l.add(read.toString()); + bindings.add(l.toArray(new String[l.size()])); } - l.add(read.toString()); - bindings.add(l.toArray(new String[l.size()])); } } - } - //generates the code - List<Class> listeners = new ArrayList<Class>(); - for (String[] binding : bindings) { - StringBuffer methodToInvoke = new StringBuffer(); - //the binding value before the generator changes it - StringBuffer oldBinding = new StringBuffer(); - //generates the method name for calling the databinding process method - for (String s : binding) { - methodToInvoke.append(s.replaceAll("\\W", "")); - oldBinding.append(s).append("."); - } - oldBinding.setLength(oldBinding.length() - 1); - //generates the code and modify the simple attribute name in the binding by the getter - listeners.addAll(gen.generateBindings(dbCreation, dbDeletion, null, jf, null, binding, 0, null, methodToInvoke.toString(), generatedFiles)); - //JavaArgument[] args = new JavaArgument[]{new JavaArgument(parameterType.getName(), "event")}; - //new value of the binding with the getters - StringBuffer newBinding = new StringBuffer(); - for (String s : binding) { - newBinding.append(s).append("."); - } - newBinding.setLength(newBinding.length() - 1); - //replaces the old binding by the newer one - gen.getBindingsToGenerate().get(field).put(attr, gen.getBindingsToGenerate().get(field).get(attr).replace(oldBinding.toString(), newBinding.toString())); + //generates the code + List<Class> listeners = new ArrayList<Class>(); + for (String[] binding : bindings) { + StringBuffer methodToInvoke = new StringBuffer(); + //the binding value before the generator changes it + StringBuffer oldBinding = new StringBuffer(); + //generates the method name for calling the databinding process method + for (String s : binding) { + methodToInvoke.append(s.replaceAll("\\W", "")); + oldBinding.append(s).append("."); + } + oldBinding.setLength(oldBinding.length() - 1); + //generates the code and modify the simple attribute name in the binding by the getter + listeners.addAll(gen.generateBindings(dbCreation, dbDeletion, null, jf, null, binding, 0, null, methodToInvoke.toString(), generatedFiles)); + //JavaArgument[] args = new JavaArgument[]{new JavaArgument(parameterType.getName(), "event")}; + //new value of the binding with the getters + StringBuffer newBinding = new StringBuffer(); + for (String s : binding) { + newBinding.append(s).append("."); + } + newBinding.setLength(newBinding.length() - 1); + //replaces the old binding by the newer one + gen.getBindingsToGenerate().get(field).put(attr, gen.getBindingsToGenerate().get(field).get(attr).replace(oldBinding.toString(), newBinding.toString())); - //method called by the listener - if (jf.getMethod("onChangeFrom" + methodToInvoke, /*args*/ null) == null) { - jf.addMethod(new JavaMethod(Modifier.PUBLIC, "void", "onChangeFrom" + methodToInvoke.toString(), /*args*/ null, null, /*"_DataSource" + dataSourceNumber + ".propertyChange(null);"*/ "processDataBinding(\"" + field + "." + attr + "\");", null)); + //method called by the listener + if (jf.getMethod("onChangeFrom" + methodToInvoke, /*args*/ null) == null) { + jf.addMethod(new JavaMethod(Modifier.PUBLIC, "void", "onChangeFrom" + methodToInvoke.toString(), /*args*/ null, null, /*"_DataSource" + dataSourceNumber + ".propertyChange(null);"*/ "processDataBinding(\"" + field + "." + attr + "\");", "Method called when the objects bound by '" + attr + "' of '" + field + "' change")); + } + else { + jf.getMethod("onChangeFrom" + methodToInvoke.toString(), /*args*/ null).appendBodyCode(/*"_DataSource" + dataSourceNumber + ".propertyChange(null);"*/"processDataBinding(\"" + field + "." + attr + "\");", "\n"); + } } - else { - jf.getMethod("onChangeFrom" + methodToInvoke.toString(), /*args*/ null).appendBodyCode(/*"_DataSource" + dataSourceNumber + ".propertyChange(null);"*/"processDataBinding(\"" + field + "." + attr + "\");", "\n"); + dbCreation.append("}\n"); + dbDeletion.append("}\n"); + dbProcess.append(field).append(".set").append(Character.toUpperCase(attr.charAt(0))).append(attr.substring(1)).append("(").append(gen.getBindingsToGenerate().get(field).get(attr)).append(");\n}\n"); + //if the binding got some elements to bind + if (listeners != null) { + //jf.addField(new JavaField(Modifier.PRIVATE, "java.beans.PropertyChangeListener", "_DataSource" + dataSourceNumber, "new org.nuiton.guix.runtime.DataBindingListener(this,\"" + field + "." + attr + "\")", null, null)); + //method to init the databinding + jf.getMethod("initDataBinding", null).appendBodyCode("applyDataBinding(\"" + field + "." + attr + "\");", "\n"); + // for (Class listener : listeners) { + // final List<Method> listenerMethods = Arrays.asList(listener.getMethods()); + // Class parameterType = listenerMethods.get(0).getParameterTypes()[0]; + // } } + //dataSourceNumber++; } - dbCreation.append("}\n"); - dbDeletion.append("}\n"); - dbProcess.append(gen.getBindingsToGenerate().get(field).get(attr)).append(");\n}\n"); - //if the binding got some elements to bind - if (listeners != null) { - //jf.addField(new JavaField(Modifier.PRIVATE, "java.beans.PropertyChangeListener", "_DataSource" + dataSourceNumber, "new org.nuiton.guix.runtime.DataBindingListener(this,\"" + field + "." + attr + "\")", null, null)); - //method to init the databinding - jf.getMethod("initDataBinding", null).appendBodyCode("applyDataBinding(\"" + field + "." + attr + "\");", "\n"); -// for (Class listener : listeners) { -// final List<Method> listenerMethods = Arrays.asList(listener.getMethods()); -// Class parameterType = listenerMethods.get(0).getParameterTypes()[0]; -// } + catch(NullPointerException eee) { + dbCreation.append("}\n"); + dbDeletion.append("}\n"); + dbProcess.append("}\n"); } - //dataSourceNumber++; } } //if the generated file has a superclass which also is a generated class, apply the databinding of the superclass to the class Modified: trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/GuixGenerator.java =================================================================== --- trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/GuixGenerator.java 2009-07-24 15:18:31 UTC (rev 1526) +++ trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/GuixGenerator.java 2009-07-24 15:19:02 UTC (rev 1527) @@ -140,6 +140,7 @@ /** * Checks if the objects of the binding exist + * and determines which objects need a listener * * @param dbCreation the method code to apply the binding * @param dbDeletion the method code to remove the binding Modified: trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/JavaFileGenerator.java =================================================================== --- trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/JavaFileGenerator.java 2009-07-24 15:18:31 UTC (rev 1526) +++ trunk/guix-compiler/src/main/java/org/nuiton/guix/generator/JavaFileGenerator.java 2009-07-24 15:19:02 UTC (rev 1527) @@ -67,9 +67,9 @@ } /** - * Generates and saves the class described by the GuixModelObject gmo + * Generates the JavaFile repesnting the class described by the GuixModelObject gmo * - * @param out the file to generate + * @return the generated JavaFile */ public abstract JavaFile generate();