1 | package de.ugoe.cs.cpdp.decentApp; |
---|
2 | |
---|
3 | import java.io.File; |
---|
4 | import java.io.IOException; |
---|
5 | import java.util.HashMap; |
---|
6 | import java.util.Map; |
---|
7 | |
---|
8 | import org.eclipse.emf.common.util.BasicDiagnostic; |
---|
9 | import org.eclipse.emf.common.util.Diagnostic; |
---|
10 | import org.eclipse.emf.common.util.EList; |
---|
11 | import org.eclipse.emf.common.util.URI; |
---|
12 | import org.eclipse.emf.ecore.EObject; |
---|
13 | import org.eclipse.emf.ecore.EOperation; |
---|
14 | import org.eclipse.emf.ecore.EPackage; |
---|
15 | import org.eclipse.emf.ecore.EStructuralFeature; |
---|
16 | import org.eclipse.emf.ecore.EValidator; |
---|
17 | import org.eclipse.emf.ecore.resource.Resource; |
---|
18 | import org.eclipse.emf.ecore.resource.ResourceSet; |
---|
19 | import org.eclipse.emf.ecore.resource.impl.BinaryResourceImpl; |
---|
20 | import org.eclipse.emf.ecore.resource.impl.ResourceFactoryImpl; |
---|
21 | import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
---|
22 | import org.eclipse.emf.ecore.util.Diagnostician; |
---|
23 | import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl; |
---|
24 | import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl; |
---|
25 | import org.eclipse.ocl.common.OCLConstants; |
---|
26 | import org.eclipse.ocl.ecore.delegate.OCLInvocationDelegateFactory; |
---|
27 | import org.eclipse.ocl.ecore.delegate.OCLSettingDelegateFactory; |
---|
28 | import org.eclipse.ocl.ecore.delegate.OCLValidationDelegateFactory; |
---|
29 | |
---|
30 | /** |
---|
31 | * Class for handling different EMF Ressources |
---|
32 | * |
---|
33 | * @author Philip Makedonski |
---|
34 | * |
---|
35 | */ |
---|
36 | public class ResourceTool { |
---|
37 | |
---|
38 | protected ResourceFactoryImpl resourceFactory = new XMIResourceFactoryImpl(); |
---|
39 | |
---|
40 | /** |
---|
41 | * Constructor |
---|
42 | * @param loggedClass |
---|
43 | */ |
---|
44 | public ResourceTool(String loggedClass) { |
---|
45 | System.setProperty("org.slf4j.simpleLogger.logFile","validation.log"); |
---|
46 | System.setProperty("org.slf4j.simpleLogger.logFile","System.out"); |
---|
47 | } |
---|
48 | |
---|
49 | /** |
---|
50 | * Initializes the validator |
---|
51 | */ |
---|
52 | protected void initializeValidator() { |
---|
53 | // OCL.initialize(null); |
---|
54 | String oclDelegateURI = OCLConstants.OCL_DELEGATE_URI+"/Pivot"; |
---|
55 | |
---|
56 | EOperation.Internal.InvocationDelegate.Factory.Registry.INSTANCE.put(oclDelegateURI, |
---|
57 | new OCLInvocationDelegateFactory(oclDelegateURI)); |
---|
58 | EStructuralFeature.Internal.SettingDelegate.Factory.Registry.INSTANCE.put(oclDelegateURI, |
---|
59 | new OCLSettingDelegateFactory(oclDelegateURI)); |
---|
60 | EValidator.ValidationDelegate.Registry.INSTANCE.put(oclDelegateURI, |
---|
61 | new OCLValidationDelegateFactory(oclDelegateURI)); |
---|
62 | |
---|
63 | // EStructuralFeature.Internal.SettingDelegate.Factory.Registry.INSTANCE.put(oclDelegateURI, |
---|
64 | // new OCLSettingDelegateFactory.Global()); |
---|
65 | // QueryDelegate.Factory.Registry.INSTANCE.put(oclDelegateURI, new OCLQueryDelegateFactory.Global()); |
---|
66 | |
---|
67 | } |
---|
68 | |
---|
69 | /** |
---|
70 | * Validates the ressource |
---|
71 | * @param resource to validate |
---|
72 | */ |
---|
73 | public void validateResource(Resource resource) { |
---|
74 | BasicDiagnostic diagnostics = new BasicDiagnostic(); |
---|
75 | boolean valid = true; |
---|
76 | for (EObject eo : resource.getContents()) |
---|
77 | { |
---|
78 | Map<Object, Object> context = new HashMap<Object, Object>(); |
---|
79 | boolean validationResult = Diagnostician.INSTANCE.validate(eo, diagnostics, context); |
---|
80 | showDiagnostics(diagnostics, ""); |
---|
81 | valid &= validationResult; |
---|
82 | } |
---|
83 | |
---|
84 | if (!valid){ |
---|
85 | System.out.println("Problem with validation!"); |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | * Output method for showing diagnostics for different ressources |
---|
91 | * @param diagnostics |
---|
92 | * @param indent |
---|
93 | */ |
---|
94 | protected void showDiagnostics(Diagnostic diagnostics, String indent) { |
---|
95 | indent+=" "; |
---|
96 | for (Diagnostic d : diagnostics.getChildren()){ |
---|
97 | System.out.println(indent+d.getSource()); |
---|
98 | System.out.println(indent+" "+d.getMessage()); |
---|
99 | showDiagnostics(d,indent); |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | /** |
---|
105 | * Loads a ressource from XMI |
---|
106 | * @param inputPath path to the xmi |
---|
107 | * @param extension of the ressource to load |
---|
108 | * @param p the given EPackage |
---|
109 | * @return |
---|
110 | */ |
---|
111 | //TODO: workarounds copied from respective methods without EPackage parameter |
---|
112 | @SuppressWarnings({ "rawtypes", "unchecked" }) |
---|
113 | public Resource loadResourceFromXMI(String inputPath, String extension, EPackage p) { |
---|
114 | Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE; |
---|
115 | Map<String, Object> m = reg.getExtensionToFactoryMap(); |
---|
116 | m.put(extension, resourceFactory); |
---|
117 | ResourceSet resSetIn = new ResourceSetImpl(); |
---|
118 | //critical part |
---|
119 | resSetIn.getPackageRegistry().put(p.getNsURI(), p); |
---|
120 | |
---|
121 | Resource inputResource = resSetIn.createResource(URI.createURI(inputPath)); |
---|
122 | try { |
---|
123 | Map options = new HashMap<>(); |
---|
124 | options.put(XMIResourceImpl.OPTION_DEFER_IDREF_RESOLUTION, Boolean.TRUE); |
---|
125 | // options.put(XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF, XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF_DISCARD); |
---|
126 | inputResource.load(options); |
---|
127 | } catch (IOException e) { |
---|
128 | e.printStackTrace(); |
---|
129 | } |
---|
130 | return inputResource; |
---|
131 | } |
---|
132 | |
---|
133 | /** |
---|
134 | * Loads a ressource from XMI |
---|
135 | * @param inputPath path to the xmi |
---|
136 | * @param extension of the ressource to load |
---|
137 | * @return |
---|
138 | */ |
---|
139 | |
---|
140 | @SuppressWarnings({ "rawtypes", "unchecked" }) |
---|
141 | public Resource loadResourceFromXMI(String inputPath, String extension) { |
---|
142 | Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE; |
---|
143 | Map<String, Object> m = reg.getExtensionToFactoryMap(); |
---|
144 | m.put(extension, resourceFactory); |
---|
145 | ResourceSet resSetIn = new ResourceSetImpl(); |
---|
146 | Resource inputResource = resSetIn.createResource(URI.createURI(inputPath)); |
---|
147 | try { |
---|
148 | Map options = new HashMap<>(); |
---|
149 | options.put(XMIResourceImpl.OPTION_DEFER_IDREF_RESOLUTION, Boolean.TRUE); |
---|
150 | // options.put(XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF, XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF_DISCARD); |
---|
151 | inputResource.load(options); |
---|
152 | } catch (IOException e) { |
---|
153 | e.printStackTrace(); |
---|
154 | } |
---|
155 | return inputResource; |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | * Gets a ressource from a binary form |
---|
160 | * @param inputPath path to the binary |
---|
161 | * @param extension of the model to load |
---|
162 | * @param p EPackage to put the loaded ressource in |
---|
163 | * @return |
---|
164 | */ |
---|
165 | public Resource getResourceFromBinary(String inputPath, String extension, EPackage p) { |
---|
166 | Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE; |
---|
167 | Map<String, Object> m = reg.getExtensionToFactoryMap(); |
---|
168 | m.put(extension, new Resource.Factory() { |
---|
169 | |
---|
170 | @Override |
---|
171 | public Resource createResource(URI uri) { |
---|
172 | return new BinaryResourceImpl(uri); |
---|
173 | } |
---|
174 | |
---|
175 | }); |
---|
176 | |
---|
177 | ResourceSet resSetIn = new ResourceSetImpl(); |
---|
178 | //critical part |
---|
179 | resSetIn.getPackageRegistry().put(p.getNsURI(), p); |
---|
180 | |
---|
181 | Resource inputResource = resSetIn.createResource(URI.createURI(inputPath)); |
---|
182 | return inputResource; |
---|
183 | } |
---|
184 | |
---|
185 | |
---|
186 | /** |
---|
187 | * Loads a ressource from a binary form |
---|
188 | * @param inputPath path to the binary |
---|
189 | * @param extension of the model to load |
---|
190 | * @param p EPackage to put the loaded ressource in |
---|
191 | * @return |
---|
192 | */ |
---|
193 | //TODO: workarounds copied from respective methods without EPackage parameter |
---|
194 | @SuppressWarnings({ "rawtypes" }) |
---|
195 | public Resource loadResourceFromBinary(String inputPath, String extension, EPackage p) { |
---|
196 | Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE; |
---|
197 | Map<String, Object> m = reg.getExtensionToFactoryMap(); |
---|
198 | m.put(extension, new Resource.Factory() { |
---|
199 | |
---|
200 | @Override |
---|
201 | public Resource createResource(URI uri) { |
---|
202 | return new BinaryResourceImpl(uri); |
---|
203 | } |
---|
204 | |
---|
205 | }); |
---|
206 | |
---|
207 | ResourceSet resSetIn = new ResourceSetImpl(); |
---|
208 | //critical part |
---|
209 | resSetIn.getPackageRegistry().put(p.getNsURI(), p); |
---|
210 | |
---|
211 | Resource inputResource = resSetIn.createResource(URI.createURI(inputPath)); |
---|
212 | if (new File(inputPath).exists()) { |
---|
213 | |
---|
214 | try { |
---|
215 | Map options = new HashMap<>(); |
---|
216 | // options.put(BinaryResourceImpl.OPTION_DEFER_IDREF_RESOLUTION, Boolean.TRUE); |
---|
217 | // options.put(BinaryResourceImpl.OPTION_DEFER_IDREF_RESOLUTION, Boolean.TRUE); |
---|
218 | // options.put(XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF, XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF_DISCARD); |
---|
219 | inputResource.load(options); |
---|
220 | } catch (IOException e) { |
---|
221 | e.printStackTrace(); |
---|
222 | } |
---|
223 | } |
---|
224 | return inputResource; |
---|
225 | } |
---|
226 | |
---|
227 | /** |
---|
228 | * Loads a ressource from a binary form |
---|
229 | * |
---|
230 | * @param inputPath path to the binary |
---|
231 | * @param extension of the model to load |
---|
232 | * @return |
---|
233 | */ |
---|
234 | @SuppressWarnings({ "rawtypes" }) |
---|
235 | public Resource loadResourceFromBinary(String inputPath, String extension) { |
---|
236 | Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE; |
---|
237 | Map<String, Object> m = reg.getExtensionToFactoryMap(); |
---|
238 | m.put(extension, new Resource.Factory() { |
---|
239 | |
---|
240 | @Override |
---|
241 | public Resource createResource(URI uri) { |
---|
242 | return new BinaryResourceImpl(uri); |
---|
243 | } |
---|
244 | |
---|
245 | }); |
---|
246 | |
---|
247 | ResourceSet resSetIn = new ResourceSetImpl(); |
---|
248 | Resource inputResource = resSetIn.createResource(URI.createURI(inputPath)); |
---|
249 | try { |
---|
250 | Map options = new HashMap<>(); |
---|
251 | // options.put(XMIResourceImpl.OPTION_DEFER_IDREF_RESOLUTION, Boolean.TRUE); |
---|
252 | // options.put(XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF, XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF_DISCARD); |
---|
253 | inputResource.load(options); |
---|
254 | } catch (IOException e) { |
---|
255 | e.printStackTrace(); |
---|
256 | } |
---|
257 | return inputResource; |
---|
258 | } |
---|
259 | |
---|
260 | /** |
---|
261 | * Stores the binary resource contents to a given path |
---|
262 | * |
---|
263 | * @param contents EList of different EObjects to store |
---|
264 | * @param outputPath path to store to |
---|
265 | * @param extension of the model to store |
---|
266 | */ |
---|
267 | @SuppressWarnings({ "rawtypes" }) |
---|
268 | public void storeBinaryResourceContents(EList<EObject> contents, String outputPath, String extension) { |
---|
269 | Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE; |
---|
270 | Map<String, Object> m = reg.getExtensionToFactoryMap(); |
---|
271 | m.put(extension, new Resource.Factory() { |
---|
272 | |
---|
273 | @Override |
---|
274 | public Resource createResource(URI uri) { |
---|
275 | return new BinaryResourceImpl(uri); |
---|
276 | } |
---|
277 | |
---|
278 | }); |
---|
279 | |
---|
280 | ResourceSet resSet = new ResourceSetImpl(); |
---|
281 | Resource outputResource = resSet.createResource(URI.createURI(outputPath)); |
---|
282 | outputResource.getContents().addAll(contents); |
---|
283 | try { |
---|
284 | Map options = new HashMap<>(); |
---|
285 | // options.put(XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF, XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF_DISCARD); |
---|
286 | outputResource.save(options); |
---|
287 | } catch (IOException e) { |
---|
288 | e.printStackTrace(); |
---|
289 | } |
---|
290 | } |
---|
291 | |
---|
292 | /** |
---|
293 | * Stores the resource contents to a given path |
---|
294 | * |
---|
295 | * @param contents EList of different EObjects to store |
---|
296 | * @param outputPath path to store to |
---|
297 | * @param extension of the model to store |
---|
298 | */ |
---|
299 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
---|
300 | public void storeResourceContents(EList<EObject> contents, String outputPath, String extension) { |
---|
301 | //TODO: duplicated from loadResourceFromXMI => move to a more appropriate location |
---|
302 | Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE; |
---|
303 | Map<String, Object> m = reg.getExtensionToFactoryMap(); |
---|
304 | m.put(extension, resourceFactory); |
---|
305 | |
---|
306 | ResourceSet resSet = new ResourceSetImpl(); |
---|
307 | Resource outputResource = resSet.createResource(URI.createURI(outputPath)); |
---|
308 | outputResource.getContents().addAll(contents); |
---|
309 | try { |
---|
310 | Map options = new HashMap<>(); |
---|
311 | options.put(XMIResourceImpl.OPTION_ENCODING, "UTF-8"); |
---|
312 | // options.put(XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF, XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF_DISCARD); |
---|
313 | outputResource.save(options); |
---|
314 | } catch (IOException e) { |
---|
315 | e.printStackTrace(); |
---|
316 | } |
---|
317 | } |
---|
318 | |
---|
319 | |
---|
320 | } |
---|