From d3cf93cf042bf57ce8b22dd2ee537bc51d45f3ce Mon Sep 17 00:00:00 2001 From: James Agnew Date: Tue, 7 Jan 2020 09:53:43 -0500 Subject: [PATCH] Work on https://github.com/jamesagnew/hapi-fhir/issues/1658 - Make sure we preserve lang attribute on narrative div --- .../hl7/fhir/utilities/xhtml/XhtmlNode.java | 13 +++++----- .../hl7/fhir/utilities/xhtml/XhtmlParser.java | 7 ++--- .../fhir/utilities/tests/XhtmlNodeTest.java | 26 ++++++++++++++++--- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xhtml/XhtmlNode.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xhtml/XhtmlNode.java index 8543406f5..0b01556d3 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xhtml/XhtmlNode.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xhtml/XhtmlNode.java @@ -394,13 +394,12 @@ public class XhtmlNode implements IBaseXhtml { val = XhtmlDt.preprocessXhtmlNamespaceDeclaration(val); try { - // TODO: this is ugly - XhtmlNode fragment = new XhtmlParser().parseFragment(val); - this.attributes = fragment.attributes; - this.childNodes = fragment.childNodes; - this.content = fragment.content; - this.name = fragment.name; - this.nodeType= fragment.nodeType; + XhtmlDocument fragment = new XhtmlParser().parse(val, "div"); + this.attributes = fragment.getAttributes(); + this.childNodes = fragment.getChildNodes(); + this.content = fragment.getContent(); + this.name = fragment.getName(); + this.nodeType= fragment.getNodeType(); } catch (Exception e) { // TODO: composer shouldn't throw exception like this throw new RuntimeException(e); diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xhtml/XhtmlParser.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xhtml/XhtmlParser.java index 9d1e1d519..75442e02f 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xhtml/XhtmlParser.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/xhtml/XhtmlParser.java @@ -54,6 +54,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.StringReader; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -446,7 +447,7 @@ private boolean elementIsOk(String name) throws FHIRFormatError { } public XhtmlDocument parse(InputStream input, String entryName) throws FHIRFormatError, IOException { - rdr = new InputStreamReader(input, "UTF-8"); + rdr = new InputStreamReader(input, StandardCharsets.UTF_8); return parse(entryName); } @@ -1176,7 +1177,7 @@ private boolean elementIsOk(String name) throws FHIRFormatError { String n = readName().toLowerCase(); readToTagEnd(); XhtmlNode result = new XhtmlNode(NodeType.Element); - + int colonIndex = n.indexOf(':'); if (colonIndex != -1) { n = n.substring(colonIndex + 1); @@ -1184,7 +1185,7 @@ private boolean elementIsOk(String name) throws FHIRFormatError { result.setName(n); unwindPoint = null; - List p = new ArrayList(); + List p = new ArrayList<>(); parseElementInner(result, p, null, true); return result; diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/XhtmlNodeTest.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/XhtmlNodeTest.java index b10221493..9a405349a 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/XhtmlNodeTest.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/XhtmlNodeTest.java @@ -1,10 +1,9 @@ package org.hl7.fhir.utilities.tests; +import org.hl7.fhir.utilities.xhtml.XhtmlNode; import org.junit.Test; -import static org.junit.Assert.*; - -import org.hl7.fhir.utilities.xhtml.XhtmlNode; +import static org.junit.Assert.assertEquals; public class XhtmlNodeTest { @@ -26,5 +25,26 @@ public class XhtmlNodeTest { } + /** + * See https://github.com/jamesagnew/hapi-fhir/issues/1658 + */ + @Test + public void testLangAttributePreserved() { + XhtmlNode dt = new XhtmlNode(); + dt.setValueAsString("
help i'm a bug
"); + assertEquals("
help i'm a bug
", dt.getValueAsString()); + assertEquals("
help i'm a bug
", new XhtmlNode().setValue(dt.getValue()).getValueAsString()); + } + + + @Test + public void testParseRsquo() { + XhtmlNode dt = new XhtmlNode(); + dt.setValueAsString("It’s January again"); + assertEquals("
It’s January again
", dt.getValueAsString()); + assertEquals("
It’s January again
", new XhtmlNode().setValue(dt.getValue()).getValueAsString()); + } + + } \ No newline at end of file