Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5f522c4887592e739a518aeebddd2aaba2add5b9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/**
 * <copyright>
 *
 * Copyright (c) 2005, 2006, 2007, 2008 Springsite BV (The Netherlands) and others
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Martin Taal
 * </copyright>
 *
 * $Id: EAnnotationParserImporter.java,v 1.8 2010/02/04 11:02:59 mtaal Exp $
 */

package org.eclipse.emf.teneo.annotations.parser;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.teneo.Constants;
import org.eclipse.emf.teneo.PersistenceOptions;
import org.eclipse.emf.teneo.annotations.pamodel.PAnnotatedEClass;
import org.eclipse.emf.teneo.annotations.pamodel.PAnnotatedEDataType;
import org.eclipse.emf.teneo.annotations.pamodel.PAnnotatedEModelElement;
import org.eclipse.emf.teneo.annotations.pamodel.PAnnotatedEPackage;
import org.eclipse.emf.teneo.annotations.pamodel.PAnnotatedEStructuralFeature;
import org.eclipse.emf.teneo.annotations.pamodel.PAnnotatedModel;
import org.eclipse.emf.teneo.annotations.pannotation.PannotationPackage;
import org.eclipse.emf.teneo.extension.ExtensionPoint;

/**
 * Walks over the pamodel and the paepackages and translates eannotations to pannotation types and
 * sets the corresponding values in the pamodel.
 * 
 * @author <a href="mailto:mtaal at elver.org">Martin Taal</a>
 */
public class EAnnotationParserImporter implements EClassResolver, ExtensionPoint {
	/** Log it */
	private final static Log log = LogFactory.getLog(EAnnotationParserImporter.class);

	/** annotation parser */
	private AnnotationParser annotationParser = new AnnotationParser();

	/** The prefix to distinguish jpa annotations from hb or jpox annotations */
	private static final String JPA_PREFIX = "jpa:";

	private String[] extraAnnotationsSources = new String[] {};

	/** Parse an pamodel */
	public void process(PAnnotatedModel paModel) {
		for (PAnnotatedEPackage pap : paModel.getPaEPackages()) {
			log.debug("Processing package " + pap.getModelEPackage().getName());
			processAnnotatedModelElement(pap, pap.eClass().getEPackage());

			// and now the eclasses
			process(pap);
		}
	}

	/** Process package */
	protected void process(PAnnotatedEPackage pap) {
		for (PAnnotatedEClass pac : pap.getPaEClasses()) {
			processAnnotatedModelElement(pac, pac.getModelEClass().getEPackage());
			process(pac);
		}
		for (PAnnotatedEDataType pac : pap.getPaEDataTypes()) {
			processAnnotatedModelElement(pac, pac.getModelEDataType().getEPackage());
		}
	}

	/** Process the efeatures */
	protected void process(PAnnotatedEClass pac) {
		log.debug("Processing eclass " + pac.getModelEClass().getName());
		for (PAnnotatedEStructuralFeature paf : pac.getPaEStructuralFeatures()) {
			processAnnotatedModelElement(paf, paf.getModelEStructuralFeature().eClass().getEPackage());
		}
	}

	/** Process a type with its eannotations */
	@SuppressWarnings("unchecked")
	protected void processAnnotatedModelElement(PAnnotatedEModelElement pee, EPackage epack) {
		log.debug("Processing " + pee.getModelElement().getName());
		final ArrayList<NamedParserNode> parsedNodes = new ArrayList<NamedParserNode>();
		for (EAnnotation annotation : pee.getModelElement().getEAnnotations()) {
			parsedNodes.addAll(process(annotation, pee.getModelElement()));
		}

		// now also do the annotations on the edatatype (if any)
		/*
		 * if (pee.getAnnotatedElement() instanceof EAttribute) { final EAttribute eattr =
		 * (EAttribute)pee.getAnnotatedElement(); final EDataType edt = (EDataType)eattr.getEType(); for
		 * (Iterator it = edt.getEAnnotations().iterator(); it.hasNext();) {
		 * parsedNodes.addAll(process((EAnnotation)it.next(), pee.getAnnotatedElement())); } }
		 */

		// now the parsed nodes should be translated into features of the
		// enamedelement
		// this is done multiplelevel
		log.debug("Number of parsed typename annotations " + parsedNodes.size());
		for (NamedParserNode namedParserNode : parsedNodes) {
			final ComplexNode cn = (ComplexNode) namedParserNode;
			if (cn.isList()) {
				// find the efeature
				final EStructuralFeature ef = getEStructuralFeature(pee.eClass(), cn.getName());
				pee.eSet(ef, cn.convert(this));
			} else {
				EObject eobj = (EObject) cn.convert(this);
				boolean found = false;
				// first find exact type
				for (EReference eref : pee.eClass().getEAllReferences()) {
					if (eref.getEReferenceType() == eobj.eClass()) {
						log.debug("Found EReference " + eref.getName() + " for " + eobj.eClass().getName());
						if (eref.isMany()) {
							((List<EObject>) pee.eGet(eref)).add(eobj);
						} else {
							pee.eSet(eref, eobj);
						}
						found = true;
						break;
					}
				}
				if (!found) {
					for (EReference eref : pee.eClass().getEAllReferences()) {
						if (eref.getEReferenceType().isInstance(eobj)) {
							log.debug("Found EReference " + eref.getName() + " for " + eobj.eClass().getName());
							if (eref.isMany()) {
								((List<EObject>) pee.eGet(eref)).add(eobj);
							} else {
								pee.eSet(eref, eobj);
							}
							found = true;
							break;
						}
					}
				}
				if (!found) {
					throw new AnnotationParserException("The eclass: " + pee.eClass().getName()
							+ " does not have an efeature for " + eobj.eClass().getName());
				}
			}
		}

		// now for each eobject find which eref stores it!
		// log.debug("Find efeature for each created eobject");
		// final ArrayList objects = new ArrayList();
		// for (Iterator it = objects.iterator(); it.hasNext();) {
		// EObject eobj = (EObject)it.next();
		// log.debug("EClass " + eobj.eClass().getName());
		// }
	}

