View Javadoc
1   package org.nuiton.eugene;
2   
3   /*-
4    * #%L
5    * EUGene :: EUGene
6    * %%
7    * Copyright (C) 2004 - 2016 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  import com.google.common.collect.ImmutableSet;
26  import org.codehaus.plexus.component.annotations.Component;
27  import org.nuiton.eugene.models.extension.tagvalue.TagValueMetadata;
28  import org.nuiton.eugene.models.extension.tagvalue.TagValueUtil;
29  import org.nuiton.eugene.models.extension.tagvalue.matcher.EqualsTagValueNameMatcher;
30  import org.nuiton.eugene.models.extension.tagvalue.provider.DefaultTagValueMetadatasProvider;
31  import org.nuiton.eugene.models.extension.tagvalue.provider.TagValueMetadatasProvider;
32  import org.nuiton.eugene.models.object.ObjectModel;
33  import org.nuiton.eugene.models.object.ObjectModelAttribute;
34  import org.nuiton.eugene.models.object.ObjectModelClassifier;
35  import org.nuiton.eugene.models.object.ObjectModelElement;
36  import org.nuiton.eugene.models.object.ObjectModelPackage;
37  
38  import java.util.Set;
39  
40  import static org.nuiton.i18n.I18n.n;
41  import static org.nuiton.i18n.I18n.t;
42  
43  /**
44   * Created on 24/09/16.
45   *
46   * Defines all tag values managed by Eugene.
47   *
48   * In another library using eugene, please extends this contract to put your
49   * own tag values, to get a unique place where to find tag values.
50   *
51   * @author Tony Chemit - chemit@codelutin.com
52   * @since 3.0
53   */
54  @Component(role = TagValueMetadatasProvider.class, hint = "eugene")
55  public class EugeneCoreTagValues extends DefaultTagValueMetadatasProvider {
56  
57      @Override
58      public String getDescription() {
59          return t("eugene.core.tagvalues");
60      }
61  
62      public enum Store implements TagValueMetadata {
63  
64          /**
65           * Tag value to add the version of the model from outside (says in the
66           * properties file associated to the model)..
67           *
68           * Actually, the eugene api does not use to modify the model. ItaTa is only
69           * used while reading the properties associated with a model and if found is
70           * directly set to the {@code version} field of the model.
71           *
72           * @since 2.3
73           */
74          version(n("eugene.core.tagValues.version"), String.class, null, ObjectModel.class),
75  
76          /**
77           * Tag value to add on constants enumeration (or other incoming dev)
78           * a prefix to constant to generate.
79           *
80           * You can globaly use it on the complete model or to a specific classifier.
81           *
82           * @since 2.5
83           */
84          documentation(n("eugene.core.tagValues.documentation"), String.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelElement.class),
85  
86          /**
87           * Tag value to add on constants enumeration (or other incoming dev)
88           * a prefix to constant to generate.
89           *
90           * You can globaly use it on the complete model or to a specific classifier.
91           *
92           * @since 2.3
93           */
94          constantPrefix(n("eugene.core.tagValues.constantPrefix"), String.class, "PROPERTY_", ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class),
95  
96          /**
97           * Tag value to specify the i18n prefix to use where generating i18n keys.
98           *
99           * You can globaly use it on the complete model or to a specific classifier.
100          *
101          * @since 2.3
102          */
103         i18n(n("eugene.core.tagValues.i18n"), String.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class),
104 
105         /**
106          * Tag value to specify the @Generated annotation class name.
107          *
108          * You can globaly use it on the complete model or to a specific classifier.
109          *
110          * @since 3.0
111          */
112         generatedAnnotation(n("eugene.core.tagValues.generatedAnnotation"), String.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class),
113 
114         /**
115          * Tag value to generate also {@code getXXX} methods for a boolean property.
116          *
117          * @since 2.12
118          */
119         generateBooleanGetMethods(n("eugene.core.tagValues.generateBooleanGetMethods"), boolean.class, "false", ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class),
120 
121         /**
122          * To add a generic to an attribute.
123          *
124          * @since 3.0
125          */
126         attributeGeneric(n("eugene.core.tagValues.attributeGeneric"), String.class, null, ObjectModelAttribute.class),
127 
128         /**
129          * Stereotype to mark an attribute with multiplicity as ordered.
130          *
131          * It means that order of insertion is maintained: in Java, it will lead
132          * to a collection typed with {@link java.util.List} or {@link java.util.LinkedHashSet}
133          * or {@link java.util.LinkedHashMap}.
134          *
135          * @since 2.8
136          */
137         ordered(n("eugene.core.tagValues.ordered"), boolean.class, null, ObjectModelAttribute.class),
138         /**
139          * Stereotype to mark an attribute with multiplicity as unique.
140          *
141          * It means that uniqueness of elements is maintained in the collection: in Java, it
142          * will lead to a collection typed with {@link java.util.Set}.
143          */
144         unique(n("eugene.core.tagValues.unique"), boolean.class, null, ObjectModelAttribute.class),
145         /**
146          * Stereotype to skip generation for some templates.
147          */
148         skip(n("eugene.core.tagValues.skip"), boolean.class, null, ObjectModelClassifier.class, ObjectModelPackage.class);
149 
150 
151         private final Set<Class<?>> targets;
152         private final Class<?> type;
153         private final String i18nDescriptionKey;
154         private final String defaultValue;
155 
156         Store(String i18nDescriptionKey, Class<?> type, String defaultValue, Class<?>... targets) {
157             this.targets = ImmutableSet.copyOf(targets);
158             this.type = type;
159             this.i18nDescriptionKey = i18nDescriptionKey;
160             this.defaultValue = defaultValue;
161         }
162 
163         @Override
164         public String getName() {
165             return name();
166         }
167 
168         @Override
169         public Set<Class<?>> getTargets() {
170             return targets;
171         }
172 
173         @Override
174         public Class<?> getType() {
175             return type;
176         }
177 
178         @Override
179         public Class<EqualsTagValueNameMatcher> getMatcherClass() {
180             return EqualsTagValueNameMatcher.class;
181         }
182 
183         @Override
184         public String getDescription() {
185             return t(i18nDescriptionKey);
186         }
187 
188         @Override
189         public String getDefaultValue() {
190             return defaultValue;
191         }
192 
193         @Override
194         public boolean isDeprecated() {
195             return false;
196         }
197 
198     }
199 
200     public EugeneCoreTagValues() {
201         super((TagValueMetadata[]) Store.values());
202     }
203 
204     /**
205      * Obtain the value of the {@link Store#documentation} tag value on the given model.
206      *
207      * @param model model to seek
208      * @return the none empty value of the found tag value or {@code null} if not found nor empty.
209      * @see Store#documentation
210      * @since 2.3
211      */
212     public String getDocumentationTagValue(ObjectModel model) {
213         return TagValueUtil.findTagValue(Store.documentation, model);
214     }
215 
216     /**
217      * Obtain the value of the {@link Store#documentation} tag value on the given element.
218      *
219      * @param element element to seek
220      * @return the none empty value of the found tag value or {@code null} if not found nor empty.
221      * @see Store#documentation
222      * @since 2.3
223      */
224     public String getDocumentationTagValue(ObjectModelElement element) {
225         return TagValueUtil.findTagValue(Store.documentation, element);
226     }
227 
228     /**
229      * Obtain the value of the {@link Store#documentation} tag value on the given package.
230      *
231      * @param aPackage package to seek
232      * @return the none empty value of the found tag value or {@code null} if not found nor empty.
233      * @see Store#documentation
234      * @since 2.12
235      */
236     public String getDocumentationTagValue(ObjectModelPackage aPackage) {
237         return TagValueUtil.findTagValue(Store.documentation, aPackage);
238     }
239 
240     /**
241      * Cherche et renvoie le préfixe i18n à utiliser sur cet element, sinon sur
242      * le model.
243      *
244      * @param element  element to seek
245      * @param aPackage package to seek
246      * @param model    model to seek
247      * @return le préfixe i18n ou <code>null</code> si non spécifié
248      * @since 2.3
249      */
250     public String getI18nPrefixTagValue(ObjectModelElement element, ObjectModelPackage aPackage, ObjectModel model) {
251         return TagValueUtil.findTagValue(Store.i18n, element, aPackage, model);
252     }
253 
254     /**
255      * Cherche et renvoie le préfixe i18n à utiliser sur cet element, sinon sur
256      * le model.
257      *
258      * @param element  element to seek
259      * @param aPackage package to seek
260      * @param model    model to seek
261      * @return le préfixe i18n ou <code>null</code> si non spécifié
262      * @since 2.3
263      */
264     public String getGeneratedAnnotationTagValue(ObjectModelElement element, ObjectModelPackage aPackage, ObjectModel model) {
265         return TagValueUtil.findTagValue(Store.generatedAnnotation, element, aPackage, model);
266     }
267 
268     /**
269      * Obtain the value of the {@link Store#generateBooleanGetMethods}
270      * tag value on the given model, package or classifier.
271      *
272      * It will first look on the model, then on package and then in the given classifier.
273      *
274      * @param classifier classifier to seek
275      * @param aPackage   package to seek
276      * @param model      model to seek
277      * @return the none empty value of the found tag value or {@code null} if not found nor empty.
278      * @see Store#generateBooleanGetMethods
279      * @since 2.12
280      */
281     public boolean isGenerateBooleanGetMethods(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) {
282         return TagValueUtil.findBooleanTagValue(Store.generateBooleanGetMethods, classifier, aPackage, model);
283     }
284 
285     /**
286      * Obtain the value of the {@link Store#constantPrefix} tag value on the given model or classifier.
287      *
288      * It will first look on the model, and then in the given classifier.
289      *
290      * @param classifier classifier to seek
291      * @param aPackage   package to seek
292      * @param model      model to seek
293      * @return the none empty value of the found tag value or {@code null} if not found nor empty.
294      * @see Store#constantPrefix
295      * @since 2.3
296      */
297     public String getConstantPrefixTagValue(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) {
298         return TagValueUtil.findTagValue(Store.constantPrefix, classifier, aPackage, model);
299     }
300 
301     /**
302      * Obtain the value of the {@link Store#attributeGeneric} tag value on the given attribute.
303      *
304      * @param attribute attribute to seek
305      * @return the none empty value of the found tag value or {@code null} if not found nor empty.
306      * @see Store#attributeGeneric
307      * @since 3.0
308      */
309     public String getAttributeGenericTagValue(ObjectModelAttribute attribute) {
310         return TagValueUtil.findTagValue(Store.attributeGeneric, attribute);
311     }
312 
313     /**
314      * Check if the given attribute has the {@link Store#ordered} boolean tag value.
315      *
316      * @param attribute attribute to test
317      * @return {@code true} if boolean tag value was found, {@code false otherwise}
318      * @see Store#ordered
319      * @since 2.9
320      */
321     public static boolean isOrdered(ObjectModelAttribute attribute) {
322         return TagValueUtil.findBooleanTagValue(Store.ordered, attribute);
323     }
324 
325     /**
326      * Check if the given attribute has the {@link Store#unique} boolean tag value.
327      *
328      * @param attribute attribute to test
329      * @return {@code true} if boolean tag value was found, {@code false otherwise}
330      * @see Store#unique
331      * @since 2.9
332      */
333     public static boolean isUnique(ObjectModelAttribute attribute) {
334         return TagValueUtil.findBooleanTagValue(Store.unique, attribute);
335     }
336 
337     /**
338      * Check if the given classifier has the {@link Store#skip} boolean tag value.
339      *
340      * @param classifier classifier to test
341      * @param aPackage   package to test
342      * @return {@code true} if boolean tag value was found, {@code false otherwise}
343      * @see Store#skip
344      * @since 2.9
345      */
346     public static boolean isSkip(ObjectModelClassifier classifier, ObjectModelPackage aPackage) {
347         return TagValueUtil.findBooleanTagValue(Store.skip, classifier, aPackage);
348     }
349 
350     /**
351      * Check if the given package has the {@link Store#skip} boolean tag value.
352      *
353      * @param aPackage package to test
354      * @return {@code true} if boolean tag value was found, {@code false otherwise}
355      * @see Store#skip
356      */
357     public static boolean isSkip(ObjectModelPackage aPackage) {
358         return TagValueUtil.findBooleanTagValue(Store.skip, aPackage);
359     }
360 }