From 9af1f06e5027c87af9f5f16ee7bf31a518c5d7b4 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Fri, 5 Oct 2018 10:47:06 +0200
Subject: [PATCH] parsing results/products returns object instead of injecting
 them

---
 .../reaction/ReactionFromXml.java             | 276 +++++++++---------
 1 file changed, 138 insertions(+), 138 deletions(-)

diff --git a/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/reaction/ReactionFromXml.java b/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/reaction/ReactionFromXml.java
index 5d9907db3a..32b7f91967 100644
--- a/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/reaction/ReactionFromXml.java
+++ b/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/reaction/ReactionFromXml.java
@@ -357,146 +357,151 @@ public class ReactionFromXml extends XmlParser {
    * 
    */
   private Reaction parseReactionExtension(Reaction result, Node node, Model model) throws ReactionParserException {
-    NodeList extensionNodes = node.getChildNodes();
-    LineProperties line = null;
-    ConnectScheme connectScheme = null;
-    EditPoints points = null;
-    Node reactantsLinkNode = null;
-    Node productsLinkNode = null;
-    Node modifiersLinkNode = null;
-    Node gateMembers = null;
-    String type = null;
-    for (int y = 0; y < extensionNodes.getLength(); y++) {
-      Node nodeReaction = extensionNodes.item(y);
-      if (nodeReaction.getNodeType() == Node.ELEMENT_NODE) {
-        if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:reactionType")) {
-          type = getNodeValue(nodeReaction);
-        } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:baseReactants")) {
-          NodeList reactantsNodes = nodeReaction.getChildNodes();
-          for (int z = 0; z < reactantsNodes.getLength(); z++) {
-            Node reactantNode = reactantsNodes.item(z);
-            if (reactantNode.getNodeType() == Node.ELEMENT_NODE) {
-              if (reactantNode.getNodeName().equalsIgnoreCase("celldesigner:baseReactant")) {
-                parseBaseReactant(result, reactantNode, model);
-              } else {
-                throw new ReactionParserException(
-                    "Unknown element of celldesigner:baseReactants: " + node.getNodeName(), result);
+    try {
+      NodeList extensionNodes = node.getChildNodes();
+      LineProperties line = null;
+      ConnectScheme connectScheme = null;
+      EditPoints points = null;
+      Node reactantsLinkNode = null;
+      Node productsLinkNode = null;
+      Node modifiersLinkNode = null;
+      Node gateMembers = null;
+      String type = null;
+      for (int y = 0; y < extensionNodes.getLength(); y++) {
+        Node nodeReaction = extensionNodes.item(y);
+        if (nodeReaction.getNodeType() == Node.ELEMENT_NODE) {
+          if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:reactionType")) {
+            type = getNodeValue(nodeReaction);
+          } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:baseReactants")) {
+            NodeList reactantsNodes = nodeReaction.getChildNodes();
+            for (int z = 0; z < reactantsNodes.getLength(); z++) {
+              Node reactantNode = reactantsNodes.item(z);
+              if (reactantNode.getNodeType() == Node.ELEMENT_NODE) {
+                if (reactantNode.getNodeName().equalsIgnoreCase("celldesigner:baseReactant")) {
+                  result.addReactant(parseBaseReactant(reactantNode, model));
+                } else {
+                  throw new ReactionParserException(
+                      "Unknown element of celldesigner:baseReactants: " + node.getNodeName(), result);
+                }
               }
             }
-          }
-        } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:baseProducts")) {
-          NodeList reactantsNodes = nodeReaction.getChildNodes();
-          for (int z = 0; z < reactantsNodes.getLength(); z++) {
-            Node reactantNode = reactantsNodes.item(z);
-            if (reactantNode.getNodeType() == Node.ELEMENT_NODE) {
-              if (reactantNode.getNodeName().equalsIgnoreCase("celldesigner:baseProduct")) {
-                parseBaseProduct(model, result, reactantNode);
-              } else {
-                throw new ReactionParserException("Unknown element of celldesigner:baseProducts: " + node.getNodeName(),
-                    result);
+          } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:baseProducts")) {
+            NodeList reactantsNodes = nodeReaction.getChildNodes();
+            for (int z = 0; z < reactantsNodes.getLength(); z++) {
+              Node reactantNode = reactantsNodes.item(z);
+              if (reactantNode.getNodeType() == Node.ELEMENT_NODE) {
+                if (reactantNode.getNodeName().equalsIgnoreCase("celldesigner:baseProduct")) {
+                  result.addProduct(parseBaseProduct(model, reactantNode));
+                } else {
+                  throw new ReactionParserException(
+                      "Unknown element of celldesigner:baseProducts: " + node.getNodeName(), result);
+                }
               }
             }
+          } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:line")) {
+            line = getLineProperties(nodeReaction);
+          } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:connectScheme")) {
+            try {
+              connectScheme = parseConnectScheme(nodeReaction);
+            } catch (InvalidXmlSchemaException e) {
+              throw new ReactionParserException(result, e);
+            }
+          } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:listOfReactantLinks")) {
+            reactantsLinkNode = nodeReaction;
+          } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:listOfProductLinks")) {
+            productsLinkNode = nodeReaction;
+          } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:listOfModification")) {
+            modifiersLinkNode = nodeReaction;
+          } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:editPoints")) {
+            points = parseEditPoints(nodeReaction);
+          } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:name")) {
+            result.setName(getNodeValue(nodeReaction));
+          } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:listOfGateMember")) {
+            gateMembers = nodeReaction;
+          } else {
+            throw new ReactionParserException(
+                "Unknown element of reaction/celldesigner:extension: " + nodeReaction.getNodeName(), result);
           }
