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 | * Geneviève Bastien - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.analysis.xml.core.module; | |
14 | ||
15 | import java.io.File; | |
16 | import java.io.FileInputStream; | |
17 | import java.io.FileOutputStream; | |
18 | import java.io.IOException; | |
19 | import java.net.URL; | |
20 | import java.nio.channels.FileChannel; | |
0f7276b6 GB |
21 | import java.util.ArrayList; |
22 | import java.util.List; | |
e11e382c FW |
23 | |
24 | import javax.xml.XMLConstants; | |
6d20d989 GB |
25 | import javax.xml.parsers.DocumentBuilder; |
26 | import javax.xml.parsers.DocumentBuilderFactory; | |
27 | import javax.xml.parsers.ParserConfigurationException; | |
e11e382c FW |
28 | import javax.xml.transform.Source; |
29 | import javax.xml.transform.stream.StreamSource; | |
30 | import javax.xml.validation.Schema; | |
31 | import javax.xml.validation.SchemaFactory; | |
32 | import javax.xml.validation.Validator; | |
33 | ||
34 | import org.eclipse.core.runtime.IPath; | |
35 | import org.eclipse.core.runtime.IStatus; | |
6d20d989 | 36 | import org.eclipse.core.runtime.Path; |
e11e382c | 37 | import org.eclipse.core.runtime.Status; |
6d20d989 | 38 | import org.eclipse.jdt.annotation.NonNull; |
e11e382c | 39 | import org.eclipse.linuxtools.internal.tmf.analysis.xml.core.Activator; |
6d20d989 | 40 | import org.eclipse.linuxtools.tmf.analysis.xml.core.stateprovider.TmfXmlStrings; |
e11e382c | 41 | import org.eclipse.osgi.util.NLS; |
6d20d989 | 42 | import org.w3c.dom.Document; |
0f7276b6 GB |
43 | import org.w3c.dom.Element; |
44 | import org.w3c.dom.Node; | |
45 | import org.w3c.dom.NodeList; | |
e11e382c FW |
46 | import org.xml.sax.SAXException; |
47 | import org.xml.sax.SAXParseException; | |
48 | ||
49 | /** | |
50 | * Class containing some utilities for the XML plug-in packages: for example, it | |
51 | * manages the XML files and validates them | |
52 | * | |
53 | * @author Geneviève Bastien | |
54 | */ | |
55 | public class XmlUtils { | |
56 | ||
57 | /** Sub-directory of the plug-in where XML files are stored */ | |
58 | private static final String XML_DIRECTORY = "xml_files"; //$NON-NLS-1$ | |
59 | ||
60 | /** Name of the XSD schema file */ | |
f47f1bbe | 61 | private static final String XSD = "xmlDefinition.xsd"; //$NON-NLS-1$ |
e11e382c FW |
62 | |
63 | /** Make this class non-instantiable */ | |
64 | private XmlUtils() { | |
65 | ||
66 | } | |
67 | ||
68 | /** | |
69 | * Get the path where the XML files are stored. Create it if it does not | |
70 | * exist | |
71 | * | |
72 | * @return path to XML files | |
73 | */ | |
74 | public static IPath getXmlFilesPath() { | |
75 | IPath path = Activator.getDefault().getStateLocation(); | |
76 | path = path.addTrailingSeparator().append(XML_DIRECTORY); | |
77 | ||
78 | /* Check if directory exists, otherwise create it */ | |
79 | File dir = path.toFile(); | |
80 | if (!dir.exists() || !dir.isDirectory()) { | |
81 | dir.mkdirs(); | |
82 | } | |
83 | ||
84 | return path; | |
85 | } | |
86 | ||
87 | /** | |
88 | * Validate the XML file input with the XSD schema | |
89 | * | |
90 | * @param xmlFile | |
91 | * XML file to validate | |
92 | * @return True if the XML validates | |
93 | */ | |
94 | public static IStatus xmlValidate(File xmlFile) { | |
95 | URL url = XmlUtils.class.getResource(XSD); | |
96 | SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); | |
97 | Source xmlSource = new StreamSource(xmlFile); | |
98 | try { | |
99 | Schema schema = schemaFactory.newSchema(url); | |
100 | Validator validator = schema.newValidator(); | |
101 | validator.validate(xmlSource); | |
102 | } catch (SAXParseException e) { | |
103 | String error = NLS.bind(Messages.XmlUtils_XmlParseError, e.getLineNumber(), e.getLocalizedMessage()); | |
104 | Activator.logError(error); | |
0f7276b6 | 105 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, error, e); |
e11e382c FW |
106 | } catch (SAXException e) { |
107 | String error = NLS.bind(Messages.XmlUtils_XmlValidationError, e.getLocalizedMessage()); | |
108 | Activator.logError(error); | |
0f7276b6 | 109 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, error, e); |
e11e382c FW |
110 | } catch (IOException e) { |
111 | String error = Messages.XmlUtils_XmlValidateError; | |
112 | Activator.logError("IO exception occurred", e); //$NON-NLS-1$ | |
0f7276b6 | 113 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, error, e); |
e11e382c FW |
114 | } |
115 | return Status.OK_STATUS; | |
116 | } | |
117 | ||
118 | /** | |
119 | * Adds an XML file to the plugin's path. The XML file should have been | |
120 | * validated using the {@link XmlUtils#xmlValidate(File)} method before | |
121 | * calling this method. | |
122 | * | |
123 | * @param fromFile | |
124 | * The XML file to add | |
125 | * @return Whether the file was successfully added | |
126 | */ | |
127 | public static IStatus addXmlFile(File fromFile) { | |
128 | ||
129 | /* Copy file to path */ | |
130 | File toFile = getXmlFilesPath().addTrailingSeparator().append(fromFile.getName()).toFile(); | |
131 | ||
132 | try { | |
133 | if (!toFile.exists()) { | |
134 | toFile.createNewFile(); | |
135 | } | |
136 | } catch (IOException e) { | |
137 | String error = Messages.XmlUtils_ErrorCopyingFile; | |
138 | Activator.logError(error, e); | |
0f7276b6 | 139 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, error, e); |
e11e382c FW |
140 | } |
141 | ||
142 | try (FileInputStream fis = new FileInputStream(fromFile); | |
143 | FileOutputStream fos = new FileOutputStream(toFile); | |
144 | FileChannel source = fis.getChannel(); | |
145 | FileChannel destination = fos.getChannel();) { | |
146 | destination.transferFrom(source, 0, source.size()); | |
147 | } catch (IOException e) { | |
148 | String error = Messages.XmlUtils_ErrorCopyingFile; | |
149 | Activator.logError(error, e); | |
0f7276b6 | 150 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, error, e); |
e11e382c FW |
151 | } |
152 | return Status.OK_STATUS; | |
153 | } | |
154 | ||
0f7276b6 GB |
155 | /** |
156 | * Get only the XML element children of an XML element. | |
157 | * | |
158 | * @param parent | |
159 | * The parent element to get children from | |
160 | * @return The list of children Element of the parent | |
161 | */ | |
162 | public static List<Element> getChildElements(Element parent) { | |
163 | NodeList childNodes = parent.getChildNodes(); | |
164 | List<Element> childElements = new ArrayList<>(); | |
165 | for (int index = 0; index < childNodes.getLength(); index++) { | |
166 | if (childNodes.item(index).getNodeType() == Node.ELEMENT_NODE) { | |
167 | childElements.add((Element) childNodes.item(index)); | |
168 | } | |
169 | } | |
170 | return childElements; | |
171 | } | |
172 | ||
6d20d989 GB |
173 | /** |
174 | * Get the XML children element of an XML element, but only those of a | |
175 | * certain type | |
176 | * | |
177 | * @param parent | |
178 | * The parent element to get the children from | |
179 | * @param elementTag | |
180 | * The tag of the elements to return | |
181 | * @return The list of children {@link Element} of the parent | |
182 | */ | |
183 | public static List<Element> getChildElements(Element parent, String elementTag) { | |
184 | /* get the state providers and find the corresponding one */ | |
185 | NodeList nodes = parent.getElementsByTagName(elementTag); | |
186 | List<Element> childElements = new ArrayList<>(); | |
187 | ||
188 | for (int i = 0; i < nodes.getLength(); i++) { | |
189 | Element node = (Element) nodes.item(i); | |
66471052 GB |
190 | if (node.getParentNode().equals(parent)) { |
191 | childElements.add(node); | |
192 | } | |
6d20d989 GB |
193 | } |
194 | return childElements; | |
195 | } | |
196 | ||
197 | /** | |
198 | * Return the node element corresponding to the requested type in the file. | |
199 | * | |
200 | * TODO: Nothing prevents from having duplicate type -> id in a same file. | |
201 | * That should not be allowed. If you want an element with the same ID as | |
202 | * another one, it should be in a different file and we should check it at | |
203 | * validation time. | |
204 | * | |
205 | * @param filePath | |
206 | * The absolute path to the XML file | |
207 | * @param elementType | |
208 | * The type of top level element to search for | |
209 | * @param elementId | |
210 | * The ID of the desired element | |
211 | * @return The XML element or <code>null</code> if not found | |
212 | */ | |
213 | public static Element getElementInFile(String filePath, @NonNull String elementType, @NonNull String elementId) { | |
214 | ||
215 | if (filePath == null) { | |
216 | return null; | |
217 | } | |
218 | ||
219 | IPath path = new Path(filePath); | |
220 | File file = path.toFile(); | |
221 | if (file == null || !file.exists() || !file.isFile() || !xmlValidate(file).isOK()) { | |
222 | return null; | |
223 | } | |
224 | ||
225 | try { | |
226 | /* Load the XML File */ | |
227 | DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); | |
228 | DocumentBuilder dBuilder; | |
229 | ||
230 | dBuilder = dbFactory.newDocumentBuilder(); | |
231 | Document doc = dBuilder.parse(file); | |
232 | doc.getDocumentElement().normalize(); | |
233 | ||
234 | /* get the state providers and find the corresponding one */ | |
235 | NodeList nodes = doc.getElementsByTagName(elementType); | |
236 | Element foundNode = null; | |
237 | ||
238 | for (int i = 0; i < nodes.getLength(); i++) { | |
239 | Element node = (Element) nodes.item(i); | |
240 | String id = node.getAttribute(TmfXmlStrings.ID); | |
241 | if (id.equals(elementId)) { | |
242 | foundNode = node; | |
243 | } | |
244 | } | |
245 | return foundNode; | |
246 | } catch (ParserConfigurationException | SAXException | IOException e) { | |
247 | return null; | |
248 | } | |
249 | ||
250 | } | |
251 | ||
e11e382c | 252 | } |