	/** Processes EAnnotations */
	private ArrayList<NamedParserNode> process(EAnnotation ea, ENamedElement ene) {
		final ArrayList<NamedParserNode> result = new ArrayList<NamedParserNode>();

		if (!isValidSource(ea.getSource())) {
			return result;
		}

		log.debug("Processing annotations ");
		for (Map.Entry<String, String> pAnnotationDetails : ea.getDetails().entrySet()) {
			final String fName = pAnnotationDetails.getKey();
			// todo externalize
			if (fName.compareToIgnoreCase(Constants.ANNOTATION_KEY_APPINFO) == 0
					|| fName.compareToIgnoreCase(Constants.ANNOTATION_KEY_VALUE) == 0) {
				log.debug("Annotation content: \n " + pAnnotationDetails.getValue());
				final String content = removeCommentLines(pAnnotationDetails.getValue());
				result.addAll(annotationParser.parse(ene, content));
			}
		}
		return result;
	}

	// removes the lines which start with a //
	private String removeCommentLines(String content) {
		if (content.indexOf("//") == -1) {
			return content;
		}
		final String[] lines = content.split("\n");
		final StringBuilder sb = new StringBuilder();
		for (String line : lines) {
			if (line.trim().startsWith("//")) {
				continue;
			}
			if (sb.length() > 0) {
				sb.append("\n");
			}
			sb.append(line);
		}
		return sb.toString();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.teneo.annotations.parser.EClassResolver#getEClass(java .lang.String)
	 */
	public EClass getEClass(String name) {
		if (name.startsWith(JPA_PREFIX)) {
			return getEClass(name.substring(JPA_PREFIX.length()));
		}
		return (EClass) PannotationPackage.eINSTANCE.getEClassifier(name);
	}

	/** Is a valid source */
	protected boolean isValidSource(String source) {
		if (source == null) {
			return false;
		}

		if (extraAnnotationsSources.length > 0) {
			for (String annotationSource : extraAnnotationsSources) {
				if (source.equals(annotationSource)) {
					return true;
				}
			}
		}

		if (source.equals(Constants.ANNOTATION_SOURCE_TENEO_JPA_AUDITING)) {
			return false;
		}

		return source.startsWith(Constants.ANNOTATION_SOURCE_TENEO_JPA)
				|| source.startsWith(Constants.ANNOTATION_SOURCE_TENEO_MAPPING);
	}

	/** Find the efeature */
	public EStructuralFeature getEStructuralFeature(EClass eClass, String name) {
		return ParserUtil.getEStructuralFeature(eClass, name);
	}

	public void setExtraAnnotationSources(PersistenceOptions po) {
		if (po.getExtraAnnotationSources() != null
				&& po.getExtraAnnotationSources().trim().length() > 0) {
			extraAnnotationsSources = po.getExtraAnnotationSources().split(",");
			for (int i = 0; i < extraAnnotationsSources.length; i++) {
				extraAnnotationsSources[i] = extraAnnotationsSources[i].trim();
				if (extraAnnotationsSources[i].startsWith(Constants.ANNOTATION_SOURCE_TENEO_JPA)
						|| extraAnnotationsSources[i].startsWith(Constants.ANNOTATION_SOURCE_TENEO_MAPPING)) {
					log.warn("Extra annotation source ("
							+ extraAnnotationsSources[i]
							+ ") starts with the default Teneo annotation source: "
							+ Constants.ANNOTATION_SOURCE_TENEO_JPA
							+ " or "
							+ Constants.ANNOTATION_SOURCE_TENEO_MAPPING
							+ ". Annotations which should sometimes be "
							+ "ignored or disabled should not start with these values as they are always considered by Teneo");
				}
			}
		}
	}
}

Back to the top