Commit | Line | Data |
---|---|---|
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 | ||
13 | package org.eclipse.linuxtools.tmf.analysis.xml.core.stateprovider; | |
14 | ||
15 | import java.io.File; | |
16 | import java.io.IOException; | |
17 | ||
18 | import javax.xml.parsers.DocumentBuilder; | |
19 | import javax.xml.parsers.DocumentBuilderFactory; | |
20 | import javax.xml.parsers.ParserConfigurationException; | |
21 | ||
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; | |
34 | ||
35 | /** | |
36 | * This is the state change input plug-in for TMF's state system which handles | |
37 | * the XML Format | |
38 | * | |
39 | * @author Florian Wininger | |
40 | */ | |
41 | public class XmlStateProvider extends AbstractTmfStateProvider { | |
42 | ||
43 | private final IPath fFilePath; | |
44 | private final String fStateId; | |
45 | ||
46 | // ------------------------------------------------------------------------ | |
47 | // Constructor | |
48 | // ------------------------------------------------------------------------ | |
49 | ||
50 | /** | |
51 | * Instantiate a new state provider plug-in. | |
52 | * | |
53 | * @param trace | |
54 | * The trace | |
55 | * @param stateid | |
56 | * The state system id, corresponding to the analysis_id | |
57 | * attribute of the state provider element of the XML file | |
58 | * @param file | |
59 | * Path to the XML file containing the state provider definition | |
60 | */ | |
61 | public XmlStateProvider(ITmfTrace trace, String stateid, IPath file) { | |
62 | super(trace, ITmfEvent.class, stateid); | |
63 | fStateId = stateid; | |
64 | fFilePath = file; | |
65 | } | |
66 | ||
67 | /** | |
68 | * Get the state id of the state provider | |
69 | * | |
70 | * @return The state id of the state provider | |
71 | */ | |
72 | public String getStateId() { | |
73 | return fStateId; | |
74 | } | |
75 | ||
76 | // ------------------------------------------------------------------------ | |
77 | // IStateChangeInput | |
78 | // ------------------------------------------------------------------------ | |
79 | ||
80 | @Override | |
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)); | |
86 | } | |
87 | /* | |
88 | * The version attribute is mandatory and XML files that don't validate | |
89 | * with the XSD are ignored, so this should never happen | |
90 | */ | |
91 | throw new IllegalStateException("The state provider XML node should have a version attribute"); //$NON-NLS-1$ | |
92 | } | |
93 | ||
94 | @Override | |
95 | public XmlStateProvider getNewInstance() { | |
96 | return new XmlStateProvider(this.getTrace(), getStateId(), fFilePath); | |
97 | } | |
98 | ||
99 | @Override | |
100 | protected void eventHandle(ITmfEvent event) { | |
101 | /* TODO: This method will be filled a few patches later */ | |
102 | } | |
103 | ||
104 | // ------------------------------------------------------------------------ | |
105 | // Operations | |
106 | // ------------------------------------------------------------------------ | |
107 | ||
108 | /** | |
109 | * Loads the XML file and returns the element at the root of the current | |
110 | * state provider. | |
111 | * | |
112 | * @return The XML node at the root of the state provider | |
113 | */ | |
114 | protected Node loadXMLNode() { | |
115 | ||
116 | try { | |
117 | File XMLFile = fFilePath.toFile(); | |
118 | if (XMLFile == null || !XMLFile.exists() || !XMLFile.isFile()) { | |
119 | return null; | |
120 | } | |
121 | ||
122 | /* Load the XML File */ | |
123 | DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); | |
124 | DocumentBuilder dBuilder; | |
125 | ||
126 | dBuilder = dbFactory.newDocumentBuilder(); | |
127 | Document doc = dBuilder.parse(XMLFile); | |
128 | doc.getDocumentElement().normalize(); | |
129 | ||
130 | /* get the state providers and find the corresponding one */ | |
131 | NodeList stateproviderNodes = doc.getElementsByTagName(TmfXmlStrings.STATE_PROVIDER); | |
132 | Element stateproviderNode = null; | |
133 | ||
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; | |
139 | } | |
140 | } | |
141 | ||
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); | |
147 | } | |
148 | ||
149 | return null; | |
150 | } | |
151 | ||
152 | /** | |
153 | * Function to load the XML file structure | |
154 | */ | |
155 | protected void loadXML() { | |
156 | Element doc = (Element) loadXMLNode(); | |
157 | if (doc == null) { | |
158 | return; | |
159 | } | |
160 | ||
161 | /* TODO: This method will be filled a few patches later */ | |
162 | } | |
163 | ||
164 | } |