Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 443988ac5f0ca9643631248f0a55aac2b4a7b3de (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
/*****************************************************************************
 * Copyright (c) 2015 CEA LIST 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:
 *   CEA LIST - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.infra.gmfdiag.common.expansionmodel;

import java.util.Map;

import javax.naming.Context;

import org.eclipse.emf.common.util.BasicDiagnostic;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.DiagnosticChain;
import org.eclipse.gmf.runtime.emf.type.core.ElementTypeRegistry;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.papyrus.infra.gmfdiag.common.expansionmodel.util.ExpansionmodelValidator;
import org.eclipse.papyrus.infra.tools.util.ClassLoaderHelper;
import org.eclipse.papyrus.infra.types.core.registries.ElementTypeConfigurationTypeRegistry;

/**
 * This utility class has been added in order to ensure that model is well built.
 * See Requirement #org.eclipse.papyrus.infra.gmfdiag.expansion.Req_061
 * 
 *
 */
public class ExpansionModelValidationUtil {

	/**
	 * This method is used to ensure that properties are filled
	 * 
	 * @param abstractRepresentation
	 *            the {@link AbstractRepresentation} that is validate
	 * @param diagnostic
	 *            the {@link Diagnostic}
	 * @param context
	 *            the {@link Context}
	 * @return false if the kind is not filled and if the properties view Factory && editPartQualified name are null or equals to "".
	 */
	public static boolean validate_facrtories(AbstractRepresentation abstractRepresentation, DiagnosticChain diagnostic, Map context) {
		boolean valid = true;
		if (diagnostic != null) {
			if (abstractRepresentation.getKind() == null) {
				if (abstractRepresentation.getEditPartQualifiedName() == null || "".equals(abstractRepresentation.getEditPartQualifiedName().trim())) {
					if (abstractRepresentation.getViewFactory() == null || "".equals(abstractRepresentation.getViewFactory().trim())) {
						valid = false;
						diagnostic.add(new BasicDiagnostic(Diagnostic.ERROR,
								ExpansionmodelValidator.DIAGNOSTIC_SOURCE,
								ExpansionmodelValidator.ABSTRACT_REPRESENTATION__VALIDATE, "The representation '"
										+ abstractRepresentation.getName() + "' has no kind , no editpartQualifiedName, no viewFactory.",
								new Object[] { abstractRepresentation }));
					}
				}

			}
		}
		return valid;
	}

	/**
	 * this method is used to verify if provider and editpart can be loaded
	 * 
	 * @param abstractRepresentation
	 * @param diagnostic
	 * @param context
	 * @return
	 */
	public static boolean validate_loadclasses(AbstractRepresentation abstractRepresentation, DiagnosticChain diagnostic, Map context) {
		boolean valid = true;
		if (diagnostic != null) {
			if (abstractRepresentation.getEditPartQualifiedName() != null && !("".equals(abstractRepresentation.getEditPartQualifiedName().trim()))) {
				Class<?> loadedClass = ClassLoaderHelper.loadClass(abstractRepresentation.getEditPartQualifiedName());
				if (loadedClass == null) {
					valid = false;
					diagnostic.add(new BasicDiagnostic(Diagnostic.ERROR,
							ExpansionmodelValidator.DIAGNOSTIC_SOURCE,
							ExpansionmodelValidator.ABSTRACT_REPRESENTATION__VALIDATE, "The representation '"
									+ abstractRepresentation.getName() + "' references an edit part that does not exist " + abstractRepresentation.getEditPartQualifiedName(),
							new Object[] { abstractRepresentation }));

				}

			}
			if (abstractRepresentation.getViewFactory() != null && !("".equals(abstractRepresentation.getViewFactory().trim()))) {
				Class<?> loadedClass = ClassLoaderHelper.loadClass(abstractRepresentation.getViewFactory());
				if (loadedClass == null) {
					valid = false;
					diagnostic.add(new BasicDiagnostic(Diagnostic.ERROR,
							ExpansionmodelValidator.DIAGNOSTIC_SOURCE,
							ExpansionmodelValidator.ABSTRACT_REPRESENTATION__VALIDATE, "The representation '"
									+ abstractRepresentation.getName() + "' references a view factory that not exist " + abstractRepresentation.getViewFactory(),
							new Object[] { abstractRepresentation }));

				}
			}
		}
		return valid;
	}

	/**
	 * this method is used to verify if provider and editpart can be loaded
	 * 
	 * @param abstractRepresentation
	 * @param diagnostic
	 * @param context
	 * @return
	 */
	public static boolean validate_loadclasses(RepresentationKind abstractRepresentation, DiagnosticChain diagnostic, Map context) {
		boolean valid = true;
		if (diagnostic != null) {
			if (abstractRepresentation.getEditPartQualifiedName() != null || !("".equals(abstractRepresentation.getEditPartQualifiedName().trim()))) {
				Class<?> loadedClass = ClassLoaderHelper.loadClass(abstractRepresentation.getEditPartQualifiedName());
				if (loadedClass == null) {
					valid = false;
					diagnostic.add(new BasicDiagnostic(Diagnostic.ERROR,
							ExpansionmodelValidator.DIAGNOSTIC_SOURCE,
							ExpansionmodelValidator.ABSTRACT_REPRESENTATION__VALIDATE, "The representation '"
									+ abstractRepresentation.getName() + "' references an edit part that does not exist " + abstractRepresentation.getEditPartQualifiedName(),
							new Object[] { abstractRepresentation }));

				}

			}
			if (abstractRepresentation.getViewFactory() != null || !("".equals(abstractRepresentation.getViewFactory().trim()))) {
				Class<?> loadedClass = ClassLoaderHelper.loadClass(abstractRepresentation.getViewFactory());
				if (loadedClass == null) {
					valid = false;
					diagnostic.add(new BasicDiagnostic(Diagnostic.ERROR,
							ExpansionmodelValidator.DIAGNOSTIC_SOURCE,
							ExpansionmodelValidator.ABSTRACT_REPRESENTATION__VALIDATE, "The representation '"
									+ abstractRepresentation.getName() + "' references a view factory that does not exist " + abstractRepresentation.getViewFactory(),
							new Object[] { abstractRepresentation }));

				}
			}
		}
		return valid;
	}

	/**
	 * this method is used to verify if the element type exists.
	 * 
	 * @param abstractRepresentation
	 * @param diagnostic
	 * @param context
	 * @return
	 */
	public static boolean validate_ElementType(Representation abstractRepresentation, DiagnosticChain diagnostic, Map context) {
		boolean valid = true;
		String elementTypeID = abstractRepresentation.getGraphicalElementType();
		if (elementTypeID != null && !elementTypeID.isEmpty()) {
			// ensure that element types model are loaded
			ElementTypeConfigurationTypeRegistry.getInstance();
			final IElementType elementType = ElementTypeRegistry.getInstance().getType(elementTypeID);
			if (elementType == null) {
				valid = false;
				diagnostic.add(new BasicDiagnostic(Diagnostic.ERROR,
						ExpansionmodelValidator.DIAGNOSTIC_SOURCE,
						ExpansionmodelValidator.ABSTRACT_REPRESENTATION__VALIDATE, "The representation '"
								+ abstractRepresentation.getName() + "' references a element type that does not exist " + abstractRepresentation.getGraphicalElementType(),
						new Object[] { abstractRepresentation }));

			}
		}
		return valid;
	}

}

Back to the top