-        } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:line")) {
-          line = getLineProperties(nodeReaction);
-        } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:connectScheme")) {
-          try {
-            connectScheme = parseConnectScheme(nodeReaction);
-          } catch (InvalidXmlSchemaException e) {
-            throw new ReactionParserException(result, e);
-          }
-        } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:listOfReactantLinks")) {
-          reactantsLinkNode = nodeReaction;
-        } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:listOfProductLinks")) {
-          productsLinkNode = nodeReaction;
-        } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:listOfModification")) {
-          modifiersLinkNode = nodeReaction;
-        } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:editPoints")) {
-          points = parseEditPoints(nodeReaction);
-        } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:name")) {
-          result.setName(getNodeValue(nodeReaction));
-        } else if (nodeReaction.getNodeName().equalsIgnoreCase("celldesigner:listOfGateMember")) {
-          gateMembers = nodeReaction;
-        } else {
-          throw new ReactionParserException(
-              "Unknown element of reaction/celldesigner:extension: " + nodeReaction.getNodeName(), result);
         }
       }
-    }
-    if (gateMembers != null) {
-      try {
-        points = gateMembersToPoints(gateMembers);
-      } catch (InvalidXmlSchemaException e) {
-        throw new ReactionParserException(result, e);
+      if (gateMembers != null) {
+        try {
+          points = gateMembersToPoints(gateMembers);
+        } catch (InvalidXmlSchemaException e) {
+          throw new ReactionParserException(result, e);
+        }
+      }
+      result = createProperTypeReaction(type, result);
+      if (connectScheme == null) {
+        throw new ReactionParserException("No connectScheme node", result);
+      }
+      if (points == null) {
+        points = new EditPoints();
+      }
+      if (result instanceof SimpleReactionInterface) {
+        createLinesForSimpleReaction(result, points, connectScheme);
+      } else if (result instanceof TwoReactantReactionInterface) {
+        createLinesForTwoReactantReaction(result, points, gateMembers != null);
+        createOperatorsForTwoReactantReaction(result, gateMembers);
+
+      } else if (result instanceof TwoProductReactionInterface) {
+        createLinesForTwoProductReaction(result, points);
+        createOperatorsForTwoProductReaction(result);
+      } else {
+        throw new ReactionParserException(
+            "Problem with parsing lines. Unknown reaction: " + type + "; " + result.getClass().getName(), result);
+      }
+      if (reactantsLinkNode != null) {
+        parseReactantLinks(result, reactantsLinkNode, model);
+      }
+      if (productsLinkNode != null) {
+        parseProductLinks(result, productsLinkNode, model);
       }
-    }
-    result = createProperTypeReaction(type, result);
-    if (connectScheme == null) {
-      throw new ReactionParserException("No connectScheme node", result);
-    }
-    if (points == null) {
-      points = new EditPoints();
-    }
-    if (result instanceof SimpleReactionInterface) {
-      createLinesForSimpleReaction(result, points, connectScheme);
-    } else if (result instanceof TwoReactantReactionInterface) {
-      createLinesForTwoReactantReaction(result, points, gateMembers != null);
-      createOperatorsForTwoReactantReaction(result, gateMembers);
-
-    } else if (result instanceof TwoProductReactionInterface) {
-      createLinesForTwoProductReaction(result, points);
-      createOperatorsForTwoProductReaction(result);
-    } else {
-      throw new ReactionParserException(
-          "Problem with parsing lines. Unknown reaction: " + type + "; " + result.getClass().getName(), result);
-    }
-    if (reactantsLinkNode != null) {
-      parseReactantLinks(result, reactantsLinkNode, model);
-    }
-    if (productsLinkNode != null) {
-      parseProductLinks(result, productsLinkNode, model);
-    }
 
-    // create operators
-    createOperators(result);
+      // create operators
+      createOperators(result);
 
-    // now try to create modifiers (we must have set fixed layout data for the
-    // core of the reaction)
-    if (modifiersLinkNode != null) {
-      parseReactionModification(result, modifiersLinkNode, model);
-    }
-    for (int i = 0; i < result.getModifiers().size(); i++) {
-      Modifier modifier = result.getModifiers().get(i);
-      if (modifier.getElement() == null) {
-        List<Modifier> modifiers = new ArrayList<Modifier>();
-        modifiers.add(modifier);
-        for (Modifier modifier2 : result.getModifiers()) {
-          if (modifierParentOperator.get(modifier2) == modifier) {
-            modifiers.add(modifier2);
+      // now try to create modifiers (we must have set fixed layout data for the
+      // core of the reaction)
+      if (modifiersLinkNode != null) {
+        parseReactionModification(result, modifiersLinkNode, model);
+      }
+      for (int i = 0; i < result.getModifiers().size(); i++) {
+        Modifier modifier = result.getModifiers().get(i);
+        if (modifier.getElement() == null) {
+          List<Modifier> modifiers = new ArrayList<Modifier>();
+          modifiers.add(modifier);
+          for (Modifier modifier2 : result.getModifiers()) {
+            if (modifierParentOperator.get(modifier2) == modifier) {
+              modifiers.add(modifier2);
+            }
           }
+          computeLineForModification(result, modifiers);
+          createOperatorFromModifiers(result, modifiers);
+          result.removeModifier(modifier);
+          i--;
+        } else if (modifier.getLine() == null) {
+          createLineForModifier(result, modifier);
         }
-        computeLineForModification(result, modifiers);
-        createOperatorFromModifiers(result, modifiers);
-        result.removeModifier(modifier);
-        i--;
-      } else if (modifier.getLine() == null) {
-        createLineForModifier(result, modifier);
       }
-    }
 
-    if (line != null) {
-      for (AbstractNode rNode : result.getNodes()) {
-        rNode.getLine().setWidth(line.getWidth());
-        rNode.getLine().setColor(line.getColor());
+      if (line != null) {
+        for (AbstractNode rNode : result.getNodes()) {
+          rNode.getLine().setWidth(line.getWidth());
+          rNode.getLine().setColor(line.getColor());
+        }
       }
-    }
-    if (result.isReversible()) {
-      for (Reactant reactant : result.getReactants()) {
-        reactant.getLine().getBeginAtd().setArrowType(result.getProducts().get(0).getLine().getEndAtd().getArrowType());
+      if (result.isReversible()) {
+        for (Reactant reactant : result.getReactants()) {
+          reactant.getLine().getBeginAtd()
+              .setArrowType(result.getProducts().get(0).getLine().getEndAtd().getArrowType());
+        }
       }
-    }
 
-    return result;
+      return result;
+    } catch (InvalidXmlSchemaException e) {
+      throw new ReactionParserException(e.getMessage(), result, e);
+    }
   }
 
   /**
@@ -1688,8 +1693,6 @@ public class ReactionFromXml extends XmlParser {
    * PArses baseReactant node from CellDEsigenr xml node and adds it into
    * reaction.
    * 
-   * @param result
-   *          reaction that is processed
    * @param reactantNode
    *          xml node to parse
    * @param model
@@ -1698,14 +1701,13 @@ public class ReactionFromXml extends XmlParser {
    *           thrown when xml node contains data that is not supported by xml
    *           schema
    */
-  private void parseBaseReactant(Reaction result, Node reactantNode, Model model) throws ReactionParserException {
+  private Reactant parseBaseReactant(Node reactantNode, Model model) throws InvalidXmlSchemaException {
     String aliasId = getNodeAttr("alias", reactantNode);
     Species alias = model.getElementByElementId(aliasId);
     if (alias == null) {
-      throw new ReactionParserException("Alias with id=" + aliasId + " doesn't exist.", result);
+      throw new InvalidXmlSchemaException("Alias with id=" + aliasId + " doesn't exist.");
     }
     Reactant reactant = new Reactant(alias);
-    result.addReactant(reactant);
     NodeList nodes = reactantNode.getChildNodes();
     for (int x = 0; x < nodes.getLength(); x++) {
       Node node = nodes.item(x);
@@ -1713,19 +1715,17 @@ public class ReactionFromXml extends XmlParser {
         if (node.getNodeName().equalsIgnoreCase("celldesigner:linkAnchor")) {
           anchorsByNodes.put(reactant, CellDesignerAnchor.valueOf(getNodeAttr("position", node)));
         } else {
-          throw new ReactionParserException(
-              "Unknown element of reaction/celldesigner:baseReactant: " + node.getNodeName(), result);
+          throw new InvalidXmlSchemaException(
+              "Unknown element of reaction/celldesigner:baseReactant: " + node.getNodeName());
         }
       }
-
     }
+    return reactant;
   }
 
   /**
    * Parses baseProduct node from CellDEsigenr xml node and adds it into reaction.
    * 
-   * @param result
-   *          reaction that is processed
    * @param reactantNode
    *          xml node to parse
    * @param model
@@ -1734,14 +1734,13 @@ public class ReactionFromXml extends XmlParser {
    *           thrown when xml node contains data that is not supported by xml
    *           schema
    */
-  private void parseBaseProduct(Model model, Reaction result, Node reactantNode) throws ReactionParserException {
+  private Product parseBaseProduct(Model model, Node reactantNode) throws InvalidXmlSchemaException {
     String aliasId = getNodeAttr("alias", reactantNode);
     Species species = model.getElementByElementId(aliasId);
     if (species == null) {
-      throw new ReactionParserException("Alias with id=" + aliasId + " doesn't exist.", result);
+      throw new InvalidXmlSchemaException("Alias with id=" + aliasId + " doesn't exist.");
     }
     Product product = new Product(species);
-    result.addProduct(product);
     NodeList nodes = reactantNode.getChildNodes();
     for (int x = 0; x < nodes.getLength(); x++) {
       Node node = nodes.item(x);
@@ -1749,11 +1748,12 @@ public class ReactionFromXml extends XmlParser {
         if (node.getNodeName().equalsIgnoreCase("celldesigner:linkAnchor")) {
           anchorsByNodes.put(product, CellDesignerAnchor.valueOf(getNodeAttr("position", node)));
         } else {
-          throw new ReactionParserException(
-              "Unknown element of reaction/celldesigner:baseProduct: " + node.getNodeName(), result);
+          throw new InvalidXmlSchemaException(
+              "Unknown element of reaction/celldesigner:baseProduct: " + node.getNodeName());
         }
       }
     }
+    return product;
 
   }
 
-- 
GitLab