-
Notifications
You must be signed in to change notification settings - Fork 32
Use UTF-8 explicitly when reading SBML/SED-ML XML #1676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
vcell-core/src/test/java/org/vcell/sbml/SBMLImportCharsetTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| package org.vcell.sbml; | ||
|
|
||
| import cbit.util.xml.VCLogger; | ||
| import cbit.util.xml.VCLoggerException; | ||
| import cbit.vcell.biomodel.BioModel; | ||
| import cbit.vcell.model.ReactionStep; | ||
| import cbit.vcell.model.SpeciesContext; | ||
| import org.junit.jupiter.api.Tag; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.vcell.sbml.vcell.SBMLImporter; | ||
|
|
||
| import java.io.File; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
|
||
| /** | ||
| * Verifies SBML import reads non-ASCII attribute values byte-for-byte from a | ||
| * UTF-8 source. The two reaction-name patterns chosen here (en-dash U+2013 and | ||
| * Greek mu U+03BC) are common in scientific notation and would mojibake under | ||
| * the previous {@code Charset.defaultCharset()} read on a non-UTF-8 JVM. | ||
| */ | ||
| @Tag("Fast") | ||
| public class SBMLImportCharsetTest { | ||
|
|
||
| private static class CapturingVCLogger extends VCLogger { | ||
| @Override public boolean hasMessages() { return false; } | ||
| @Override public void sendAllMessages() { } | ||
| @Override public void sendMessage(Priority p, ErrorType et, String message) throws VCLoggerException { | ||
| if (p == Priority.HighPriority) { | ||
| throw new VCLoggerException(p + " " + et + ": " + message); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static final String SBML_WITH_NON_ASCII = | ||
| "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + | ||
| "<sbml xmlns=\"http://www.sbml.org/sbml/level2/version4\" level=\"2\" version=\"4\">\n" + | ||
| " <model id=\"charsetTestModel\">\n" + | ||
| " <listOfCompartments>\n" + | ||
| " <compartment id=\"c1\" size=\"1.0\"/>\n" + | ||
| " </listOfCompartments>\n" + | ||
| " <listOfSpecies>\n" + | ||
| " <species id=\"s1\" name=\"μ-prot\" compartment=\"c1\" initialConcentration=\"1.0\"/>\n" + | ||
| " </listOfSpecies>\n" + | ||
| " <listOfReactions>\n" + | ||
| " <reaction id=\"r1\" name=\"k_14–3–3\">\n" + | ||
| " <listOfProducts>\n" + | ||
| " <speciesReference species=\"s1\"/>\n" + | ||
| " </listOfProducts>\n" + | ||
| " <kineticLaw>\n" + | ||
| " <math xmlns=\"http://www.w3.org/1998/Math/MathML\">\n" + | ||
| " <cn>1.0</cn>\n" + | ||
| " </math>\n" + | ||
| " </kineticLaw>\n" + | ||
| " </reaction>\n" + | ||
| " </listOfReactions>\n" + | ||
| " </model>\n" + | ||
| "</sbml>\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()); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.