Author: tchemit Date: 2009-04-15 15:08:45 +0000 (Wed, 15 Apr 2009) New Revision: 1424 Added: topia/trunk/topia-persistence/src/test/java/org/codelutin/topia/generator/TopiaGeneratorUtilTest.java Modified: topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/BeanGenerator.java topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityInterfaceGenerator.java topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/TopiaGeneratorUtil.java Log: - BeanGenerator can generate abstract class now - add a method to convert identifier to constant and use it in EntityInterfaceGenerator Modified: topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/BeanGenerator.java =================================================================== --- topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/BeanGenerator.java 2009-04-15 07:33:53 UTC (rev 1423) +++ topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/BeanGenerator.java 2009-04-15 15:08:45 UTC (rev 1424) @@ -40,6 +40,7 @@ import java.util.Iterator; import java.util.List; +import java.util.Set; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.codelutin.generator.ObjectModelGenerator; @@ -48,6 +49,8 @@ import org.codelutin.generator.models.object.ObjectModelClass; import org.codelutin.generator.models.object.ObjectModelClassifier; import org.codelutin.generator.models.object.ObjectModelInterface; +import org.codelutin.generator.models.object.ObjectModelOperation; +import org.codelutin.generator.models.object.ObjectModelParameter; import org.codelutin.topia.persistence.TopiaEntity; /** @@ -173,7 +176,8 @@ }*/ } - generateAttributes(output, attributes); + generateInterfaceOperations(output, clazz); + generateAttributes(output, attributes); /*{ protected final PropertyChangeSupport pcs; /** @@ -428,6 +432,46 @@ } } + protected void generateInterfaceOperations(Writer output, ObjectModelClassifier classifier) throws IOException { + for (ObjectModelOperation op : classifier.getOperations()) { + String opName = op.getName(); +/*{ /** +}*/ + if (TopiaGeneratorUtil.hasDocumentation(op)) { + String opDocumentation = op.getDocumentation(); +/*{ * <%=opName%> : <%=opDocumentation%> +}*/ + } + Collection<ObjectModelParameter> params = op.getParameters(); + for (ObjectModelParameter param : params) { + String paramName = param.getName(); + String paramDocumentation = param.getDocumentation(); +/*{ * @param <%=paramName%> <%=paramDocumentation%> + }*/ + } + String opVisibility = op.getVisibility(); + String opType = op.getReturnType(); +/*{ *) + <%=opVisibility%> abstract <%=opType%> <%=opName%>(}*/ + String comma = ""; + for (ObjectModelParameter param : params) { + String paramName = param.getName(); + String paramType = param.getType(); +/*{<%=comma%><%=paramType%> <%=paramName%>}*/ + comma = ", "; + } +/*{)}*/ + Set<String> exceptions = op.getExceptions(); + comma = " throws "; + for (String exception : exceptions) { +/*{<%=comma%><%=exception%>}*/ + comma = ", "; + } +/*{; + +}*/ + } + } protected void generateToString(Writer output, ObjectModelClass clazz) throws IOException { /*{ @Override @@ -465,6 +509,6 @@ if (clazz.isAbstract()) { return true; } - return false; + return !clazz.getOperations().isEmpty(); } } //BeanGenerator Modified: topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityInterfaceGenerator.java =================================================================== --- topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityInterfaceGenerator.java 2009-04-15 07:33:53 UTC (rev 1423) +++ topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityInterfaceGenerator.java 2009-04-15 15:08:45 UTC (rev 1424) @@ -509,7 +509,8 @@ String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); attrName = GeneratorUtil.toLowerCaseFirstLetter(assocAttrName); } - String attrColName = attrName.toUpperCase(); + String attrColName = TopiaGeneratorUtil.convertVariableNameToConstantName(attrName); + //String attrColName = attrName.toUpperCase(); /*{ public static final String <%=attrColName%> = "<%=attrName%>"; }*/ @@ -521,7 +522,8 @@ for (ObjectModelAttribute attr : assoc.getParticipantsAttributes()) { if (attr != null) { String attrName = attr.getName(); - String attrColName = attrName.toUpperCase(); + String attrColName = TopiaGeneratorUtil.convertVariableNameToConstantName(attrName); + //String attrColName = attrName.toUpperCase(); /*{ public static final String <%=attrColName%> = "<%=attrName%>"; }*/ Modified: topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/TopiaGeneratorUtil.java =================================================================== --- topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/TopiaGeneratorUtil.java 2009-04-15 07:33:53 UTC (rev 1423) +++ topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/TopiaGeneratorUtil.java 2009-04-15 15:08:45 UTC (rev 1424) @@ -933,4 +933,33 @@ return result; } + /** + * 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) { + //TODO Faire des tests pour savoir si variableName est non null et valide + //TODO Ameliorer l'algo pour tenir compte des caractères non alpha + //TODO pour le moment cela convient, donc... + 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(); + } + } // GeneratorUtil Added: topia/trunk/topia-persistence/src/test/java/org/codelutin/topia/generator/TopiaGeneratorUtilTest.java =================================================================== --- topia/trunk/topia-persistence/src/test/java/org/codelutin/topia/generator/TopiaGeneratorUtilTest.java (rev 0) +++ topia/trunk/topia-persistence/src/test/java/org/codelutin/topia/generator/TopiaGeneratorUtilTest.java 2009-04-15 15:08:45 UTC (rev 1424) @@ -0,0 +1,44 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package org.codelutin.topia.generator; + +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author chemit + */ +public class TopiaGeneratorUtilTest { + + @Test + public void testConvertVariableNameToConstantName() { + + String variableName = "abc"; + String expResult = "ABC"; + String result = TopiaGeneratorUtil.convertVariableNameToConstantName(variableName); + assertEquals(expResult, result); + + variableName = "ABC"; + expResult = "ABC"; + result = TopiaGeneratorUtil.convertVariableNameToConstantName(variableName); + assertEquals(expResult, result); + + variableName = "abC"; + expResult = "AB_C"; + result = TopiaGeneratorUtil.convertVariableNameToConstantName(variableName); + assertEquals(expResult, result); + + variableName = "AbC"; + expResult = "AB_C"; + result = TopiaGeneratorUtil.convertVariableNameToConstantName(variableName); + assertEquals(expResult, result); + + variableName = "AbC"; + expResult = "AB_C"; + result = TopiaGeneratorUtil.convertVariableNameToConstantName(variableName); + assertEquals(expResult, result); + } +} \ No newline at end of file
participants (1)
-
tchemit@users.labs.libre-entreprise.org