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; | |
21 | ||
22 | import javax.xml.XMLConstants; | |
23 | import javax.xml.transform.Source; | |
24 | import javax.xml.transform.stream.StreamSource; | |
25 | import javax.xml.validation.Schema; | |
26 | import javax.xml.validation.SchemaFactory; | |
27 | import javax.xml.validation.Validator; | |
28 | ||
29 | import org.eclipse.core.runtime.IPath; | |
30 | import org.eclipse.core.runtime.IStatus; | |
31 | import org.eclipse.core.runtime.Status; | |
32 | import org.eclipse.linuxtools.internal.tmf.analysis.xml.core.Activator; | |
33 | import org.eclipse.osgi.util.NLS; | |
34 | import org.xml.sax.SAXException; | |
35 | import org.xml.sax.SAXParseException; | |
36 | ||
37 | /** | |
38 | * Class containing some utilities for the XML plug-in packages: for example, it | |
39 | * manages the XML files and validates them | |
40 | * | |
41 | * @author Geneviève Bastien | |
42 | */ | |
43 | public class XmlUtils { | |
44 | ||
45 | /** Sub-directory of the plug-in where XML files are stored */ | |
46 | private static final String XML_DIRECTORY = "xml_files"; //$NON-NLS-1$ | |
47 | ||
48 | /** Name of the XSD schema file */ | |
49 | private static final String XSD = "xmldefinition.xsd"; //$NON-NLS-1$ | |
50 | ||
51 | /** Make this class non-instantiable */ | |
52 | private XmlUtils() { | |
53 | ||
54 | } | |
55 | ||
56 | /** | |
57 | * Get the path where the XML files are stored. Create it if it does not | |
58 | * exist | |
59 | * | |
60 | * @return path to XML files | |
61 | */ | |
62 | public static IPath getXmlFilesPath() { | |
63 | IPath path = Activator.getDefault().getStateLocation(); | |
64 | path = path.addTrailingSeparator().append(XML_DIRECTORY); | |
65 | ||
66 | /* Check if directory exists, otherwise create it */ | |
67 | File dir = path.toFile(); | |
68 | if (!dir.exists() || !dir.isDirectory()) { | |
69 | dir.mkdirs(); | |
70 | } | |
71 | ||
72 | return path; | |
73 | } | |
74 | ||
75 | /** | |
76 | * Validate the XML file input with the XSD schema | |
77 | * | |
78 | * @param xmlFile | |
79 | * XML file to validate | |
80 | * @return True if the XML validates | |
81 | */ | |
82 | public static IStatus xmlValidate(File xmlFile) { | |
83 | URL url = XmlUtils.class.getResource(XSD); | |
84 | SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); | |
85 | Source xmlSource = new StreamSource(xmlFile); | |
86 | try { | |
87 | Schema schema = schemaFactory.newSchema(url); | |
88 | Validator validator = schema.newValidator(); | |
89 | validator.validate(xmlSource); | |
90 | } catch (SAXParseException e) { | |
91 | String error = NLS.bind(Messages.XmlUtils_XmlParseError, e.getLineNumber(), e.getLocalizedMessage()); | |
92 | Activator.logError(error); | |
93 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, error, e); | |
94 | } catch (SAXException e) { | |
95 | String error = NLS.bind(Messages.XmlUtils_XmlValidationError, e.getLocalizedMessage()); | |
96 | Activator.logError(error); | |
97 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, error, e); | |
98 | } catch (IOException e) { | |
99 | String error = Messages.XmlUtils_XmlValidateError; | |
100 | Activator.logError("IO exception occurred", e); //$NON-NLS-1$ | |
101 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, error, e); | |
102 | } | |
103 | return Status.OK_STATUS; | |
104 | } | |
105 | ||
106 | /** | |
107 | * Adds an XML file to the plugin's path. The XML file should have been | |
108 | * validated using the {@link XmlUtils#xmlValidate(File)} method before | |
109 | * calling this method. | |
110 | * | |
111 | * @param fromFile | |
112 | * The XML file to add | |
113 | * @return Whether the file was successfully added | |
114 | */ | |
115 | public static IStatus addXmlFile(File fromFile) { | |
116 | ||
117 | /* Copy file to path */ | |
118 | File toFile = getXmlFilesPath().addTrailingSeparator().append(fromFile.getName()).toFile(); | |
119 | ||
120 | try { | |
121 | if (!toFile.exists()) { | |
122 | toFile.createNewFile(); | |
123 | } | |
124 | } catch (IOException e) { | |
125 | String error = Messages.XmlUtils_ErrorCopyingFile; | |
126 | Activator.logError(error, e); | |
127 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, error, e); | |
128 | } | |
129 | ||
130 | try (FileInputStream fis = new FileInputStream(fromFile); | |
131 | FileOutputStream fos = new FileOutputStream(toFile); | |
132 | FileChannel source = fis.getChannel(); | |
133 | FileChannel destination = fos.getChannel();) { | |
134 | destination.transferFrom(source, 0, source.size()); | |
135 | } catch (IOException e) { | |
136 | String error = Messages.XmlUtils_ErrorCopyingFile; | |
137 | Activator.logError(error, e); | |
138 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, error, e); | |
139 | } | |
140 | return Status.OK_STATUS; | |
141 | } | |
142 | ||
143 | } |