View Javadoc
1   /*
2    * #%L
3    * EUGene :: Java templates
4    * %%
5    * Copyright (C) 2004 - 2010 CodeLutin, Tony Chemit
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
21   */
22  
23  package org.nuiton.eugene.java;
24  
25  import org.apache.commons.lang3.StringUtils;
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.codehaus.plexus.component.annotations.Component;
29  import org.nuiton.eugene.Template;
30  import org.nuiton.eugene.models.object.ObjectModelAttribute;
31  import org.nuiton.eugene.models.object.ObjectModelInterface;
32  import org.nuiton.eugene.models.object.ObjectModelJavaModifier;
33  
34  import java.util.Set;
35  
36  /*{generator option: parentheses = false}*/
37  /*{generator option: writeString = +}*/
38  
39  /**
40   * JavaInterfaceTransformer generates simple interfaces for Java language.
41   *
42   * Created: 7 nov. 2009
43   *
44   * @author Florian Desbois - desbois@codelutin.com
45   * @since 2.0.2
46   */
47  @Component(role = Template.class, hint = "org.nuiton.eugene.java.JavaInterfaceTransformer")
48  public class JavaInterfaceTransformer extends ObjectModelTransformerToJava {
49  
50      private static final Log log =
51              LogFactory.getLog(JavaInterfaceTransformer.class);
52  
53      @Override
54      public void transformFromInterface(ObjectModelInterface input) {
55  
56          boolean canGenerate = canGenerate(input);
57          if (!canGenerate) {
58              
59              return;
60          }
61  
62          ObjectModelInterface output = createInterface(input.getName(),
63                                                        input.getPackageName());
64  
65          if (log.isDebugEnabled()) {
66              log.debug("generate interface " +
67                        output.getQualifiedName());
68          }
69  
70          // extend interface
71  
72          for (ObjectModelInterface extend : input.getInterfaces()) {
73              addInterface(output, extend.getQualifiedName());
74          }
75  
76          String prefix = getConstantPrefix(input);
77  
78          if (StringUtils.isEmpty(prefix)) {
79  
80              // no specific prefix, so no prefix
81              if (log.isWarnEnabled()) {
82                  log.warn("[" + input.getName() + "] Will generate constants with NO prefix, not a good idea...");
83              }
84          }
85          setConstantPrefix(prefix);
86  
87          Set<String> constants = addConstantsFromDependency(input, output);
88  
89          // constant attributes
90          for (ObjectModelAttribute attr : input.getAttributes()) {
91  
92              if (attr.isStatic() ||
93                  !StringUtils.isNotEmpty(attr.getDefaultValue())) {
94  
95                  // only static attribut with value
96                  continue;
97              }
98  
99              String constantName = attr.getName();
100 
101             if (constants.contains(constantName)) {
102 
103                 // already generated
104                 continue;
105             }
106 
107             // add constant
108             addConstant(output,
109                         constantName,
110                         attr.getType(),
111                         attr.getDefaultValue(),
112                         ObjectModelJavaModifier.PUBLIC
113             );
114         }
115 
116 
117         // interface operations
118         JavaGeneratorUtil.cloneOperations(this,
119                                           input.getOperations(),
120                                           output,
121                                           false
122         );
123     }
124 
125     protected boolean canGenerate(ObjectModelInterface input) {
126         
127         // check if not found in class-path
128         boolean b = !getResourcesHelper().isJavaFileInClassPath(input.getQualifiedName());
129 
130         if (b) {
131 
132             // only generate when no stereotype are on interface ? Strange !?
133             b = input.getStereotypes().isEmpty();
134         }
135         return b;
136     }
137 
138 }