11package uk .nhs .adaptors .gp2gp .ehr .mapper ;
22
33import org .apache .commons .lang3 .StringUtils ;
4+ import org .apache .commons .text .StringEscapeUtils ;
45import org .hl7 .fhir .dstu3 .model .BooleanType ;
56import org .hl7 .fhir .dstu3 .model .Extension ;
67import org .hl7 .fhir .dstu3 .model .Quantity ;
@@ -52,12 +53,15 @@ public static String processQuantity(Quantity valueQuantity) {
5253 }
5354
5455 private static String getPhysicalQuantityXml (Quantity valueQuantity ) {
56+ var escapedCode = StringEscapeUtils .escapeXml11 (valueQuantity .getCode ());
57+ var escapedUnit = StringEscapeUtils .escapeXml11 (valueQuantity .getUnit ());
58+
5559 if (UNITS_OF_MEASURE_SYSTEM .equals (valueQuantity .getSystem ()) && valueQuantity .hasCode ()) {
5660 return """
5761 <value xsi:type="PQ" value="%s" unit="%s" />"""
5862 .formatted (
5963 valueQuantity .getValue (),
60- valueQuantity . getCode ()
64+ escapedCode
6165 );
6266 }
6367 if (hasValidSystem (valueQuantity ) && valueQuantity .hasCode () && valueQuantity .hasUnit ()) {
@@ -68,9 +72,9 @@ private static String getPhysicalQuantityXml(Quantity valueQuantity) {
6872 .formatted (
6973 valueQuantity .getValue (),
7074 valueQuantity .getValue (),
71- valueQuantity . getCode () ,
75+ escapedCode ,
7276 getSystemWithoutPrefix (valueQuantity .getSystem ()),
73- valueQuantity . getUnit ()
77+ escapedUnit
7478 );
7579 }
7680 if (hasValidSystem (valueQuantity ) && valueQuantity .hasCode ()) {
@@ -81,7 +85,7 @@ private static String getPhysicalQuantityXml(Quantity valueQuantity) {
8185 .formatted (
8286 valueQuantity .getValue (),
8387 valueQuantity .getValue (),
84- valueQuantity . getCode () ,
88+ escapedCode ,
8589 getSystemWithoutPrefix (valueQuantity .getSystem ())
8690 );
8791 }
@@ -95,7 +99,7 @@ private static String getPhysicalQuantityXml(Quantity valueQuantity) {
9599 .formatted (
96100 valueQuantity .getValue (),
97101 valueQuantity .getValue (),
98- valueQuantity . getUnit ()
102+ escapedUnit
99103 );
100104 }
101105
@@ -105,6 +109,9 @@ private static String getPhysicalQuantityXml(Quantity valueQuantity) {
105109 }
106110
107111 private static String getPhysicalQuantityIntervalXml (Quantity valueQuantity ) {
112+ var escapedCode = StringEscapeUtils .escapeXml11 (valueQuantity .getCode ());
113+ var escapedUnit = StringEscapeUtils .escapeXml11 (valueQuantity .getUnit ());
114+
108115 if (UNITS_OF_MEASURE_SYSTEM .equals (valueQuantity .getSystem ()) && valueQuantity .hasCode ()) {
109116 return """
110117 <value xsi:type="IVL_PQ">%n\
@@ -113,7 +120,7 @@ private static String getPhysicalQuantityIntervalXml(Quantity valueQuantity) {
113120 .formatted (
114121 getHighOrLow (valueQuantity ),
115122 valueQuantity .getValue (),
116- valueQuantity . getCode () ,
123+ escapedCode ,
117124 isInclusive (valueQuantity )
118125 );
119126 }
@@ -129,9 +136,9 @@ private static String getPhysicalQuantityIntervalXml(Quantity valueQuantity) {
129136 valueQuantity .getValue (),
130137 isInclusive (valueQuantity ),
131138 valueQuantity .getValue (),
132- valueQuantity . getCode () ,
139+ escapedCode ,
133140 getSystemWithoutPrefix (valueQuantity .getSystem ()),
134- valueQuantity . getUnit () ,
141+ escapedUnit ,
135142 getHighOrLow (valueQuantity )
136143 );
137144 }
@@ -147,7 +154,7 @@ private static String getPhysicalQuantityIntervalXml(Quantity valueQuantity) {
147154 valueQuantity .getValue (),
148155 isInclusive (valueQuantity ),
149156 valueQuantity .getValue (),
150- valueQuantity . getCode () ,
157+ escapedCode ,
151158 getSystemWithoutPrefix (valueQuantity .getSystem ()),
152159 getHighOrLow (valueQuantity )
153160 );
@@ -166,7 +173,7 @@ private static String getPhysicalQuantityIntervalXml(Quantity valueQuantity) {
166173 valueQuantity .getValue (),
167174 isInclusive (valueQuantity ),
168175 valueQuantity .getValue (),
169- valueQuantity . getUnit () ,
176+ escapedUnit ,
170177 getHighOrLow (valueQuantity )
171178 );
172179 }
0 commit comments