TMF: Refactor XML model code, using factories to re-use element parsers
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.analysis.xml.core / src / org / eclipse / linuxtools / tmf / analysis / xml / core / stateprovider / XmlStateProvider.java
index 4955d189fc932a04fd977657f8b5523e36f98488..bb98befdc7d3522b3d29ae8bd9d7f9aa7ebb3091 100644 (file)
 
 package org.eclipse.linuxtools.tmf.analysis.xml.core.stateprovider;
 
-import java.io.File;
-import java.io.IOException;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
-
 import org.eclipse.core.runtime.IPath;
-import org.eclipse.linuxtools.internal.tmf.analysis.xml.core.Activator;
-import org.eclipse.linuxtools.internal.tmf.analysis.xml.core.stateprovider.model.TmfXmlEventHandler;
-import org.eclipse.linuxtools.internal.tmf.analysis.xml.core.stateprovider.model.TmfXmlLocation;
-import org.eclipse.linuxtools.tmf.analysis.xml.core.module.Messages;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.linuxtools.tmf.analysis.xml.core.model.ITmfXmlModelFactory;
+import org.eclipse.linuxtools.tmf.analysis.xml.core.model.TmfXmlEventHandler;
+import org.eclipse.linuxtools.tmf.analysis.xml.core.model.TmfXmlLocation;
+import org.eclipse.linuxtools.tmf.analysis.xml.core.model.readwrite.TmfXmlModelFactoryReadWrite;
+import org.eclipse.linuxtools.tmf.analysis.xml.core.module.IXmlStateSystemContainer;
+import org.eclipse.linuxtools.tmf.analysis.xml.core.module.XmlUtils;
 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
 import org.eclipse.linuxtools.tmf.core.statesystem.AbstractTmfStateProvider;
-import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystemBuilder;
+import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystem;
 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
-import org.eclipse.osgi.util.NLS;
-import org.w3c.dom.Document;
 import org.w3c.dom.Element;
-import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
-import org.xml.sax.SAXException;
 
 /**
  * This is the state change input plug-in for TMF's state system which handles
@@ -46,21 +39,10 @@ import org.xml.sax.SAXException;
  *
  * @author Florian Wininger
  */
