1 /*******************************************************************************
2 * Copyright (c) 2014 École Polytechnique de Montréal
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
10 * Florian Wininger - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.analysis
.xml
.core
.stateprovider
;
16 import java
.io
.IOException
;
18 import javax
.xml
.parsers
.DocumentBuilder
;
19 import javax
.xml
.parsers
.DocumentBuilderFactory
;
20 import javax
.xml
.parsers
.ParserConfigurationException
;
22 import org
.eclipse
.core
.runtime
.IPath
;
23 import org
.eclipse
.linuxtools
.internal
.tmf
.analysis
.xml
.core
.Activator
;
24 import org
.eclipse
.linuxtools
.tmf
.analysis
.xml
.core
.module
.Messages
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.AbstractTmfStateProvider
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
28 import org
.eclipse
.osgi
.util
.NLS
;
29 import org
.w3c
.dom
.Document
;
30 import org
.w3c
.dom
.Element
;
31 import org
.w3c
.dom
.Node
;
32 import org
.w3c
.dom
.NodeList
;
33 import org
.xml
.sax
.SAXException
;
36 * This is the state change input plug-in for TMF's state system which handles
39 * @author Florian Wininger
41 public class XmlStateProvider
extends AbstractTmfStateProvider
{
43 private final IPath fFilePath
;
44 private final String fStateId
;
46 // ------------------------------------------------------------------------
48 // ------------------------------------------------------------------------
51 * Instantiate a new state provider plug-in.
56 * The state system id, corresponding to the analysis_id
57 * attribute of the state provider element of the XML file
59 * Path to the XML file containing the state provider definition
61 public XmlStateProvider(ITmfTrace trace
, String stateid
, IPath file
) {
62 super(trace
, ITmfEvent
.class, stateid
);
68 * Get the state id of the state provider
70 * @return The state id of the state provider
72 public String
getStateId() {
76 // ------------------------------------------------------------------------
78 // ------------------------------------------------------------------------
81 public int getVersion() {
82 Node ssNode
= loadXMLNode();
83 if (ssNode
instanceof Element
) {
84 Element element
= (Element
) ssNode
;
85 return Integer
.valueOf(element
.getAttribute(TmfXmlStrings
.VERSION
));
88 * The version attribute is mandatory and XML files that don't validate
89 * with the XSD are ignored, so this should never happen
91 throw new IllegalStateException("The state provider XML node should have a version attribute"); //$NON-NLS-1$
95 public XmlStateProvider
getNewInstance() {
96 return new XmlStateProvider(this.getTrace(), getStateId(), fFilePath
);
100 protected void eventHandle(ITmfEvent event
) {
101 /* TODO: This method will be filled a few patches later */
104 // ------------------------------------------------------------------------
106 // ------------------------------------------------------------------------
109 * Loads the XML file and returns the element at the root of the current
112 * @return The XML node at the root of the state provider
114 protected Node
loadXMLNode() {
117 File XMLFile
= fFilePath
.toFile();
118 if (XMLFile
== null || !XMLFile
.exists() || !XMLFile
.isFile()) {
122 /* Load the XML File */
123 DocumentBuilderFactory dbFactory
= DocumentBuilderFactory
.newInstance();
124 DocumentBuilder dBuilder
;
126 dBuilder
= dbFactory
.newDocumentBuilder();
127 Document doc
= dBuilder
.parse(XMLFile
);
128 doc
.getDocumentElement().normalize();
130 /* get the state providers and find the corresponding one */
131 NodeList stateproviderNodes
= doc
.getElementsByTagName(TmfXmlStrings
.STATE_PROVIDER
);
132 Element stateproviderNode
= null;
134 for (int i
= 0; i
< stateproviderNodes
.getLength(); i
++) {
135 Element node
= (Element
) stateproviderNodes
.item(i
);
136 String analysisid
= node
.getAttribute(TmfXmlStrings
.ANALYSIS_ID
);
137 if (analysisid
.equals(fStateId
)) {
138 stateproviderNode
= node
;
142 return stateproviderNode
;
143 } catch (ParserConfigurationException
| IOException e
) {
144 Activator
.logError("Error loading XML file", e
); //$NON-NLS-1$
145 } catch (SAXException e
) {
146 Activator
.logError(NLS
.bind(Messages
.XmlUtils_XmlValidationError
, e
.getLocalizedMessage()), e
);
153 * Function to load the XML file structure
155 protected void loadXML() {
156 Element doc
= (Element
) loadXMLNode();
161 /* TODO: This method will be filled a few patches later */