ctf: move ArrayDefinition.isString to be part of the declaration
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.analysis.xml.core / src / org / eclipse / linuxtools / tmf / analysis / xml / core / module / XmlUtils.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 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
12
13package org.eclipse.linuxtools.tmf.analysis.xml.core.module;
14
15import java.io.File;
16import java.io.FileInputStream;
17import java.io.FileOutputStream;
18import java.io.IOException;
19import java.net.URL;
20import java.nio.channels.FileChannel;
0f7276b6
GB
21import java.util.ArrayList;
22import java.util.List;
e11e382c
FW
23
24import javax.xml.XMLConstants;
6d20d989
GB
25import javax.xml.parsers.DocumentBuilder;
26import javax.xml.parsers.DocumentBuilderFactory;
27import javax.xml.parsers.ParserConfigurationException;
e11e382c
FW
28import javax.xml.transform.Source;
29import javax.xml.transform.stream.StreamSource;
30import javax.xml.validation.Schema;
31import javax.xml.validation.SchemaFactory;
32import javax.xml.validation.Validator;
33
34import org.eclipse.core.runtime.IPath;
35import org.eclipse.core.runtime.IStatus;
6d20d989 36import org.eclipse.core.runtime.Path;
e11e382c 37import org.eclipse.core.runtime.Status;
6d20d989 38import org.eclipse.jdt.annotation.NonNull;
e11e382c 39import org.eclipse.linuxtools.internal.tmf.analysis.xml.core.Activator;
6d20d989 40import org.eclipse.linuxtools.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
e11e382c 41import org.eclipse.osgi.util.NLS;
6d20d989 42import org.w3c.dom.Document;
0f7276b6
GB
43import org.w3c.dom.Element;
44import org.w3c.dom.Node;
45import org.w3c.dom.NodeList;
e11e382c
FW
46import org.xml.sax.SAXException;
47import 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 */
55public 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);
190 childElements.add(node);
191 }
192 return childElements;
193 }
194
195 /**
196 * Return the node element corresponding to the requested type in the file.
197 *
198 * TODO: Nothing prevents from having duplicate type -> id in a same file.
199 * That should not be allowed. If you want an element with the same ID as
200 * another one, it should be in a different file and we should check it at
201 * validation time.
202 *
203 * @param filePath
204 * The absolute path to the XML file
205 * @param elementType
206 * The type of top level element to search for
207 * @param elementId
208 * The ID of the desired element
209 * @return The XML element or <code>null</code> if not found
210 */
211 public static Element getElementInFile(String filePath, @NonNull String elementType, @NonNull String elementId) {
212
213 if (filePath == null) {
214 return null;
215 }
216
217 IPath path = new Path(filePath);
218 File file = path.toFile();
219 if (file == null || !file.exists() || !file.isFile() || !xmlValidate(file).isOK()) {
220 return null;
221 }
222
223 try {
224 /* Load the XML File */
225 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
226 DocumentBuilder dBuilder;
227
228 dBuilder = dbFactory.newDocumentBuilder();
229 Document doc = dBuilder.parse(file);
230 doc.getDocumentElement().normalize();
231
232 /* get the state providers and find the corresponding one */
233 NodeList nodes = doc.getElementsByTagName(elementType);
234 Element foundNode = null;
235
236 for (int i = 0; i < nodes.getLength(); i++) {
237 Element node = (Element) nodes.item(i);
238 String id = node.getAttribute(TmfXmlStrings.ID);
239 if (id.equals(elementId)) {
240 foundNode = node;
241 }
242 }
243 return foundNode;
244 } catch (ParserConfigurationException | SAXException | IOException e) {
245 return null;
246 }
247
248 }
249
e11e382c 250}
This page took 0.037203 seconds and 5 git commands to generate.