diff --git a/vcell-core/src/main/java/org/jlibsedml/Libsedml.java b/vcell-core/src/main/java/org/jlibsedml/Libsedml.java index 3bf97cea85..bf00e30755 100644 --- a/vcell-core/src/main/java/org/jlibsedml/Libsedml.java +++ b/vcell-core/src/main/java/org/jlibsedml/Libsedml.java @@ -11,7 +11,7 @@ import java.io.InputStream; import java.io.OutputStreamWriter; import java.io.StringReader; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.zip.ZipEntry; @@ -100,8 +100,8 @@ public static SedMLDocument readDocument(File file) throws XMLException { */ public static SedMLDocument readDocument(InputStream is, String encoding) throws XMLException, IOException { if(encoding == null) { - encoding = Charset.defaultCharset().name(); - } + encoding = StandardCharsets.UTF_8.name(); + } List lines = IOUtils.readLines(is, encoding); String content = StringUtils.join(lines, "\n"); return readDocumentFromString(content); diff --git a/vcell-core/src/main/java/org/jlibsedml/XpathGeneratorHelper.java b/vcell-core/src/main/java/org/jlibsedml/XpathGeneratorHelper.java index d61cad14c6..5ca73c1561 100644 --- a/vcell-core/src/main/java/org/jlibsedml/XpathGeneratorHelper.java +++ b/vcell-core/src/main/java/org/jlibsedml/XpathGeneratorHelper.java @@ -3,6 +3,7 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -51,7 +52,7 @@ public boolean addIdentifiersAsDataGenerators(final AbstractTask task, final Str String modelStrRep = modelResolver.getModelXMLFor(modelFound.getSourceURI()); if (modelStrRep == null) return false; - Document doc = utils.readDoc(new ByteArrayInputStream(modelStrRep.getBytes())); + Document doc = utils.readDoc(new ByteArrayInputStream(modelStrRep.getBytes(StandardCharsets.UTF_8))); List configs = new ArrayList<>(); for (IdName idn : idNameList) { diff --git a/vcell-core/src/main/java/org/jlibsedml/modelsupport/KisaoTermParser.java b/vcell-core/src/main/java/org/jlibsedml/modelsupport/KisaoTermParser.java index 2020ed8d33..9a9f8b52b5 100644 --- a/vcell-core/src/main/java/org/jlibsedml/modelsupport/KisaoTermParser.java +++ b/vcell-core/src/main/java/org/jlibsedml/modelsupport/KisaoTermParser.java @@ -7,6 +7,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -34,7 +35,7 @@ class KisaoTermParser { KisaoOntology parse() { InputStream is2 = KisaoTermParser.class.getClassLoader() .getResourceAsStream(Kisao_OBO); - BufferedReader isr = new BufferedReader(new InputStreamReader(is2)); + BufferedReader isr = new BufferedReader(new InputStreamReader(is2, StandardCharsets.UTF_8)); String line = null; boolean inPreamble = true; boolean inState = false; diff --git a/vcell-core/src/main/java/org/vcell/sbml/vcell/SBMLImporter.java b/vcell-core/src/main/java/org/vcell/sbml/vcell/SBMLImporter.java index 4dd853f051..cbe15b8506 100644 --- a/vcell-core/src/main/java/org/vcell/sbml/vcell/SBMLImporter.java +++ b/vcell-core/src/main/java/org/vcell/sbml/vcell/SBMLImporter.java @@ -83,7 +83,6 @@ import javax.xml.stream.XMLStreamException; import java.beans.PropertyVetoException; import java.io.*; -import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.*; import java.util.Map.Entry; @@ -1931,7 +1930,7 @@ private SBMLDocument readSbmlDocument(File sbmlFile){ final String defaultErrorPrefix = "Unable to read SBML file"; try { // Read SBML model into libSBML SBMLDocument and create an SBML model - List readLines = FileUtils.readLines(sbmlFile, Charset.defaultCharset()); + List readLines = FileUtils.readLines(sbmlFile, StandardCharsets.UTF_8); StringBuilder sb = new StringBuilder(); //Temporary fix for org.sbml.jsbml.xml.parsers.RenderParser.processEndDocument(SBMLDocument sbmlDocument) //throws NPE when "\n" + + "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " 1.0\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + "\n"; + + @Test + public void importsUtf8ReactionAndSpeciesNames() throws Exception { + Path tmp = Files.createTempFile("vcell-charset-test-", ".xml"); + try { + Files.write(tmp, SBML_WITH_NON_ASCII.getBytes(StandardCharsets.UTF_8)); + + SBMLImporter importer = new SBMLImporter(tmp.toAbsolutePath().toString(), new CapturingVCLogger(), false); + BioModel bioModel = importer.getBioModel(); + assertNotNull(bioModel); + + ReactionStep r1 = null; + for (ReactionStep rs : bioModel.getModel().getReactionSteps()) { + if ("r1".equals(rs.getName())) { r1 = rs; break; } + } + assertNotNull(r1, "expected reaction with id 'r1' in imported model"); + assertEquals("k_14–3–3", r1.getSbmlName(), + "reaction sbmlName must preserve U+2013 EN DASH characters"); + + SpeciesContext s1 = null; + for (SpeciesContext sc : bioModel.getModel().getSpeciesContexts()) { + if ("s1".equals(sc.getName())) { s1 = sc; break; } + } + assertNotNull(s1, "expected species with id 's1' in imported model"); + assertEquals("μ-prot", s1.getSbmlName(), + "species sbmlName must preserve U+03BC GREEK SMALL LETTER MU"); + } finally { + Files.deleteIfExists(tmp); + } + } + + @Test + public void inputStreamPathPreservesUtf8() throws Exception { + try (java.io.ByteArrayInputStream in = + new java.io.ByteArrayInputStream(SBML_WITH_NON_ASCII.getBytes(StandardCharsets.UTF_8))) { + SBMLImporter importer = new SBMLImporter(in, new CapturingVCLogger(), false); + BioModel bioModel = importer.getBioModel(); + assertNotNull(bioModel); + + ReactionStep r1 = null; + for (ReactionStep rs : bioModel.getModel().getReactionSteps()) { + if ("r1".equals(rs.getName())) { r1 = rs; break; } + } + assertNotNull(r1); + assertEquals("k_14–3–3", r1.getSbmlName()); + } + } +}