8374b020a54ec73ebeef9f1ab8b46896c8eb190e
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.analysis.xml.core / src / org / eclipse / tracecompass / internal / tmf / analysis / xml / core / pattern / stateprovider / XmlPatternStateProvider.java
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
11 import java.util.Collections;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.Map;
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;
20 import org.eclipse.tracecompass.common.core.NonNullUtils;
21 import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.Activator;
22 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
23 import org.eclipse.tracecompass.tmf.analysis.xml.core.model.ITmfXmlModelFactory;
24 import org.eclipse.tracecompass.tmf.analysis.xml.core.model.TmfXmlLocation;
25 import org.eclipse.tracecompass.tmf.analysis.xml.core.model.TmfXmlPatternEventHandler;
26 import org.eclipse.tracecompass.tmf.analysis.xml.core.model.TmfXmlScenarioHistoryBuilder;
27 import org.eclipse.tracecompass.tmf.analysis.xml.core.model.readwrite.TmfXmlReadWriteModelFactory;
28 import org.eclipse.tracecompass.tmf.analysis.xml.core.module.IXmlStateSystemContainer;
29 import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlUtils;
30 import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
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;
36 import org.w3c.dom.NodeList;
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
49 /** Map for defined values */
50 private final Map<String, String> fDefinedValues = new HashMap<>();
51
52 /** List of all Locations */
53 private final @NonNull Set<@NonNull TmfXmlLocation> fLocations;
54
55 /** Map for stored values */
56 private final @NonNull Map<@NonNull String, @NonNull String> fStoredFields = new HashMap<>();
57
58 private TmfXmlPatternEventHandler fHandler;
59
60 private final ISegmentListener fListener;
61
62 private final @NonNull TmfXmlScenarioHistoryBuilder fHistoryBuilder;
63
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;
80 fHistoryBuilder = new TmfXmlScenarioHistoryBuilder();
81 final String pathString = fFilePath.makeAbsolute().toOSString();
82 Element doc = XmlUtils.getElementInFile(pathString, TmfXmlStrings.PATTERN, fStateId);
83 if (doc == null) {
84 fLocations = new HashSet<>();
85 Activator.logError("Failed to find a pattern in " + pathString); //$NON-NLS-1$
86 return;
87 }
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);
121 }
122
123 @Override
124 public String getAttributeValue(String name) {
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;
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) {
184 fHandler.handleEvent(event);
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() {
198 waitForEmptyQueue();
199 fListener.onNewSegment(XmlPatternSegmentStoreModule.END_SEGMENT);
200 fHandler.dispose();
201 super.dispose();
202 }
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 }
This page took 0.125917 seconds and 4 git commands to generate.