diff --git a/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/reaction/ReactionCollectionXmlParser.java b/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/reaction/ReactionCollectionXmlParser.java index 2f06de27b17abbeb88ad01bbe0543a0ba359c362..3125c6585a97b3531fc6039344a622db4fe65b23 100644 --- a/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/reaction/ReactionCollectionXmlParser.java +++ b/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/reaction/ReactionCollectionXmlParser.java @@ -9,6 +9,7 @@ import org.w3c.dom.Node; import org.w3c.dom.NodeList; import lcsb.mapviewer.common.XmlParser; +import lcsb.mapviewer.common.exception.InvalidArgumentException; import lcsb.mapviewer.common.exception.InvalidXmlSchemaException; import lcsb.mapviewer.converter.ConverterException; import lcsb.mapviewer.converter.model.celldesigner.CellDesignerElementCollection; @@ -17,6 +18,7 @@ import lcsb.mapviewer.converter.model.celldesigner.structure.CellDesignerElement import lcsb.mapviewer.model.map.InconsistentModelException; import lcsb.mapviewer.model.map.model.Model; import lcsb.mapviewer.model.map.reaction.Reaction; +import lcsb.mapviewer.modelutils.map.ElementUtils; /** * Parser used for extracting collections of reaction from CellDesigner xml. @@ -26,88 +28,92 @@ import lcsb.mapviewer.model.map.reaction.Reaction; */ public class ReactionCollectionXmlParser extends XmlParser { - /** - * Default class logger. - */ - @SuppressWarnings("unused") - private Logger logger = Logger.getLogger(ReactionCollectionXmlParser.class.getName()); + /** + * Default class logger. + */ + @SuppressWarnings("unused") + private Logger logger = Logger.getLogger(ReactionCollectionXmlParser.class.getName()); - /** - * Model for which parsing (or transformation to xml) is being done. - */ - private Model model = null; + /** + * Model for which parsing (or transformation to xml) is being done. + */ + private Model model = null; - /** - * Object used for parsing single reaction. - */ - private ReactionXmlParser xmlStructureFactory; + /** + * Object used for parsing single reaction. + */ + private ReactionXmlParser xmlStructureFactory; - /** - * Default constructor. Model is required because some nodes require access to - * other parts of the model. - * - * @param sbgn - * should sbgn standard be used - * @param elements - * collection of {@link CellDesignerElement cell designer elements} - * parsed from xml - * @param model - * model that is parsed/transformed into xml - */ - public ReactionCollectionXmlParser(Model model, CellDesignerElementCollection elements, boolean sbgn) { - this.model = model; - xmlStructureFactory = new ReactionXmlParser(elements, sbgn); - } + /** + * Default constructor. Model is required because some nodes require access to + * other parts of the model. + * + * @param sbgn + * should sbgn standard be used + * @param elements + * collection of {@link CellDesignerElement cell designer elements} + * parsed from xml + * @param model + * model that is parsed/transformed into xml + */ + public ReactionCollectionXmlParser(Model model, CellDesignerElementCollection elements, boolean sbgn) { + this.model = model; + xmlStructureFactory = new ReactionXmlParser(elements, sbgn); + } - /** - * Parse CellDesigner xml node with set of reactions into list of - * {@link Reaction}. - * - * @param reactionsNode - * xml node - * @return list of reaction taken from xml node - * @throws CellDesignerParserException - * thrown when there is a problem with xml node - * @throws InvalidXmlSchemaException - * thrown when reactionsNode is invalid xml - */ - public List<Reaction> parseXmlReactionCollection(Node reactionsNode) throws CellDesignerParserException, InvalidXmlSchemaException { - List<Reaction> result = new ArrayList<Reaction>(); - NodeList nodes = reactionsNode.getChildNodes(); - for (int x = 0; x < nodes.getLength(); x++) { - Node node = nodes.item(x); - if (node.getNodeType() == Node.ELEMENT_NODE) { - if (node.getNodeName().equalsIgnoreCase("reaction")) { - Reaction reaction = xmlStructureFactory.getReaction(node, model); - result.add(reaction); - } else { - throw new InvalidXmlSchemaException("Unknown element of model/listOfReactions: " + node.getNodeName()); - } - } - } + /** + * Parse CellDesigner xml node with set of reactions into list of + * {@link Reaction}. + * + * @param reactionsNode + * xml node + * @return list of reaction taken from xml node + * @throws CellDesignerParserException + * thrown when there is a problem with xml node + * @throws InvalidXmlSchemaException + * thrown when reactionsNode is invalid xml + */ + public List<Reaction> parseXmlReactionCollection(Node reactionsNode) + throws CellDesignerParserException, InvalidXmlSchemaException { + List<Reaction> result = new ArrayList<Reaction>(); + NodeList nodes = reactionsNode.getChildNodes(); + for (int x = 0; x < nodes.getLength(); x++) { + Node node = nodes.item(x); + if (node.getNodeType() == Node.ELEMENT_NODE) { + if (node.getNodeName().equalsIgnoreCase("reaction")) { + Reaction reaction = xmlStructureFactory.getReaction(node, model); + result.add(reaction); + } else { + throw new InvalidXmlSchemaException("Unknown element of model/listOfReactions: " + node.getNodeName()); + } + } + } - return result; - } + return result; + } - /** - * Transforms set of reactions into CellDesigner xml string. - * - * @param collection - * set of reactions - * @return CellDesigner xml string representing set of reactions - * @throws ConverterException - */ - public String reactionCollectionToXmlString(Collection<Reaction> collection) throws InconsistentModelException { - String result = ""; - result += "<listOfReactions>\n"; - for (Reaction reaction : collection) { - try { - result += xmlStructureFactory.toXml(reaction); - } catch (SelfReactionException e) { - logger.warn("Reaction omitted: " + e.getMessage()); - } - } - result += "</listOfReactions>\n"; - return result; - } + /** + * Transforms set of reactions into CellDesigner xml string. + * + * @param collection + * set of reactions + * @return CellDesigner xml string representing set of reactions + * @throws ConverterException + */ + public String reactionCollectionToXmlString(Collection<Reaction> collection) throws InconsistentModelException { + String result = ""; + result += "<listOfReactions>\n"; + for (Reaction reaction : collection) { + try { + result += xmlStructureFactory.toXml(reaction); + } catch (InvalidArgumentException e) { + throw new InconsistentModelException( + new ElementUtils().getElementTag(reaction) + "Problem with exporting reaction", e); + } catch (SelfReactionException e) { + logger.warn("Reaction omitted: " + e.getMessage()); + } + } + result += "</listOfReactions>\n"; + return result; + } }