1 | /** |
2 | * |
3 | */ |
4 | package de.uka.ipd.sdq.dsexplore.qml.reader; |
5 | |
6 | import java.io.File; |
7 | import java.util.List; |
8 | import java.util.ArrayList; |
9 | |
10 | import org.eclipse.emf.common.util.Diagnostic; |
11 | import org.eclipse.emf.common.util.URI; |
12 | import org.eclipse.emf.ecore.EObject; |
13 | import org.eclipse.emf.ecore.resource.Resource; |
14 | import org.eclipse.emf.ecore.resource.ResourceSet; |
15 | import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
16 | import org.eclipse.emf.ecore.util.Diagnostician; |
17 | import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl; |
18 | |
19 | import de.uka.ipd.sdq.dsexplore.qml.contracttype.QMLContractType.Dimension; |
20 | import de.uka.ipd.sdq.dsexplore.qml.declarations.QMLDeclarations.QMLDeclarationsPackage; |
21 | |
22 | /** |
23 | * Reads the Dimension defined in the file. Be sure the definition |
24 | * does not contain multiple dimension definitions. |
25 | * |
26 | * @author noorshams |
27 | * |
28 | */ |
29 | public class QMLDimensionReader { |
30 | |
31 | public List<Dimension> getDimensions(String[] dimensionPaths) { |
32 | List<Dimension> dimensions = new ArrayList<Dimension>(); |
33 | for (String dimensionPath : dimensionPaths) { |
34 | dimensions.add(this.getDimension(dimensionPath)); |
35 | } |
36 | return dimensions; |
37 | } |
38 | |
39 | public Dimension getDimension(String dimensionPath) { |
40 | // Create a resource set to hold the resources. |
41 | // |
42 | ResourceSet resourceSet = new ResourceSetImpl(); |
43 | |
44 | // Register the appropriate resource factory to handle all file |
45 | // extensions. |
46 | // |
47 | resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap() |
48 | .put(Resource.Factory.Registry.DEFAULT_EXTENSION, |
49 | new XMIResourceFactoryImpl()); |
50 | |
51 | // Register the package to ensure it is available during loading. |
52 | // |
53 | resourceSet.getPackageRegistry().put(QMLDeclarationsPackage.eNS_URI, |
54 | QMLDeclarationsPackage.eINSTANCE); |
55 | |
56 | // Construct the URI for the instance file. |
57 | // The argument is treated as a file path only if it denotes an existing |
58 | // file. |
59 | // Otherwise, it's directly treated as a URL. |
60 | // |
61 | File file = new File(dimensionPath); |
62 | URI uri = file.isFile() ? URI.createFileURI(file.getAbsolutePath()) |
63 | : URI.createURI(dimensionPath); |
64 | |
65 | // Demand load resource for this file. |
66 | // |
67 | Resource resource = resourceSet.getResource(uri, true); |
68 | |
69 | // Validate the contents of the loaded resource. |
70 | // |
71 | |
72 | Dimension dimension = null; |
73 | |
74 | for (EObject eObject : resource.getContents()) { |
75 | Diagnostic diagnostic = Diagnostician.INSTANCE.validate(eObject); |
76 | if (diagnostic.getSeverity() != Diagnostic.OK) { |
77 | new QMLDeclarationsReader().printDiagnostic(diagnostic, ""); |
78 | throw new RuntimeException("QML validation failed!"); |
79 | } |
80 | if(!(eObject instanceof Dimension)) { |
81 | throw new RuntimeException("Check QML definition! Dimension expected!"); |
82 | } |
83 | dimension = ((Dimension)eObject); |
84 | //XXX: first dimension found will be returned |
85 | break; |
86 | } |
87 | return dimension; |
88 | } |
89 | |
90 | } |