tmf: Add getDocumentFromFile() to XmlUtils
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.analysis.xml.core / src / org / eclipse / tracecompass / internal / tmf / analysis / xml / core / module / XmlUtils.java
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);
This page took 0.025779 seconds and 5 git commands to generate.