Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f0e5abdd40fccf7a98b02de18d7a6ab4b8395133 (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
/*******************************************************************************
 * Copyright (c) 2015 EclipseSource Muenchen GmbH 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:
 *     Stefan Dirix - initial API and implementation
 *******************************************************************************/
package org.eclipse.papyrus.compare.diagram.tests.util;

import java.util.Map;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.xmi.XMLResource;
import org.eclipse.papyrus.infra.core.resource.AbstractBaseModel;
import org.eclipse.papyrus.infra.core.resource.sasheditor.DiModel;
import org.eclipse.papyrus.infra.gmfdiag.css.resource.CSSNotationModel;
import org.eclipse.papyrus.uml.tools.model.UmlModel;

/**
 * Util class to access the save parameters of Papyrus.
 * 
 * @author Stefan Dirix <sdirix@eclipsesource.com>
 */
public final class PapyrusSaveParameterUtil {

	/**
	 * No public constructor for util classes.
	 */
	private PapyrusSaveParameterUtil() {
	}

	/**
	 * Returns the save parameter for UML model files used by Papyrus.
	 * 
	 * @return A collection of save parameters.
	 */
	public static Map<Object, Object> getUMLSaveParameter() {
		final UMLSaveParametersHelper helper = new UMLSaveParametersHelper();
		return helper.getSaveParametersForTest();
	}

	/**
	 * Returns the save parameter for DI model files used by Papyrus.
	 * 
	 * @return A collection of save parameters.
	 */
	public static Map<Object, Object> getDISaveParameter() {
		final DISaveParametersHelper helper = new DISaveParametersHelper();
		return helper.getSaveParametersForTest();
	}

	/**
	 * Returns the save parameter for Notation model files used by Papyrus.
	 * 
	 * @return A collection of save parameters.
	 */
	public static Map<Object, Object> getNotationSaveParameter() {
		final NotationSaveParametersHelper helper = new NotationSaveParametersHelper();
		return helper.getSaveParametersForTest();
	}

	/**
	 * Returns the default parameter for abstract model files offered by Papyrus.
	 * 
	 * @return A collection of save parameters.
	 */
	public static Map<Object, Object> getDefaultSaveParameter() {
		final DefaultSaveParametersHelper helper = new DefaultSaveParametersHelper();
		return helper.getSaveParametersForTest();
	}

	/**
	 * Helper class to retrieve the save parameters of Papyrus uml models.
	 */
	private static class UMLSaveParametersHelper extends UmlModel {
		public Map<Object, Object> getSaveParametersForTest() {
			return super.getSaveOptions();
		}
	}

	/**
	 * Helper class to retrieve the save parameters of Papyrus di models.
	 */
	private static class DISaveParametersHelper extends DiModel {
		public Map<Object, Object> getSaveParametersForTest() {
			return super.getSaveOptions();
		}
	}

	/**
	 * Helper class to retrieve the save parameters of Papyrus notation models.
	 */
	private static class NotationSaveParametersHelper extends CSSNotationModel {
		public Map<Object, Object> getSaveParametersForTest() {
			// after Papyrus 2019-03 they introduced a field modelKind of type ModelKind and
			// return the default save options only if modelKind == ModelKind.master
			// to remain backwards compatible, we just skip the code in
			// AbstractModelWithSharedResource.getSaveOptions()
			// instead of setting the modelKind to ModelKind.master
			return getDefaultSaveOptions();
		}
	}

	/**
	 * Helper class to retrieve the save parameters of Papyrus abstract base models.
	 */
	private static class DefaultSaveParametersHelper extends AbstractBaseModel {
		@Override
		protected String getModelFileExtension() {
			return null;
		}

		@Override
		public String getIdentifier() {
			return null;
		}

		public Map<Object, Object> getSaveParametersForTest() {
			return super.getSaveOptions();
		}

		// since Papyrus 2.0 we have to implement canPersist
		// we omit @Override on purpose to be backward compatible
		@SuppressWarnings("all")
		public boolean canPersist(EObject eObject) {
			return false;
		}

		// since Papyrus 2.0 we have to implement persist
		// we omit @Override on purpose to be backward compatible
		@SuppressWarnings("all")
		public void persist(EObject object) {
			// no implementation
		}
	}

	/**
	 * Tests if two save parameter maps are equal. Since some options use objects as values an ordinary
	 * {@link Map#equals(Object)} will always return {@code false}. These options are checked manually, the
	 * "normal" options are checked via {@link Map#equals(Object)}
	 * 
	 * @param saveParameters1
	 *            Save parameter map to compare.
	 * @param saveParameters2
	 *            Save parameter map to compare.
	 * @return {@code true} if the given parameter are equal, {@code false} otherwise.
	 */
	public static boolean isEqual(final Map<?, ?> saveParameters1, final Map<?, ?> saveParameters2) {
		// Check URI Handler manually since the value is a dynamic object
		if (saveParameters1.containsKey(XMLResource.OPTION_URI_HANDLER)) {
			final Object uriHandler1 = saveParameters1.get(XMLResource.OPTION_URI_HANDLER);
			final Object uriHandler2 = saveParameters2.get(XMLResource.OPTION_URI_HANDLER);

			if (uriHandler1 != null && uriHandler2 != null) {
				if (!uriHandler1.getClass().equals(uriHandler2.getClass())) {
					return false;
				}
				saveParameters1.remove(XMLResource.OPTION_URI_HANDLER);
				saveParameters2.remove(XMLResource.OPTION_URI_HANDLER);
			}
		}
		return saveParameters1.equals(saveParameters2);
	}
}

Back to the top