1 | /** |
2 | * <copyright> |
3 | * </copyright> |
4 | * |
5 | * $Id$ |
6 | */ |
7 | package de.uka.ipd.sdq.dsexplore.qml.reader; |
8 | |
9 | import java.io.File; |
10 | import java.util.ArrayList; |
11 | import java.util.List; |
12 | |
13 | import org.eclipse.emf.common.util.Diagnostic; |
14 | import org.eclipse.emf.common.util.URI; |
15 | import org.eclipse.emf.ecore.EObject; |
16 | import org.eclipse.emf.ecore.resource.Resource; |
17 | import org.eclipse.emf.ecore.resource.ResourceSet; |
18 | import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
19 | import org.eclipse.emf.ecore.util.Diagnostician; |
20 | import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl; |
21 | |
22 | import de.uka.ipd.sdq.dsexplore.qml.contract.QMLContract.RefinedQMLContract; |
23 | import de.uka.ipd.sdq.dsexplore.qml.contract.QMLContract.SimpleQMLContract; |
24 | import de.uka.ipd.sdq.dsexplore.qml.contracttype.QMLContractType.QMLContractType; |
25 | import de.uka.ipd.sdq.dsexplore.qml.declarations.QMLDeclarations.QMLDeclaration; |
26 | import de.uka.ipd.sdq.dsexplore.qml.declarations.QMLDeclarations.QMLDeclarations; |
27 | import de.uka.ipd.sdq.dsexplore.qml.declarations.QMLDeclarations.QMLDeclarationsPackage; |
28 | import de.uka.ipd.sdq.dsexplore.qml.profile.QMLProfile.RefinedQMLProfile; |
29 | import de.uka.ipd.sdq.dsexplore.qml.profile.QMLProfile.SimpleQMLProfile; |
30 | |
31 | /** |
32 | * Basic class for reading qml definitions. |
33 | * |
34 | * @author noorshams |
35 | */ |
36 | public class QMLDeclarationsReader { |
37 | /** |
38 | * Assumes 1 QML declaration in the file |
39 | * @param path |
40 | * the file path or URI. |
41 | */ |
42 | public QMLDeclarations getQMLDeclarations(String QMLDeclarationsPath) { |
43 | // Create a resource set to hold the resources. |
44 | // |
45 | ResourceSet resourceSet = new ResourceSetImpl(); |
46 | |
47 | // Register the appropriate resource factory to handle all file |
48 | // extensions. |
49 | // |
50 | resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap() |
51 | .put(Resource.Factory.Registry.DEFAULT_EXTENSION, |
52 | new XMIResourceFactoryImpl()); |
53 | |
54 | // Register the package to ensure it is available during loading. |
55 | // |
56 | resourceSet.getPackageRegistry().put(QMLDeclarationsPackage.eNS_URI, |
57 | QMLDeclarationsPackage.eINSTANCE); |
58 | |
59 | // Construct the URI for the instance file. |
60 | // The argument is treated as a file path only if it denotes an existing |
61 | // file. |
62 | // Otherwise, it's directly treated as a URL. |
63 | // |
64 | File file = new File(QMLDeclarationsPath); |
65 | URI uri = file.isFile() ? URI.createFileURI(file.getAbsolutePath()) |
66 | : URI.createURI(QMLDeclarationsPath); |
67 | |
68 | // Demand load resource for this file. |
69 | // |
70 | Resource resource = resourceSet.getResource(uri, true); |
71 | |
72 | // Validate the contents of the loaded resource. |
73 | // |
74 | |
75 | //List<QMLDeclarations> declarationsList = new ArrayList<QMLDeclarations>(); |
76 | QMLDeclarations returnDeclaration = null; |
77 | |
78 | for (EObject eObject : resource.getContents()) { |
79 | Diagnostic diagnostic = Diagnostician.INSTANCE.validate(eObject); |
80 | if (diagnostic.getSeverity() != Diagnostic.OK) { |
81 | printDiagnostic(diagnostic, ""); |
82 | throw new RuntimeException("QML validation failed: "+diagnostic.getMessage()+". See console output for details."); |
83 | } |
84 | if(!(eObject instanceof QMLDeclarations)) { |
85 | throw new RuntimeException("Check QML definition: The root element of the file must be a QMLDeclarations element."); |
86 | } |
87 | |
88 | returnDeclaration = ((QMLDeclarations)eObject); |
89 | break; |
90 | } |
91 | return returnDeclaration; |
92 | |
93 | } |
94 | |
95 | /** |
96 | * <!-- begin-user-doc --> Prints diagnostics with indentation. <!-- |
97 | * end-user-doc --> |
98 | * |
99 | * @param diagnostic |
100 | * the diagnostic to print. |
101 | * @param indent |
102 | * the indentation for printing. |
103 | */ |
104 | protected void printDiagnostic(Diagnostic diagnostic, String indent) { |
105 | System.out.print(indent); |
106 | System.out.println(diagnostic.getMessage()); |
107 | for (Diagnostic child : diagnostic.getChildren()) { |
108 | printDiagnostic(child, indent + " "); |
109 | } |
110 | } |
111 | |
112 | public List<QMLContractType> getQMLContractTypes(QMLDeclarations declarations) { |
113 | if (declarations == null) { |
114 | return null; |
115 | } |
116 | List<QMLContractType> contractTypes = new ArrayList<QMLContractType>(); |
117 | for (QMLDeclaration declaration : declarations.getQmlDeclarations()) { |
118 | if(declaration instanceof QMLContractType) { |
119 | contractTypes.add((QMLContractType)declaration); |
120 | } |
121 | } |
122 | return contractTypes; |
123 | } |
124 | |
125 | public List<SimpleQMLContract> getSimpleQMLContracts(QMLDeclarations declarations) { |
126 | if (declarations == null) { |
127 | return null; |
128 | } |
129 | List<SimpleQMLContract> simpleContracts = new ArrayList<SimpleQMLContract>(); |
130 | for (QMLDeclaration declaration : declarations.getQmlDeclarations()) { |
131 | if(declaration instanceof SimpleQMLContract) { |
132 | simpleContracts.add((SimpleQMLContract)declaration); |
133 | } |
134 | } |
135 | return simpleContracts; |
136 | } |
137 | |
138 | public List<SimpleQMLProfile> getSimpleQMLProfiles(QMLDeclarations declarations) { |
139 | if (declarations == null) { |
140 | return null; |
141 | } |
142 | List<SimpleQMLProfile> simpleProfiles = new ArrayList<SimpleQMLProfile>(); |
143 | for (QMLDeclaration declaration : declarations.getQmlDeclarations()) { |
144 | if(declaration instanceof SimpleQMLProfile) { |
145 | simpleProfiles.add((SimpleQMLProfile)declaration); |
146 | } |
147 | } |
148 | return simpleProfiles; |
149 | } |
150 | |
151 | public List<RefinedQMLContract> getRefinedQMLContracts(QMLDeclarations declarations) { |
152 | if (declarations == null) { |
153 | return null; |
154 | } |
155 | List<RefinedQMLContract> refinedContracts = new ArrayList<RefinedQMLContract>(); |
156 | for (QMLDeclaration declaration : declarations.getQmlDeclarations()) { |
157 | if(declaration instanceof RefinedQMLContract) { |
158 | refinedContracts.add((RefinedQMLContract)declaration); |
159 | } |
160 | } |
161 | return refinedContracts; |
162 | } |
163 | |
164 | public List<RefinedQMLProfile> getRefinedQMLProfiles(QMLDeclarations declarations) { |
165 | if (declarations == null) { |
166 | return null; |
167 | } |
168 | List<RefinedQMLProfile> simpleProfiles = new ArrayList<RefinedQMLProfile>(); |
169 | for (QMLDeclaration declaration : declarations.getQmlDeclarations()) { |
170 | if(declaration instanceof RefinedQMLProfile) { |
171 | simpleProfiles.add((RefinedQMLProfile)declaration); |
172 | } |
173 | } |
174 | return simpleProfiles; |
175 | } |
176 | |
177 | } |