@@ -48,45 +48,44 @@ public List<Composition.SectionComponent> parse(Node html) {
4848 removeEmptyNodes (html );
4949
5050 var bodyNode = xmlUtils .getNodesByXPath (html , "./body" ).stream ()
51- .findFirst ()
52- .orElseThrow (() -> new IllegalStateException ("Missing body node in html" ));
51+ .findFirst ()
52+ .orElseThrow (() -> new IllegalStateException ("Missing body node in html" ));
5353
5454 var childNodes = bodyNode .getChildNodes ();
5555 if (childNodes .getLength () != 0 && !H2 .equals (childNodes .item (0 ).getNodeName ())) {
5656 throw new IllegalStateException ("First body node must be H2" );
5757 }
5858
59- // this will hold a list of paris: H2 node as key and all next nodes (until next
60- // H2 or body end) as a new Document
59+ // this will hold a list of paris: H2 node as key and all next nodes (until next H2 or body end) as a new Document
6160 var items = new ArrayList <Pair <Node , Document >>();
6261 Document targetDocument = null ;
6362 for (int i = 0 ; i < childNodes .getLength (); i ++) {
6463 var currentNode = childNodes .item (i );
6564 if (H2 .equals (currentNode .getNodeName ())) {
66- // since this is the H2, we begin to capture following nodes as a new Document
65+ //since this is the H2, we begin to capture following nodes as a new Document
6766 targetDocument = createNewDocument ("div" , "http://www.w3.org/1999/xhtml" );
6867 items .add (Pair .of (currentNode , targetDocument ));
6968 } else {
7069 if (targetDocument == null ) {
7170 throw new IllegalStateException ("Target document not initialized" );
7271 }
7372 targetDocument .getDocumentElement ()
74- .appendChild (targetDocument .importNode (currentNode , true ));
73+ .appendChild (targetDocument .importNode (currentNode , true ));
7574 }
7675 }
7776
7877 return items .stream ()
79- .map (kv -> ParsedHtml
80- .builder ()
81- .html (serialize (kv .getValue ()))
82- .h2Value (kv .getKey ().getTextContent ())
83- .h2Id (Optional .ofNullable (kv .getKey ().getAttributes ())
84- .map (h2IdAttribute -> h2IdAttribute .getNamedItem ("id" ))
85- .map (Node ::getNodeValue )
86- .orElse (null ))
87- .build ())
88- .map (HtmlParser ::buildSectionComponent )
89- .collect (Collectors .toList ());
78+ .map (kv -> ParsedHtml
79+ .builder ()
80+ .html (serialize (kv .getValue ()))
81+ .h2Value (kv .getKey ().getTextContent ())
82+ .h2Id (Optional .ofNullable (kv .getKey ().getAttributes ())
83+ .map (h2IdAttribute -> h2IdAttribute .getNamedItem ("id" ))
84+ .map (Node ::getNodeValue )
85+ .orElse (null ))
86+ .build ())
87+ .map (HtmlParser ::buildSectionComponent )
88+ .collect (Collectors .toList ());
9089 }
9190
9291 @ SneakyThrows
@@ -108,30 +107,32 @@ public static String serialize(Document document) {
108107 @ SneakyThrows
109108 public static void removeEmptyNodes (Node document ) {
110109 XPathExpression xpathExp = XPathFactory .newInstance ().newXPath ()
111- .compile ("//text()[normalize-space(.) = '']" );
110+ .compile ("//text()[normalize-space(.) = '']" );
112111 NodeList emptyTextNodes = (NodeList ) xpathExp .evaluate (document , XPathConstants .NODESET );
113112 for (int i = 0 ; i < emptyTextNodes .getLength (); i ++) {
114113 Node emptyTextNode = emptyTextNodes .item (i );
115114 emptyTextNode .getParentNode ().removeChild (emptyTextNode );
116115 }
117116
117+
118118 }
119119
120+
120121 private static Composition .SectionComponent buildSectionComponent (ParsedHtml parsedHtml ) {
121122 var sectionComponent = new Composition .SectionComponent ()
122- .setTitle (parsedHtml .h2Value )
123- .setText (buildNarrative (parsedHtml .html ));
123+ .setTitle (parsedHtml .h2Value )
124+ .setText (buildNarrative (parsedHtml .html ));
124125 if (StringUtils .isNotBlank (parsedHtml .h2Id )) {
125126 sectionComponent .setCode (new CodeableConcept ()
126- .addCoding (new Coding ()
127- .setCode (parsedHtml .h2Id )));
127+ .addCoding (new Coding ()
128+ .setCode (parsedHtml .h2Id )));
128129 }
129130 return sectionComponent ;
130131 }
131132
132133 private static Narrative buildNarrative (String html ) {
133134 var narrative = new Narrative ()
134- .setStatus (Narrative .NarrativeStatus .GENERATED );
135+ .setStatus (Narrative .NarrativeStatus .GENERATED );
135136 narrative .setDivAsString (html );
136137 return narrative ;
137138 }
0 commit comments