View Javadoc
1   /*
2    * #%L
3    * ToPIA :: Persistence
4    * $Id$
5    * $HeadURL$
6    * %%
7    * Copyright (C) 2004 - 2014 CodeLutin
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as 
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Lesser Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * #L%
23   */
24  
25  package org.nuiton.topia.generator;
26  
27  import org.apache.commons.lang3.StringUtils;
28  import org.nuiton.eugene.models.object.ObjectModel;
29  import org.nuiton.eugene.models.object.ObjectModelAttribute;
30  import org.nuiton.eugene.models.object.ObjectModelClass;
31  import org.nuiton.eugene.models.object.ObjectModelOperation;
32  import org.nuiton.eugene.models.object.validator.ObjectModelValidator;
33  
34  import java.util.ArrayList;
35  import java.util.HashSet;
36  import java.util.List;
37  import java.util.Set;
38  
39  /**
40   * Validateur qui valide :
41   * - les types des attributs
42   * - les nom des attributs
43   * - les duplication d'attibuts
44   *
45   * @author chatellier &lt;chatellier@codelutin.com&gt;
46   * @version $Id$
47   */
48  public class TopiaJavaValidator extends ObjectModelValidator {
49  
50      /**
51       * Constructor.
52       *
53       * @param model model to validate
54       */
55      public TopiaJavaValidator(ObjectModel model) {
56          super(model);
57      }
58  
59      @Override
60      protected boolean validateAttribute(ObjectModelAttribute attr) {
61  
62          boolean isValid = super.validateAttribute(attr);
63  
64          // type null ou vide
65          if (attr.getType() == null || attr.getType().isEmpty()) {
66              isValid = false;
67  
68              addError(attr, "Invalid type \"" + attr.getType() + "\"");
69          }
70  
71          // name = java reserved keywords
72          if (!isJavaIdentifier(attr.getName())) {
73              isValid = false;
74  
75              addError(attr, "Attribute name " + attr.getName()
76                             + " is not valid java identifier");
77          }
78  
79          // test sur les mots réservés ?
80  
81          return isValid;
82  
83      }
84  
85      @Override
86      protected boolean validateClass(ObjectModelClass clazz) {
87  
88          boolean isValid = super.validateClass(clazz);
89  
90          // test attribute names duplication
91          Set<String> attributesName = new HashSet<String>();
92          for (ObjectModelAttribute attr : clazz.getAttributes()) {
93              if (!attr.isNavigable()) {
94  
95                  // not navigable, so will not use it...
96                  continue;
97              }
98              String attrName = attr.getName();
99              if (!attributesName.add(attrName)) {
100                 addError(attr, "Attribute name " + attrName
101                                + " already exists");
102 
103                 isValid = false;
104             }
105         }
106 
107         if (TopiaGeneratorUtil.isEntity(clazz)) {
108 
109             Set<String> methodsName = new HashSet<String>();
110             for (ObjectModelAttribute attr : clazz.getAttributes()) {
111 
112                 String capitalizeAttrName = StringUtils.capitalize(attr.getName());
113                 methodsName.add(TopiaGeneratorUtil.OPERATION_GETTER_BOOLEAN_PREFIX + capitalizeAttrName);
114                 methodsName.add(TopiaGeneratorUtil.OPERATION_GETTER_DEFAULT_PREFIX + capitalizeAttrName);
115                 methodsName.add("set" + capitalizeAttrName);
116             }
117 
118             // test if there is a method an already reserved name
119             for (ObjectModelOperation operation : clazz.getOperations()) {
120                 String operationName = operation.getName();
121                 if (methodsName.contains(operationName)) {
122                     addError(operation, "Operation name " + operationName
123                                         + " is already reserved for a getter/setter of an entity attribute");
124 
125                     isValid = false;
126                 }
127             }
128         }
129 
130         return isValid;
131     }
132 
133     @Override
134     protected boolean validateModel(ObjectModel model) {
135 
136         return super.validateModel(model);
137     }
138 
139     /**
140      * Returns true if s is a legal Java identifier.
141      *
142      * @param s string to test
143      * @return true if s is a legal Java identifier
144      */
145     public static boolean isJavaIdentifier(String s) {
146         if (s.length() == 0 || !Character.isJavaIdentifierStart(s.charAt(0))) {
147             return false;
148         }
149         for (int i = 1; i < s.length(); i++) {
150             if (!Character.isJavaIdentifierPart(s.charAt(i))) {
151                 return false;
152             }
153         }
154         return true;
155     }
156 }