From a45c50b50c5da5ef35a1a85f29386ec0fa6487db Mon Sep 17 00:00:00 2001 From: Piotr Gawron <piotr.gawron@uni.lu> Date: Mon, 19 Nov 2018 13:15:06 +0100 Subject: [PATCH] warnings are passed to the sbgn file --- .../model/sbgnml/SbgnmlXmlConverter.java | 141 +++++++++++------- .../model/sbgnml/SbgnmlXmlExporter.java | 1 - .../model/sbgnml/CellDesignerToSbgnTest.java | 101 +++++++------ 3 files changed, 146 insertions(+), 97 deletions(-) diff --git a/converter-SBGNML/src/main/java/lcsb/mapviewer/converter/model/sbgnml/SbgnmlXmlConverter.java b/converter-SBGNML/src/main/java/lcsb/mapviewer/converter/model/sbgnml/SbgnmlXmlConverter.java index 3618c14e95..08174a6e20 100644 --- a/converter-SBGNML/src/main/java/lcsb/mapviewer/converter/model/sbgnml/SbgnmlXmlConverter.java +++ b/converter-SBGNML/src/main/java/lcsb/mapviewer/converter/model/sbgnml/SbgnmlXmlConverter.java @@ -5,11 +5,22 @@ package lcsb.mapviewer.converter.model.sbgnml; import java.io.File; import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import javax.xml.bind.JAXBException; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringEscapeUtils; +import org.apache.log4j.Logger; +import org.apache.log4j.spi.LoggingEvent; +import org.apache.tools.ant.filters.StringInputStream; +import org.sbgn.SbgnUtil; +import org.sbgn.bindings.Sbgn; + +import lcsb.mapviewer.common.EventStorageLoggerAppender; import lcsb.mapviewer.common.MimeType; import lcsb.mapviewer.converter.ConverterException; import lcsb.mapviewer.converter.ConverterParams; @@ -18,67 +29,95 @@ import lcsb.mapviewer.converter.InvalidInputDataExecption; import lcsb.mapviewer.model.map.InconsistentModelException; import lcsb.mapviewer.model.map.model.Model; -import org.sbgn.SbgnUtil; -import org.sbgn.bindings.Sbgn; - /** * @author Michał Kuźma * */ public class SbgnmlXmlConverter implements IConverter { + Logger logger = Logger.getLogger(SbgnmlXmlConverter.class); + + @Override + public Model createModel(ConverterParams params) throws InvalidInputDataExecption { + SbgnmlXmlParser parser = new SbgnmlXmlParser(); + return parser.createModel(params); + } + + @Override + public InputStream exportModelToInputStream(Model model) throws InconsistentModelException, ConverterException { + EventStorageLoggerAppender appender = new EventStorageLoggerAppender(); + Sbgn exportedData; + StringBuilder notes = new StringBuilder(""); + try { + Logger.getRootLogger().addAppender(appender); + SbgnmlXmlExporter exporter = new SbgnmlXmlExporter(); + exportedData = exporter.toSbgnml(model); + if (!appender.getWarnings().isEmpty()) { + for (LoggingEvent event : appender.getWarnings()) { + if (event.getMessage() instanceof String) { + notes.append("\n" + ((String) event.getMessage())); + } + } + } + } finally { + Logger.getRootLogger().removeAppender(appender); + } + try { + File tempFile = File.createTempFile("sbgnmlExportTempFile", ".tmp"); + InputStream result; + SbgnUtil.writeToFile(exportedData, tempFile); + try (FileInputStream inputStream = new FileInputStream(tempFile)) { + String xml = IOUtils.toString(inputStream, "UTF-8"); + if (notes.length() > 0) { + int position = xml.indexOf("<map language"); + if (position < 0) { + throw new ConverterException("problematic output sbgn. Cannot find map tag"); + } + String notesNode = "<notes><html:body xmlns:html=\"http://www.w3.org/1999/xhtml\">" + + StringEscapeUtils.escapeXml(notes.toString()) + "\n</html:body></notes> "; + xml = xml.substring(0, position) + notesNode + xml.substring(position, xml.length()); + } + result = new StringInputStream(xml); + } + return result; + } catch (IOException e) { + throw new ConverterException(e); + } catch (JAXBException e) { + throw new ConverterException(e); + } + } - @Override - public Model createModel(ConverterParams params) throws InvalidInputDataExecption { - SbgnmlXmlParser parser = new SbgnmlXmlParser(); - return parser.createModel(params); - } + @Override + public File exportModelToFile(Model model, String filePath) throws InconsistentModelException, ConverterException { + InputStream inputStream = exportModelToInputStream(model); - @Override - public InputStream exportModelToInputStream(Model model) throws InconsistentModelException, ConverterException { - SbgnmlXmlExporter exporter = new SbgnmlXmlExporter(); - Sbgn exportedData = exporter.toSbgnml(model); - File tempFile; - try { - tempFile = File.createTempFile("sbgnmlExportTempFile", ".tmp"); - SbgnUtil.writeToFile(exportedData, tempFile); - InputStream result = new FileInputStream(tempFile); - return result; - } catch (IOException e) { - throw new ConverterException(e); - } catch (JAXBException e) { - throw new ConverterException(e); - } - } + try { + File exportedFile = new File(filePath); + if (exportedFile.getParentFile() != null) { + exportedFile.getParentFile().mkdirs(); + } + OutputStream outputStream = new FileOutputStream(exportedFile); + IOUtils.copy(inputStream, outputStream); + outputStream.close(); - @Override - public File exportModelToFile(Model model, String filePath) throws InconsistentModelException, ConverterException { - SbgnmlXmlExporter exporter = new SbgnmlXmlExporter(); - Sbgn exportedData = exporter.toSbgnml(model); - File exportedFile = new File(filePath); - if (exportedFile.getParentFile() != null) { - exportedFile.getParentFile().mkdirs(); - } - try { - SbgnUtil.writeToFile(exportedData, exportedFile); - } catch (JAXBException e) { - throw new ConverterException(e); - } - return exportedFile; - } + return exportedFile; + } catch (IOException e) { + throw new ConverterException(e); + } + } - @Override - public String getCommonName() { - return "SBGN-ML"; - } + @Override + public String getCommonName() { + return "SBGN-ML"; + } - @Override - public MimeType getMimeType() { - return MimeType.TEXT; - } + @Override + public MimeType getMimeType() { + return MimeType.TEXT; + } - @Override - public String getFileExtension() { - return "sbgn"; - } + @Override + public String getFileExtension() { + return "sbgn"; + } } diff --git a/converter-SBGNML/src/main/java/lcsb/mapviewer/converter/model/sbgnml/SbgnmlXmlExporter.java b/converter-SBGNML/src/main/java/lcsb/mapviewer/converter/model/sbgnml/SbgnmlXmlExporter.java index 0015b30511..824664c47b 100644 --- a/converter-SBGNML/src/main/java/lcsb/mapviewer/converter/model/sbgnml/SbgnmlXmlExporter.java +++ b/converter-SBGNML/src/main/java/lcsb/mapviewer/converter/model/sbgnml/SbgnmlXmlExporter.java @@ -187,7 +187,6 @@ public class SbgnmlXmlExporter { } sbgnData.setMap(map); - return sbgnData; } diff --git a/converter-SBGNML/src/test/java/lcsb/mapviewer/converter/model/sbgnml/CellDesignerToSbgnTest.java b/converter-SBGNML/src/test/java/lcsb/mapviewer/converter/model/sbgnml/CellDesignerToSbgnTest.java index bc4b09114b..edaea41e34 100644 --- a/converter-SBGNML/src/test/java/lcsb/mapviewer/converter/model/sbgnml/CellDesignerToSbgnTest.java +++ b/converter-SBGNML/src/test/java/lcsb/mapviewer/converter/model/sbgnml/CellDesignerToSbgnTest.java @@ -1,7 +1,11 @@ package lcsb.mapviewer.converter.model.sbgnml; +import static org.junit.Assert.assertTrue; + import java.io.File; +import java.io.FileInputStream; +import org.apache.commons.io.IOUtils; import org.apache.log4j.Logger; import org.junit.After; import org.junit.Before; @@ -13,50 +17,57 @@ import lcsb.mapviewer.converter.model.celldesigner.CellDesignerXmlParser; import lcsb.mapviewer.model.map.model.Model; public class CellDesignerToSbgnTest { - Logger logger = Logger.getLogger(CellDesignerToSbgnTest.class); - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testSample() throws Exception { - try { - IConverter converter = new CellDesignerXmlParser(); - IConverter converter2 = new SbgnmlXmlConverter(); - - Model model; - model = converter.createModel(new ConverterParams().filename("testFiles/cellDesigner/sample.xml")); - - String output = File.createTempFile("temp-sbgn-output", ".sbgn").getAbsolutePath(); - converter2.exportModelToFile(model, output); - new File(output).delete(); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testSample2() throws Exception { - try { - IConverter converter = new CellDesignerXmlParser(); - IConverter converter2 = new SbgnmlXmlConverter(); - - Model model; - model = converter.createModel(new ConverterParams().filename("testFiles/cellDesigner/bubbles.xml")); - - String output = File.createTempFile("temp-sbgn-output", ".sbgn").getAbsolutePath(); - converter2.exportModelToFile(model, output); - new File(output).delete(); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } + Logger logger = Logger.getLogger(CellDesignerToSbgnTest.class); + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testSample() throws Exception { + try { + IConverter converter = new CellDesignerXmlParser(); + IConverter converter2 = new SbgnmlXmlConverter(); + + Model model = converter.createModel(new ConverterParams().filename("testFiles/cellDesigner/sample.xml")); + + String output = File.createTempFile("temp-sbgn-output", ".sbgn").getAbsolutePath(); + converter2.exportModelToFile(model, output); + + try (FileInputStream inputStream = new FileInputStream(output)) { + String everything = IOUtils.toString(inputStream, "UTF-8"); + assertTrue("Warnings are not exported into sbgn", everything.contains("Element type is not supported")); + } + converter2.createModel(new ConverterParams().filename(output)); + + new File(output).delete(); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + + } + + @Test + public void testSample2() throws Exception { + try { + IConverter converter = new CellDesignerXmlParser(); + IConverter converter2 = new SbgnmlXmlConverter(); + + Model model; + model = converter.createModel(new ConverterParams().filename("testFiles/cellDesigner/bubbles.xml")); + + String output = File.createTempFile("temp-sbgn-output", ".sbgn").getAbsolutePath(); + converter2.exportModelToFile(model, output); + new File(output).delete(); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } } -- GitLab