From 71e8a8294abc81ee88bf006b083ef1ab47ab879a Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Sun, 28 Jul 2024 20:04:18 +0800 Subject: [PATCH] more work on forcing valid html generation --- .../r5/test/NarrativeGenerationTests.java | 2 +- .../fhir/utilities/i18n/I18nConstants.java | 1 + .../hl7/fhir/utilities/xhtml/XhtmlNode.java | 28 ++++++++++++++----- .../hl7/fhir/utilities/xhtml/XhtmlParser.java | 1 + 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/NarrativeGenerationTests.java b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/NarrativeGenerationTests.java index e736032a9..2372a130d 100644 --- a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/NarrativeGenerationTests.java +++ b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/NarrativeGenerationTests.java @@ -233,7 +233,7 @@ public class NarrativeGenerationTests { @ParameterizedTest(name = "{index}: file {0}") @MethodSource("data") public void test(String id, TestDetails test) throws Exception { - XhtmlNode.setCheckPara(true); + XhtmlNode.setCheckParaGeneral(true); if (test.getRegister() != null) { if (test.getRegister().endsWith(".json")) { context.cacheResource(new JsonParser().parse(TestingUtilities.loadTestResourceStream("r5", "narrative", test.getRegister()))); diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/I18nConstants.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/I18nConstants.java index 947b34def..2c832b462 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/I18nConstants.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/I18nConstants.java @@ -157,6 +157,7 @@ public class I18nConstants { public static final String CONCEPTMAP_SHAREABLE_MISSING = "CONCEPTMAP_SHAREABLE_MISSING"; public static final String CONCEPTMAP_SHAREABLE_MISSING_HL7 = "CONCEPTMAP_SHAREABLE_MISSING_HL7"; public static final String CONCEPTMAP_VS_CONCEPT_CODE_UNKNOWN_SYSTEM = "CONCEPTMAP_VS_CONCEPT_CODE_UNKNOWN_SYSTEM"; + public static final String CONCEPTMAP_VS_CONCEPT_CODE_UNKNOWN_SYSTEM_VERSION = "CONCEPTMAP_VS_CONCEPT_CODE_UNKNOWN_SYSTEM_VERSION"; public static final String CONCEPTMAP_VS_INVALID_CONCEPT_CODE = "CONCEPTMAP_VS_INVALID_CONCEPT_CODE"; public static final String CONCEPTMAP_VS_INVALID_CONCEPT_CODE_VER = "CONCEPTMAP_VS_INVALID_CONCEPT_CODE_VER"; public static final String CONCEPTMAP_VS_TOO_MANY_CODES = "CONCEPTMAP_VS_TOO_MANY_CODES"; 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 04f6ca5f3..72e251336 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 @@ -72,7 +72,9 @@ public class XhtmlNode extends XhtmlFluent implements IBaseXhtml { } } - private static boolean checkPara = false; + private static boolean checkParaGeneral = false; + private boolean checkParaTree = false; + public static final String NBSP = Character.toString((char)0xa0); public static final String XMLNS = "http://www.w3.org/1999/xhtml"; private static final String DECL_XMLNS = " xmlns=\""+XMLNS+"\""; @@ -91,6 +93,7 @@ public class XhtmlNode extends XhtmlFluent implements IBaseXhtml { public XhtmlNode() { super(); + checkParaTree = checkParaGeneral; } @@ -119,7 +122,7 @@ public class XhtmlNode extends XhtmlFluent implements IBaseXhtml { public XhtmlNode setName(String name) { assert name.contains(":") == false : "Name should not contain any : but was " + name; - if (checkPara && "p".equals(name)) { + if (checkParaTree && "p".equals(name)) { isInPara = true; } this.name = name; @@ -1160,7 +1163,8 @@ public class XhtmlNode extends XhtmlFluent implements IBaseXhtml { private void checkWhenAddingNode(XhtmlNode node) { - if (checkPara) { + node.checkParaTree = checkParaTree; + if (checkParaTree) { if (isInPara) { if (Utilities.existsInList(node.name, "div", "blockquote", "table", "ol", "ul", "p")) { throw new Error("Error: attempt to add "+node.name+" inside an html paragraph"); @@ -1176,13 +1180,23 @@ public class XhtmlNode extends XhtmlFluent implements IBaseXhtml { } - public static boolean isCheckPara() { - return checkPara; + public static boolean isCheckParaGeneral() { + return checkParaGeneral; } - public static void setCheckPara(boolean checkPara) { - XhtmlNode.checkPara = checkPara; + public static void setCheckParaGeneral(boolean checkParaGeneral) { + XhtmlNode.checkParaGeneral = checkParaGeneral; + } + + + public boolean isCheckParaTree() { + return checkParaTree; + } + + + public void setCheckParaTree(boolean checkParaTree) { + this.checkParaTree = checkParaTree; } } \ No newline at end of file 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 cb899d208..2709e434b 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 @@ -500,6 +500,7 @@ public class XhtmlParser { if ((entryName != null) && !n.getName().equals(entryName)) throw new FHIRFormatError("Unable to Parse HTML - starts with '"+n+"' not '"+entryName+"'"+descLoc()); XhtmlNode root = result.addTag(n.getName()); + root.setCheckParaTree(false); root.setLocation(markLocation()); parseAttributes(root); markLocation();