diff --git a/commons/src/main/java/lcsb/mapviewer/common/XmlParser.java b/commons/src/main/java/lcsb/mapviewer/common/XmlParser.java index 0d5ab74603a074c3ba1b4dab1a38ad7b8d920a6e..be3ac5c7e215a327272a02e4e85324575d628737 100644 --- a/commons/src/main/java/lcsb/mapviewer/common/XmlParser.java +++ b/commons/src/main/java/lcsb/mapviewer/common/XmlParser.java @@ -31,7 +31,6 @@ import org.xml.sax.InputSource; import org.xml.sax.SAXException; import lcsb.mapviewer.common.exception.InvalidArgumentException; -import lcsb.mapviewer.common.exception.InvalidStateException; import lcsb.mapviewer.common.exception.InvalidXmlSchemaException; /** @@ -40,7 +39,7 @@ import lcsb.mapviewer.common.exception.InvalidXmlSchemaException; * @author Piotr Gawron * */ -public class XmlParser { +final public class XmlParser { /** * Default class logger. @@ -53,38 +52,35 @@ public class XmlParser { private static final int HEX_BASE = 16; /** - * {@link DocumentBuilderFactory} used to create {@link DocumentBuilder} objects - * that will manipulate xml nodes. + * {@link DocumentBuilder} objects that will manipulate xml nodes. */ - private static DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); - private static DocumentBuilderFactory nonValidateDocumentBuilderFactory; + private static DocumentBuilder validatedDocumentBuilder; + private static DocumentBuilder nonValidatedDocumentBuilder; + static { - nonValidateDocumentBuilderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilderFactory nonValidateDocumentBuilderFactory = DocumentBuilderFactory.newInstance(); nonValidateDocumentBuilderFactory.setNamespaceAware(false); nonValidateDocumentBuilderFactory.setValidating(false); try { - nonValidateDocumentBuilderFactory.setFeature("http://xml.org/sax/features/namespaces", false); - nonValidateDocumentBuilderFactory.setFeature("http://xml.org/sax/features/validation", false); - nonValidateDocumentBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", - false); - nonValidateDocumentBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", - false); + nonValidateDocumentBuilderFactory.setFeature( + "http://xml.org/sax/features/namespaces", false); + nonValidateDocumentBuilderFactory.setFeature( + "http://xml.org/sax/features/validation", false); + nonValidateDocumentBuilderFactory.setFeature( + "http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false); + nonValidateDocumentBuilderFactory.setFeature( + "http://apache.org/xml/features/nonvalidating/load-external-dtd", false); + nonValidatedDocumentBuilder = nonValidateDocumentBuilderFactory.newDocumentBuilder(); + validatedDocumentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); } catch (ParserConfigurationException e) { logger.error(e, e); } } /** - * DOM document builder used for xml transformations. - */ - private DocumentBuilder validatedDocumentBuilder; - - private DocumentBuilder nonValidatedDocumentBuilder; - - /** - * Default constructor that prevents from instantiation of the class. + * private constructor. This class has no state and contains only static utility methods. */ - protected XmlParser() { + private XmlParser() { } /** @@ -98,7 +94,7 @@ public class XmlParser { * @return node from nodes list with the tagName name, <b>null</b> if such node * doesn't exist */ - protected Node getNode(final String tagName, final NodeList nodes) { + public static Node getNode(final String tagName, final NodeList nodes) { for (int x = 0; x < nodes.getLength(); x++) { Node node = nodes.item(x); if (node.getNodeType() == Node.ELEMENT_NODE) { @@ -121,7 +117,7 @@ public class XmlParser { * @return node from nodes list with the tagName name, <b>null</b> if such node * doesn't exist */ - protected Node getNode(final String tagName, final Node parentNode) { + public static Node getNode(final String tagName, final Node parentNode) { return getNode(tagName, parentNode.getChildNodes()); } @@ -135,7 +131,7 @@ public class XmlParser { * list of input nodes * @return list of nodes with 'tagName' name */ - protected List<Node> getNodes(final String tagName, final NodeList nodes) { + public static List<Node> getNodes(final String tagName, final NodeList nodes) { List<Node> result = new ArrayList<Node>(); for (int x = 0; x < nodes.getLength(); x++) { Node node = nodes.item(x); @@ -157,7 +153,7 @@ public class XmlParser { * @return the value of node attribute, empty string("") if attribute doesn't * exist */ - protected String getNodeAttr(final String attrName, final Node node) { + public static String getNodeAttr(final String attrName, final Node node) { NamedNodeMap attrs = node.getAttributes(); for (int y = 0; y < attrs.getLength(); y++) { Node attr = attrs.item(y); @@ -177,7 +173,7 @@ public class XmlParser { * @return the text value of node or empty string ("") if the text could be * found. */ - protected String getNodeValue(final Node node) { + static String getNodeValue(final Node node) { if (node == null) { return ""; } @@ -200,44 +196,16 @@ public class XmlParser { * @throws InvalidXmlSchemaException * thrown when there is a problem with xml */ - protected Document getXmlDocumentFromInputSource(final InputSource stream, boolean validate) + static Document getXmlDocumentFromInputSource(final InputSource stream, boolean validate) throws InvalidXmlSchemaException { - try { - DocumentBuilder db = getDocumentBuilder(validate); - synchronized (db) { // DocumentBuilder cannot parse few objects at the - // same time - return db.parse(stream); - } - } catch (SAXException e) { - throw new InvalidXmlSchemaException("Problem with xml parser", e); - } catch (IOException e) { - throw new InvalidXmlSchemaException("Problem with xml parser", e); - } - } - - /** - * Return proper document builder. - * - * @param validate - * - should the document builder validate input xml or not - * @return - */ - private DocumentBuilder getDocumentBuilder(boolean validate) { try { if (validate) { - if (validatedDocumentBuilder == null) { - validatedDocumentBuilder = documentBuilderFactory.newDocumentBuilder(); - } - return validatedDocumentBuilder; + return validatedDocumentBuilder.parse(stream); } else { - if (nonValidatedDocumentBuilder == null) { - nonValidatedDocumentBuilder = nonValidateDocumentBuilderFactory.newDocumentBuilder(); - } - return nonValidatedDocumentBuilder; - + return nonValidatedDocumentBuilder.parse(stream); } - } catch (ParserConfigurationException e) { - throw new InvalidStateException("Problem with xml parser"); + } catch (SAXException | IOException e) { + throw new InvalidXmlSchemaException("Problem with xml parser", e); } } @@ -254,7 +222,7 @@ public class XmlParser { * @throws InvalidXmlSchemaException * thrown when there is a problem with xml */ - protected Document getXmlDocumentFromString(final String text, boolean validate) throws InvalidXmlSchemaException { + private static Document getXmlDocumentFromString(final String text, boolean validate) throws InvalidXmlSchemaException { InputSource is = new InputSource(); is.setCharacterStream(new StringReader(text)); try { @@ -274,7 +242,7 @@ public class XmlParser { * @throws InvalidXmlSchemaException * thrown when there is a problem with xml */ - protected Document getXmlDocumentFromString(final String text) throws InvalidXmlSchemaException { + public static Document getXmlDocumentFromString(final String text) throws InvalidXmlSchemaException { return getXmlDocumentFromString(text, true); } @@ -285,7 +253,7 @@ public class XmlParser { * node that should be transformed into xml string * @return string representation of the xml node */ - protected String nodeToString(final Node node) { + static String nodeToString(final Node node) { return nodeToString(node, false); } @@ -298,7 +266,7 @@ public class XmlParser { * should the top level node exist in the output * @return string representation of the xml node */ - protected String nodeToString(final Node node, final boolean includeHeadNode) { + static String nodeToString(final Node node, final boolean includeHeadNode) { if (node == null) { return null; } @@ -331,7 +299,7 @@ public class XmlParser { * string representing color * @return Color object for the fiven string */ - protected Color stringToColor(final String color) { + static Color stringToColor(final String color) { try { String alpha = color.substring(0, 2); Color tmp = new Color(hexToInteger(color.substring(2))); @@ -348,7 +316,7 @@ public class XmlParser { * string representation in hex base * @return Integer value of the hex string */ - private Integer hexToInteger(String hexString) { + static Integer hexToInteger(String hexString) { return Integer.valueOf(hexString, HEX_BASE); } @@ -359,7 +327,7 @@ public class XmlParser { * color that should be converted into string * @return hex string representation of the color */ - protected String colorToString(final Color color) { + static String colorToString(final Color color) { return String.format("%08X", color.getRGB()); } @@ -372,7 +340,7 @@ public class XmlParser { * @throws IOException * thrown when there are some problems with a file */ - protected String fileToString(final String fileName) throws IOException { + static String fileToString(final String fileName) throws IOException { BufferedReader reader = new BufferedReader(new FileReader(fileName)); String line = null; StringBuilder stringBuilder = new StringBuilder(); @@ -397,11 +365,10 @@ public class XmlParser { * @throws IOException * thrown if there are some problems with input stream */ - protected String inputStreamToString(final InputStream inputStream) throws IOException { + static String inputStreamToString(final InputStream inputStream) throws IOException { StringWriter writer = new StringWriter(); IOUtils.copy(inputStream, writer, "UTF-8"); - String result = writer.toString(); - return result; + return writer.toString(); } /** @@ -411,7 +378,7 @@ public class XmlParser { * string to be escaped * @return escaped string, ready to be used in xml */ - protected String escapeXml(final String string) { + public static String escapeXml(final String string) { if (string == null) { return null; } @@ -419,7 +386,7 @@ public class XmlParser { return StringEscapeUtils.escapeXml10(string).replaceAll("\n", " ").replace("\r", " "); } - public List<Node> getAllNotNecessirellyDirectChild(String tagName, Node root) { + private static List<Node> getAllNotNecessirellyDirectChild(String tagName, Node root) { List<Node> result = new ArrayList<>(); for (int x = 0; x < root.getChildNodes().getLength(); x++) { Node node = root.getChildNodes().item(x); diff --git a/commons/src/test/java/lcsb/mapviewer/common/XmlParserTest.java b/commons/src/test/java/lcsb/mapviewer/common/XmlParserTest.java index a796375a3ca3fd7a4e5b880dcf9e1da55f74def2..7ec5e6faaa99372507a88bca8e87cf378ae21433 100644 --- a/commons/src/test/java/lcsb/mapviewer/common/XmlParserTest.java +++ b/commons/src/test/java/lcsb/mapviewer/common/XmlParserTest.java @@ -38,8 +38,6 @@ import lcsb.mapviewer.common.exception.InvalidXmlSchemaException; public class XmlParserTest { Logger logger = Logger.getLogger(XmlParserTest.class); - XmlParser parser = new XmlParser(); - @Before public void setUp() throws Exception { } @@ -53,11 +51,11 @@ public class XmlParserTest { try { String strColor = "ffcbcd09"; - Color c = parser.stringToColor(strColor); - String resultString = parser.colorToString(c); + Color c = XmlParser.stringToColor(strColor); + String resultString = XmlParser.colorToString(c); assertTrue("Different string representation: " + strColor + " - " + resultString, strColor.equalsIgnoreCase(resultString)); - Color c2 = parser.stringToColor(resultString); + Color c2 = XmlParser.stringToColor(resultString); assertEquals(c.getRed(), c2.getRed()); assertEquals(c.getGreen(), c2.getGreen()); @@ -73,7 +71,7 @@ public class XmlParserTest { try { String strColor = "hello world"; - parser.stringToColor(strColor); + XmlParser.stringToColor(strColor); fail("Exception expected"); } catch (InvalidArgumentException e) { } catch (Exception e) { @@ -87,11 +85,11 @@ public class XmlParserTest { try { String strColor = "fecbcd09"; - Color c = parser.stringToColor(strColor); - String resultString = parser.colorToString(c); + Color c = XmlParser.stringToColor(strColor); + String resultString = XmlParser.colorToString(c); assertTrue("Different string representation: " + strColor + " - " + resultString, strColor.equalsIgnoreCase(resultString)); - Color c2 = parser.stringToColor(resultString); + Color c2 = XmlParser.stringToColor(resultString); assertEquals(c.getRed(), c2.getRed()); assertEquals(c.getGreen(), c2.getGreen()); @@ -106,7 +104,7 @@ public class XmlParserTest { @Test public void testGetXmlDocumentFromString() throws Exception { try { - Document validDoc = parser.getXmlDocumentFromString("<node>test</node>"); + Document validDoc = XmlParser.getXmlDocumentFromString("<node>test</node>"); assertNotNull(validDoc); } catch (Exception e) { e.printStackTrace(); @@ -119,8 +117,8 @@ public class XmlParserTest { public void testNodeToString() throws Exception { try { String xml = "<test_node>test_x</test_node>"; - Document validDoc = parser.getXmlDocumentFromString(xml); - String str = parser.nodeToString(validDoc); + Document validDoc = XmlParser.getXmlDocumentFromString(xml); + String str = XmlParser.nodeToString(validDoc); assertEquals(xml.trim(), str.trim()); } catch (Exception e) { e.printStackTrace(); @@ -132,7 +130,7 @@ public class XmlParserTest { @Test public void testEmptyNodeToString() throws Exception { try { - String str = parser.nodeToString(null); + String str = XmlParser.nodeToString(null); assertNull(str); } catch (Exception e) { e.printStackTrace(); @@ -145,8 +143,8 @@ public class XmlParserTest { public void testNodeToStringWithHeader() throws Exception { try { String xml = "<test_node>test_x</test_node>"; - Document validDoc = parser.getXmlDocumentFromString(xml); - String str = parser.nodeToString(validDoc, true); + Document validDoc = XmlParser.getXmlDocumentFromString(xml); + String str = XmlParser.nodeToString(validDoc, true); assertTrue(str.contains(xml)); } catch (Exception e) { e.printStackTrace(); @@ -180,7 +178,7 @@ public class XmlParserTest { ; Element el = new Tmp(); root.appendChild(el); - assertNotNull(parser.nodeToString(root, true)); + assertNotNull(XmlParser.nodeToString(root, true)); } catch (Exception e) { e.printStackTrace(); throw e; @@ -190,7 +188,7 @@ public class XmlParserTest { @Test public void testGetXmlDocumentFromInvalidString() throws Exception { try { - parser.getXmlDocumentFromString("<node>test<node>"); + XmlParser.getXmlDocumentFromString("<node>test<node>"); fail("Exception expected"); } catch (InvalidXmlSchemaException e) { } catch (Exception e) { @@ -203,7 +201,7 @@ public class XmlParserTest { @Test public void testGetXmlDocumentFromInvalidInputStream() throws Exception { try { - parser.getXmlDocumentFromInputSource(new InputSource(), true); + XmlParser.getXmlDocumentFromInputSource(new InputSource(), true); fail("Exception expected"); } catch (InvalidXmlSchemaException e) { } catch (Exception e) { @@ -216,7 +214,7 @@ public class XmlParserTest { @Test public void testEscapeXml() throws Exception { try { - String str = parser.escapeXml("<xml>node</xml>"); + String str = XmlParser.escapeXml("<xml>node</xml>"); assertFalse(str.contains("<")); } catch (Exception e) { e.printStackTrace(); @@ -228,7 +226,7 @@ public class XmlParserTest { @Test public void testEscapeNullXml() throws Exception { try { - String str = parser.escapeXml(null); + String str = XmlParser.escapeXml(null); assertNull(str); } catch (Exception e) { e.printStackTrace(); @@ -240,7 +238,7 @@ public class XmlParserTest { @Test public void testFileToString() throws Exception { try { - String str = parser.fileToString("testFiles/test.txt"); + String str = XmlParser.fileToString("testFiles/test.txt"); assertTrue(str.contains("test")); assertTrue(str.contains("file")); assertTrue(str.contains("with some content")); @@ -255,7 +253,7 @@ public class XmlParserTest { public void testInputStreamToString() throws Exception { try { InputStream stream = new ByteArrayInputStream("stream string".getBytes(StandardCharsets.UTF_8)); - String str = parser.inputStreamToString(stream); + String str = XmlParser.inputStreamToString(stream); assertEquals("stream string", str); } catch (Exception e) { e.printStackTrace(); @@ -267,9 +265,9 @@ public class XmlParserTest { @Test public void testGetNodeValue() throws Exception { try { - Document document = parser.getXmlDocumentFromString("<node>content</node>"); - Node node = parser.getNode("node", document); - String str = parser.getNodeValue(node); + Document document = XmlParser.getXmlDocumentFromString("<node>content</node>"); + Node node = XmlParser.getNode("node", document); + String str = XmlParser.getNodeValue(node); assertEquals("content", str); } catch (Exception e) { e.printStackTrace(); @@ -281,7 +279,7 @@ public class XmlParserTest { @Test public void testGetNodeValue2() throws Exception { try { - String str = parser.getNodeValue(null); + String str = XmlParser.getNodeValue(null); assertEquals("", str); } catch (Exception e) { e.printStackTrace(); @@ -293,9 +291,9 @@ public class XmlParserTest { @Test public void testGetNodeValue3() throws Exception { try { - Document document = parser.getXmlDocumentFromString("<node></node>"); - Node node = parser.getNode("node", document); - String str = parser.getNodeValue(node); + Document document = XmlParser.getXmlDocumentFromString("<node></node>"); + Node node = XmlParser.getNode("node", document); + String str = XmlParser.getNodeValue(node); assertEquals("", str); } catch (Exception e) { e.printStackTrace(); @@ -307,9 +305,9 @@ public class XmlParserTest { @Test public void testGetNodeValue4() throws Exception { try { - Document document = parser.getXmlDocumentFromString("<node><subnode/></node>"); - Node node = parser.getNode("node", document); - String str = parser.getNodeValue(node); + Document document = XmlParser.getXmlDocumentFromString("<node><subnode/></node>"); + Node node = XmlParser.getNode("node", document); + String str = XmlParser.getNodeValue(node); assertEquals("", str); } catch (Exception e) { e.printStackTrace(); @@ -321,7 +319,7 @@ public class XmlParserTest { @Test public void testInputStreamToStringThrowsException() throws Exception { try { - parser.inputStreamToString(new InputStream() { + XmlParser.inputStreamToString(new InputStream() { @Override public int read() throws IOException { throw new IOException(); @@ -339,7 +337,7 @@ public class XmlParserTest { @Test public void testFileToStringThrowsException() throws Exception { try { - parser.fileToString("testFiles/unknown file.txt"); + XmlParser.fileToString("testFiles/unknown file.txt"); fail("Exception expected"); } catch (IOException e) { } catch (Exception e) { @@ -352,11 +350,11 @@ public class XmlParserTest { @Test public void testGetNodeAttr() throws Exception { try { - Document document = parser.getXmlDocumentFromString("<node attr=\"val\">content</node>"); - Node node = parser.getNode("node", document); - String str = parser.getNodeAttr("attr", node); + Document document = XmlParser.getXmlDocumentFromString("<node attr=\"val\">content</node>"); + Node node = XmlParser.getNode("node", document); + String str = XmlParser.getNodeAttr("attr", node); assertEquals("val", str); - str = parser.getNodeAttr("attr2", node); + str = XmlParser.getNodeAttr("attr2", node); assertEquals("", str); } catch (Exception e) { e.printStackTrace(); @@ -368,9 +366,9 @@ public class XmlParserTest { @Test public void testGetNodes() throws Exception { try { - Document document = parser.getXmlDocumentFromString("<node><subnode>content1</subnode><subnode>content2</subnode><other/></node>"); - Node node = parser.getNode("node", document); - List<Node> nodes = parser.getNodes("subnode", node.getChildNodes()); + Document document = XmlParser.getXmlDocumentFromString("<node><subnode>content1</subnode><subnode>content2</subnode><other/></node>"); + Node node = XmlParser.getNode("node", document); + List<Node> nodes = XmlParser.getNodes("subnode", node.getChildNodes()); assertEquals(2, nodes.size()); } catch (Exception e) { e.printStackTrace(); @@ -382,9 +380,9 @@ public class XmlParserTest { @Test public void testGetNode() throws Exception { try { - Document document = parser.getXmlDocumentFromString("<node><subnode>content1</subnode><subnode>content2</subnode><other/></node>"); - Node node = parser.getNode("node", document); - Node child = parser.getNode("other", node.getChildNodes()); + Document document = XmlParser.getXmlDocumentFromString("<node><subnode>content1</subnode><subnode>content2</subnode><other/></node>"); + Node node = XmlParser.getNode("node", document); + Node child = XmlParser.getNode("other", node.getChildNodes()); assertNotNull(child); } catch (Exception e) { e.printStackTrace(); @@ -396,9 +394,9 @@ public class XmlParserTest { @Test public void testGetNode2() throws Exception { try { - Document document = parser.getXmlDocumentFromString("<node attr=\"x\"><subnode>content1</subnode><subnode>content2</subnode><other/></node>"); - Node node = parser.getNode("node", document); - Node child = parser.getNode("other2", node.getChildNodes()); + Document document = XmlParser.getXmlDocumentFromString("<node attr=\"x\"><subnode>content1</subnode><subnode>content2</subnode><other/></node>"); + Node node = XmlParser.getNode("node", document); + Node child = XmlParser.getNode("other2", node.getChildNodes()); assertNull(child); } catch (Exception e) { e.printStackTrace(); @@ -410,9 +408,9 @@ public class XmlParserTest { @Test public void testGetNode3() throws Exception { try { - Document document = parser.getXmlDocumentFromString("<node attr=\"x\">xxx</node>"); - Node node = parser.getNode("node", document); - Node child = parser.getNode("other2", node.getChildNodes()); + Document document = XmlParser.getXmlDocumentFromString("<node attr=\"x\">xxx</node>"); + Node node = XmlParser.getNode("node", document); + Node child = XmlParser.getNode("other2", node.getChildNodes()); assertNull(child); } catch (Exception e) { e.printStackTrace(); 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 44ebeb6677f5790802ad14a6a6298cb5e8d13496..3027456c2ffc4a0c59823a0a4d7e558537b3089c 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 @@ -2,14 +2,10 @@ package lcsb.mapviewer.converter.model.celldesigner; import java.awt.geom.Line2D; import java.awt.geom.Rectangle2D; -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; import java.util.ArrayList; import java.util.List; +import lcsb.mapviewer.common.XmlParser; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.StringEscapeUtils; import org.apache.log4j.Logger; @@ -22,10 +18,9 @@ import org.w3c.dom.NodeList; import lcsb.mapviewer.common.EventStorageLoggerAppender; import lcsb.mapviewer.common.MimeType; import lcsb.mapviewer.common.Pair; -import lcsb.mapviewer.common.XmlParser; import lcsb.mapviewer.common.exception.InvalidXmlSchemaException; import lcsb.mapviewer.converter.ConverterParams; -import lcsb.mapviewer.converter.IConverter; +import lcsb.mapviewer.converter.Converter; import lcsb.mapviewer.converter.InvalidInputDataExecption; import lcsb.mapviewer.converter.model.celldesigner.alias.AliasCollectionXmlParser; import lcsb.mapviewer.converter.model.celldesigner.annotation.RestAnnotationParser; @@ -62,9 +57,9 @@ import lcsb.mapviewer.model.map.species.Species; * cases: * <ul> * <li>CellDesigner xml -> our model. To perform this action - * {@link #createModel(Params)} method should be called.</li> + * {@link Converter#createModel(ConverterParams)}} method should be called.</li> * <li>our model -> CellDesigner xml. To perform this action - * {@link #toXml(Model)} method should be called.</li> + * {@link #model2Xml(Model)} method should be called.</li> * </ul> * <p/> * CellDEsigner format is the extension of <a href="http://sbml.org">SBML</a>. @@ -75,7 +70,7 @@ import lcsb.mapviewer.model.map.species.Species; * @author Piotr Gawron * */ -public class CellDesignerXmlParser extends XmlParser implements IConverter { +public class CellDesignerXmlParser extends Converter { /** * Default class logger. @@ -137,20 +132,20 @@ public class CellDesignerXmlParser extends XmlParser implements IConverter { NodeList root = doc.getChildNodes(); // Navigate down the hierarchy to get to the CEO node - Node sbmlNode = getNode("SBML", root); + Node sbmlNode = XmlParser.getNode("SBML", root); if (sbmlNode == null) { throw new InvalidInputDataExecption("No SBML node"); } - Node modelNode = getNode("model", sbmlNode.getChildNodes()); + Node modelNode = XmlParser.getNode("model", sbmlNode.getChildNodes()); if (modelNode == null) { throw new InvalidInputDataExecption("No model node in SBML"); } // we ignore metaid - it's useless and obstruct data model - // model.setMetaId(getNodeAttr("metaId", modelNode)); - model.setIdModel(getNodeAttr("id", modelNode)); + // model.setMetaId(XmlParser.getNodeAttr("metaId", modelNode)); + model.setIdModel(XmlParser.getNodeAttr("id", modelNode)); - Node compartmentNode = getNode("listOfCompartments", modelNode.getChildNodes()); + Node compartmentNode = XmlParser.getNode("listOfCompartments", modelNode.getChildNodes()); if (compartmentNode != null) { List<CellDesignerCompartment> compartments = compartmentCollectionXmlParser .parseXmlCompartmentCollection(compartmentNode); @@ -159,7 +154,7 @@ public class CellDesignerXmlParser extends XmlParser implements IConverter { InternalModelSpeciesData modelData = new InternalModelSpeciesData(); - Node speciesNode = getNode("listOfSpecies", modelNode.getChildNodes()); + Node speciesNode = XmlParser.getNode("listOfSpecies", modelNode.getChildNodes()); if (speciesNode != null) { List<Pair<String, ? extends CellDesignerSpecies<?>>> species = speciesSbmlParser .parseSbmlSpeciesCollection(speciesNode); @@ -209,7 +204,7 @@ public class CellDesignerXmlParser extends XmlParser implements IConverter { model.addParameters(parameterParser.parseXmlParameterCollection(parametersNode)); } - Node annotationNode = getNode("annotation", modelNode.getChildNodes()); + Node annotationNode = XmlParser.getNode("annotation", modelNode.getChildNodes()); if (annotationNode == null) { throw new InvalidInputDataExecption("No annotation node in SBML/model"); } @@ -357,7 +352,7 @@ public class CellDesignerXmlParser extends XmlParser implements IConverter { CellDesignerElementCollection elements) throws InvalidXmlSchemaException { SpeciesCollectionXmlParser parser = new SpeciesCollectionXmlParser(elements); - Node extensionNode = getNode("celldesigner:extension", annotationNode.getChildNodes()); + Node extensionNode = XmlParser.getNode("celldesigner:extension", annotationNode.getChildNodes()); if (extensionNode == null) { throw new InvalidXmlSchemaException("No celldesigner:extension node in SBML/model/annotation"); } @@ -380,8 +375,8 @@ public class CellDesignerXmlParser extends XmlParser implements IConverter { // model.setVersion(getNodeValue(node)); continue; } else if (node.getNodeName().equalsIgnoreCase("celldesigner:modelDisplay")) { - model.setWidth(getNodeAttr("sizeX", node)); - model.setHeight(getNodeAttr("sizeY", node)); + model.setWidth(XmlParser.getNodeAttr("sizeX", node)); + model.setHeight(XmlParser.getNodeAttr("sizeY", node)); } else if (node.getNodeName().equalsIgnoreCase("celldesigner:listOfSpeciesAliases")) { listofSpeciesAlias = node; } else if (node.getNodeName().equalsIgnoreCase("celldesigner:listOfComplexSpeciesAliases")) { @@ -468,8 +463,8 @@ public class CellDesignerXmlParser extends XmlParser implements IConverter { } if (includedSpecies != null) { - parseAnnotationAliasesConnections(model, getNode("celldesigner:listOfSpeciesAliases", nodes)); - parseAnnotationComplexAliasesConnections(model, getNode("celldesigner:listOfComplexSpeciesAliases", nodes)); + parseAnnotationAliasesConnections(model, XmlParser.getNode("celldesigner:listOfSpeciesAliases", nodes)); + parseAnnotationComplexAliasesConnections(model, XmlParser.getNode("celldesigner:listOfComplexSpeciesAliases", nodes)); } if (listOfGroups != null) { @@ -496,8 +491,8 @@ public class CellDesignerXmlParser extends XmlParser implements IConverter { Node node = nodes.item(x); if (node.getNodeType() == Node.ELEMENT_NODE) { if (node.getNodeName().equalsIgnoreCase("celldesigner:complexSpeciesAlias")) { - String aliasId = getNodeAttr("id", node); - String complexId = getNodeAttr("complexSpeciesAlias", node); + String aliasId = XmlParser.getNodeAttr("id", node); + String complexId = XmlParser.getNodeAttr("complexSpeciesAlias", node); Complex alias = model.getElementByElementId(aliasId); if (alias == null) { throw new InvalidXmlSchemaException("Alias does not exist " + aliasId); @@ -534,8 +529,8 @@ public class CellDesignerXmlParser extends XmlParser implements IConverter { Node node = nodes.item(x); if (node.getNodeType() == Node.ELEMENT_NODE) { if (node.getNodeName().equalsIgnoreCase("celldesigner:speciesAlias")) { - String aliasId = getNodeAttr("id", node); - String complexId = getNodeAttr("complexSpeciesAlias", node); + String aliasId = XmlParser.getNodeAttr("id", node); + String complexId = XmlParser.getNodeAttr("complexSpeciesAlias", node); Element alias = model.getElementByElementId(aliasId); if (alias == null) { throw new InvalidXmlSchemaException("Alias does not exist " + aliasId); @@ -555,8 +550,9 @@ public class CellDesignerXmlParser extends XmlParser implements IConverter { } } - public String toXml(Model model) throws InconsistentModelException { - return toXml(model, true); + @Override + public String model2Xml(Model model) throws InconsistentModelException { + return model2Xml(model, true); } /** @@ -569,7 +565,7 @@ public class CellDesignerXmlParser extends XmlParser implements IConverter { * thrown when then model is invalid * @throws InconsistentModelException */ - public String toXml(Model model, boolean appendWarnings) throws InconsistentModelException { + public String model2Xml(Model model, boolean appendWarnings) throws InconsistentModelException { EventStorageLoggerAppender appender = new EventStorageLoggerAppender(); try { Logger.getRootLogger().addAppender(appender); @@ -606,7 +602,7 @@ public class CellDesignerXmlParser extends XmlParser implements IConverter { if (model.getNotes() != null || !appender.getWarnings().isEmpty()) { result.append("<notes>"); if (model.getNotes() != null) { - result.append(escapeXml(model.getNotes())); + result.append(XmlParser.escapeXml(model.getNotes())); } if (appendWarnings) { for (LoggingEvent event : appender.getWarnings()) { @@ -679,25 +675,6 @@ public class CellDesignerXmlParser extends XmlParser implements IConverter { return result.toString(); } - @Override - public InputStream exportModelToInputStream(Model model) throws InconsistentModelException { - String exportedString = toXml(model); - InputStream inputStream = new ByteArrayInputStream(exportedString.getBytes()); - return inputStream; - } - - @Override - public File exportModelToFile(Model model, String filePath) throws InconsistentModelException, IOException { - File file = new File(filePath); - String exportedString = toXml(model); - FileWriter fileWriter = new FileWriter(file); - fileWriter.write(exportedString); - fileWriter.flush(); - fileWriter.close(); - - return file; - } - @Override public String getCommonName() { return "CellDesigner SBML"; diff --git a/converter/src/main/java/lcsb/mapviewer/converter/annotation/XmlAnnotationParser.java b/converter/src/main/java/lcsb/mapviewer/converter/annotation/XmlAnnotationParser.java index a80ab4bd6ae5aa9100ba60750e0b30950965ad2a..e1c24e81fa9ef5484b365ccbdb18c15e1ab91492 100644 --- a/converter/src/main/java/lcsb/mapviewer/converter/annotation/XmlAnnotationParser.java +++ b/converter/src/main/java/lcsb/mapviewer/converter/annotation/XmlAnnotationParser.java @@ -24,7 +24,7 @@ import lcsb.mapviewer.model.map.MiriamType; * @author Piotr Gawron * */ -public class XmlAnnotationParser extends XmlParser { +public class XmlAnnotationParser { /** * Deafult logger. @@ -51,7 +51,7 @@ public class XmlAnnotationParser extends XmlParser { */ public Set<MiriamData> parse(String data) throws InvalidXmlSchemaException { // start from creating a DOM parser and parse the whole document - Document doc = getXmlDocumentFromString(data); + Document doc = XmlParser.getXmlDocumentFromString(data); NodeList root = doc.getChildNodes(); @@ -69,7 +69,7 @@ public class XmlAnnotationParser extends XmlParser { * thrown when there is a problem with xml */ public Set<MiriamData> parseRdfNode(NodeList root) throws InvalidXmlSchemaException { - Node rdf = getNode("rdf:RDF", root); + Node rdf = XmlParser.getNode("rdf:RDF", root); return parseRdfNode(rdf); } @@ -85,7 +85,7 @@ public class XmlAnnotationParser extends XmlParser { public Set<MiriamData> parseRdfNode(Node rdf) throws InvalidXmlSchemaException { Set<MiriamData> miriamDataSet = new HashSet<>(); if (rdf != null) { - Node description = getNode("rdf:Description", rdf.getChildNodes()); + Node description = XmlParser.getNode("rdf:Description", rdf.getChildNodes()); if (description != null) { NodeList list = description.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { @@ -125,14 +125,14 @@ public class XmlAnnotationParser extends XmlParser { NodeList list = node.getChildNodes(); String relationTypeString = node.getNodeName(); MiriamRelationType relationType = MiriamRelationType.getTypeByStringRepresentation(relationTypeString); - Node bag = getNode("rdf:Bag", list); + Node bag = XmlParser.getNode("rdf:Bag", list); if (bag == null) { throw new InvalidXmlSchemaException("No rdf:Bag node found"); } list = bag.getChildNodes(); - List<Node> nodes = getNodes("rdf:li", list); + List<Node> nodes = XmlParser.getNodes("rdf:li", list); for (Node li : nodes) { - String dataTypeUri = getNodeAttr("rdf:resource", li); + String dataTypeUri = XmlParser.getNodeAttr("rdf:resource", li); if (dataTypeUri == null || dataTypeUri.isEmpty()) { throw new InvalidXmlSchemaException("rdf:li does not have a rdf:resource attribute"); }