tmf xml: Add a few package-info to tmf.analysis.xml.core.model.*
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.analysis.xml.core / src / org / eclipse / tracecompass / tmf / analysis / xml / core / stateprovider / XmlStateProvider.java
CommitLineData
e11e382c
FW
1/*******************************************************************************
2 * Copyright (c) 2014 École Polytechnique de Montréal
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 * Contributors:
10 * Florian Wininger - Initial API and implementation
11 *******************************************************************************/
12
2bdf0193 13package org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider;
e11e382c 14
0f7276b6
GB
15import java.util.Collections;
16import java.util.HashMap;
17import java.util.HashSet;
12685851 18import java.util.List;
0f7276b6
GB
19import java.util.Map;
20import java.util.Set;
e11e382c 21
e11e382c 22import org.eclipse.core.runtime.IPath;
1d7e62f9 23import org.eclipse.jdt.annotation.NonNull;
e894a508 24import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
2bdf0193
AM
25import org.eclipse.tracecompass.tmf.analysis.xml.core.model.ITmfXmlModelFactory;
26import org.eclipse.tracecompass.tmf.analysis.xml.core.model.TmfXmlEventHandler;
27import org.eclipse.tracecompass.tmf.analysis.xml.core.model.TmfXmlLocation;
28import org.eclipse.tracecompass.tmf.analysis.xml.core.model.readwrite.TmfXmlReadWriteModelFactory;
29import org.eclipse.tracecompass.tmf.analysis.xml.core.module.IXmlStateSystemContainer;
30import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlUtils;
31import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
32import org.eclipse.tracecompass.tmf.core.statesystem.AbstractTmfStateProvider;
33import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
e11e382c 34import org.w3c.dom.Element;
e11e382c 35import org.w3c.dom.NodeList;
e11e382c
FW
36
37/**
38 * This is the state change input plug-in for TMF's state system which handles
39 * the XML Format
40 *
41 * @author Florian Wininger
42 */
1d7e62f9 43public class XmlStateProvider extends AbstractTmfStateProvider implements IXmlStateSystemContainer {
0f7276b6 44
e11e382c 45 private final IPath fFilePath;
1d7e62f9 46 @NonNull private final String fStateId;
e11e382c 47
0f7276b6
GB
48 /** List of all Event Handlers */
49 private final Set<TmfXmlEventHandler> fEventHandlers = new HashSet<>();
50
51 /** List of all Locations */
52 private final Set<TmfXmlLocation> fLocations;
53
54 /** Map for defined values */
55 private final Map<String, String> fDefinedValues = new HashMap<>();
56
e11e382c
FW
57 // ------------------------------------------------------------------------
58 // Constructor
59 // ------------------------------------------------------------------------
60
61 /**
62 * Instantiate a new state provider plug-in.
63 *
64 * @param trace
65 * The trace
66 * @param stateid
67 * The state system id, corresponding to the analysis_id
68 * attribute of the state provider element of the XML file
69 * @param file
70 * Path to the XML file containing the state provider definition
71 */
1d7e62f9 72 public XmlStateProvider(ITmfTrace trace, @NonNull String stateid, IPath file) {
e11e382c
FW
73 super(trace, ITmfEvent.class, stateid);
74 fStateId = stateid;
75 fFilePath = file;
1d7e62f9 76 Element doc = XmlUtils.getElementInFile(fFilePath.makeAbsolute().toOSString(), TmfXmlStrings.STATE_PROVIDER, fStateId);
0f7276b6
GB
77 if (doc == null) {
78 fLocations = new HashSet<>();
79 return;
80 }
81
b1ebf44e 82 ITmfXmlModelFactory modelFactory = TmfXmlReadWriteModelFactory.getInstance();
0f7276b6
GB
83 /* parser for defined Values */
84 NodeList definedStateNodes = doc.getElementsByTagName(TmfXmlStrings.DEFINED_VALUE);
85 for (int i = 0; i < definedStateNodes.getLength(); i++) {
86 Element element = (Element) definedStateNodes.item(i);
87 fDefinedValues.put(element.getAttribute(TmfXmlStrings.NAME), element.getAttribute(TmfXmlStrings.VALUE));
88 }
89
90 /* parser for the locations */
12685851 91 List<Element> childElements = XmlUtils.getChildElements(doc, TmfXmlStrings.LOCATION);
0f7276b6 92 Set<TmfXmlLocation> locations = new HashSet<>();
12685851
GB
93 for (Element element : childElements) {
94 if (element == null) {
95 continue;
96 }
1d7e62f9 97 TmfXmlLocation location = modelFactory.createLocation(element, this);
0f7276b6
GB
98 locations.add(location);
99 }
100 fLocations = Collections.unmodifiableSet(locations);
101
102 /* parser for the event handlers */
12685851
GB
103 childElements = XmlUtils.getChildElements(doc, TmfXmlStrings.EVENT_HANDLER);
104 for (Element element : childElements) {
105 if (element == null) {
106 continue;
107 }
1d7e62f9 108 TmfXmlEventHandler handler = modelFactory.createEventHandler(element, this);
0f7276b6
GB
109 fEventHandlers.add(handler);
110 }
e11e382c
FW
111 }
112
113 /**
114 * Get the state id of the state provider
115 *
116 * @return The state id of the state provider
117 */
1d7e62f9 118 @NonNull
e11e382c
FW
119 public String getStateId() {
120 return fStateId;
121 }
122
123 // ------------------------------------------------------------------------
124 // IStateChangeInput
125 // ------------------------------------------------------------------------
126
127 @Override
128 public int getVersion() {
1d7e62f9
GB
129 Element ssNode = XmlUtils.getElementInFile(fFilePath.makeAbsolute().toOSString(), TmfXmlStrings.STATE_PROVIDER, fStateId);
130 if (ssNode != null) {
131 return Integer.parseInt(ssNode.getAttribute(TmfXmlStrings.VERSION));
e11e382c
FW
132 }
133 /*
134 * The version attribute is mandatory and XML files that don't validate
135 * with the XSD are ignored, so this should never happen
136 */
137 throw new IllegalStateException("The state provider XML node should have a version attribute"); //$NON-NLS-1$
138 }
139
140 @Override
141 public XmlStateProvider getNewInstance() {
142 return new XmlStateProvider(this.getTrace(), getStateId(), fFilePath);
143 }
144
145 @Override
146 protected void eventHandle(ITmfEvent event) {
0f7276b6
GB
147 if (event == null) {
148 return;
149 }
150 for (TmfXmlEventHandler eventHandler : fEventHandlers) {
151 eventHandler.handleEvent(event);
152 }
153 }
154
155 @Override
1d7e62f9 156 public ITmfStateSystem getStateSystem() {
0f7276b6 157 return ss;
e11e382c
FW
158 }
159
160 // ------------------------------------------------------------------------
161 // Operations
162 // ------------------------------------------------------------------------
163
1d7e62f9 164 @Override
0f7276b6
GB
165 public Iterable<TmfXmlLocation> getLocations() {
166 return fLocations;
167 }
e11e382c 168
0f7276b6
GB
169 /**
170 * Get the defined value associated with a constant
171 *
172 * @param constant
173 * The constant defining this value
174 * @return The actual value corresponding to this constant
175 */
176 public String getDefinedValue(String constant) {
177 return fDefinedValues.get(constant);
178 }
179
1d7e62f9 180 @Override
0f7276b6
GB
181 public String getAttributeValue(String name) {
182 String attribute = name;
183 if (attribute.startsWith(TmfXmlStrings.VARIABLE_PREFIX)) {
184 /* search the attribute in the map without the fist character $ */
185 attribute = getDefinedValue(attribute.substring(1));
186 }
187 return attribute;
e11e382c
FW
188 }
189
190}
This page took 0.049565 seconds and 5 git commands to generate.