View Javadoc
1   package org.nuiton.eugene.java;
2   
3   /*
4    * #%L
5    * EUGene :: Java templates
6    * %%
7    * Copyright (C) 2012 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.ModelReader;
28  import org.nuiton.eugene.models.extension.tagvalue.TagValueMetadata;
29  import org.nuiton.eugene.models.extension.tagvalue.TagValueUtil;
30  import org.nuiton.eugene.models.extension.tagvalue.matcher.EqualsTagValueNameMatcher;
31  import org.nuiton.eugene.models.extension.tagvalue.provider.DefaultTagValueMetadatasProvider;
32  import org.nuiton.eugene.models.extension.tagvalue.provider.TagValueMetadatasProvider;
33  import org.nuiton.eugene.models.object.ObjectModel;
34  import org.nuiton.eugene.models.object.ObjectModelClassifier;
35  import org.nuiton.eugene.models.object.ObjectModelPackage;
36  
37  import java.util.Set;
38  
39  import static org.nuiton.i18n.I18n.n;
40  import static org.nuiton.i18n.I18n.t;
41  
42  /**
43   * Defines all tag values managed by Java templates.
44   *
45   * @author Tony Chemit - chemit@codelutin.com
46   * @since 2.5.6
47   */
48  @Component(role = TagValueMetadatasProvider.class, hint = "eugene-java-templates")
49  public class EugeneJavaTagValues extends DefaultTagValueMetadatasProvider {
50  
51      @Override
52      public String getDescription() {
53          return t("eugene.java.tagvalues");
54      }
55  
56      public enum Store implements TagValueMetadata {
57  
58          /**
59           * Boolean tag value for JavaBean objects to place on a classifier or package.
60           *
61           * @see #isBean(ObjectModelClassifier, ObjectModelPackage)
62           * @since 2.5.6
63           */
64          bean(n("eugene.java.stereotype.bean"), boolean.class, null, ObjectModelClassifier.class, ObjectModelPackage.class),
65  
66          /**
67           * To use java 8 new syntax and api in generation.
68           *
69           * You can globally use it on the complete model.
70           *
71           * @see #isUseJava8(ObjectModel)
72           * @since 2.15
73           */
74          java8(n("eugene.java.tagvalue.java8"), boolean.class, "false", ObjectModel.class);
75  
76          private final Set<Class<?>> targets;
77          private final Class<?> type;
78          private final String i18nDescriptionKey;
79          private final String defaultValue;
80  
81          Store(String i18nDescriptionKey, Class<?> type, String defaultValue, Class<?>... targets) {
82              this.targets = ImmutableSet.copyOf(targets);
83              this.type = type;
84              this.i18nDescriptionKey = i18nDescriptionKey;
85              this.defaultValue = defaultValue;
86          }
87  
88          @Override
89          public String getName() {
90              return name();
91          }
92  
93          @Override
94          public Set<Class<?>> getTargets() {
95              return targets;
96          }
97  
98          @Override
99          public Class<?> getType() {
100             return type;
101         }
102 
103         @Override
104         public Class<EqualsTagValueNameMatcher> getMatcherClass() {
105             return EqualsTagValueNameMatcher.class;
106         }
107 
108         @Override
109         public String getDescription() {
110             return t(i18nDescriptionKey);
111         }
112 
113         @Override
114         public String getDefaultValue() {
115             return defaultValue;
116         }
117 
118         @Override
119         public boolean isDeprecated() {
120             return false;
121         }
122 
123     }
124 
125     public EugeneJavaTagValues() {
126         super((TagValueMetadata[]) Store.values());
127     }
128 
129     /**
130      * Obtain the value of the {@link Store#java8} tag value on the given model.
131      *
132      * @param model model to seek
133      * @return {@code true} the none empty value of the found tag value or {@code false} if not found nor empty.
134      * @see Store#java8
135      * @since 2.15
136      */
137     public boolean isUseJava8(ObjectModel model) {
138         return TagValueUtil.findBooleanTagValue(Store.java8, model);
139     }
140 
141     /**
142      * Check if the given classifier has the {@link Store#bean} stereotype.
143      *
144      * @param classifier classifier to test
145      * @return {@code true} if tag value was found, {@code false otherwise}
146      * @see Store#bean
147      */
148     public boolean isBean(ObjectModelClassifier classifier, ObjectModelPackage aPackage) {
149         return TagValueUtil.findBooleanTagValue(Store.bean, classifier, aPackage);
150     }
151 
152 }