r1181 - in trunk: . eugene/src/main/java/org/nuiton/eugene/java eugene-java-templates eugene-java-templates/src eugene-java-templates/src/main eugene-java-templates/src/main/java eugene-java-templates/src/main/java/org eugene-java-templates/src/main/java/org/nuiton eugene-java-templates/src/main/java/org/nuiton/eugene eugene-java-templates/src/main/java/org/nuiton/eugene/java eugene-java-templates/src/test eugene-java-templates/src/test/java eugene-java-templates/src/test/java/org eugene-ja
Author: tchemit Date: 2012-10-24 21:30:08 +0200 (Wed, 24 Oct 2012) New Revision: 1181 Url: http://nuiton.org/repositories/revision/eugene/1181 Log: fixes #2282: Extract eugene generator into a new module (there have nothing to do inside eugene module) Added: trunk/eugene-java-templates/ trunk/eugene-java-templates/LICENSE.txt trunk/eugene-java-templates/README.txt trunk/eugene-java-templates/changelog.txt trunk/eugene-java-templates/pom.xml trunk/eugene-java-templates/src/ trunk/eugene-java-templates/src/main/ trunk/eugene-java-templates/src/main/java/ trunk/eugene-java-templates/src/main/java/org/ trunk/eugene-java-templates/src/main/java/org/nuiton/ trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/ trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/ trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaBeanTransformer.java trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaEnumerationTransformer.java trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaInterfaceTransformer.java trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/package-info.java trunk/eugene-java-templates/src/main/resources/ trunk/eugene-java-templates/src/test/ trunk/eugene-java-templates/src/test/java/ trunk/eugene-java-templates/src/test/java/org/ trunk/eugene-java-templates/src/test/java/org/nuiton/ trunk/eugene-java-templates/src/test/java/org/nuiton/eugene/ trunk/eugene-java-templates/src/test/java/org/nuiton/eugene/java/ trunk/eugene-java-templates/src/test/resources/ Removed: trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaBeanTransformer.java trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaEnumerationTransformer.java trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaInterfaceTransformer.java Modified: trunk/eugene-maven-plugin/pom.xml trunk/pom.xml Deleted: trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaBeanTransformer.java =================================================================== --- trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaBeanTransformer.java 2012-10-24 19:03:21 UTC (rev 1180) +++ trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaBeanTransformer.java 2012-10-24 19:30:08 UTC (rev 1181) @@ -1,877 +0,0 @@ -/* - * #%L - * ToPIA :: Persistence - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2004 - 2010 CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ -package org.nuiton.eugene.java; - -import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.nuiton.eugene.EugeneTagValues; -import org.nuiton.eugene.models.object.ObjectModelAttribute; -import org.nuiton.eugene.models.object.ObjectModelClass; -import org.nuiton.eugene.models.object.ObjectModelInterface; -import org.nuiton.eugene.models.object.ObjectModelJavaModifier; -import org.nuiton.eugene.models.object.ObjectModelOperation; - -import java.beans.PropertyChangeListener; -import java.beans.PropertyChangeSupport; -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; -import java.util.Set; - -/*{generator option: parentheses = false}*/ -/*{generator option: writeString = +}*/ - -/** - * JavaBeanTransformer generates simple bean with pcs support - * (and nothing else) according to the JavaBeans 1.1 norm. - * - * Since version 2.2.1, it is possible to - * <ul> - * <li>generate a simple POJO (says with no PCS support) by using the tag value {@link EugeneTagValues#TAG_NO_PCS}.</li> - * <li>generate i18n keys using the tag value {@link EugeneTagValues#TAG_I18N_PREFIX}.</li> - * </ul> - * - * @author tchemit <chemit@codelutin.com> - * @plexus.component role="org.nuiton.eugene.Template" role-hint="org.nuiton.eugene.java.JavaBeanTransformer" - * @since 2.0.2 - */ -public class JavaBeanTransformer extends ObjectModelTransformerToJava { - - private static final Log log = LogFactory.getLog(JavaBeanTransformer.class); - - public static final String DEFAULT_CONSTANT_PREFIX = "PROPERTY_"; - - @Override - public void transformFromClass(ObjectModelClass input) { - - if (!JavaGeneratorUtil.hasBeanStereotype(input)) { - - // not a bean - return; - } - - if (canGenerateImpl(input)) { - - generateImpl(input); - } - - if (!canGenerateAbstract(input)) { - - // nothing more to do - return; - } - - // test if a super class has bean stereotype - boolean superClassIsBean = false; - Collection<ObjectModelClass> superclasses = input.getSuperclasses(); - if(CollectionUtils.isNotEmpty(superclasses)) { - for (ObjectModelClass superclass : superclasses) { - if (JavaGeneratorUtil.hasBeanStereotype(superclass)) { - superClassIsBean = true; - break; - } - } - } - - ObjectModelClass output = - createAbstractClass(input.getName(), input.getPackageName()); - - if (log.isDebugEnabled()) { - log.debug("will generate " + output.getQualifiedName()); - } - - String i18nPrefix = JavaGeneratorUtil.getI18nPrefixTagValue(input, model); - if (!StringUtils.isEmpty(i18nPrefix)) { - generateI18nBlock(input, output, i18nPrefix); - } - - String noPCSTagValue = JavaGeneratorUtil.getNoPCSTagValue(model, input); - boolean usePCS = StringUtils.isEmpty(noPCSTagValue) || - !"true".equals(noPCSTagValue.trim()); - - String noGenerateBooleanGetMethods = - JavaGeneratorUtil.getDoNotGenerateBooleanGetMethods(model, input); - boolean generateBooleanGetMethods = - StringUtils.isEmpty(noGenerateBooleanGetMethods) || - !"true".equals(noGenerateBooleanGetMethods.trim()); - - String prefix = getConstantPrefix(input, DEFAULT_CONSTANT_PREFIX); - - setConstantPrefix(prefix); - - addSuperClass(input, output); - - boolean serializableFound = addInterfaces(input, output); - - if (superClassIsBean) { - serializableFound = true; - } - - addSerializable(input, output, serializableFound); - - Set<String> constantNames = addConstantsFromDependency(input, output); - - // Get available properties - List<ObjectModelAttribute> properties = getProperties(input); - - // Add properties constant - for (ObjectModelAttribute attr : properties) { - - createPropertyConstant(output, attr, prefix, constantNames); - } - - // Add properties field + javabean methods - for (ObjectModelAttribute attr : properties) { - - createProperty(output, attr, usePCS, generateBooleanGetMethods); - } - - // Add operations - createAbstractOperations(output, input.getOperations()); - - if (!superClassIsBean) { - - if (usePCS) { - - // Add property change support - createPropertyChangeSupport(output); - } - - boolean hasAMultipleProperty = containsMutiple(properties); - - // Add helper operations - if (hasAMultipleProperty) { - createGetChildMethod(output); - } - } - } - - protected boolean canGenerateAbstract(ObjectModelClass input) { - boolean b = !isInClassPath(input); - return b; - } - - protected boolean canGenerateImpl(ObjectModelClass input) { - String fqn = input.getQualifiedName() + "Impl"; - - if (isInClassPath(fqn)) { - - // already in class-path - return false; - } - - boolean hasOperations = !input.getAllOtherOperations(true).isEmpty() || - !input.getOperations().isEmpty(); - - // generate only if no user operations found - return !hasOperations; - } - - protected void createPropertyConstant(ObjectModelClass output, - ObjectModelAttribute attr, - String prefix, - Set<String> constantNames) { - - String attrName = getAttributeName(attr); - - String constantName = prefix + builder.getConstantName(attrName); - - if (constantNames.contains(constantName)) { - - // already generated - return; - } - - addConstant(output, - constantName, - String.class, - "\"" + attrName + "\"", - ObjectModelJavaModifier.PUBLIC - ); - } - - protected String getAttributeName(ObjectModelAttribute attr) { - String attrName = attr.getName(); - if (attr.hasAssociationClass()) { - String assocAttrName = JavaGeneratorUtil.getAssocAttrName(attr); - attrName = JavaGeneratorUtil.toLowerCaseFirstLetter(assocAttrName); - } - return attrName; - } - - protected String getAttributeType(ObjectModelAttribute attr) { - String attrType = attr.getType(); - if (attr.hasAssociationClass()) { - attrType = attr.getAssociationClass().getName(); - } - return attrType; - } - - protected boolean containsMutiple(List<ObjectModelAttribute> attributes) { - - boolean result = false; - - for (ObjectModelAttribute attr : attributes) { - - if (JavaGeneratorUtil.isNMultiplicity(attr)) { - result = true; - - break; - } - - } - return result; - } - - protected void createProperty(ObjectModelClass output, - ObjectModelAttribute attr, - boolean usePCS, - boolean generateBooleanGetMethods) { - - String attrName = getAttributeName(attr); - String attrType = getAttributeType(attr); - - boolean multiple = JavaGeneratorUtil.isNMultiplicity(attr); - - String constantName = getConstantName(attrName); - String simpleType = JavaGeneratorUtil.getSimpleName(attrType); - - if (multiple) { - - createGetChildMethod(output, - attrName, - attrType, - simpleType - ); - - createIsEmptyMethod(output, - attrName - ); - - createSizeMethod(output, - attrName - ); - - createAddChildMethod(output, - attrName, - attrType, - constantName, - usePCS - ); - - createAddAllChildrenMethod(output, - attrName, - attrType, - constantName, - usePCS - ); - - createRemoveChildMethod(output, - attrName, - attrType, - constantName, - usePCS - ); - - createRemoveAllChildrenMethod(output, - attrName, - attrType, - constantName, - usePCS - ); - - createContainsChildMethod(output, - attrName, - attrType, - constantName, - usePCS - ); - - createContainsAllChildrenMethod(output, - attrName, - attrType, - constantName, - usePCS - ); - - // Change type for Multiple attribute - if (attr.isOrdered()) { - attrType = List.class.getName() + "<" + attrType + ">"; - } else { - attrType = Collection.class.getName() + "<" + attrType + ">"; - } - - simpleType = JavaGeneratorUtil.getSimpleName(attrType); - } - - boolean booleanProperty = JavaGeneratorUtil.isBooleanPrimitive(attr); - - if (booleanProperty && !multiple) { - - // creates a isXXX method - createGetMethod(output, - attrName, - attrType, - JavaGeneratorUtil.OPERATION_GETTER_BOOLEAN_PREFIX - ); - } - - if (multiple || !booleanProperty || generateBooleanGetMethods) { - - createGetMethod(output, - attrName, - attrType, - JavaGeneratorUtil.OPERATION_GETTER_DEFAULT_PREFIX - ); - - } - createSetMethod(output, - attrName, - attrType, - simpleType, - constantName, - usePCS - ); - - // Add attribute to the class - addAttribute(output, - attrName, - attrType, - "", - ObjectModelJavaModifier.PROTECTED - ); - - } - - protected void createAbstractOperations(ObjectModelClass ouput, - Iterable<ObjectModelOperation> operations) { - JavaGeneratorUtil.cloneOperations( - this, - operations, - ouput, - true, - ObjectModelJavaModifier.ABSTRACT - ); - } - - protected List<ObjectModelAttribute> getProperties(ObjectModelClass input) { - List<ObjectModelAttribute> attributes = - (List<ObjectModelAttribute>) input.getAttributes(); - - List<ObjectModelAttribute> attrs = - new ArrayList<ObjectModelAttribute>(); - for (ObjectModelAttribute attr : attributes) { - if (attr.isNavigable()) { - - // only keep navigable attributes - attrs.add(attr); - } - } - return attrs; - } - - protected void createGetMethod(ObjectModelClass output, - String attrName, - String attrType, - String methodPrefix) { - - ObjectModelOperation getter = addOperation( - output, - getJavaBeanMethodName(methodPrefix , attrName), - attrType, - ObjectModelJavaModifier.PUBLIC - ); - setOperationBody(getter, "" - /*{ - return <%=attrName%>; - }*/ - ); - } - - protected void createGetChildMethod(ObjectModelClass output, - String attrName, - String attrType, - String simpleType) { - ObjectModelOperation getChild = addOperation( - output, - getJavaBeanMethodName("get", attrName), - attrType, - ObjectModelJavaModifier.PUBLIC - ); - addParameter(getChild, "int", "index"); - setOperationBody(getChild, "" - /*{ - <%=simpleType%> o = getChild(<%=attrName%>, index); - return o; - }*/ - ); - } - - protected void createIsEmptyMethod(ObjectModelClass output, - String attrName) { - ObjectModelOperation getChild = addOperation( - output, - getJavaBeanMethodName("is", attrName)+"Empty", - boolean.class, - ObjectModelJavaModifier.PUBLIC - ); - setOperationBody(getChild, "" - /*{ - return <%=attrName%> != null && !<%=attrName%>.isEmpty(); - }*/ - ); - } - - protected void createSizeMethod(ObjectModelClass output, - String attrName) { - ObjectModelOperation getChild = addOperation( - output, - getJavaBeanMethodName("size", attrName), - int.class, - ObjectModelJavaModifier.PUBLIC - ); - setOperationBody(getChild, "" - /*{ - return <%=attrName%> == null ? 0 : <%=attrName%>.size(); - }*/ - ); - } - protected void createAddChildMethod(ObjectModelClass output, - String attrName, - String attrType, - String constantName, - boolean usePCS) { - ObjectModelOperation addChild = addOperation( - output, - getJavaBeanMethodName("add", attrName), - "void", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(addChild, attrType, attrName); - - String methodName = getJavaBeanMethodName("get", attrName); - StringBuilder buffer = new StringBuilder("" - /*{ - <%=methodName%>().add(<%=attrName%>); - }*/ - ); - if (usePCS) { - buffer.append("" - /*{ - firePropertyChange(<%=constantName%>, null, <%=attrName%>); - }*/ - ); - } - setOperationBody(addChild, buffer.toString()); - } - - protected void createAddAllChildrenMethod(ObjectModelClass output, - String attrName, - String attrType, - String constantName, - boolean usePCS) { - ObjectModelOperation addAllChild = addOperation( - output, - getJavaBeanMethodName("addAll", attrName), - "void", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(addAllChild, "java.util.Collection<" + attrType + ">", attrName); - - String methodName = getJavaBeanMethodName("get", attrName); - StringBuilder buffer = new StringBuilder("" - /*{ - <%=methodName%>().addAll(<%=attrName%>); - }*/ - ); - if (usePCS) { - buffer.append("" - /*{ - firePropertyChange(<%=constantName%>, null, <%=attrName%>); - }*/ - ); - } - setOperationBody(addAllChild, buffer.toString()); - } - - protected void createRemoveChildMethod(ObjectModelClass output, - String attrName, - String attrType, - String constantName, - boolean usePCS) { - ObjectModelOperation operation = addOperation( - output, - getJavaBeanMethodName("remove", attrName), - "boolean", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, attrType, attrName); - String methodName = getJavaBeanMethodName("get", attrName); - StringBuilder buffer = new StringBuilder(); - buffer.append("" - /*{ - boolean removed = <%=methodName%>().remove(<%=attrName%>); - }*/ - ); - - if (usePCS) { - buffer.append("" - /*{ - if (removed) { - firePropertyChange(<%=constantName%>, <%=attrName%>, null); - } - }*/ - ); - } - buffer.append("" - /*{ - return removed; - }*/ - ); - setOperationBody(operation, buffer.toString()); - } - - protected void createRemoveAllChildrenMethod(ObjectModelClass output, - String attrName, - String attrType, - String constantName, - boolean usePCS) { - - ObjectModelOperation operation = addOperation( - output, - getJavaBeanMethodName("removeAll", attrName), - "boolean", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, "java.util.Collection<" + attrType + ">", attrName); - StringBuilder buffer = new StringBuilder(); - String methodName = getJavaBeanMethodName("get", attrName); - buffer.append("" - /*{ - boolean removed = <%=methodName%>().removeAll(<%=attrName%>); - }*/ - ); - - if (usePCS) { - buffer.append("" - /*{ - if (removed) { - firePropertyChange(<%=constantName%>, <%=attrName%>, null); - } - }*/ - ); - } - buffer.append("" - /*{ - return removed; - }*/ - ); - setOperationBody(operation, buffer.toString()); - } - - protected void createContainsChildMethod(ObjectModelClass output, - String attrName, - String attrType, - String constantName, - boolean usePCS) { - - ObjectModelOperation operation = addOperation( - output, - getJavaBeanMethodName("contains", attrName), - "boolean", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, attrType, attrName); - StringBuilder buffer = new StringBuilder(); - String methodName = getJavaBeanMethodName("get", attrName); - buffer.append("" - /*{ - boolean contains = <%=methodName%>().contains(<%=attrName%>); - }*/ - ); - - buffer.append("" - /*{ - return contains; - }*/ - ); - setOperationBody(operation, buffer.toString()); - } - - protected void createContainsAllChildrenMethod(ObjectModelClass output, - String attrName, - String attrType, - String constantName, - boolean usePCS) { - - ObjectModelOperation operation = addOperation( - output, - getJavaBeanMethodName("containsAll", attrName), - "boolean", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, "java.util.Collection<" + attrType + ">", attrName); - StringBuilder buffer = new StringBuilder(); - String methodName = getJavaBeanMethodName("get", attrName); - buffer.append("" - /*{ - boolean contains = <%=methodName%>().containsAll(<%=attrName%>); - }*/ - ); - - buffer.append("" - /*{ - return contains; - }*/ - ); - setOperationBody(operation, buffer.toString()); - } - - protected void createSetMethod(ObjectModelClass output, - String attrName, - String attrType, - String simpleType, - String constantName, - boolean usePCS) { - ObjectModelOperation operation = addOperation( - output, - getJavaBeanMethodName("set", attrName), - "void", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, attrType, attrName); - - if (usePCS) { - String methodName = getJavaBeanMethodName("get", attrName); - setOperationBody(operation, "" - /*{ - <%=simpleType%> oldValue = <%=methodName%>(); - this.<%=attrName%> = <%=attrName%>; - firePropertyChange(<%=constantName%>, oldValue, <%=attrName%>); - }*/ - ); - } else { - setOperationBody(operation, "" - /*{ - this.<%=attrName%> = <%=attrName%>; - }*/ - ); - } - } - - protected void createGetChildMethod(ObjectModelClass output) { - ObjectModelOperation getChild = addOperation( - output, - "getChild", "<T> T", - ObjectModelJavaModifier.PROTECTED - ); - addParameter(getChild, "java.util.Collection<T>", "childs"); - addParameter(getChild, "int", "index"); - setOperationBody(getChild, "" - /*{ - if (childs != null) { - int i = 0; - for (T o : childs) { - if (index == i) { - return o; - } - i++; - } - } - return null; - }*/ - ); - } - - protected void addSerializable(ObjectModelClass input, - ObjectModelClass output, - boolean interfaceFound) { - if (!interfaceFound) { - addInterface(output, Serializable.class); - } - - // Generate the serialVersionUID - long serialVersionUID = JavaGeneratorUtil.generateSerialVersionUID(input); - - addConstant(output, - JavaGeneratorUtil.SERIAL_VERSION_UID, - "long", - serialVersionUID + "L", - ObjectModelJavaModifier.PRIVATE - ); - } - - /** - * Add all interfaces defines in input class and returns if - * {@link Serializable} interface was found. - * - * @param input the input model class to process - * @param output the output generated class - * @return {@code true} if {@link Serializable} was found from input, - * {@code false} otherwise - */ - protected boolean addInterfaces(ObjectModelClass input, - ObjectModelClass output) { - boolean foundSerializable = false; - for (ObjectModelInterface parentInterface : input.getInterfaces()) { - String fqn = parentInterface.getQualifiedName(); - addInterface(output, fqn); - if (Serializable.class.getName().equals(fqn)) { - foundSerializable = true; - } - } - return foundSerializable; - } - - protected void addSuperClass(ObjectModelClass input, - ObjectModelClass output) { - // Set superclass - Iterator<ObjectModelClass> j = input.getSuperclasses().iterator(); - if (j.hasNext()) { - ObjectModelClass p = j.next(); - // We want to set the inheritance to the implementation class of the father - // Ex for model : A -> B (a inherits B) we want : A -> BImpl -> B - String qualifiedName = p.getQualifiedName() + "Impl"; - setSuperClass(output, qualifiedName); - } - } - - protected ObjectModelClass generateImpl(ObjectModelClass input) { - - ObjectModelClass resultClassImpl = createClass( - input.getName() + "Impl", - input.getPackageName() - ); - - // set the abstract resulClass as the resultClassImpl super class - setSuperClass(resultClassImpl, input.getQualifiedName()); - - // add a fix serialVersionUID, since the class has no field nor method - addConstant(resultClassImpl, - JavaGeneratorUtil.SERIAL_VERSION_UID, - "long", - "1L", - ObjectModelJavaModifier.PRIVATE - ); - return resultClassImpl; - } - - protected void createPropertyChangeSupport(ObjectModelClass output) { - - addAttribute(output, - "pcs", - PropertyChangeSupport.class, - "new PropertyChangeSupport(this)", - ObjectModelJavaModifier.PROTECTED, - ObjectModelJavaModifier.FINAL - ); - - // Add PropertyListener - - ObjectModelOperation operation; - - operation = addOperation(output, - "addPropertyChangeListener", - "void", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, PropertyChangeListener.class, "listener"); - setOperationBody(operation, "" - /*{ - pcs.addPropertyChangeListener(listener); - }*/ - ); - - operation = addOperation(output, - "addPropertyChangeListener", - "void", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, String.class, "propertyName"); - addParameter(operation, PropertyChangeListener.class, "listener"); - setOperationBody(operation, "" - /*{ - pcs.addPropertyChangeListener(propertyName, listener); - }*/ - ); - - operation = addOperation(output, - "removePropertyChangeListener", - "void", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, PropertyChangeListener.class, "listener"); - setOperationBody(operation, "" - /*{ - pcs.removePropertyChangeListener(listener); - }*/ - ); - - operation = addOperation(output, - "removePropertyChangeListener", - "void", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, String.class, "propertyName"); - addParameter(operation, PropertyChangeListener.class, "listener"); - setOperationBody(operation, "" - /*{ - pcs.removePropertyChangeListener(propertyName, listener); - }*/ - ); - - operation = addOperation(output, - "firePropertyChange", - "void", - ObjectModelJavaModifier.PROTECTED - ); - addParameter(operation, String.class, "propertyName"); - addParameter(operation, Object.class, "oldValue"); - addParameter(operation, Object.class, "newValue"); - setOperationBody(operation, "" - /*{ - pcs.firePropertyChange(propertyName, oldValue, newValue); - }*/ - ); - - operation = addOperation(output, - "firePropertyChange", - "void", - ObjectModelJavaModifier.PROTECTED - ); - addParameter(operation, String.class, "propertyName"); - addParameter(operation, Object.class, "newValue"); - setOperationBody(operation, "" - /*{ - firePropertyChange(propertyName, null, newValue); - }*/ - ); - } - -} \ No newline at end of file Deleted: trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaEnumerationTransformer.java =================================================================== --- trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaEnumerationTransformer.java 2012-10-24 19:03:21 UTC (rev 1180) +++ trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaEnumerationTransformer.java 2012-10-24 19:30:08 UTC (rev 1181) @@ -1,83 +0,0 @@ -/* - * #%L - * EUGene :: EUGene - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2004 - 2011 CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ -package org.nuiton.eugene.java; - - -/*{generator option: parentheses = false}*/ -/*{generator option: writeString = +}*/ - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.nuiton.eugene.models.object.ObjectModelEnumeration; - -import java.util.Collection; - -/** - * JavaEnumerationTransformer generates a enumeration for enuration with - * stereotype enumeration. - * - * @author tchemit <chemit@codelutin.com> - * @plexus.component role="org.nuiton.eugene.Template" role-hint="org.nuiton.eugene.java.JavaEnumerationTransformer" - * @since 2.5 - */ -public class JavaEnumerationTransformer extends ObjectModelTransformerToJava { - - private static final Log log = - LogFactory.getLog(JavaEnumerationTransformer.class); - - @Override - public void transformFromEnumeration(ObjectModelEnumeration input) { - if (!canGenerate(input)) { - - if (log.isDebugEnabled()) { - log.debug("Skip generation for " + input.getQualifiedName()); - } - return; - } - - ObjectModelEnumeration output = - createEnumeration(input.getName(), input.getPackageName()); - - if (log.isDebugEnabled()) { - log.debug("will generate " + output.getQualifiedName()); - } - - Collection<String> literals = input.getLiterals(); - - for (String literal : literals) { - addLiteral(output, literal); - } - } - - protected boolean canGenerate(ObjectModelEnumeration input) { - boolean b = !JavaGeneratorUtil.hasSkipStereotype(input); - if (b) { - - // check if not found in class-path - b = !isInClassPath(input); - } - return b; - } -} \ No newline at end of file Deleted: trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaInterfaceTransformer.java =================================================================== --- trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaInterfaceTransformer.java 2012-10-24 19:03:21 UTC (rev 1180) +++ trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaInterfaceTransformer.java 2012-10-24 19:30:08 UTC (rev 1181) @@ -1,139 +0,0 @@ -/* - * #%L - * EUGene :: EUGene - * * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2004 - 2010 CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ - -package org.nuiton.eugene.java; - -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.nuiton.eugene.models.object.ObjectModelAttribute; -import org.nuiton.eugene.models.object.ObjectModelInterface; -import org.nuiton.eugene.models.object.ObjectModelJavaModifier; - -import java.util.Set; - -/*{generator option: parentheses = false}*/ -/*{generator option: writeString = +}*/ - -/** - * JavaInterfaceTransformer generates simple interfaces for Java language. - * <p/> - * Created: 7 nov. 2009 - * - * @author fdesbois <fdesbois@codelutin.com> - * @plexus.component role="org.nuiton.eugene.Template" role-hint="org.nuiton.eugene.java.JavaInterfaceTransformer" - * @since 2.0.2 - */ -public class JavaInterfaceTransformer extends ObjectModelTransformerToJava { - - private static final Log log = - LogFactory.getLog(JavaInterfaceTransformer.class); - - @Override - public void transformFromInterface(ObjectModelInterface input) { - - boolean canGenerate = canGenerate(input); - if (!canGenerate) { - - return; - } - - ObjectModelInterface output = createInterface(input.getName(), - input.getPackageName()); - - if (log.isDebugEnabled()) { - log.debug("generate interface " + - output.getQualifiedName()); - } - - // extend interface - - for (ObjectModelInterface extend : input.getInterfaces()) { - addInterface(output, extend.getQualifiedName()); - } - - String prefix = getConstantPrefix(input, ""); - - if (StringUtils.isEmpty(prefix)) { - - // no specific prefix, so no prefix - if (log.isWarnEnabled()) { - log.warn("[" + input.getName() + "] Will generate constants with NO prefix, not a good idea..."); - } - } - setConstantPrefix(prefix); - - Set<String> constants = addConstantsFromDependency(input, output); - - // constant attributes - for (ObjectModelAttribute attr : input.getAttributes()) { - - if (attr.isStatic() || - !StringUtils.isNotEmpty(attr.getDefaultValue())) { - - // only static attribut with value - continue; - } - - String constantName = attr.getName(); - - if (constants.contains(constantName)) { - - // already generated - continue; - } - - // add constant - addConstant(output, - constantName, - attr.getType(), - attr.getDefaultValue(), - ObjectModelJavaModifier.PUBLIC - ); - } - - - // interface operations - JavaGeneratorUtil.cloneOperations(this, - input.getOperations(), - output, - false - ); - } - - protected boolean canGenerate(ObjectModelInterface input) { - - // check if not found in class-path - boolean b = !isInClassPath(input); - - if (b) { - - // only generate when no stereotype are on interface ? Strange !? - b = input.getStereotypes().isEmpty(); - } - return b; - } - -} Property changes on: trunk/eugene-java-templates ___________________________________________________________________ Added: svn:ignore + target *.ipr *.iws *.iml .idea .project .classpath Property changes on: trunk/eugene-java-templates/LICENSE.txt ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Property changes on: trunk/eugene-java-templates/README.txt ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Property changes on: trunk/eugene-java-templates/changelog.txt ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/eugene-java-templates/pom.xml =================================================================== --- trunk/eugene-java-templates/pom.xml (rev 0) +++ trunk/eugene-java-templates/pom.xml 2012-10-24 19:30:08 UTC (rev 1181) @@ -0,0 +1,134 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + #%L + EUGene :: EUGene + + $HeadURL$ + %% + Copyright (C) 2004 - 2010 CodeLutin + %% + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Lesser Public License for more details. + + You should have received a copy of the GNU General Lesser Public + License along with this program. If not, see + <http://www.gnu.org/licenses/lgpl-3.0.html>. + #L% + --> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.nuiton</groupId> + <artifactId>eugene</artifactId> + <version>2.6-SNAPSHOT</version> + </parent> + + <groupId>org.nuiton.eugene</groupId> + <artifactId>eugene-java-templates</artifactId> + + <dependencies> + + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>eugene</artifactId> + <version>${project.version}</version> + </dependency> + + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + </dependency> + + <dependency> + <groupId>log4j</groupId> + <artifactId>log4j</artifactId> + <scope>test</scope> + </dependency> + + </dependencies> + + <name>EUGene :: Java templates</name> + + <inceptionYear>2012</inceptionYear> + + <properties> + <!-- extra files to include in release --> + <redmine.releaseFiles>${redmine.libReleaseFiles}</redmine.releaseFiles> + </properties> + + <build> + + <plugins> + <plugin> + <groupId>org.nuiton.processor</groupId> + <artifactId>maven-processor-plugin</artifactId> + <executions> + <execution> + <phase>generate-sources</phase> + <goals> + <goal>process</goal> + </goals> + </execution> + </executions> + <configuration> + <includes>**/*.java</includes> + <filters> + org.nuiton.processor.filters.GeneratorTemplatesFilter + </filters> + </configuration> + </plugin> + + <!-- expose new plexus components --> + <plugin> + <groupId>org.codehaus.plexus</groupId> + <artifactId>plexus-component-metadata</artifactId> + <executions> + <execution> + <goals> + <goal>generate-metadata</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + + </build> + + <profiles> + + <!-- perform only on a release stage when using the maven-release-plugin --> + <profile> + <id>reporting</id> + <activation> + <property> + <name>performRelease</name> + <value>true</value> + </property> + </activation> + + <reporting> + <plugins> + <plugin> + <groupId>org.codehaus.plexus</groupId> + <artifactId>plexus-maven-plugin</artifactId> + <version>1.3.8</version> + </plugin> + </plugins> + </reporting> + + </profile> + </profiles> + +</project> + + + Property changes on: trunk/eugene-java-templates/pom.xml ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaBeanTransformer.java (from rev 1180, trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaBeanTransformer.java) =================================================================== --- trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaBeanTransformer.java (rev 0) +++ trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaBeanTransformer.java 2012-10-24 19:30:08 UTC (rev 1181) @@ -0,0 +1,877 @@ +/* + * #%L + * EUGene :: Java templates + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2012 CodeLutin, Tony Chemit + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ +package org.nuiton.eugene.java; + +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.eugene.EugeneTagValues; +import org.nuiton.eugene.models.object.ObjectModelAttribute; +import org.nuiton.eugene.models.object.ObjectModelClass; +import org.nuiton.eugene.models.object.ObjectModelInterface; +import org.nuiton.eugene.models.object.ObjectModelJavaModifier; +import org.nuiton.eugene.models.object.ObjectModelOperation; + +import java.beans.PropertyChangeListener; +import java.beans.PropertyChangeSupport; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +/*{generator option: parentheses = false}*/ +/*{generator option: writeString = +}*/ + +/** + * JavaBeanTransformer generates simple bean with pcs support + * (and nothing else) according to the JavaBeans 1.1 norm. + * + * Since version 2.2.1, it is possible to + * <ul> + * <li>generate a simple POJO (says with no PCS support) by using the tag value {@link EugeneTagValues#TAG_NO_PCS}.</li> + * <li>generate i18n keys using the tag value {@link EugeneTagValues#TAG_I18N_PREFIX}.</li> + * </ul> + * + * @author tchemit <chemit@codelutin.com> + * @plexus.component role="org.nuiton.eugene.Template" role-hint="org.nuiton.eugene.java.JavaBeanTransformer" + * @since 2.0.2 + */ +public class JavaBeanTransformer extends ObjectModelTransformerToJava { + + private static final Log log = LogFactory.getLog(JavaBeanTransformer.class); + + public static final String DEFAULT_CONSTANT_PREFIX = "PROPERTY_"; + + @Override + public void transformFromClass(ObjectModelClass input) { + + if (!JavaGeneratorUtil.hasBeanStereotype(input)) { + + // not a bean + return; + } + + if (canGenerateImpl(input)) { + + generateImpl(input); + } + + if (!canGenerateAbstract(input)) { + + // nothing more to do + return; + } + + // test if a super class has bean stereotype + boolean superClassIsBean = false; + Collection<ObjectModelClass> superclasses = input.getSuperclasses(); + if(CollectionUtils.isNotEmpty(superclasses)) { + for (ObjectModelClass superclass : superclasses) { + if (JavaGeneratorUtil.hasBeanStereotype(superclass)) { + superClassIsBean = true; + break; + } + } + } + + ObjectModelClass output = + createAbstractClass(input.getName(), input.getPackageName()); + + if (log.isDebugEnabled()) { + log.debug("will generate " + output.getQualifiedName()); + } + + String i18nPrefix = JavaGeneratorUtil.getI18nPrefixTagValue(input, model); + if (!StringUtils.isEmpty(i18nPrefix)) { + generateI18nBlock(input, output, i18nPrefix); + } + + String noPCSTagValue = JavaGeneratorUtil.getNoPCSTagValue(model, input); + boolean usePCS = StringUtils.isEmpty(noPCSTagValue) || + !"true".equals(noPCSTagValue.trim()); + + String noGenerateBooleanGetMethods = + JavaGeneratorUtil.getDoNotGenerateBooleanGetMethods(model, input); + boolean generateBooleanGetMethods = + StringUtils.isEmpty(noGenerateBooleanGetMethods) || + !"true".equals(noGenerateBooleanGetMethods.trim()); + + String prefix = getConstantPrefix(input, DEFAULT_CONSTANT_PREFIX); + + setConstantPrefix(prefix); + + addSuperClass(input, output); + + boolean serializableFound = addInterfaces(input, output); + + if (superClassIsBean) { + serializableFound = true; + } + + addSerializable(input, output, serializableFound); + + Set<String> constantNames = addConstantsFromDependency(input, output); + + // Get available properties + List<ObjectModelAttribute> properties = getProperties(input); + + // Add properties constant + for (ObjectModelAttribute attr : properties) { + + createPropertyConstant(output, attr, prefix, constantNames); + } + + // Add properties field + javabean methods + for (ObjectModelAttribute attr : properties) { + + createProperty(output, attr, usePCS, generateBooleanGetMethods); + } + + // Add operations + createAbstractOperations(output, input.getOperations()); + + if (!superClassIsBean) { + + if (usePCS) { + + // Add property change support + createPropertyChangeSupport(output); + } + + boolean hasAMultipleProperty = containsMutiple(properties); + + // Add helper operations + if (hasAMultipleProperty) { + createGetChildMethod(output); + } + } + } + + protected boolean canGenerateAbstract(ObjectModelClass input) { + boolean b = !isInClassPath(input); + return b; + } + + protected boolean canGenerateImpl(ObjectModelClass input) { + String fqn = input.getQualifiedName() + "Impl"; + + if (isInClassPath(fqn)) { + + // already in class-path + return false; + } + + boolean hasOperations = !input.getAllOtherOperations(true).isEmpty() || + !input.getOperations().isEmpty(); + + // generate only if no user operations found + return !hasOperations; + } + + protected void createPropertyConstant(ObjectModelClass output, + ObjectModelAttribute attr, + String prefix, + Set<String> constantNames) { + + String attrName = getAttributeName(attr); + + String constantName = prefix + builder.getConstantName(attrName); + + if (constantNames.contains(constantName)) { + + // already generated + return; + } + + addConstant(output, + constantName, + String.class, + "\"" + attrName + "\"", + ObjectModelJavaModifier.PUBLIC + ); + } + + protected String getAttributeName(ObjectModelAttribute attr) { + String attrName = attr.getName(); + if (attr.hasAssociationClass()) { + String assocAttrName = JavaGeneratorUtil.getAssocAttrName(attr); + attrName = JavaGeneratorUtil.toLowerCaseFirstLetter(assocAttrName); + } + return attrName; + } + + protected String getAttributeType(ObjectModelAttribute attr) { + String attrType = attr.getType(); + if (attr.hasAssociationClass()) { + attrType = attr.getAssociationClass().getName(); + } + return attrType; + } + + protected boolean containsMutiple(List<ObjectModelAttribute> attributes) { + + boolean result = false; + + for (ObjectModelAttribute attr : attributes) { + + if (JavaGeneratorUtil.isNMultiplicity(attr)) { + result = true; + + break; + } + + } + return result; + } + + protected void createProperty(ObjectModelClass output, + ObjectModelAttribute attr, + boolean usePCS, + boolean generateBooleanGetMethods) { + + String attrName = getAttributeName(attr); + String attrType = getAttributeType(attr); + + boolean multiple = JavaGeneratorUtil.isNMultiplicity(attr); + + String constantName = getConstantName(attrName); + String simpleType = JavaGeneratorUtil.getSimpleName(attrType); + + if (multiple) { + + createGetChildMethod(output, + attrName, + attrType, + simpleType + ); + + createIsEmptyMethod(output, + attrName + ); + + createSizeMethod(output, + attrName + ); + + createAddChildMethod(output, + attrName, + attrType, + constantName, + usePCS + ); + + createAddAllChildrenMethod(output, + attrName, + attrType, + constantName, + usePCS + ); + + createRemoveChildMethod(output, + attrName, + attrType, + constantName, + usePCS + ); + + createRemoveAllChildrenMethod(output, + attrName, + attrType, + constantName, + usePCS + ); + + createContainsChildMethod(output, + attrName, + attrType, + constantName, + usePCS + ); + + createContainsAllChildrenMethod(output, + attrName, + attrType, + constantName, + usePCS + ); + + // Change type for Multiple attribute + if (attr.isOrdered()) { + attrType = List.class.getName() + "<" + attrType + ">"; + } else { + attrType = Collection.class.getName() + "<" + attrType + ">"; + } + + simpleType = JavaGeneratorUtil.getSimpleName(attrType); + } + + boolean booleanProperty = JavaGeneratorUtil.isBooleanPrimitive(attr); + + if (booleanProperty && !multiple) { + + // creates a isXXX method + createGetMethod(output, + attrName, + attrType, + JavaGeneratorUtil.OPERATION_GETTER_BOOLEAN_PREFIX + ); + } + + if (multiple || !booleanProperty || generateBooleanGetMethods) { + + createGetMethod(output, + attrName, + attrType, + JavaGeneratorUtil.OPERATION_GETTER_DEFAULT_PREFIX + ); + + } + createSetMethod(output, + attrName, + attrType, + simpleType, + constantName, + usePCS + ); + + // Add attribute to the class + addAttribute(output, + attrName, + attrType, + "", + ObjectModelJavaModifier.PROTECTED + ); + + } + + protected void createAbstractOperations(ObjectModelClass ouput, + Iterable<ObjectModelOperation> operations) { + JavaGeneratorUtil.cloneOperations( + this, + operations, + ouput, + true, + ObjectModelJavaModifier.ABSTRACT + ); + } + + protected List<ObjectModelAttribute> getProperties(ObjectModelClass input) { + List<ObjectModelAttribute> attributes = + (List<ObjectModelAttribute>) input.getAttributes(); + + List<ObjectModelAttribute> attrs = + new ArrayList<ObjectModelAttribute>(); + for (ObjectModelAttribute attr : attributes) { + if (attr.isNavigable()) { + + // only keep navigable attributes + attrs.add(attr); + } + } + return attrs; + } + + protected void createGetMethod(ObjectModelClass output, + String attrName, + String attrType, + String methodPrefix) { + + ObjectModelOperation getter = addOperation( + output, + getJavaBeanMethodName(methodPrefix , attrName), + attrType, + ObjectModelJavaModifier.PUBLIC + ); + setOperationBody(getter, "" + /*{ + return <%=attrName%>; + }*/ + ); + } + + protected void createGetChildMethod(ObjectModelClass output, + String attrName, + String attrType, + String simpleType) { + ObjectModelOperation getChild = addOperation( + output, + getJavaBeanMethodName("get", attrName), + attrType, + ObjectModelJavaModifier.PUBLIC + ); + addParameter(getChild, "int", "index"); + setOperationBody(getChild, "" + /*{ + <%=simpleType%> o = getChild(<%=attrName%>, index); + return o; + }*/ + ); + } + + protected void createIsEmptyMethod(ObjectModelClass output, + String attrName) { + ObjectModelOperation getChild = addOperation( + output, + getJavaBeanMethodName("is", attrName)+"Empty", + boolean.class, + ObjectModelJavaModifier.PUBLIC + ); + setOperationBody(getChild, "" + /*{ + return <%=attrName%> != null && !<%=attrName%>.isEmpty(); + }*/ + ); + } + + protected void createSizeMethod(ObjectModelClass output, + String attrName) { + ObjectModelOperation getChild = addOperation( + output, + getJavaBeanMethodName("size", attrName), + int.class, + ObjectModelJavaModifier.PUBLIC + ); + setOperationBody(getChild, "" + /*{ + return <%=attrName%> == null ? 0 : <%=attrName%>.size(); + }*/ + ); + } + protected void createAddChildMethod(ObjectModelClass output, + String attrName, + String attrType, + String constantName, + boolean usePCS) { + ObjectModelOperation addChild = addOperation( + output, + getJavaBeanMethodName("add", attrName), + "void", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(addChild, attrType, attrName); + + String methodName = getJavaBeanMethodName("get", attrName); + StringBuilder buffer = new StringBuilder("" + /*{ + <%=methodName%>().add(<%=attrName%>); + }*/ + ); + if (usePCS) { + buffer.append("" + /*{ + firePropertyChange(<%=constantName%>, null, <%=attrName%>); + }*/ + ); + } + setOperationBody(addChild, buffer.toString()); + } + + protected void createAddAllChildrenMethod(ObjectModelClass output, + String attrName, + String attrType, + String constantName, + boolean usePCS) { + ObjectModelOperation addAllChild = addOperation( + output, + getJavaBeanMethodName("addAll", attrName), + "void", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(addAllChild, "java.util.Collection<" + attrType + ">", attrName); + + String methodName = getJavaBeanMethodName("get", attrName); + StringBuilder buffer = new StringBuilder("" + /*{ + <%=methodName%>().addAll(<%=attrName%>); + }*/ + ); + if (usePCS) { + buffer.append("" + /*{ + firePropertyChange(<%=constantName%>, null, <%=attrName%>); + }*/ + ); + } + setOperationBody(addAllChild, buffer.toString()); + } + + protected void createRemoveChildMethod(ObjectModelClass output, + String attrName, + String attrType, + String constantName, + boolean usePCS) { + ObjectModelOperation operation = addOperation( + output, + getJavaBeanMethodName("remove", attrName), + "boolean", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, attrType, attrName); + String methodName = getJavaBeanMethodName("get", attrName); + StringBuilder buffer = new StringBuilder(); + buffer.append("" + /*{ + boolean removed = <%=methodName%>().remove(<%=attrName%>); + }*/ + ); + + if (usePCS) { + buffer.append("" + /*{ + if (removed) { + firePropertyChange(<%=constantName%>, <%=attrName%>, null); + } + }*/ + ); + } + buffer.append("" + /*{ + return removed; + }*/ + ); + setOperationBody(operation, buffer.toString()); + } + + protected void createRemoveAllChildrenMethod(ObjectModelClass output, + String attrName, + String attrType, + String constantName, + boolean usePCS) { + + ObjectModelOperation operation = addOperation( + output, + getJavaBeanMethodName("removeAll", attrName), + "boolean", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, "java.util.Collection<" + attrType + ">", attrName); + StringBuilder buffer = new StringBuilder(); + String methodName = getJavaBeanMethodName("get", attrName); + buffer.append("" + /*{ + boolean removed = <%=methodName%>().removeAll(<%=attrName%>); + }*/ + ); + + if (usePCS) { + buffer.append("" + /*{ + if (removed) { + firePropertyChange(<%=constantName%>, <%=attrName%>, null); + } + }*/ + ); + } + buffer.append("" + /*{ + return removed; + }*/ + ); + setOperationBody(operation, buffer.toString()); + } + + protected void createContainsChildMethod(ObjectModelClass output, + String attrName, + String attrType, + String constantName, + boolean usePCS) { + + ObjectModelOperation operation = addOperation( + output, + getJavaBeanMethodName("contains", attrName), + "boolean", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, attrType, attrName); + StringBuilder buffer = new StringBuilder(); + String methodName = getJavaBeanMethodName("get", attrName); + buffer.append("" + /*{ + boolean contains = <%=methodName%>().contains(<%=attrName%>); + }*/ + ); + + buffer.append("" + /*{ + return contains; + }*/ + ); + setOperationBody(operation, buffer.toString()); + } + + protected void createContainsAllChildrenMethod(ObjectModelClass output, + String attrName, + String attrType, + String constantName, + boolean usePCS) { + + ObjectModelOperation operation = addOperation( + output, + getJavaBeanMethodName("containsAll", attrName), + "boolean", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, "java.util.Collection<" + attrType + ">", attrName); + StringBuilder buffer = new StringBuilder(); + String methodName = getJavaBeanMethodName("get", attrName); + buffer.append("" + /*{ + boolean contains = <%=methodName%>().containsAll(<%=attrName%>); + }*/ + ); + + buffer.append("" + /*{ + return contains; + }*/ + ); + setOperationBody(operation, buffer.toString()); + } + + protected void createSetMethod(ObjectModelClass output, + String attrName, + String attrType, + String simpleType, + String constantName, + boolean usePCS) { + ObjectModelOperation operation = addOperation( + output, + getJavaBeanMethodName("set", attrName), + "void", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, attrType, attrName); + + if (usePCS) { + String methodName = getJavaBeanMethodName("get", attrName); + setOperationBody(operation, "" + /*{ + <%=simpleType%> oldValue = <%=methodName%>(); + this.<%=attrName%> = <%=attrName%>; + firePropertyChange(<%=constantName%>, oldValue, <%=attrName%>); + }*/ + ); + } else { + setOperationBody(operation, "" + /*{ + this.<%=attrName%> = <%=attrName%>; + }*/ + ); + } + } + + protected void createGetChildMethod(ObjectModelClass output) { + ObjectModelOperation getChild = addOperation( + output, + "getChild", "<T> T", + ObjectModelJavaModifier.PROTECTED + ); + addParameter(getChild, "java.util.Collection<T>", "childs"); + addParameter(getChild, "int", "index"); + setOperationBody(getChild, "" + /*{ + if (childs != null) { + int i = 0; + for (T o : childs) { + if (index == i) { + return o; + } + i++; + } + } + return null; + }*/ + ); + } + + protected void addSerializable(ObjectModelClass input, + ObjectModelClass output, + boolean interfaceFound) { + if (!interfaceFound) { + addInterface(output, Serializable.class); + } + + // Generate the serialVersionUID + long serialVersionUID = JavaGeneratorUtil.generateSerialVersionUID(input); + + addConstant(output, + JavaGeneratorUtil.SERIAL_VERSION_UID, + "long", + serialVersionUID + "L", + ObjectModelJavaModifier.PRIVATE + ); + } + + /** + * Add all interfaces defines in input class and returns if + * {@link Serializable} interface was found. + * + * @param input the input model class to process + * @param output the output generated class + * @return {@code true} if {@link Serializable} was found from input, + * {@code false} otherwise + */ + protected boolean addInterfaces(ObjectModelClass input, + ObjectModelClass output) { + boolean foundSerializable = false; + for (ObjectModelInterface parentInterface : input.getInterfaces()) { + String fqn = parentInterface.getQualifiedName(); + addInterface(output, fqn); + if (Serializable.class.getName().equals(fqn)) { + foundSerializable = true; + } + } + return foundSerializable; + } + + protected void addSuperClass(ObjectModelClass input, + ObjectModelClass output) { + // Set superclass + Iterator<ObjectModelClass> j = input.getSuperclasses().iterator(); + if (j.hasNext()) { + ObjectModelClass p = j.next(); + // We want to set the inheritance to the implementation class of the father + // Ex for model : A -> B (a inherits B) we want : A -> BImpl -> B + String qualifiedName = p.getQualifiedName() + "Impl"; + setSuperClass(output, qualifiedName); + } + } + + protected ObjectModelClass generateImpl(ObjectModelClass input) { + + ObjectModelClass resultClassImpl = createClass( + input.getName() + "Impl", + input.getPackageName() + ); + + // set the abstract resulClass as the resultClassImpl super class + setSuperClass(resultClassImpl, input.getQualifiedName()); + + // add a fix serialVersionUID, since the class has no field nor method + addConstant(resultClassImpl, + JavaGeneratorUtil.SERIAL_VERSION_UID, + "long", + "1L", + ObjectModelJavaModifier.PRIVATE + ); + return resultClassImpl; + } + + protected void createPropertyChangeSupport(ObjectModelClass output) { + + addAttribute(output, + "pcs", + PropertyChangeSupport.class, + "new PropertyChangeSupport(this)", + ObjectModelJavaModifier.PROTECTED, + ObjectModelJavaModifier.FINAL + ); + + // Add PropertyListener + + ObjectModelOperation operation; + + operation = addOperation(output, + "addPropertyChangeListener", + "void", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, PropertyChangeListener.class, "listener"); + setOperationBody(operation, "" + /*{ + pcs.addPropertyChangeListener(listener); + }*/ + ); + + operation = addOperation(output, + "addPropertyChangeListener", + "void", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, String.class, "propertyName"); + addParameter(operation, PropertyChangeListener.class, "listener"); + setOperationBody(operation, "" + /*{ + pcs.addPropertyChangeListener(propertyName, listener); + }*/ + ); + + operation = addOperation(output, + "removePropertyChangeListener", + "void", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, PropertyChangeListener.class, "listener"); + setOperationBody(operation, "" + /*{ + pcs.removePropertyChangeListener(listener); + }*/ + ); + + operation = addOperation(output, + "removePropertyChangeListener", + "void", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, String.class, "propertyName"); + addParameter(operation, PropertyChangeListener.class, "listener"); + setOperationBody(operation, "" + /*{ + pcs.removePropertyChangeListener(propertyName, listener); + }*/ + ); + + operation = addOperation(output, + "firePropertyChange", + "void", + ObjectModelJavaModifier.PROTECTED + ); + addParameter(operation, String.class, "propertyName"); + addParameter(operation, Object.class, "oldValue"); + addParameter(operation, Object.class, "newValue"); + setOperationBody(operation, "" + /*{ + pcs.firePropertyChange(propertyName, oldValue, newValue); + }*/ + ); + + operation = addOperation(output, + "firePropertyChange", + "void", + ObjectModelJavaModifier.PROTECTED + ); + addParameter(operation, String.class, "propertyName"); + addParameter(operation, Object.class, "newValue"); + setOperationBody(operation, "" + /*{ + firePropertyChange(propertyName, null, newValue); + }*/ + ); + } + +} \ No newline at end of file Property changes on: trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaBeanTransformer.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaEnumerationTransformer.java (from rev 1179, trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaEnumerationTransformer.java) =================================================================== --- trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaEnumerationTransformer.java (rev 0) +++ trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaEnumerationTransformer.java 2012-10-24 19:30:08 UTC (rev 1181) @@ -0,0 +1,83 @@ +/* + * #%L + * EUGene :: Java templates + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2011 CodeLutin, Tony Chemit + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ +package org.nuiton.eugene.java; + + +/*{generator option: parentheses = false}*/ +/*{generator option: writeString = +}*/ + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.eugene.models.object.ObjectModelEnumeration; + +import java.util.Collection; + +/** + * JavaEnumerationTransformer generates a enumeration for enuration with + * stereotype enumeration. + * + * @author tchemit <chemit@codelutin.com> + * @plexus.component role="org.nuiton.eugene.Template" role-hint="org.nuiton.eugene.java.JavaEnumerationTransformer" + * @since 2.5 + */ +public class JavaEnumerationTransformer extends ObjectModelTransformerToJava { + + private static final Log log = + LogFactory.getLog(JavaEnumerationTransformer.class); + + @Override + public void transformFromEnumeration(ObjectModelEnumeration input) { + if (!canGenerate(input)) { + + if (log.isDebugEnabled()) { + log.debug("Skip generation for " + input.getQualifiedName()); + } + return; + } + + ObjectModelEnumeration output = + createEnumeration(input.getName(), input.getPackageName()); + + if (log.isDebugEnabled()) { + log.debug("will generate " + output.getQualifiedName()); + } + + Collection<String> literals = input.getLiterals(); + + for (String literal : literals) { + addLiteral(output, literal); + } + } + + protected boolean canGenerate(ObjectModelEnumeration input) { + boolean b = !JavaGeneratorUtil.hasSkipStereotype(input); + if (b) { + + // check if not found in class-path + b = !isInClassPath(input); + } + return b; + } +} \ No newline at end of file Property changes on: trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaEnumerationTransformer.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaInterfaceTransformer.java (from rev 1179, trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaInterfaceTransformer.java) =================================================================== --- trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaInterfaceTransformer.java (rev 0) +++ trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaInterfaceTransformer.java 2012-10-24 19:30:08 UTC (rev 1181) @@ -0,0 +1,139 @@ +/* + * #%L + * EUGene :: Java templates + * * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2010 CodeLutin, Tony Chemit + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.eugene.java; + +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.eugene.models.object.ObjectModelAttribute; +import org.nuiton.eugene.models.object.ObjectModelInterface; +import org.nuiton.eugene.models.object.ObjectModelJavaModifier; + +import java.util.Set; + +/*{generator option: parentheses = false}*/ +/*{generator option: writeString = +}*/ + +/** + * JavaInterfaceTransformer generates simple interfaces for Java language. + * <p/> + * Created: 7 nov. 2009 + * + * @author fdesbois <fdesbois@codelutin.com> + * @plexus.component role="org.nuiton.eugene.Template" role-hint="org.nuiton.eugene.java.JavaInterfaceTransformer" + * @since 2.0.2 + */ +public class JavaInterfaceTransformer extends ObjectModelTransformerToJava { + + private static final Log log = + LogFactory.getLog(JavaInterfaceTransformer.class); + + @Override + public void transformFromInterface(ObjectModelInterface input) { + + boolean canGenerate = canGenerate(input); + if (!canGenerate) { + + return; + } + + ObjectModelInterface output = createInterface(input.getName(), + input.getPackageName()); + + if (log.isDebugEnabled()) { + log.debug("generate interface " + + output.getQualifiedName()); + } + + // extend interface + + for (ObjectModelInterface extend : input.getInterfaces()) { + addInterface(output, extend.getQualifiedName()); + } + + String prefix = getConstantPrefix(input, ""); + + if (StringUtils.isEmpty(prefix)) { + + // no specific prefix, so no prefix + if (log.isWarnEnabled()) { + log.warn("[" + input.getName() + "] Will generate constants with NO prefix, not a good idea..."); + } + } + setConstantPrefix(prefix); + + Set<String> constants = addConstantsFromDependency(input, output); + + // constant attributes + for (ObjectModelAttribute attr : input.getAttributes()) { + + if (attr.isStatic() || + !StringUtils.isNotEmpty(attr.getDefaultValue())) { + + // only static attribut with value + continue; + } + + String constantName = attr.getName(); + + if (constants.contains(constantName)) { + + // already generated + continue; + } + + // add constant + addConstant(output, + constantName, + attr.getType(), + attr.getDefaultValue(), + ObjectModelJavaModifier.PUBLIC + ); + } + + + // interface operations + JavaGeneratorUtil.cloneOperations(this, + input.getOperations(), + output, + false + ); + } + + protected boolean canGenerate(ObjectModelInterface input) { + + // check if not found in class-path + boolean b = !isInClassPath(input); + + if (b) { + + // only generate when no stereotype are on interface ? Strange !? + b = input.getStereotypes().isEmpty(); + } + return b; + } + +} Property changes on: trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaInterfaceTransformer.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/package-info.java =================================================================== --- trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/package-info.java (rev 0) +++ trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/package-info.java 2012-10-24 19:30:08 UTC (rev 1181) @@ -0,0 +1,33 @@ +/** + * Eugene java package : Java generators. + * <pre> + * - Generator : {@link JavaBeanTransformer } to generate a bean. + * - Generator : {@link JavaEnumerationTransformer } to generate a enumeration. + * - Generator : {@link JavaInterfaceTransformer } to generate a interface. + * </pre> + */ +package org.nuiton.eugene.java; + +/* + * #%L + * EUGene :: Java templates + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2012 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ Property changes on: trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/package-info.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/eugene-maven-plugin/pom.xml =================================================================== --- trunk/eugene-maven-plugin/pom.xml 2012-10-24 19:03:21 UTC (rev 1180) +++ trunk/eugene-maven-plugin/pom.xml 2012-10-24 19:30:08 UTC (rev 1181) @@ -50,6 +50,13 @@ </dependency> <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>eugene-java-templates</artifactId> + <version>${project.version}</version> + <scope>runtime</scope> + </dependency> + + <dependency> <groupId>org.nuiton</groupId> <artifactId>nuiton-utils</artifactId> </dependency> Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2012-10-24 19:03:21 UTC (rev 1180) +++ trunk/pom.xml 2012-10-24 19:30:08 UTC (rev 1181) @@ -40,6 +40,7 @@ <modules> <module>eugene</module> + <module>eugene-java-templates</module> <module>eugene-maven-plugin</module> <!--<module>ant-eugene-task</module>--> </modules> @@ -396,8 +397,8 @@ <projectId>eugene</projectId> <processorPluginVersion>1.2.2</processorPluginVersion> - <nuitonUtilsVersion>2.5.3</nuitonUtilsVersion> - <nuitonI18nVersion>2.4.1</nuitonI18nVersion> + <nuitonUtilsVersion>2.6.3</nuitonUtilsVersion> + <nuitonI18nVersion>2.5</nuitonI18nVersion> <!--Multilanguage maven-site --> <locales>en,fr</locales>
participants (1)
-
tchemit@users.nuiton.org