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 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.analysis
.xml
.core
.module
;
16 import java
.io
.FileInputStream
;
17 import java
.io
.FileOutputStream
;
18 import java
.io
.IOException
;
20 import java
.nio
.channels
.FileChannel
;
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
;
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
;
38 * Class containing some utilities for the XML plug-in packages: for example, it
39 * manages the XML files and validates them
41 * @author Geneviève Bastien
43 public class XmlUtils
{
45 /** Sub-directory of the plug-in where XML files are stored */
46 private static final String XML_DIRECTORY
= "xml_files"; //$NON-NLS-1$
48 /** Name of the XSD schema file */
49 private static final String XSD
= "xmldefinition.xsd"; //$NON-NLS-1$
51 /** Make this class non-instantiable */
57 * Get the path where the XML files are stored. Create it if it does not
60 * @return path to XML files
62 public static IPath
getXmlFilesPath() {
63 IPath path
= Activator
.getDefault().getStateLocation();
64 path
= path
.addTrailingSeparator().append(XML_DIRECTORY
);
66 /* Check if directory exists, otherwise create it */
67 File dir
= path
.toFile();
68 if (!dir
.exists() || !dir
.isDirectory()) {
76 * Validate the XML file input with the XSD schema
79 * XML file to validate
80 * @return True if the XML validates
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
);
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
);
103 return Status
.OK_STATUS
;
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.
112 * The XML file to add
113 * @return Whether the file was successfully added
115 public static IStatus
addXmlFile(File fromFile
) {
117 /* Copy file to path */
118 File toFile
= getXmlFilesPath().addTrailingSeparator().append(fromFile
.getName()).toFile();
121 if (!toFile
.exists()) {
122 toFile
.createNewFile();
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
);
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
);
140 return Status
.OK_STATUS
;