1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
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
;
11 import java
.util
.Collections
;
12 import java
.util
.HashMap
;
13 import java
.util
.HashSet
;
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
;
39 * State provider for the pattern analysis
41 * @author Jean-Christian Kouame
43 public class XmlPatternStateProvider
extends AbstractTmfStateProvider
implements IXmlStateSystemContainer
{
45 private final IPath fFilePath
;
47 private final @NonNull String fStateId
;
49 /** Map for defined values */
50 private final Map
<String
, String
> fDefinedValues
= new HashMap
<>();
52 /** List of all Locations */
53 private final @NonNull Set
<@NonNull TmfXmlLocation
> fLocations
;
55 /** Map for stored values */
56 private final @NonNull Map
<@NonNull String
, @NonNull String
> fStoredFields
= new HashMap
<>();
58 private TmfXmlPatternEventHandler fHandler
;
60 private final ISegmentListener fListener
;
62 private final @NonNull TmfXmlScenarioHistoryBuilder fHistoryBuilder
;
68 * The state id, which corresponds to the id of the analysis
69 * defined in the XML file
73 * Listener for segment creation
75 public XmlPatternStateProvider(@NonNull ITmfTrace trace
, @NonNull String stateid
, @Nullable IPath file
, ISegmentListener listener
) {
76 super(trace
, stateid
);
80 fHistoryBuilder
= new TmfXmlScenarioHistoryBuilder();
81 final String pathString
= fFilePath
.makeAbsolute().toOSString();
82 Element doc
= XmlUtils
.getElementInFile(pathString
, TmfXmlStrings
.PATTERN
, fStateId
);
84 fLocations
= new HashSet
<>();
85 Activator
.logError("Failed to find a pattern in " + pathString
); //$NON-NLS-1$
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
);
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
));
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) {
113 TmfXmlLocation location
= modelFactory
.createLocation(element
, this);
114 locations
.add(location
);
116 fLocations
= Collections
.unmodifiableSet(locations
);
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);
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));
134 * Get the defined value associated with a constant
137 * The constant defining this value
138 * @return The actual value corresponding to this constant
140 public String
getDefinedValue(String constant
) {
141 return fDefinedValues
.get(constant
);
145 * Get the stored fiels map
147 * @return The map of stored fields
149 public @NonNull Map
<@NonNull String
, @NonNull String
> getStoredFields() {
150 return fStoredFields
;
154 public int getVersion() {
159 public @NonNull ITmfStateProvider
getNewInstance() {
160 return new XmlPatternStateProvider(getTrace(), getStateId(), fFilePath
, fListener
);
164 * Get the state ID of the provider. It corresponds to the analysis ID.
166 * @return the state Id
168 public @NonNull String
getStateId() {
173 public ITmfStateSystem
getStateSystem() {
174 return getStateSystemBuilder();
178 public @NonNull Iterable
<@NonNull TmfXmlLocation
> getLocations() {
183 protected void eventHandle(@NonNull ITmfEvent event
) {
184 fHandler
.handleEvent(event
);
188 * Get the listerner for segments creation
190 * @return The segment listener
192 public ISegmentListener
getListener() {
197 public void dispose() {
199 fListener
.onNewSegment(XmlPatternSegmentStoreModule
.END_SEGMENT
);
205 * Get the history builder of this analysis
207 * @return The history builder
209 public @NonNull TmfXmlScenarioHistoryBuilder
getHistoryBuilder() {
210 return fHistoryBuilder
;