-public class XmlStateProvider extends AbstractTmfStateProvider {
-
-    /** Root quark, to get values at the root of the state system */
-    public static final int ROOT_QUARK = -1;
-    /**
-     * Error quark, value taken when a state system quark query is in error.
-     *
-     * FIXME: Originally in the code, the -1 was used for both root quark and
-     * return errors, so it has the same value as root quark, but maybe it can
-     * be changed to something else -2? A quark can never be negative
-     */
-    public static final int ERROR_QUARK = -1;
+public class XmlStateProvider extends AbstractTmfStateProvider implements IXmlStateSystemContainer {
 
     private final IPath fFilePath;
-    private final String fStateId;
+    @NonNull private final String fStateId;
 
     /** List of all Event Handlers */
     private final Set<TmfXmlEventHandler> fEventHandlers = new HashSet<>();
@@ -86,16 +68,17 @@ public class XmlStateProvider extends AbstractTmfStateProvider {
      * @param file
      *            Path to the XML file containing the state provider definition
      */
-    public XmlStateProvider(ITmfTrace trace, String stateid, IPath file) {
+    public XmlStateProvider(ITmfTrace trace, @NonNull String stateid, IPath file) {
         super(trace, ITmfEvent.class, stateid);
         fStateId = stateid;
         fFilePath = file;
-        Element doc = loadXMLNode();
+        Element doc = XmlUtils.getElementInFile(fFilePath.makeAbsolute().toOSString(), TmfXmlStrings.STATE_PROVIDER, fStateId);
         if (doc == null) {
             fLocations = new HashSet<>();
             return;
         }
 
+        ITmfXmlModelFactory modelFactory = TmfXmlModelFactoryReadWrite.getInstance();
         /* parser for defined Values */
         NodeList definedStateNodes = doc.getElementsByTagName(TmfXmlStrings.DEFINED_VALUE);
         for (int i = 0; i < definedStateNodes.getLength(); i++) {
@@ -108,7 +91,7 @@ public class XmlStateProvider extends AbstractTmfStateProvider {
         Set<TmfXmlLocation> locations = new HashSet<>();
         for (int i = 0; i < locationNodes.getLength(); i++) {
             Element element = (Element) locationNodes.item(i);
-            TmfXmlLocation location = new TmfXmlLocation(element, this);
+            TmfXmlLocation location = modelFactory.createLocation(element, this);
             locations.add(location);
         }
         fLocations = Collections.unmodifiableSet(locations);
@@ -117,7 +100,7 @@ public class XmlStateProvider extends AbstractTmfStateProvider {
         NodeList nodes = doc.getElementsByTagName(TmfXmlStrings.EVENT_HANDLER);
         for (int i = 0; i < nodes.getLength(); i++) {
             Element element = (Element) nodes.item(i);
-            TmfXmlEventHandler handler = new TmfXmlEventHandler(element, this);
+            TmfXmlEventHandler handler = modelFactory.createEventHandler(element, this);
             fEventHandlers.add(handler);
         }
     }
@@ -127,6 +110,7 @@ public class XmlStateProvider extends AbstractTmfStateProvider {
      *
      * @return The state id of the state provider
      */
+    @NonNull
     public String getStateId() {
         return fStateId;
     }
@@ -137,10 +121,9 @@ public class XmlStateProvider extends AbstractTmfStateProvider {
 
     @Override
     public int getVersion() {
-        Node ssNode = loadXMLNode();
-        if (ssNode instanceof Element) {
-            Element element = (Element) ssNode;
-            return Integer.valueOf(element.getAttribute(TmfXmlStrings.VERSION));
+        Element ssNode = XmlUtils.getElementInFile(fFilePath.makeAbsolute().toOSString(), TmfXmlStrings.STATE_PROVIDER, fStateId);
+        if (ssNode != null) {
+            return Integer.parseInt(ssNode.getAttribute(TmfXmlStrings.VERSION));
         }
         /*
          * The version attribute is mandatory and XML files that don't validate
@@ -165,7 +148,7 @@ public class XmlStateProvider extends AbstractTmfStateProvider {
     }
 
     @Override
-    public ITmfStateSystemBuilder getAssignedStateSystem() {
+    public ITmfStateSystem getStateSystem() {
         return ss;
     }
 
@@ -173,55 +156,7 @@ public class XmlStateProvider extends AbstractTmfStateProvider {
     // Operations
     // ------------------------------------------------------------------------
 
-    /**
-     * Loads the XML file and returns the element at the root of the current
-     * state provider.
-     *
-     * @return The XML node at the root of the state provider
-     */
-    private Element loadXMLNode() {
-
-        try {
-            File XMLFile = fFilePath.toFile();
-            if (XMLFile == null || !XMLFile.exists() || !XMLFile.isFile()) {
-                return null;
-            }
-
-            /* Load the XML File */
-            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
-            DocumentBuilder dBuilder;
-
-            dBuilder = dbFactory.newDocumentBuilder();
-            Document doc = dBuilder.parse(XMLFile);
-            doc.getDocumentElement().normalize();
-
-            /* get the state providers and find the corresponding one */
-            NodeList stateproviderNodes = doc.getElementsByTagName(TmfXmlStrings.STATE_PROVIDER);
-            Element stateproviderNode = null;
-
-            for (int i = 0; i < stateproviderNodes.getLength(); i++) {
-                Element node = (Element) stateproviderNodes.item(i);
-                String analysisid = node.getAttribute(TmfXmlStrings.ID);
-                if (analysisid.equals(fStateId)) {
-                    stateproviderNode = node;
-                }
-            }
-
-            return stateproviderNode;
-        } catch (ParserConfigurationException | IOException e) {
-            Activator.logError("Error loading XML file", e); //$NON-NLS-1$
-        } catch (SAXException e) {
-            Activator.logError(NLS.bind(Messages.XmlUtils_XmlValidationError, e.getLocalizedMessage()), e);
-        }
-
-        return null;
-    }
-
-    /**
-     * Get the list of locations defined in this state provider
-     *
-     * @return The list of {@link TmfXmlLocation}
-     */
+    @Override
     public Iterable<TmfXmlLocation> getLocations() {
         return fLocations;
     }
@@ -237,14 +172,7 @@ public class XmlStateProvider extends AbstractTmfStateProvider {
         return fDefinedValues.get(constant);
     }
 
-    /**
-     * Get the requested value for an attribute. If the value is a pre-defined
-     * value, we return the string corresponding in the defined values map.
-     *
-     * @param name
-     *            the string to get
-     * @return the actual string value
-     */
+    @Override
     public String getAttributeValue(String name) {
         String attribute = name;
         if (attribute.startsWith(TmfXmlStrings.VARIABLE_PREFIX)) {
This page took 0.026199 seconds and 5 git commands to generate.