Commit | Line | Data |
---|---|---|
38e2a2e9 JCK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2016 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | ******************************************************************************/ | |
9 | package org.eclipse.tracecompass.internal.tmf.analysis.xml.core.pattern.stateprovider; | |
10 | ||
3a5f73a1 JCK |
11 | import java.util.Collections; |
12 | import java.util.HashMap; | |
38e2a2e9 | 13 | import java.util.HashSet; |
3a5f73a1 | 14 | import java.util.Map; |
38e2a2e9 JCK |
15 | import java.util.Set; |
16 | ||
17 | import org.eclipse.core.runtime.IPath; | |
18 | import org.eclipse.jdt.annotation.NonNull; | |
19 | import org.eclipse.jdt.annotation.Nullable; | |
3a5f73a1 | 20 | import org.eclipse.tracecompass.common.core.NonNullUtils; |
38e2a2e9 | 21 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.Activator; |
6eca054d GB |
22 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.model.ITmfXmlModelFactory; |
23 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.model.TmfXmlLocation; | |
24 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.model.TmfXmlPatternEventHandler; | |
25 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.model.TmfXmlScenarioHistoryBuilder; | |
26 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.model.readwrite.TmfXmlReadWriteModelFactory; | |
27 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module.IXmlStateSystemContainer; | |
28 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module.XmlUtils; | |
29 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.stateprovider.TmfXmlStrings; | |
38e2a2e9 | 30 | import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem; |
38e2a2e9 JCK |
31 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
32 | import org.eclipse.tracecompass.tmf.core.statesystem.AbstractTmfStateProvider; | |
33 | import org.eclipse.tracecompass.tmf.core.statesystem.ITmfStateProvider; | |
34 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; | |
35 | import org.w3c.dom.Element; | |
3a5f73a1 | 36 | import org.w3c.dom.NodeList; |
38e2a2e9 JCK |
37 | |
38 | /** | |
39 | * State provider for the pattern analysis | |
40 | * | |
41 | * @author Jean-Christian Kouame | |
42 | */ | |
43 | public class XmlPatternStateProvider extends AbstractTmfStateProvider implements IXmlStateSystemContainer { | |
44 | ||
45 | private final IPath fFilePath; | |
46 | ||
47 | private final @NonNull String fStateId; | |
48 | ||
3a5f73a1 JCK |
49 | /** Map for defined values */ |
50 | private final Map<String, String> fDefinedValues = new HashMap<>(); | |
51 | ||
38e2a2e9 JCK |
52 | /** List of all Locations */ |
53 | private final @NonNull Set<@NonNull TmfXmlLocation> fLocations; | |
54 | ||
3a5f73a1 JCK |
55 | /** Map for stored values */ |
56 | private final @NonNull Map<@NonNull String, @NonNull String> fStoredFields = new HashMap<>(); | |
57 | ||
58 | private TmfXmlPatternEventHandler fHandler; | |
59 | ||
38e2a2e9 JCK |
60 | private final ISegmentListener fListener; |
61 | ||
3a5f73a1 JCK |
62 | private final @NonNull TmfXmlScenarioHistoryBuilder fHistoryBuilder; |
63 | ||
38e2a2e9 JCK |
64 | /** |
65 | * @param trace | |
66 | * The active trace | |
67 | * @param stateid | |
68 | * The state id, which corresponds to the id of the analysis | |
69 | * defined in the XML file | |
70 | * @param file | |
71 | * The XML file | |
72 | * @param listener | |
73 | * Listener for segment creation | |
74 | */ | |
75 | public XmlPatternStateProvider(@NonNull ITmfTrace trace, @NonNull String stateid, @Nullable IPath file, ISegmentListener listener) { | |
76 | super(trace, stateid); | |
77 | fStateId = stateid; | |
78 | fFilePath = file; | |
79 | fListener = listener; | |
3a5f73a1 | 80 | fHistoryBuilder = new TmfXmlScenarioHistoryBuilder(); |
38e2a2e9 JCK |
81 | final String pathString = fFilePath.makeAbsolute().toOSString(); |
82 | Element doc = XmlUtils.getElementInFile(pathString, TmfXmlStrings.PATTERN, fStateId); | |
38e2a2e9 | 83 | if (doc == null) { |
3a5f73a1 | 84 | fLocations = new HashSet<>(); |
38e2a2e9 JCK |
85 | Activator.logError("Failed to find a pattern in " + pathString); //$NON-NLS-1$ |
86 | return; | |
87 | } | |
3a5f73a1 JCK |
88 | |
89 | /* parser for defined Fields */ | |
90 | NodeList storedFieldNodes = doc.getElementsByTagName(TmfXmlStrings.STORED_FIELD); | |
91 | for (int i = 0; i < storedFieldNodes.getLength(); i++) { | |
92 | Element element = (Element) storedFieldNodes.item(i); | |
93 | String key = element.getAttribute(TmfXmlStrings.ALIAS); | |
94 | fStoredFields.put(element.getAttribute(TmfXmlStrings.ID), key.isEmpty() ? element.getAttribute(TmfXmlStrings.ID) : key); | |
95 | } | |
96 | ||
97 | /* parser for defined Values */ | |
98 | NodeList definedStateNodes = doc.getElementsByTagName(TmfXmlStrings.DEFINED_VALUE); | |
99 | for (int i = 0; i < definedStateNodes.getLength(); i++) { | |
100 | Element element = (Element) definedStateNodes.item(i); | |
101 | fDefinedValues.put(element.getAttribute(TmfXmlStrings.NAME), element.getAttribute(TmfXmlStrings.VALUE)); | |
102 | } | |
103 | ||
104 | ITmfXmlModelFactory modelFactory = TmfXmlReadWriteModelFactory.getInstance(); | |
105 | /* parser for the locations */ | |
106 | NodeList locationNodes = doc.getElementsByTagName(TmfXmlStrings.LOCATION); | |
107 | final Set<@NonNull TmfXmlLocation> locations = new HashSet<>(); | |
108 | for (int i = 0; i < locationNodes.getLength(); i++) { | |
109 | Element element = (Element) locationNodes.item(i); | |
110 | if (element == null) { | |
111 | continue; | |
112 | } | |
113 | TmfXmlLocation location = modelFactory.createLocation(element, this); | |
114 | locations.add(location); | |
115 | } | |
116 | fLocations = Collections.unmodifiableSet(locations); | |
117 | ||
118 | /* parser for the event handlers */ | |
119 | NodeList nodes = doc.getElementsByTagName(TmfXmlStrings.PATTERN_HANDLER); | |
120 | fHandler = modelFactory.createPatternEventHandler(NonNullUtils.checkNotNull((Element) nodes.item(0)), this); | |
38e2a2e9 JCK |
121 | } |
122 | ||
123 | @Override | |
124 | public String getAttributeValue(String name) { | |
3a5f73a1 JCK |
125 | String attribute = name; |
126 | if (attribute.startsWith(TmfXmlStrings.VARIABLE_PREFIX)) { | |
127 | /* search the attribute in the map without the fist character $ */ | |
128 | attribute = getDefinedValue(attribute.substring(1)); | |
129 | } | |
130 | return attribute; | |
131 | } | |
132 | ||
133 | /** | |
134 | * Get the defined value associated with a constant | |
135 | * | |
136 | * @param constant | |
137 | * The constant defining this value | |
138 | * @return The actual value corresponding to this constant | |
139 | */ | |
140 | public String getDefinedValue(String constant) { | |
141 | return fDefinedValues.get(constant); | |
142 | } | |
143 | ||
144 | /** | |
145 | * Get the stored fiels map | |
146 | * | |
147 | * @return The map of stored fields | |
148 | */ | |
149 | public @NonNull Map<@NonNull String, @NonNull String> getStoredFields() { | |
150 | return fStoredFields; | |
38e2a2e9 JCK |
151 | } |
152 | ||
153 | @Override | |
154 | public int getVersion() { | |
155 | return 0; | |
156 | } | |
157 | ||
158 | @Override | |
159 | public @NonNull ITmfStateProvider getNewInstance() { | |
160 | return new XmlPatternStateProvider(getTrace(), getStateId(), fFilePath, fListener); | |
161 | } | |
162 | ||
163 | /** | |
164 | * Get the state ID of the provider. It corresponds to the analysis ID. | |
165 | * | |
166 | * @return the state Id | |
167 | */ | |
168 | public @NonNull String getStateId() { | |
169 | return fStateId; | |
170 | } | |
171 | ||
172 | @Override | |
173 | public ITmfStateSystem getStateSystem() { | |
174 | return getStateSystemBuilder(); | |
175 | } | |
176 | ||
177 | @Override | |
178 | public @NonNull Iterable<@NonNull TmfXmlLocation> getLocations() { | |
179 | return fLocations; | |
180 | } | |
181 | ||
182 | @Override | |
183 | protected void eventHandle(@NonNull ITmfEvent event) { | |
3a5f73a1 | 184 | fHandler.handleEvent(event); |
38e2a2e9 JCK |
185 | } |
186 | ||
187 | /** | |
188 | * Get the listerner for segments creation | |
189 | * | |
190 | * @return The segment listener | |
191 | */ | |
192 | public ISegmentListener getListener() { | |
193 | return fListener; | |
194 | } | |
195 | ||
196 | @Override | |
197 | public void dispose() { | |
3a5f73a1 | 198 | waitForEmptyQueue(); |
38e2a2e9 | 199 | fListener.onNewSegment(XmlPatternSegmentStoreModule.END_SEGMENT); |
3a5f73a1 | 200 | fHandler.dispose(); |
38e2a2e9 JCK |
201 | super.dispose(); |
202 | } | |
3a5f73a1 JCK |
203 | |
204 | /** | |
205 | * Get the history builder of this analysis | |
206 | * | |
207 | * @return The history builder | |
208 | */ | |
209 | public @NonNull TmfXmlScenarioHistoryBuilder getHistoryBuilder() { | |
210 | return fHistoryBuilder; | |
211 | } | |
212 | } |