1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.nuiton.eugene.models.object.xml;
24
25 import com.google.common.collect.Iterables;
26 import org.apache.commons.lang3.StringUtils;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.nuiton.eugene.EugeneCoreTagValues;
30 import org.nuiton.eugene.models.object.ObjectModelElement;
31 import org.nuiton.eugene.models.object.ObjectModelJavaModifier;
32 import org.nuiton.eugene.models.object.ObjectModelModifier;
33
34 import java.util.ArrayList;
35 import java.util.HashMap;
36 import java.util.HashSet;
37 import java.util.LinkedHashSet;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Set;
41
42
43
44
45
46
47
48 public abstract class ObjectModelElementImpl implements ObjectModelElement {
49
50
51 private static final Log log = LogFactory.getLog(ObjectModelElementImpl.class);
52
53 protected ObjectModelImpl objectModelImpl;
54
55 protected ObjectModelElement declaringElement;
56
57 protected String name;
58
59 protected String documentation;
60
61 protected Map<String, String> tagValues;
62
63 protected List<String> comments;
64
65 protected Set<ObjectModelModifier> modifiers;
66
67 public ObjectModelElementImpl() {
68 tagValues = new HashMap<>();
69 comments = new ArrayList<>();
70 modifiers = new HashSet<>();
71 }
72
73
74 public void setObjectModelImpl(ObjectModelImpl objectModelImpl) {
75 this.objectModelImpl = objectModelImpl;
76 }
77
78
79 public void setDeclaringElement(ObjectModelElement declaringElement) {
80 this.declaringElement = declaringElement;
81 }
82
83
84
85
86
87
88 public ObjectModelImpl getModel() {
89 if (objectModelImpl != null) {
90 return objectModelImpl;
91 } else if (declaringElement != null) {
92 return ((ObjectModelElementImpl) declaringElement).getModel();
93 }
94 return null;
95 }
96
97 public void postInit() {
98 }
99
100 public void setName(String name) {
101 this.name = name;
102 }
103
104 public void setDocumentation(String documentation) {
105 this.documentation = documentation;
106 }
107
108 public ObjectModelImplRef addStereotype(ObjectModelImplRef stereotype) {
109 if (stereotype == null) {
110 return new ObjectModelImplRef();
111 }
112 addStereotype(stereotype.getName());
113 return stereotype;
114 }
115
116 public ObjectModelImplTagValue addTagValue(ObjectModelImplTagValue tagValue) {
117 if (tagValue == null) {
118 return new ObjectModelImplTagValue();
119 }
120 addTagValue(tagValue.getName(), tagValue.getValue());
121 return tagValue;
122 }
123
124 protected void addOrRemoveModifier(ObjectModelModifier modifier, boolean add) {
125 if (add) {
126 addModifier(modifier);
127 } else {
128 removeModifier(modifier);
129 }
130 }
131
132 public void addModifier(ObjectModelModifier... modifiers) {
133 if (modifiers == null || (modifiers.length == 1 && modifiers[0] == null)) {
134 throw new IllegalArgumentException("Modifier is null");
135 }
136 for (ObjectModelModifier modifier : modifiers) {
137 if (getAuthorizedModifiers().contains(modifier)) {
138 this.modifiers.add(modifier);
139 } else {
140 throw new UnsupportedOperationException("Forbidden modifier: " + modifier.getName());
141 }
142 }
143 }
144
145 protected void removeModifiers(Iterable<? extends ObjectModelModifier> modifiers) {
146 removeModifier(Iterables.toArray(modifiers, ObjectModelModifier.class));
147 }
148
149 protected void removeModifier(ObjectModelModifier... modifiers) {
150 for (ObjectModelModifier modifier : modifiers) {
151 this.modifiers.remove(modifier);
152 }
153 }
154
155 protected abstract Set<ObjectModelModifier> getAuthorizedModifiers();
156
157 public void setStatic(boolean isStatic) {
158 addOrRemoveModifier(ObjectModelJavaModifier.STATIC, isStatic);
159 }
160
161 public void addComment(String comment) {
162 comments.add(comment);
163 }
164
165 @Override
166 public String getName() {
167 return name;
168 }
169
170 @Override
171 public ObjectModelElement getDeclaringElement() {
172 return declaringElement;
173 }
174
175 @Override
176 public String getDocumentation() {
177 if (documentation == null && hasTagValue(EugeneCoreTagValues.Store.documentation.getName())) {
178 String doc = getTagValue(EugeneCoreTagValues.Store.documentation.getName());
179 if (StringUtils.isNotEmpty(doc)) {
180 documentation = doc;
181 }
182 }
183 return documentation;
184 }
185
186 @Override
187 public String getDescription() {
188 return getDocumentation().substring(0, getDocumentation().indexOf("--"));
189 }
190
191 @Override
192 public String getSourceDocumentation() {
193 return getDocumentation().substring(getDocumentation().indexOf("--") + 2);
194 }
195
196 @Override
197 public Set<String> getStereotypes() {
198 Set<String> result = new LinkedHashSet<>();
199 for (Map.Entry<String, String> entry : tagValues.entrySet()) {
200 if ("true".equals(entry.getValue())) {
201 result.add(entry.getKey());
202 }
203 }
204 return result;
205 }
206
207 @Override
208 public boolean hasStereotype(String stereotypeName) {
209 return tagValues.containsKey(stereotypeName);
210 }
211
212 @Override
213 public void addStereotype(String stereotype) {
214 tagValues.put(stereotype, "true");
215 }
216
217 @Override
218 public void removeStereotype(String stereotype) {
219 tagValues.remove(stereotype);
220 }
221
222 @Override
223 public Map<String, String> getTagValues() {
224 return tagValues;
225 }
226
227 @Override
228 public String getTagValue(String tagValue) {
229 return tagValue == null ? null : tagValues.get(tagValue);
230 }
231
232 @Override
233 public boolean hasTagValue(String tagValue) {
234 return tagValues.containsKey(tagValue);
235 }
236
237 @Override
238 public void removeTagValue(String tagvalue) {
239 tagValues.remove(tagvalue);
240 }
241
242 @Override
243 public void addTagValue(String tagValue, String value) {
244 String oldValue = getTagValue(tagValue);
245 if (StringUtils.isNotEmpty(oldValue)) {
246 if (oldValue.equals(value)) {
247
248 return;
249 }
250 log.warn("Replace tagValue '" + tagValue + "' (old:" + oldValue + ", new: " + value + ")");
251 }
252 tagValues.put(tagValue, value);
253 }
254
255 @Override
256 public boolean isStatic() {
257 return modifiers.contains(ObjectModelJavaModifier.STATIC);
258 }
259
260 @Override
261 public List<String> getComments() {
262 return comments;
263 }
264
265 }