Skip to content

Commit

Permalink
Disable HTML escaping for JSON-LD, rewrite htmlEscape
Browse files Browse the repository at this point in the history
  • Loading branch information
lszeremeta committed Apr 9, 2021
1 parent 12de21d commit 80573f1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
21 changes: 10 additions & 11 deletions src/main/java/pl/edu/uwb/ii/sdfeater/Molecule.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
package pl.edu.uwb.ii.sdfeater;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.ResourceFactory;
Expand Down Expand Up @@ -406,7 +406,7 @@ StringBuilder constructJSONLDMolecule(SDFEater.Subject subject) {

output_str.append(" {\n");
if (subject == SDFEater.Subject.iri) {
output_str.append(" \"@id\" : \"" + subjectBase + createID() + "\",\n");
output_str.append(" \"@id\" : " + printValueAsNumberOrStringInJSONLD(subjectBase + createID()) + ",\n");
} else if (subject == SDFEater.Subject.uuid) {
output_str.append(" \"@id\" : \"urn:uuid:" + uuid + "\",\n");
} else if (subject == SDFEater.Subject.bnode) {
Expand Down Expand Up @@ -504,7 +504,7 @@ void printRDFaMolecule(SDFEater.Subject subject) {
if (output_str.length() > 0) {
if (subject == SDFEater.Subject.iri) {
String mID = createID();
System.out.print(" <div typeof='schema:MolecularEntity' about='" + htmlEscape(subjectBase, true) + mID + "'");
System.out.print(" <div typeof='schema:MolecularEntity' about='" + htmlEscape(subjectBase + mID, true) + "'");

if (subjectBase.contains("#")) {
System.out.print(" id='" + subjectBase.substring(subjectBase.lastIndexOf('#') + 1) + mID + "'");
Expand Down Expand Up @@ -571,7 +571,7 @@ void printMicrodataMolecule(SDFEater.Subject subject) {
if (output_str.length() > 0) {
if (subject == SDFEater.Subject.iri) {
String mID = createID();
System.out.print(" <div itemscope itemtype='http://schema.org/MolecularEntity' itemid='" + htmlEscape(subjectBase, true) + mID + "'");
System.out.print(" <div itemscope itemtype='http://schema.org/MolecularEntity' itemid='" + htmlEscape(subjectBase + mID, true) + "'");

if (subjectBase.contains("#")) {
System.out.print(" id='" + subjectBase.substring(subjectBase.lastIndexOf('#') + 1) + mID + "'");
Expand Down Expand Up @@ -630,7 +630,7 @@ private String printValueAsNumberOrStringInJSONLD(String value) {
if (isNumber(value)) {
return value;
} else {
return new Gson().toJson(value);
return new GsonBuilder().disableHtmlEscaping().create().toJson(value);
}
}

Expand All @@ -644,15 +644,14 @@ private String printValueAsNumberOrStringInJSONLD(String value) {
* @return Escaped HTML value
*/
private String htmlEscape(String value, boolean quote) {
value = value.replace("&", "&amp;");
value = value.replace("<", "&lt;");
value = value.replace(">", "&gt;");
String escapedValue = value.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;");

if (quote) {
value = value.replace("\"", "&quot;");
value = value.replace("'", "&#x27;");
escapedValue = escapedValue.replace("\"", "&quot;");
escapedValue = escapedValue.replace("'", "&#x27;");
}

return value;
return escapedValue;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/pl/edu/uwb/ii/sdfeater/OutputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
class OutputTest {
private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
private final String[] testMoleculeDataFields = {"alternateName", "description", "identifier", "inChI", "inChIKey", "iupacName", "molecularFormula", "molecularWeight", "smiles"};
private final String[] testMoleculeData = {"(+)-2-fenchanone", "A fenchone that has", "4695-62-9", "InChI", "1S/C10H16O/c1-9(2)7-4-5-10(3,6-7)8(9)11/h7H,4-6H2,1-3H3/t7-,10+/m1/s1", "LHXDLQBQYFFVNW-XCBNKYQSSA-N", "(1S,4R)-fenchan-2-one", "C10H16O", "152.23340", "CC1(C)[C@@H]2CC[C@@](C)(C2)C1", "(-)-Epicatechin", "A catechin with", "490-46-0", "1S/C15H14O6/c16-8-4-11(18)9-6-13(20)15(21-14(9)5-8)7-1-2-10(17)12(19)3-7/h1-5,13,15-20H,6H2/t13-,15-/m1/s1", "PFTAWBLQPZVEMU-UKRRQHHQSA-N", "(2R,3R)-2-(3,4-dihydroxyphenyl)-3,4-dihydro-2H-chromene-3,5,7-triol", "C15H14O6", "290.26810", "[H][C@@]1(Oc2cc(O)cc(O)c2C[C@H]1O)c1ccc(O)c(O)c1"};
private final String[] testMoleculeData = {"(+)-2-fenchanone", "A fenchone that has", "4695-62-9", "InChI=1S/C10H16O/c1-9(2)7-4-5-10(3,6-7)8(9)11/h7H,4-6H2,1-3H3/t7-,10+/m1/s1", "LHXDLQBQYFFVNW-XCBNKYQSSA-N", "(1S,4R)-fenchan-2-one", "C10H16O", "152.23340", "CC1(C)[C@@H]2CC[C@@](C)(C2)C1=O", "(-)-Epicatechin", "A catechin with", "490-46-0", "InChI=1S/C15H14O6/c16-8-4-11(18)9-6-13(20)15(21-14(9)5-8)7-1-2-10(17)12(19)3-7/h1-5,13,15-20H,6H2/t13-,15-/m1/s1", "PFTAWBLQPZVEMU-UKRRQHHQSA-N", "(2R,3R)-2-(3,4-dihydroxyphenyl)-3,4-dihydro-2H-chromene-3,5,7-triol", "C15H14O6", "290.26810", "[H][C@@]1(Oc2cc(O)cc(O)c2C[C@H]1O)c1ccc(O)c(O)c1"};
private final File file = new File(Paths.get("src", "test", "resources", "chebi_test.sdf").toFile().getAbsolutePath());
private Molecule molecule;

Expand Down

0 comments on commit 80573f1

Please sign in to comment.