diff --git a/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/CellDesignerParserException.java b/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/CellDesignerParserException.java index 3009ac1a3a426b88498723d37f3d71e7d4d0662d..fa08dbc952962ac9215b98fb2ba3491aebcd91e8 100644 --- a/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/CellDesignerParserException.java +++ b/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/CellDesignerParserException.java @@ -1,57 +1,69 @@ package lcsb.mapviewer.converter.model.celldesigner; /** - * Exception that shold be thrown when the parser encouter problem with - * cellDesigner file. + * Exception that should be thrown when the parser encounter problem with + * CellDesigner file. * * @author Piotr Gawron * */ public abstract class CellDesignerParserException extends Exception { - /** - * - */ - private static final long serialVersionUID = 1L; - - /** - * Default constructor - initializes instance variable to unknown. - */ - - public CellDesignerParserException() { - super(); // call superclass constructor - } - - /** - * Constructor receives some kind of message. - * - * @param err - * message associated with exception - */ - - public CellDesignerParserException(final String err) { - super(err); - } - - /** - * Constructor receives some kind of message and parent exception. - * - * @param err - * message associated with exception - * @param throwable - * parent exception - */ - public CellDesignerParserException(final String err, final Throwable throwable) { - super(err, throwable); - } - - /** - * Public constructor with parent exception that was catched. - * - * @param e - * parent exception - */ - - public CellDesignerParserException(final Throwable e) { - super(e); - } + /** + * + */ + private static final long serialVersionUID = 1L; + + /** + * Default constructor - initializes instance variable to unknown. + */ + + public CellDesignerParserException() { + super(); // call superclass constructor + } + + /** + * Constructor receives some kind of message. + * + * @param err + * message associated with exception + */ + + public CellDesignerParserException(final String err) { + super(err); + } + + /** + * Constructor receives some kind of message and parent exception. + * + * @param err + * message associated with exception + * @param throwable + * parent exception + */ + public CellDesignerParserException(final String err, final Throwable throwable) { + super(err, throwable); + } + + /** + * Public constructor with parent exception that was caught. + * + * @param e + * parent exception + */ + + public CellDesignerParserException(final Throwable e) { + super(e); + } + + public String getMessageWithContext() { + String message; + if (this.getCause() != null) { + message = this.getCause().getMessage(); + } else { + message = this.getMessage(); + } + return getMessageContext() + " " + message; + } + + public abstract String getMessageContext(); } diff --git a/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/CellDesignerXmlParser.java b/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/CellDesignerXmlParser.java index a22d3705a50c6340d8ac074a519ef3f6e13b35d4..44ebeb6677f5790802ad14a6a6298cb5e8d13496 100644 --- a/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/CellDesignerXmlParser.java +++ b/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/CellDesignerXmlParser.java @@ -243,7 +243,7 @@ public class CellDesignerXmlParser extends XmlParser implements IConverter { } catch (InvalidXmlSchemaException e) { throw new InvalidInputDataExecption(e); } catch (CellDesignerParserException e) { - throw new InvalidInputDataExecption(e); + throw new InvalidInputDataExecption(e.getMessageWithContext(), e); } return model; diff --git a/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/compartment/CompartmentCollectionXmlParser.java b/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/compartment/CompartmentCollectionXmlParser.java index 6057b82fe7b620fa880501edd10447a1b1e1f97f..f5ef296f76ac9ed0284442f3b203432d0ad8562c 100644 --- a/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/compartment/CompartmentCollectionXmlParser.java +++ b/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/compartment/CompartmentCollectionXmlParser.java @@ -27,82 +27,85 @@ import lcsb.mapviewer.model.map.compartment.PathwayCompartment; */ public class CompartmentCollectionXmlParser extends XmlParser { - /** - * Default class logger. - */ - @SuppressWarnings("unused") - private Logger logger = Logger.getLogger(CompartmentCollectionXmlParser.class.getName()); + /** + * Default class logger. + */ + @SuppressWarnings("unused") + private Logger logger = Logger.getLogger(CompartmentCollectionXmlParser.class.getName()); - /** - * CellDesigner xml parser for single compartment. - */ - private CompartmentXmlParser compartmentParser; + /** + * CellDesigner xml parser for single compartment. + */ + private CompartmentXmlParser compartmentParser; - /** - * Collection of {@link CellDesignerElement cell designer elements} parsed - * from xml. - */ - private CellDesignerElementCollection elements; + /** + * Collection of {@link CellDesignerElement cell designer elements} parsed from + * xml. + */ + private CellDesignerElementCollection elements; - /** - * Default constructor. - * - * @param elements - * collection of {@link CellDesignerElement cell designer elements} - * parsed from xml - */ - public CompartmentCollectionXmlParser(CellDesignerElementCollection elements) { - this.elements = elements; - compartmentParser = new CompartmentXmlParser(elements); - } + /** + * Default constructor. + * + * @param elements + * collection of {@link CellDesignerElement cell designer elements} + * parsed from xml + */ + public CompartmentCollectionXmlParser(CellDesignerElementCollection elements) { + this.elements = elements; + compartmentParser = new CompartmentXmlParser(elements); + } - /** - * Parse CellDEsigner xml node with collection of compartments. - * - * @param compartmentsNode - * xml node to parse - * @return list of compartments obtained from xml node - * @throws InvalidXmlSchemaException - * thrown when there is a problem with xml - */ - public List<CellDesignerCompartment> parseXmlCompartmentCollection(Node compartmentsNode) throws InvalidXmlSchemaException { - List<CellDesignerCompartment> result = new ArrayList<CellDesignerCompartment>(); - NodeList nodes = compartmentsNode.getChildNodes(); - for (int x = 0; x < nodes.getLength(); x++) { - Node node = nodes.item(x); - if (node.getNodeType() == Node.ELEMENT_NODE) { - if (node.getNodeName().equalsIgnoreCase("compartment")) { - Pair<String, CellDesignerCompartment> compartmentPair = compartmentParser.parseXmlElement(node); - result.add(compartmentPair.getRight()); - } else { - throw new InvalidXmlSchemaException("Unknown element of model/listOfCompartments: " + node.getNodeName()); - } - } - } - return result; - } + /** + * Parse CellDEsigner xml node with collection of compartments. + * + * @param compartmentsNode + * xml node to parse + * @return list of compartments obtained from xml node + * @throws InvalidXmlSchemaException + * thrown when there is a problem with xml + * @throws CompartmentParserException + * thrown when there is a problem with parsing a compartment + */ + public List<CellDesignerCompartment> parseXmlCompartmentCollection(Node compartmentsNode) + throws InvalidXmlSchemaException, CompartmentParserException { + List<CellDesignerCompartment> result = new ArrayList<>(); + NodeList nodes = compartmentsNode.getChildNodes(); + for (int x = 0; x < nodes.getLength(); x++) { + Node node = nodes.item(x); + if (node.getNodeType() == Node.ELEMENT_NODE) { + if (node.getNodeName().equalsIgnoreCase("compartment")) { + Pair<String, CellDesignerCompartment> compartmentPair = compartmentParser.parseXmlElement(node); + result.add(compartmentPair.getRight()); + } else { + throw new InvalidXmlSchemaException("Unknown element of model/listOfCompartments: " + node.getNodeName()); + } + } + } + return result; + } - /** - * Creates CellDesigner xml string from set of compartments. - * - * @param collection - * collection of compartments to be transformed into xml - * @return xml representation of the compartments - */ - public String toXml(Collection<Compartment> collection) { - StringBuilder result = new StringBuilder(); - result.append("<listOfCompartments>\n"); - Set<String> addedIds = new HashSet<>(); - for (Compartment compartment : collection) { - String sbmlId = elements.getElementId(compartment); - if (!addedIds.contains(sbmlId) && !(compartment instanceof PathwayCompartment)) { - addedIds.add(sbmlId); - result.append(compartmentParser.toXml(compartment)); - } - } - Compartment defaultCompartment = new Compartment("default"); - result.append(compartmentParser.toXml(defaultCompartment)); - result.append("</listOfCompartments>\n"); - return result.toString(); - } + /** + * Creates CellDesigner xml string from set of compartments. + * + * @param collection + * collection of compartments to be transformed into xml + * @return xml representation of the compartments + */ + public String toXml(Collection<Compartment> collection) { + StringBuilder result = new StringBuilder(); + result.append("<listOfCompartments>\n"); + Set<String> addedIds = new HashSet<>(); + for (Compartment compartment : collection) { + String sbmlId = elements.getElementId(compartment); + if (!addedIds.contains(sbmlId) && !(compartment instanceof PathwayCompartment)) { + addedIds.add(sbmlId); + result.append(compartmentParser.toXml(compartment)); + } + } + Compartment defaultCompartment = new Compartment("default"); + result.append(compartmentParser.toXml(defaultCompartment)); + result.append("</listOfCompartments>\n"); + return result.toString(); + } } diff --git a/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/compartment/CompartmentParserException.java b/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/compartment/CompartmentParserException.java new file mode 100644 index 0000000000000000000000000000000000000000..6e8afa1fb25d0e6dd23bad6caa74ee82533cba76 --- /dev/null +++ b/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/compartment/CompartmentParserException.java @@ -0,0 +1,126 @@ +package lcsb.mapviewer.converter.model.celldesigner.compartment; + +import org.apache.log4j.Logger; + +import lcsb.mapviewer.converter.model.celldesigner.CellDesignerParserException; +import lcsb.mapviewer.model.map.compartment.Compartment; + +/** + * Exception thrown when problem with parsing {@link Compartment} is + * encountered. + * + * @author Piotr Gawron + * + */ +public class CompartmentParserException extends CellDesignerParserException { + Logger logger = Logger.getLogger(CompartmentParserException.class); + + /** + * + */ + private static final long serialVersionUID = 1L; + + /** + * Identifier of compartment that was a reason for this exception. + */ + private String compartmentId; + + /** + * Default constructor. + * + * @param message + * message thrown + * @param compartmentId + * {@link #compartmentId} + */ + public CompartmentParserException(String message, String compartmentId) { + super(message); + this.compartmentId = compartmentId; + } + + /** + * Default constructor. + * + * @param message + * message thrown + * @param compartment + * {@link #compartmentId} + */ + public CompartmentParserException(String message, Compartment compartment) { + super(message); + if (compartment != null) { + setCompartmentId(compartment.getElementId()); + } + } + + /** + * Default constructor. + * + * @param message + * message thrown + * @param compartment + * {@link #compartmentId} + * @param e + * super exception + */ + public CompartmentParserException(String message, Compartment compartment, Exception e) { + super(message, e); + if (compartment != null) { + setCompartmentId(compartment.getElementId()); + } + } + + /** + * Default constructor. + * + * @param compartment + * {@link #compartmentId} + * @param e + * super exception + */ + public CompartmentParserException(Compartment compartment, Throwable e) { + super(e); + if (compartment != null) { + setCompartmentId(compartment.getElementId()); + } + } + + /** + * Default constructor. + * + * @param compartmentId + * {@link #compartmentId} + * @param e + * super exception + */ + public CompartmentParserException(String compartmentId, Throwable e) { + super(e); + setCompartmentId(compartmentId); + } + + /** + * @return the compartmentId + * @see #compartmentId + */ + public String getCompartmentId() { + return compartmentId; + } + + /** + * @param compartmentId + * the compartmentId to set + * @see #compartmentId + */ + public void setCompartmentId(String compartmentId) { + this.compartmentId = compartmentId; + } + + @Override + public String getMessageContext() { + if (compartmentId != null) { + return "[Compartment: " + compartmentId + "]"; + } + return null; + } + +} diff --git a/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/compartment/CompartmentXmlParser.java b/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/compartment/CompartmentXmlParser.java index b284a5ab92e67bfb983bb9ffd3a41c5eb376c89c..0c70794f2e649473e2489129773446899819e28e 100644 --- a/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/compartment/CompartmentXmlParser.java +++ b/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/compartment/CompartmentXmlParser.java @@ -23,149 +23,155 @@ import lcsb.mapviewer.model.map.compartment.PathwayCompartment; */ public class CompartmentXmlParser extends AbstractElementXmlParser<CellDesignerCompartment, Compartment> { - /** - * Default class logger. - */ - @SuppressWarnings("unused") - private static Logger logger = Logger.getLogger(CompartmentXmlParser.class.getName()); + /** + * Default class logger. + */ + @SuppressWarnings("unused") + private static Logger logger = Logger.getLogger(CompartmentXmlParser.class.getName()); - /** - * Parser used to retrieve Miriam data for the element. - */ - private XmlAnnotationParser xmlAnnotationParser = new XmlAnnotationParser(); + /** + * Parser used to retrieve Miriam data for the element. + */ + private XmlAnnotationParser xmlAnnotationParser = new XmlAnnotationParser(); - /** - * Collection of {@link CellDesignerElement cell designer elements} parsed - * from xml. - */ - private CellDesignerElementCollection elements; + /** + * Collection of {@link CellDesignerElement cell designer elements} parsed from + * xml. + */ + private CellDesignerElementCollection elements; - /** - * Xml parser used for processing notes into structurized data. - */ - private RestAnnotationParser rap = new RestAnnotationParser(); + /** + * Xml parser used for processing notes into structurized data. + */ + private RestAnnotationParser rap = new RestAnnotationParser(); - /** - * Default constructor. - * - * @param elements - * collection of {@link CellDesignerElement cell designer elements} - * parsed from xml - */ - public CompartmentXmlParser(CellDesignerElementCollection elements) { - this.elements = elements; - } + /** + * Default constructor. + * + * @param elements + * collection of {@link CellDesignerElement cell designer elements} + * parsed from xml + */ + public CompartmentXmlParser(CellDesignerElementCollection elements) { + this.elements = elements; + } - @Override - public Pair<String, CellDesignerCompartment> parseXmlElement(Node compartmentNode) throws InvalidXmlSchemaException { - CellDesignerCompartment compartment = new CellDesignerCompartment(); - // we ignore metaid - it's useless and obstruct data model - // compartment.setMetaId(getNodeAttr("metaid", compartmentNode)); - compartment.setElementId(getNodeAttr("id", compartmentNode)); - compartment.setName(decodeName(getNodeAttr("name", compartmentNode))); - NodeList nodes = compartmentNode.getChildNodes(); - for (int x = 0; x < nodes.getLength(); x++) { - Node node = nodes.item(x); - if (node.getNodeType() == Node.ELEMENT_NODE) { - if (node.getNodeName().equalsIgnoreCase("annotation")) { - parseAnnotationNode(compartment, node); - } else if (node.getNodeName().equalsIgnoreCase("notes")) { - compartment.setNotes(rap.getNotes(node)); - } else { - throw new InvalidXmlSchemaException("Unknown element of Compartment: " + node.getNodeName()); - } - } - } - return new Pair<String, CellDesignerCompartment>(compartment.getElementId(), compartment); - } + @Override + public Pair<String, CellDesignerCompartment> parseXmlElement(Node compartmentNode) throws CompartmentParserException { + CellDesignerCompartment compartment = new CellDesignerCompartment(); + // we ignore metaid - it's useless and obstruct data model + // compartment.setMetaId(getNodeAttr("metaid", compartmentNode)); + compartment.setElementId(getNodeAttr("id", compartmentNode)); + compartment.setName(decodeName(getNodeAttr("name", compartmentNode))); + try { + NodeList nodes = compartmentNode.getChildNodes(); + for (int x = 0; x < nodes.getLength(); x++) { + Node node = nodes.item(x); + if (node.getNodeType() == Node.ELEMENT_NODE) { + if (node.getNodeName().equalsIgnoreCase("annotation")) { + parseAnnotationNode(compartment, node); + } else if (node.getNodeName().equalsIgnoreCase("notes")) { + compartment.setNotes(rap.getNotes(node)); + } else { + throw new InvalidXmlSchemaException("Unknown element of Compartment: " + node.getNodeName()); + } + } + } + return new Pair<String, CellDesignerCompartment>(compartment.getElementId(), compartment); + } catch (InvalidXmlSchemaException e) { + throw new CompartmentParserException(compartment.getElementId(), e); + } + } - /** - * Process annotation part of the xml node. - * - * @param compartment - * object that we create - * @param xmlNode - * annotation xml node - * @throws InvalidXmlSchemaException - * thrown when there is a problem with xml - */ - private void parseAnnotationNode(CellDesignerCompartment compartment, Node xmlNode) throws InvalidXmlSchemaException { - NodeList annotationNodes = xmlNode.getChildNodes(); - for (int y = 0; y < annotationNodes.getLength(); y++) { - Node annotationNode = annotationNodes.item(y); - if (annotationNode.getNodeType() == Node.ELEMENT_NODE) { - if (annotationNode.getNodeName().equalsIgnoreCase("rdf:RDF")) { - compartment.addMiriamData(xmlAnnotationParser.parseRdfNode(annotationNode)); - } else if (annotationNode.getNodeName().equalsIgnoreCase("celldesigner:extension")) { - parseExtensionNode(compartment, annotationNode); - } else { - throw new InvalidXmlSchemaException("Unknown element of Compartment/annotation: " + annotationNode.getNodeName()); - } - } - } - } + /** + * Process annotation part of the xml node. + * + * @param compartment + * object that we create + * @param xmlNode + * annotation xml node + * @throws InvalidXmlSchemaException + * thrown when there is a problem with xml + */ + private void parseAnnotationNode(CellDesignerCompartment compartment, Node xmlNode) throws InvalidXmlSchemaException { + NodeList annotationNodes = xmlNode.getChildNodes(); + for (int y = 0; y < annotationNodes.getLength(); y++) { + Node annotationNode = annotationNodes.item(y); + if (annotationNode.getNodeType() == Node.ELEMENT_NODE) { + if (annotationNode.getNodeName().equalsIgnoreCase("rdf:RDF")) { + compartment.addMiriamData(xmlAnnotationParser.parseRdfNode(annotationNode)); + } else if (annotationNode.getNodeName().equalsIgnoreCase("celldesigner:extension")) { + parseExtensionNode(compartment, annotationNode); + } else { + throw new InvalidXmlSchemaException( + "Unknown xml tag of compartment->annotation: " + annotationNode.getNodeName()); + } + } + } + } - /** - * Process extension part of the xml node. - * - * @param compartment - * object that we create - * @param xmlNode - * annotation xml node - * @throws InvalidXmlSchemaException - * thrown when xmlNode is invalid - */ - private void parseExtensionNode(CellDesignerCompartment compartment, Node xmlNode) throws InvalidXmlSchemaException { - NodeList annotationNodes = xmlNode.getChildNodes(); - for (int y = 0; y < annotationNodes.getLength(); y++) { - Node annotationNode = annotationNodes.item(y); - if (annotationNode.getNodeType() == Node.ELEMENT_NODE) { - if (annotationNode.getNodeName().equalsIgnoreCase("celldesigner:name")) { - // ignore it (we already have this name) - continue; - } else { - throw new InvalidXmlSchemaException("Unknown element of Compartment/annotation/extension: " + annotationNode.getNodeName()); - } - } - } - } + /** + * Process extension part of the xml node. + * + * @param compartment + * object that we create + * @param xmlNode + * annotation xml node + * @throws InvalidXmlSchemaException + * thrown when xmlNode is invalid + */ + private void parseExtensionNode(CellDesignerCompartment compartment, Node xmlNode) throws InvalidXmlSchemaException { + NodeList annotationNodes = xmlNode.getChildNodes(); + for (int y = 0; y < annotationNodes.getLength(); y++) { + Node annotationNode = annotationNodes.item(y); + if (annotationNode.getNodeType() == Node.ELEMENT_NODE) { + if (annotationNode.getNodeName().equalsIgnoreCase("celldesigner:name")) { + // ignore it (we already have this name) + continue; + } else { + throw new InvalidXmlSchemaException( + "Unknown element of Compartment/annotation/extension: " + annotationNode.getNodeName()); + } + } + } + } - @Override - public String toXml(Compartment compartment) { - StringBuilder sb = new StringBuilder(""); - sb.append("<compartment "); - sb.append("metaid=\"" + elements.getElementId(compartment) + "\" "); - sb.append("id=\"" + elements.getElementId(compartment) + "\" "); - sb.append("name=\"" + encodeName(compartment.getName()) + "\" "); - sb.append("size=\"1\" "); - sb.append("units=\"volume\" "); - String parentName = "default"; + @Override + public String toXml(Compartment compartment) { + StringBuilder sb = new StringBuilder(""); + sb.append("<compartment "); + sb.append("metaid=\"" + elements.getElementId(compartment) + "\" "); + sb.append("id=\"" + elements.getElementId(compartment) + "\" "); + sb.append("name=\"" + encodeName(compartment.getName()) + "\" "); + sb.append("size=\"1\" "); + sb.append("units=\"volume\" "); + String parentName = "default"; - if (compartment.getCompartment() != null && !(compartment.getCompartment() instanceof PathwayCompartment)) { - parentName = elements.getElementId(compartment.getCompartment()); - } + if (compartment.getCompartment() != null && !(compartment.getCompartment() instanceof PathwayCompartment)) { + parentName = elements.getElementId(compartment.getCompartment()); + } - // default is the top level compartment - if (!compartment.getElementId().equals("default")) { - sb.append("outside=\"" + parentName + "\" "); - } - sb.append(">\n"); - sb.append("<annotation>\n"); - sb.append("<celldesigner:extension>\n"); - sb.append("<celldesigner:name>" + encodeName(compartment.getName()) + "</celldesigner:name>\n"); - sb.append("</celldesigner:extension>\n"); - sb.append(xmlAnnotationParser.dataSetToXmlString(compartment.getMiriamData())); - sb.append("</annotation>\n"); - sb.append("<notes>"); - sb.append("<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title/></head><body>"); - RestAnnotationParser rap = new RestAnnotationParser(); - sb.append(rap.createAnnotationString(compartment)); - if (compartment.getNotes() != null) { - sb.append(compartment.getNotes()); - } - sb.append("</body></html>"); - sb.append("</notes>\n"); - sb.append("</compartment>\n"); - return sb.toString(); - } + // default is the top level compartment + if (!compartment.getElementId().equals("default")) { + sb.append("outside=\"" + parentName + "\" "); + } + sb.append(">\n"); + sb.append("<annotation>\n"); + sb.append("<celldesigner:extension>\n"); + sb.append("<celldesigner:name>" + encodeName(compartment.getName()) + "</celldesigner:name>\n"); + sb.append("</celldesigner:extension>\n"); + sb.append(xmlAnnotationParser.dataSetToXmlString(compartment.getMiriamData())); + sb.append("</annotation>\n"); + sb.append("<notes>"); + sb.append("<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title/></head><body>"); + RestAnnotationParser rap = new RestAnnotationParser(); + sb.append(rap.createAnnotationString(compartment)); + if (compartment.getNotes() != null) { + sb.append(compartment.getNotes()); + } + sb.append("</body></html>"); + sb.append("</notes>\n"); + sb.append("</compartment>\n"); + return sb.toString(); + } } diff --git a/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/reaction/ReactionParserException.java b/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/reaction/ReactionParserException.java index 86ed00cd4cd3f3e722d1a58c5bf5a96f7f94f565..0da75c8289fa85a78cd529ab695a5923703d9389 100644 --- a/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/reaction/ReactionParserException.java +++ b/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/reaction/ReactionParserException.java @@ -12,91 +12,99 @@ import lcsb.mapviewer.model.map.reaction.Reaction; */ public class ReactionParserException extends CellDesignerParserException { - /** - * - */ - private static final long serialVersionUID = 1L; + /** + * + */ + private static final long serialVersionUID = 1L; - /** - * Identifier of reaction that was a reason for this exception. - */ - private String reactionId; + /** + * Identifier of reaction that was a reason for this exception. + */ + private String reactionId; - /** - * Default constructor. - * - * @param message - * message thrown - * @param reactionId - * {@link #reactionId} - */ - public ReactionParserException(String message, String reactionId) { - super(message); - this.reactionId = reactionId; - } + /** + * Default constructor. + * + * @param message + * message thrown + * @param reactionId + * {@link #reactionId} + */ + public ReactionParserException(String message, String reactionId) { + super(message); + this.reactionId = reactionId; + } - /** - * Default constructor. - * - * @param message - * message thrown - * @param reaction - * {@link #reactionId} - */ - public ReactionParserException(String message, Reaction reaction) { - super(message); - if (reaction != null) { - setReactionId(reaction.getIdReaction()); - } - } + /** + * Default constructor. + * + * @param message + * message thrown + * @param reaction + * {@link #reactionId} + */ + public ReactionParserException(String message, Reaction reaction) { + super(message); + if (reaction != null) { + setReactionId(reaction.getIdReaction()); + } + } - /** - * Default constructor. - * - * @param message - * message thrown - * @param reaction - * {@link #reactionId} - * @param e - * super exception - */ - public ReactionParserException(String message, Reaction reaction, Exception e) { - super(message, e); - if (reaction != null) { - setReactionId(reaction.getIdReaction()); - } - } + /** + * Default constructor. + * + * @param message + * message thrown + * @param reaction + * {@link #reactionId} + * @param e + * super exception + */ + public ReactionParserException(String message, Reaction reaction, Exception e) { + super(message, e); + if (reaction != null) { + setReactionId(reaction.getIdReaction()); + } + } - /** - * Default constructor. - * - * @param reaction - * {@link #reactionId} - * @param e - * super exception - */ - public ReactionParserException(Reaction reaction, Throwable e) { - super(e); - if (reaction != null) { - setReactionId(reaction.getIdReaction()); - } - } + /** + * Default constructor. + * + * @param reaction + * {@link #reactionId} + * @param e + * super exception + */ + public ReactionParserException(Reaction reaction, Throwable e) { + super(e); + if (reaction != null) { + setReactionId(reaction.getIdReaction()); + } + } - /** - * @return the reactionId - * @see #reactionId - */ - public String getReactionId() { - return reactionId; - } + /** + * @return the reactionId + * @see #reactionId + */ + public String getReactionId() { + return reactionId; + } - /** - * @param reactionId - * the reactionId to set - * @see #reactionId - */ - public void setReactionId(String reactionId) { - this.reactionId = reactionId; - } + /** + * @param reactionId + * the reactionId to set + * @see #reactionId + */ + public void setReactionId(String reactionId) { + this.reactionId = reactionId; + } + + @Override + public String getMessageContext() { + if (reactionId != null) { + return "[Reaction: " + reactionId + "]"; + } + return null; + } } diff --git a/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/species/AbstractElementXmlParser.java b/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/species/AbstractElementXmlParser.java index fcd1d7833f8dbde1ddd24bdb5e766145aa7d3efc..90ba13c3a6b2f031f29fc3629bbfbbabceae3561 100644 --- a/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/species/AbstractElementXmlParser.java +++ b/converter-CellDesigner/src/main/java/lcsb/mapviewer/converter/model/celldesigner/species/AbstractElementXmlParser.java @@ -11,7 +11,9 @@ import org.w3c.dom.NodeList; import lcsb.mapviewer.common.Pair; import lcsb.mapviewer.common.XmlParser; import lcsb.mapviewer.common.exception.InvalidXmlSchemaException; +import lcsb.mapviewer.converter.model.celldesigner.CellDesignerParserException; import lcsb.mapviewer.converter.model.celldesigner.annotation.RestAnnotationParser; +import lcsb.mapviewer.converter.model.celldesigner.compartment.CompartmentParserException; import lcsb.mapviewer.converter.model.celldesigner.structure.CellDesignerElement; import lcsb.mapviewer.model.map.InconsistentModelException; import lcsb.mapviewer.model.map.species.Element; @@ -57,8 +59,9 @@ public abstract class AbstractElementXmlParser<T extends CellDesignerElement<?>, * and element parsed from xml node in {@link Pair#right}. * @throws InvalidXmlSchemaException * thrown when xml is invalid + * @throws CompartmentParserException */ - public abstract Pair<String, T> parseXmlElement(Node node) throws InvalidXmlSchemaException; + public abstract Pair<String, T> parseXmlElement(Node node) throws InvalidXmlSchemaException, CellDesignerParserException; /** * Default constructor. @@ -132,12 +135,12 @@ public abstract class AbstractElementXmlParser<T extends CellDesignerElement<?>, * and element parsed from xml node in {@link Pair#right}. * @throws InvalidXmlSchemaException * thrown when xmlString is invalid + * @throws CellDesignerParserException */ - public Pair<String, T> parseXmlElement(String xmlString) throws InvalidXmlSchemaException { + public Pair<String, T> parseXmlElement(String xmlString) throws InvalidXmlSchemaException, CellDesignerParserException { Document doc = getXmlDocumentFromString(xmlString); NodeList root = doc.getChildNodes(); return parseXmlElement(root.item(0)); - } /** diff --git a/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/AllCellDesignerTests.java b/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/AllCellDesignerTests.java index 7f521c9341b93e355b276921906c863e47c6b134..f40328f788cde5be11c89e766430d345490da24e 100644 --- a/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/AllCellDesignerTests.java +++ b/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/AllCellDesignerTests.java @@ -28,7 +28,6 @@ import lcsb.mapviewer.converter.model.celldesigner.unit.UnitXmlParserTest; AllStructureTests.class, // AllTypesTests.class, // CellDesignerElementCollectionTest.class, // - CellDesignerParserExceptionTest.class, // CellDesignerXmlParserTest.class, // ComplexParserTests.class, // InvalidGroupExceptionTest.class, // diff --git a/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/CellDesignerParserExceptionTest.java b/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/CellDesignerParserExceptionTest.java deleted file mode 100644 index b586a2cc6624dd9d34d799d880f4db32b71102a4..0000000000000000000000000000000000000000 --- a/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/CellDesignerParserExceptionTest.java +++ /dev/null @@ -1,52 +0,0 @@ -package lcsb.mapviewer.converter.model.celldesigner; - -import static org.junit.Assert.assertNotNull; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -public class CellDesignerParserExceptionTest { - - private static class Tmp extends CellDesignerParserException { - - /** - * - */ - private static final long serialVersionUID = 1L; - - public Tmp(String string) { - super(string); - } - - public Tmp() { - super(); - } - - public Tmp(String string, Exception exception) { - super(string, exception); - } - - public Tmp(Exception exception) { - super(exception); - } - - } - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testConstructor() { - assertNotNull(new Tmp()); - assertNotNull(new Tmp("str")); - assertNotNull(new Tmp("str2", new Exception())); - assertNotNull(new Tmp(new Exception())); - } - -} diff --git a/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/compartment/CompartmentXmlParserTest.java b/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/compartment/CompartmentXmlParserTest.java index abda4fa5533377a45132beebcbefb97e50bfc6c6..674077ea546d3fd9033d55a4cb62bc13a9a001c7 100644 --- a/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/compartment/CompartmentXmlParserTest.java +++ b/converter-CellDesigner/src/test/java/lcsb/mapviewer/converter/model/celldesigner/compartment/CompartmentXmlParserTest.java @@ -19,7 +19,6 @@ import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import lcsb.mapviewer.common.Pair; -import lcsb.mapviewer.common.exception.InvalidXmlSchemaException; import lcsb.mapviewer.converter.model.celldesigner.CellDesignerElementCollection; import lcsb.mapviewer.converter.model.celldesigner.CellDesignerTestFunctions; import lcsb.mapviewer.converter.model.celldesigner.species.ProteinXmlParserTest; @@ -27,122 +26,122 @@ import lcsb.mapviewer.converter.model.celldesigner.structure.CellDesignerCompart import lcsb.mapviewer.model.map.compartment.Compartment; public class CompartmentXmlParserTest extends CellDesignerTestFunctions { - protected Logger logger = Logger.getLogger(ProteinXmlParserTest.class.getName()); - - CompartmentXmlParser compartmentParser; - CellDesignerElementCollection elements; - String testProteinFile = "testFiles/xmlNodeTestExamples/compartment.xml"; - - @Before - public void setUp() throws Exception { - elements = new CellDesignerElementCollection(); - compartmentParser = new CompartmentXmlParser(elements); - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testParseXmlCompartment() { - try { - String xmlString = readFile(testProteinFile); - - Pair<String, CellDesignerCompartment> result = compartmentParser.parseXmlElement(xmlString); - CellDesignerCompartment compartment = result.getRight(); - assertNotNull(compartment); - assertEquals("c1", compartment.getElementId()); - assertEquals(1, compartment.getMiriamData().size()); - assertEquals("c1", compartment.getName()); - - } catch (Exception e) { - e.printStackTrace(); - fail("Unexpected exception occured"); - } - } - - @Test - public void testToXml() throws Exception { - try { - String xmlString = readFile(testProteinFile); - - Pair<String, CellDesignerCompartment> result = compartmentParser.parseXmlElement(xmlString); - CellDesignerCompartment compartment = result.getRight(); - Compartment alias = compartment.createModelElement("id"); - String transformedXml = compartmentParser.toXml(alias); - assertNotNull(transformedXml); - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - DocumentBuilder builder = factory.newDocumentBuilder(); - InputSource is = new InputSource(new StringReader(transformedXml)); - Document doc = builder.parse(is); - NodeList root = doc.getChildNodes(); - assertEquals("compartment", root.item(0).getNodeName()); - - Pair<String, CellDesignerCompartment> result2 = compartmentParser.parseXmlElement(compartmentParser.toXml(alias)); - CellDesignerCompartment compartment2 = result2.getRight(); - assertEquals(alias.getMiriamData().size(), compartment2.getMiriamData().size()); - assertEquals(alias.getName(), compartment2.getName()); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testParseInvalidCompartment() throws Exception { - try { - compartmentParser.parseXmlElement(readFile("testFiles/invalid/compartment.xml")); - fail("Exception expected"); - - } catch (InvalidXmlSchemaException e) { - assertTrue(e.getMessage().contains("Unknown element of Compartment")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testParseInvalidCompartment2() throws Exception { - try { - compartmentParser.parseXmlElement(readFile("testFiles/invalid/compartment2.xml")); - fail("Exception expected"); - - } catch (InvalidXmlSchemaException e) { - assertTrue(e.getMessage().contains("Unknown element of Compartment/annotation")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testParseInvalidCompartment3() throws Exception { - try { - compartmentParser.parseXmlElement(readFile("testFiles/invalid/compartment3.xml")); - fail("Exception expected"); - - } catch (InvalidXmlSchemaException e) { - assertTrue(e.getMessage().contains("Unknown element of Compartment/annotation/extension")); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testToXmlWithParent() throws Exception { - try { - Compartment child = new Compartment("c_1"); - Compartment parent = new Compartment("p_1"); - child.setCompartment(parent); - - String xml = compartmentParser.toXml(child); - assertTrue(xml.contains(parent.getElementId())); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } + protected Logger logger = Logger.getLogger(ProteinXmlParserTest.class.getName()); + + CompartmentXmlParser compartmentParser; + CellDesignerElementCollection elements; + String testProteinFile = "testFiles/xmlNodeTestExamples/compartment.xml"; + + @Before + public void setUp() throws Exception { + elements = new CellDesignerElementCollection(); + compartmentParser = new CompartmentXmlParser(elements); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testParseXmlCompartment() { + try { + String xmlString = readFile(testProteinFile); + + Pair<String, CellDesignerCompartment> result = compartmentParser.parseXmlElement(xmlString); + CellDesignerCompartment compartment = result.getRight(); + assertNotNull(compartment); + assertEquals("c1", compartment.getElementId()); + assertEquals(1, compartment.getMiriamData().size()); + assertEquals("c1", compartment.getName()); + + } catch (Exception e) { + e.printStackTrace(); + fail("Unexpected exception occured"); + } + } + + @Test + public void testToXml() throws Exception { + try { + String xmlString = readFile(testProteinFile); + + Pair<String, CellDesignerCompartment> result = compartmentParser.parseXmlElement(xmlString); + CellDesignerCompartment compartment = result.getRight(); + Compartment alias = compartment.createModelElement("id"); + String transformedXml = compartmentParser.toXml(alias); + assertNotNull(transformedXml); + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + InputSource is = new InputSource(new StringReader(transformedXml)); + Document doc = builder.parse(is); + NodeList root = doc.getChildNodes(); + assertEquals("compartment", root.item(0).getNodeName()); + + Pair<String, CellDesignerCompartment> result2 = compartmentParser.parseXmlElement(compartmentParser.toXml(alias)); + CellDesignerCompartment compartment2 = result2.getRight(); + assertEquals(alias.getMiriamData().size(), compartment2.getMiriamData().size()); + assertEquals(alias.getName(), compartment2.getName()); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testParseInvalidCompartment() throws Exception { + try { + compartmentParser.parseXmlElement(readFile("testFiles/invalid/compartment.xml")); + fail("Exception expected"); + + } catch (CompartmentParserException e) { + assertTrue(e.getMessage().contains("Unknown element of Compartment")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testParseInvalidCompartment2() throws Exception { + try { + compartmentParser.parseXmlElement(readFile("testFiles/invalid/compartment2.xml")); + fail("Exception expected"); + + } catch (CompartmentParserException e) { + assertTrue(e.getMessage().contains("Unknown xml tag of compartment")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testParseInvalidCompartment3() throws Exception { + try { + compartmentParser.parseXmlElement(readFile("testFiles/invalid/compartment3.xml")); + fail("Exception expected"); + + } catch (CompartmentParserException e) { + assertTrue(e.getMessage().contains("Unknown element of Compartment/annotation/extension")); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testToXmlWithParent() throws Exception { + try { + Compartment child = new Compartment("c_1"); + Compartment parent = new Compartment("p_1"); + child.setCompartment(parent); + + String xml = compartmentParser.toXml(child); + assertTrue(xml.contains(parent.getElementId())); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } } diff --git a/converter/src/main/java/lcsb/mapviewer/converter/InvalidInputDataExecption.java b/converter/src/main/java/lcsb/mapviewer/converter/InvalidInputDataExecption.java index ebc92805a166ffd772c6113a65f84b3aaeafa4f4..beb5e030263771059539640016e412157d12235a 100644 --- a/converter/src/main/java/lcsb/mapviewer/converter/InvalidInputDataExecption.java +++ b/converter/src/main/java/lcsb/mapviewer/converter/InvalidInputDataExecption.java @@ -8,61 +8,61 @@ package lcsb.mapviewer.converter; */ public class InvalidInputDataExecption extends Exception { - /** - * - */ - private static final long serialVersionUID = 1L; + /** + * + */ + private static final long serialVersionUID = 1L; - /** - * Default constructor with exception message. - * - * @param message - * error message - */ - public InvalidInputDataExecption(String message) { - super(message); - } + /** + * Default constructor with exception message. + * + * @param message + * error message + */ + public InvalidInputDataExecption(String message) { + super(message); + } - /** - * Default constructor with super exception as a source. - * - * @param e - * super exception - */ - public InvalidInputDataExecption(Exception e) { - super(e); - } + /** + * Default constructor with super exception as a source. + * + * @param e + * super exception + */ + public InvalidInputDataExecption(Exception e) { + super(e); + } - /** - * Default constructor - initializes instance variable to unknown. - */ + /** + * Default constructor - initializes instance variable to unknown. + */ - public InvalidInputDataExecption() { - super(); // call superclass constructor - } + public InvalidInputDataExecption() { + super(); // call superclass constructor + } - /** - * Default constructor. - * - * @param message - * exception message - * @param e - * super exception - */ - public InvalidInputDataExecption(String message, Exception e) { - super(message, e); - } + /** + * Default constructor. + * + * @param message + * exception message + * @param e + * super exception + */ + public InvalidInputDataExecption(String message, Exception e) { + super(message, e); + } - /** - * Default constructor with exception message. - * - * @param message - * error message - * @param filename - * name of the file where data is invalid - */ - public InvalidInputDataExecption(String message, String filename) { - super("[File \"" + filename + "\"]: " + message.trim()); - } + /** + * Default constructor with exception message. + * + * @param message + * error message + * @param filename + * name of the file where data is invalid + */ + public InvalidInputDataExecption(String message, String filename) { + super("[File \"" + filename + "\"]: " + message.trim()); + } }