From f10786aafc3988ec1e5f7327ee820848c899f019 Mon Sep 17 00:00:00 2001 From: Jean-Christian Kouame Date: Thu, 21 Apr 2016 10:49:37 -0400 Subject: [PATCH] tmf: Add getDocumentFromFile() to XmlUtils Change-Id: I318607d8043e6b1925734fff51642d2274b88336 Signed-off-by: Jean-Christian Kouame Reviewed-on: https://git.eclipse.org/r/71783 Reviewed-by: Matthew Khouzam Reviewed-by: Hudson CI --- .../core/tests/common/TmfXmlTestFiles.java | 8 +--- .../core/module/XmlAnalysisModuleSource.java | 8 +--- .../analysis/xml/core/module/XmlUtils.java | 37 ++++++++++++------- .../ui/module/TmfXmlAnalysisOutputSource.java | 8 +--- 4 files changed, 27 insertions(+), 34 deletions(-) diff --git a/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/common/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/common/TmfXmlTestFiles.java b/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/common/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/common/TmfXmlTestFiles.java index b81c68fe4c..384b5c2789 100644 --- a/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/common/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/common/TmfXmlTestFiles.java +++ b/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/common/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/common/TmfXmlTestFiles.java @@ -17,12 +17,11 @@ import static org.junit.Assert.fail; import java.io.File; import java.io.IOException; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Path; +import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module.XmlUtils; import org.eclipse.tracecompass.tmf.analysis.xml.core.tests.Activator; import org.w3c.dom.Document; import org.xml.sax.SAXException; @@ -85,10 +84,7 @@ public enum TmfXmlTestFiles { /* Initialize the state provider module */ Document doc = null; try { - DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); - DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); - doc = dBuilder.parse(getFile()); - doc.getDocumentElement().normalize(); + doc = XmlUtils.getDocumentFromFile(getFile()); } catch (ParserConfigurationException e) { fail("Xml document parse exception"); } catch (SAXException e) { diff --git a/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/core/module/XmlAnalysisModuleSource.java b/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/core/module/XmlAnalysisModuleSource.java index 0e7cdff7a5..8900602e60 100644 --- a/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/core/module/XmlAnalysisModuleSource.java +++ b/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/core/module/XmlAnalysisModuleSource.java @@ -20,8 +20,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.eclipse.core.runtime.FileLocator; @@ -94,11 +92,7 @@ public class XmlAnalysisModuleSource implements IAnalysisModuleSource { } try { - /* Load the XML File */ - DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); - DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); - Document doc = dBuilder.parse(xmlFile); - doc.getDocumentElement().normalize(); + Document doc = XmlUtils.getDocumentFromFile(xmlFile); /* get State Providers modules */ NodeList stateproviderNodes = doc.getElementsByTagName(TmfXmlStrings.STATE_PROVIDER); diff --git a/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/core/module/XmlUtils.java b/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/core/module/XmlUtils.java index 53619e3487..28105b0444 100644 --- a/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/core/module/XmlUtils.java +++ b/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/core/module/XmlUtils.java @@ -27,7 +27,6 @@ import java.util.List; import java.util.Map; import javax.xml.XMLConstants; -import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Source; @@ -250,12 +249,7 @@ public class XmlUtils { 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(); + Document doc = getDocumentFromFile(file); /* get State Providers modules */ NodeList stateproviderNodes = doc.getElementsByTagName(TmfXmlStrings.STATE_PROVIDER); @@ -275,6 +269,27 @@ public class XmlUtils { return ids; } + /** + * Load the XML File + * + * @param file + * The XML file + * @return The document representing the XML file + * @throws ParserConfigurationException + * if a DocumentBuilder cannot be created + * @throws SAXException + * If any parse errors occur. + * @throws IOException + * If any IO errors occur. + * @since 2.0 + */ + public static Document getDocumentFromFile(File file) throws ParserConfigurationException, SAXException, IOException { + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + Document doc = dbFactory.newDocumentBuilder().parse(file); + doc.getDocumentElement().normalize(); + return doc; + } + /** * Get only the XML element children of an XML element. * @@ -346,13 +361,7 @@ public class XmlUtils { } try { - /* Load the XML File */ - DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); - DocumentBuilder dBuilder; - - dBuilder = dbFactory.newDocumentBuilder(); - Document doc = dBuilder.parse(file); - doc.getDocumentElement().normalize(); + Document doc = getDocumentFromFile(file); /* get the state providers and find the corresponding one */ NodeList nodes = doc.getElementsByTagName(elementType); diff --git a/tmf/org.eclipse.tracecompass.tmf.analysis.xml.ui/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/ui/module/TmfXmlAnalysisOutputSource.java b/tmf/org.eclipse.tracecompass.tmf.analysis.xml.ui/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/ui/module/TmfXmlAnalysisOutputSource.java index 406bf0550a..bf668a74d3 100644 --- a/tmf/org.eclipse.tracecompass.tmf.analysis.xml.ui/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/ui/module/TmfXmlAnalysisOutputSource.java +++ b/tmf/org.eclipse.tracecompass.tmf.analysis.xml.ui/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/ui/module/TmfXmlAnalysisOutputSource.java @@ -17,8 +17,6 @@ import java.io.IOException; import java.util.List; import java.util.Map; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.eclipse.jdt.annotation.NonNull; @@ -151,11 +149,7 @@ public class TmfXmlAnalysisOutputSource implements ITmfNewAnalysisModuleListener } try { - /* Load the XML File */ - DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); - DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); - Document doc = dBuilder.parse(xmlFile); - doc.getDocumentElement().normalize(); + Document doc = XmlUtils.getDocumentFromFile(xmlFile); /* get state provider views if the analysis has state systems */ if (module instanceof ITmfAnalysisModuleWithStateSystems) { -- 2.34.1