tmf: Add getDocumentFromFile() to XmlUtils
authorJean-Christian Kouame <jean-christian.kouame@ericsson.com>
Thu, 21 Apr 2016 14:49:37 +0000 (10:49 -0400)
committerJean-Christian Kouame <jean-christian.kouame@ericsson.com>
Mon, 30 May 2016 21:40:35 +0000 (17:40 -0400)
Change-Id: I318607d8043e6b1925734fff51642d2274b88336
Signed-off-by: Jean-Christian Kouame <jean-christian.kouame@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/71783
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-by: Hudson CI
tmf/org.eclipse.tracecompass.tmf.analysis.xml.core.tests/common/org/eclipse/tracecompass/tmf/analysis/xml/core/tests/common/TmfXmlTestFiles.java
tmf/org.eclipse.tracecompass.tmf.analysis.xml.core/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/core/module/XmlAnalysisModuleSource.java
tmf/org.eclipse.tracecompass.tmf.analysis.xml.core/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/core/module/XmlUtils.java
tmf/org.eclipse.tracecompass.tmf.analysis.xml.ui/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/ui/module/TmfXmlAnalysisOutputSource.java

index b81c68fe4c018601db66c62130d4888ecffd31e3..384b5c27896904bace7b5cd452543326e321e4d2 100644 (file)
@@ -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) {
index 0e7cdff7a5ee103a06ccb45d2e2d52e8c493c32a..8900602e60bd18ec29135db8b7b156aeaad46aaa 100644 (file)
@@ -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);
index 53619e3487656b87ec04016babe73ccaf5f40859..28105b0444bafd01643850e8b1f7ee831f9f7288 100644 (file)
@@ -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);
index 406bf0550af359a5f3cdd2a53473fd1492277098..bf668a74d304977473c5b401f667490486c3f31d 100644 (file)
@@ -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) {
This page took 0.027239 seconds and 5 git commands to generate.