/*******************************************************************************
- * Copyright (c) 2014 École Polytechnique de Montréal
+ * Copyright (c) 2014, 2016 École Polytechnique de Montréal
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
package org.eclipse.tracecompass.tmf.analysis.xml.core.module;
+import static org.eclipse.tracecompass.common.core.NonNullUtils.nullToEmptyString;
+
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.URL;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
/** Name of the XSD schema file */
private static final String XSD = "xmlDefinition.xsd"; //$NON-NLS-1$
+ /**
+ * Extension for XML files
+ * @since 2.0
+ */
+ public static final String XML_EXTENSION = "xml"; //$NON-NLS-1$
+
/** Make this class non-instantiable */
private XmlUtils() {
/* Copy file to path */
File toFile = getXmlFilesPath().addTrailingSeparator().append(fromFile.getName()).toFile();
+ return copyXmlFile(fromFile, toFile);
+ }
+
+ /**
+ * List all files under the XML analysis files path. It returns a map where
+ * the key is the file name.
+ *
+ * @return A map with all the XML analysis files
+ * @since 2.0
+ */
+ public static synchronized @NonNull Map<String, File> listFiles() {
+ IPath pathToFiles = XmlUtils.getXmlFilesPath();
+ File folder = pathToFiles.toFile();
+
+ Map<String, File> fileMap = new HashMap<>();
+ if ((folder.isDirectory() && folder.exists())) {
+ File[] listOfFiles = folder.listFiles();
+ if (listOfFiles != null) {
+ for (File file : listOfFiles) {
+ IPath path = new Path(file.getName());
+ if (path.getFileExtension().equals(XML_EXTENSION)) {
+ fileMap.put(file.getName(), file);
+ }
+ }
+ } else {
+ Activator.logError("I/O error occured while accessing files in folder " + folder.getPath()); //$NON-NLS-1$
+ }
+ }
+ return Collections.unmodifiableMap(fileMap);
+ }
+
+ /**
+ * Delete an XML analysis file
+ *
+ * @param name
+ * The XML file to delete
+ * @since 2.0
+ */
+ public static void deleteFile(String name) {
+ Map<String, File> files = listFiles();
+ File file = files.get(name);
+ if (file == null) {
+ return;
+ }
+ file.delete();
+ }
+
+ /**
+ * Export an XML analysis file to an external path
+ *
+ * @param from
+ * The name of the file to export
+ * @param to
+ * The full path of the file to write to
+ * @return Whether the file was successfully exported
+ * @since 2.0
+ */
+ public static IStatus exportXmlFile(String from, String to) {
+
+ /* Copy file to path */
+ File fromFile = getXmlFilesPath().addTrailingSeparator().append(from).toFile();
+
+ if (!fromFile.exists()) {
+ Activator.logError("Failed to find XML analysis file " + fromFile.getName()); //$NON-NLS-1$
+ return Status.CANCEL_STATUS;
+ }
+
+ File toFile = new File(to);
+
+ return copyXmlFile(fromFile, toFile);
+ }
+
+ private static IStatus copyXmlFile(File fromFile, File toFile) {
try {
if (!toFile.exists()) {
toFile.createNewFile();
return Status.OK_STATUS;
}
+ /**
+ * Get the IDs of all the analysis described in a single file
+ *
+ * @param fileName
+ * The file name
+ * @return The list of IDs
+ * @since 2.0
+ */
+ public static List<String> getAnalysisIdsFromFile(String fileName) {
+ List<String> ids = new ArrayList<>();
+ File file = getXmlFilesPath().addTrailingSeparator().append(fileName).toFile();
+ if (file.exists()) {
+ try {
+ /* Load the XML File */
+ DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
+ DocumentBuilder dBuilder;
+ dBuilder = dbFactory.newDocumentBuilder();
+ Document doc = dBuilder.parse(file);
+ doc.getDocumentElement().normalize();
+
+ /* get State Providers modules */
+ NodeList stateproviderNodes = doc.getElementsByTagName(TmfXmlStrings.STATE_PROVIDER);
+ for (int i = 0; i < stateproviderNodes.getLength(); i++) {
+ ids.add(nullToEmptyString(((Element) stateproviderNodes.item(i)).getAttribute(TmfXmlStrings.ID)));
+ }
+
+ /* get patterns modules */
+ NodeList patternNodes = doc.getElementsByTagName(TmfXmlStrings.PATTERN);
+ for (int i = 0; i < patternNodes.getLength(); i++) {
+ ids.add(nullToEmptyString(((Element) patternNodes.item(i)).getAttribute(TmfXmlStrings.ID)));
+ }
+ } catch (ParserConfigurationException | SAXException | IOException e) {
+ Activator.logError("Failed to get analyses IDs from " + fileName); //$NON-NLS-1$
+ }
+ }
+ return ids;
+ }
+
/**
* Get only the XML element children of an XML element.
*
* The parent element to get children from
* @return The list of children Element of the parent
*/
- public static List<@Nullable Element> getChildElements(Element parent) {
+ public static @NonNull List<@Nullable Element> getChildElements(Element parent) {
NodeList childNodes = parent.getChildNodes();
List<@Nullable Element> childElements = new ArrayList<>();
for (int index = 0; index < childNodes.getLength(); index++) {
}
}
-
}