001package org.hl7.fhir.utilities.xhtml;
002
003/*-
004 * #%L
005 * org.hl7.fhir.utilities
006 * %%
007 * Copyright (C) 2014 - 2019 Health Level 7
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023
024import java.io.IOException;
025
026import org.hl7.fhir.exceptions.FHIRException;
027import org.hl7.fhir.utilities.Utilities;
028import org.hl7.fhir.utilities.xml.IXMLWriter;
029import org.hl7.fhir.utilities.xml.XMLUtil;
030import org.w3c.dom.Element;
031import org.w3c.dom.Node;
032
033public class CDANarrativeFormat {
034
035  /** 
036   * for a CDA narrative, return the matching XHTML. 
037   * 
038   * For further information, see http://wiki.hl7.org/index.php?title=CDA_Narrative_to_html_mapping
039   * 
040   * @param ed
041   * @return
042   * @throws FHIRException
043   */
044  public XhtmlNode convert(Element ed) throws FHIRException {
045    XhtmlNode div = new XhtmlNode(NodeType.Element, "div");
046    processAttributes(ed, div, "ID", "language", "styleCode");
047    processChildren(ed, div);
048    return div;
049  }
050
051  private void processChildren(Element ed, XhtmlNode x) throws FHIRException {
052    for (Node n : XMLUtil.children(ed)) 
053      processChildNode(n, x);
054  }
055  
056  private void processChildNode(Node n, XhtmlNode xn) throws FHIRException {
057    switch (n.getNodeType()) {
058    case Node.ATTRIBUTE_NODE: 
059    case Node.CDATA_SECTION_NODE:
060    case Node.DOCUMENT_FRAGMENT_NODE: 
061    case Node.DOCUMENT_TYPE_NODE: 
062    case Node.DOCUMENT_NODE: 
063    case Node.ENTITY_NODE: 
064    case Node.PROCESSING_INSTRUCTION_NODE:
065    case Node.NOTATION_NODE:
066      return;
067    case Node.ENTITY_REFERENCE_NODE: 
068      throw new Error("Not handled yet");
069    case Node.COMMENT_NODE: 
070      xn.addComment(n.getTextContent());
071      return;
072    case Node.TEXT_NODE: 
073      if (!Utilities.isWhitespace(n.getTextContent()))
074        xn.addText(n.getTextContent());
075      return;
076    case Node.ELEMENT_NODE:
077      Element e = (Element) n;
078      if (n.getNodeName().equals("br"))
079        processBreak(e, xn);
080      else if (n.getNodeName().equals("caption"))
081        processCaption(e, xn);
082      else if (n.getNodeName().equals("col"))
083        processCol(e, xn);
084      else if (n.getNodeName().equals("colgroup"))
085        processColGroup(e, xn);
086      else if (n.getNodeName().equals("content"))
087        processContent(e, xn);
088      else if (n.getNodeName().equals("footnote"))
089        processFootNote(e, xn);
090      else if (n.getNodeName().equals("footnoteRef"))
091        processFootNodeRef(e, xn);
092      else if (n.getNodeName().equals("item"))
093        processItem(e, xn);
094      else if (n.getNodeName().equals("linkHtml"))
095        processlinkHtml(e, xn);
096      else if (n.getNodeName().equals("list"))
097        processList(e, xn);
098      else if (n.getNodeName().equals("paragraph"))
099        processParagraph(e, xn);
100      else if (n.getNodeName().equals("renderMultiMedia"))
101        processRenderMultiMedia(e, xn);
102      else if (n.getNodeName().equals("sub"))
103        processSub(e, xn);
104      else if (n.getNodeName().equals("sup"))
105        processSup(e, xn);
106      else if (n.getNodeName().equals("table"))
107        processTable(e, xn);
108      else if (n.getNodeName().equals("tbody"))
109        processTBody(e, xn);
110      else if (n.getNodeName().equals("td"))
111        processTd(e, xn);
112      else if (n.getNodeName().equals("tfoot"))
113        processTFoot(e, xn);
114      else if (n.getNodeName().equals("th"))
115        processTh(e, xn);
116      else if (n.getNodeName().equals("thead"))
117        processTHead(e, xn);
118      else if (n.getNodeName().equals("tr"))
119        processTr(e, xn);
120      else
121        throw new FHIRException("Unknown element "+n.getNodeName());
122    }
123  }
124
125  private void processBreak(Element e, XhtmlNode xn) {
126    xn.addTag("br");
127  }
128
129  private void processCaption(Element e, XhtmlNode xn) throws FHIRException {
130    XhtmlNode xc = xn.addTag("h2");
131    processAttributes(e, xc, "ID", "language", "styleCode");
132    processChildren(e, xc);
133  }
134
135  private void processCol(Element e, XhtmlNode xn) throws FHIRException {
136    XhtmlNode xc = xn.addTag("col");
137    processAttributes(e, xc, "ID", "language", "styleCode", "span", "width", "align", "char", "charoff", "valign");
138    processChildren(e, xc);
139  }
140
141  private void processColGroup(Element e, XhtmlNode xn) throws FHIRException {
142    XhtmlNode xc = xn.addTag("colgroup");
143    processAttributes(e, xc, "ID", "language", "styleCode", "span", "width", "align", "char", "charoff", "valign");
144    processChildren(e, xc);
145  }
146
147  private void processContent(Element e, XhtmlNode xn) throws FHIRException {
148    XhtmlNode xc = xn.addTag("span");
149    processAttributes(e, xc, "ID", "language", "styleCode");
150    // todo: do something with revised..., "revised"
151    processChildren(e, xc);
152  }
153
154  private void processFootNote(Element e, XhtmlNode xn) {
155    throw new Error("element "+e.getNodeName()+" not handled yet");
156  }
157
158  private void processFootNodeRef(Element e, XhtmlNode xn) {
159    throw new Error("element "+e.getNodeName()+" not handled yet");
160  }
161
162  private void processItem(Element e, XhtmlNode xn) throws FHIRException {
163    XhtmlNode xc = xn.addTag("li");
164    processAttributes(e, xc, "ID", "language", "styleCode");
165    processChildren(e, xc);
166  }
167
168  private void processlinkHtml(Element e, XhtmlNode xn) throws FHIRException {
169    XhtmlNode xc = xn.addTag("a");
170    processAttributes(e, xc, "name", "href", "rel", "rev", "title", "ID", "language", "styleCode");
171    processChildren(e, xc);
172  }
173
174  private void processList(Element e, XhtmlNode xn) throws FHIRException {
175    String lt = e.getAttribute("listType");
176    XhtmlNode xc = xn.addTag("ordered".equals(lt) ? "ol" : "ul");
177    processAttributes(e, xc, "ID", "language", "styleCode");
178    processChildren(e, xc);
179  }
180
181  private void processParagraph(Element e, XhtmlNode xn) throws FHIRException {
182    XhtmlNode xc = xn.addTag("p");
183    processAttributes(e, xc, "ID", "language", "styleCode");
184    processChildren(e, xc);
185  }
186
187  private void processRenderMultiMedia(Element e, XhtmlNode xn) throws FHIRException {
188    XhtmlNode xc = xn.addTag("img");
189    String v = e.getAttribute("referencedObject");
190    xc.attribute("src", v);
191    processAttributes(e, xc, "ID", "language", "styleCode");
192    processChildren(e, xc);
193  }
194
195  private void processSub(Element e, XhtmlNode xn) throws FHIRException {
196    XhtmlNode xc = xn.addTag("sub");
197    processChildren(e, xc);
198  }
199
200  private void processSup(Element e, XhtmlNode xn) throws FHIRException {
201    XhtmlNode xc = xn.addTag("sup");
202    processChildren(e, xc);
203  }
204
205  private void processTable(Element e, XhtmlNode xn) throws FHIRException {
206    XhtmlNode xc = xn.addTag("table");
207    processAttributes(e, xc, "ID", "language", "styleCode", "summary", "width", "border", "frame", "rules", "cellspacing", "cellpadding");
208    processChildren(e, xc);
209  }
210
211  private void processTBody(Element e, XhtmlNode xn) throws FHIRException {
212    XhtmlNode xc = xn.addTag("tbody");
213    processAttributes(e, xc, "ID", "language", "styleCode", "align", "char", "charoff", "valign");
214    processChildren(e, xc);
215  }
216
217  private void processTd(Element e, XhtmlNode xn) throws FHIRException {
218    XhtmlNode xc = xn.addTag("td");
219    processAttributes(e, xc, "ID", "language", "styleCode", "abbr", "axis", "headers", "scope", "rowspan", "colspan", "align", "char", "charoff", "valign");
220    processChildren(e, xc);
221  }
222
223  private void processTFoot(Element e, XhtmlNode xn) {
224    throw new Error("element "+e.getNodeName()+" not handled yet");
225  }
226
227  private void processTh(Element e, XhtmlNode xn) throws FHIRException {
228    XhtmlNode xc = xn.addTag("th");
229    processAttributes(e, xc, "ID", "language", "styleCode", "abbr", "axis", "headers", "scope", "rowspan", "colspan", "align", "char", "charoff", "valign");
230    processChildren(e, xc);
231  }
232
233  private void processTHead(Element e, XhtmlNode xn) throws FHIRException {
234    XhtmlNode xc = xn.addTag("thead");
235    processAttributes(e, xc, "ID", "language", "styleCode", "align", "char", "charoff", "valign");
236    processChildren(e, xc);
237  }
238
239  private void processTr(Element e, XhtmlNode xn) throws FHIRException {
240    XhtmlNode xc = xn.addTag("tr");
241    processAttributes(e, xc, "ID", "language", "styleCode", "align", "char", "charoff", "valign");
242    processChildren(e, xc);
243  }
244
245  private void processAttributes(Element element, XhtmlNode xn, String... names) {
246    for (String n : names) {
247      if (element.hasAttribute(n)) {
248        String v = element.getAttribute(n);
249        if (n.equals("ID"))
250          xn.attribute("id", v);
251        else
252          xn.attribute(n, v);
253      }
254    }
255  }
256
257  /**
258   * For XHTML return the matching CDA narrative. This is only guaranteed to work for XML produced from CDA, but will try whatever
259   * @param node
260   * @return
261   * @throws IOException 
262   * @throws FHIRException 
263   */
264  public void convert(IXMLWriter xml, XhtmlNode div) throws IOException, FHIRException {
265    processAttributes(div, xml, "ID", "language", "styleCode");
266    xml.enter("text");
267    processChildren(xml, div);
268    xml.exit("text");
269  }
270
271  private void processChildren(IXMLWriter xml, XhtmlNode x) throws IOException, FHIRException {
272    for (XhtmlNode n : x.getChildNodes()) 
273      processChildNode(xml, n);
274  }
275  
276  private void processChildNode(IXMLWriter xml, XhtmlNode n) throws IOException, FHIRException {
277    switch (n.getNodeType()) {
278    case DocType: 
279    case Document: 
280    case Instruction: 
281      return;
282    case Comment: 
283      xml.comment(n.getContent(), true);
284      return;
285    case Text: 
286      xml.text(n.getContent());
287      return;
288    case Element:
289      if (n.getName().equals("br"))
290        processBreak(xml, n);
291      else if (n.getName().equals("h2"))
292        processCaption(xml, n);
293      else if (n.getName().equals("col"))
294        processCol(xml, n);
295      else if (n.getName().equals("colgroup"))
296        processColGroup(xml, n);
297      else if (n.getName().equals("span"))
298        processContent(xml, n);
299      else if (n.getName().equals("footnote"))
300        processFootNote(xml, n);
301      else if (n.getName().equals("footnoteRef"))
302        processFootNodeRef(xml, n);
303      else if (n.getName().equals("li"))
304        processItem(xml, n);
305      else if (n.getName().equals("linkHtml"))
306        processlinkHtml(xml, n);
307      else if (n.getName().equals("ul") || n.getName().equals("ol"))
308        processList(xml, n);
309      else if (n.getName().equals("p"))
310        processParagraph(xml, n);
311      else if (n.getName().equals("img"))
312        processRenderMultiMedia(xml, n);
313      else if (n.getName().equals("sub"))
314        processSub(xml, n);
315      else if (n.getName().equals("sup"))
316        processSup(xml, n);
317      else if (n.getName().equals("table"))
318        processTable(xml, n);
319      else if (n.getName().equals("tbody"))
320        processTBody(xml, n);
321      else if (n.getName().equals("td"))
322        processTd(xml, n);
323      else if (n.getName().equals("tfoot"))
324        processTFoot(xml, n);
325      else if (n.getName().equals("th"))
326        processTh(xml, n);
327      else if (n.getName().equals("thead"))
328        processTHead(xml, n);
329      else if (n.getName().equals("tr"))
330        processTr(xml, n);
331      else
332        throw new FHIRException("Unknown element "+n.getName());
333    }
334  }
335
336  private void processBreak(IXMLWriter xml, XhtmlNode n) throws IOException {
337    xml.element("br");
338  }
339
340  private void processCaption(IXMLWriter xml, XhtmlNode n) throws IOException, FHIRException {
341    processAttributes(n, xml, "id", "language", "styleCode");
342    xml.enter("caption");
343    processChildren(xml, n);
344    xml.exit("caption");
345  }
346
347  private void processCol(IXMLWriter xml, XhtmlNode n) throws IOException, FHIRException {
348    processAttributes(n, xml, "id", "language", "styleCode", "span", "width", "align", "char", "charoff", "valign");
349    xml.enter("col");
350    processChildren(xml, n);
351    xml.exit("col");
352  }
353
354  private void processColGroup(IXMLWriter xml, XhtmlNode n) throws IOException, FHIRException {
355    processAttributes(n, xml, "id", "language", "styleCode", "span", "width", "align", "char", "charoff", "valign");
356    xml.enter("colgroup");
357    processChildren(xml, n);
358    xml.exit("colgroup");
359  }
360
361  private void processContent(IXMLWriter xml, XhtmlNode n) throws IOException, FHIRException {
362    processAttributes(n, xml, "id", "language", "styleCode");
363    xml.enter("content");
364    // todo: do something with revised..., "revised"
365    processChildren(xml, n);
366    xml.exit("content");
367  }
368
369  private void processFootNote(IXMLWriter xml, XhtmlNode n) {
370    throw new Error("element "+n.getName()+" not handled yet");
371  }
372
373  private void processFootNodeRef(IXMLWriter xml, XhtmlNode n) {
374    throw new Error("element "+n.getName()+" not handled yet");
375  }
376
377  private void processItem(IXMLWriter xml, XhtmlNode n) throws IOException, FHIRException {
378    processAttributes(n, xml, "id", "language", "styleCode");
379    xml.enter("item");
380    processChildren(xml, n);
381    xml.exit("item");
382  }
383
384  private void processlinkHtml(IXMLWriter xml, XhtmlNode n) throws IOException, FHIRException {
385    String v = n.getAttribute("src");
386    xml.attribute("referencedObject", v);
387    processAttributes(n, xml, "name", "href", "rel", "rev", "title", "id", "language", "styleCode");
388    xml.enter("linkHtml");
389    processChildren(xml, n);
390    xml.exit("linkHtml");
391  }
392
393  private void processList(IXMLWriter xml, XhtmlNode n) throws IOException, FHIRException {
394    if (n.getName().equals("ol"))
395      xml.attribute("listType", "ordered");
396    else
397      xml.attribute("listType", "unordered");
398    processAttributes(n, xml, "id", "language", "styleCode");
399    xml.enter("list");
400    processChildren(xml, n);
401    xml.exit("list");
402  }
403
404  private void processParagraph(IXMLWriter xml, XhtmlNode n) throws IOException, FHIRException {
405    processAttributes(n, xml, "id", "language", "styleCode");
406    xml.enter("paragraph");
407    processChildren(xml, n);
408    xml.exit("paragraph");
409  }
410
411  private void processRenderMultiMedia(IXMLWriter xml, XhtmlNode n) throws IOException, FHIRException {
412    String v = n.getAttribute("src");
413    xml.attribute("referencedObject", v);
414    processAttributes(n, xml, "id", "language", "styleCode");
415    xml.enter("renderMultiMedia");
416    processChildren(xml, n);
417    xml.exit("renderMultiMedia");
418  }
419
420  private void processSub(IXMLWriter xml, XhtmlNode n) throws IOException, FHIRException {
421    xml.enter("sub");
422    processChildren(xml, n);
423    xml.exit("sub");
424  }
425
426  private void processSup(IXMLWriter xml, XhtmlNode n) throws IOException, FHIRException {
427    xml.enter("sup");
428    processChildren(xml, n);
429    xml.exit("sup");
430  }
431
432  private void processTable(IXMLWriter xml, XhtmlNode n) throws IOException, FHIRException {
433    processAttributes(n, xml, "id", "language", "styleCode", "summary", "width", "border", "frame", "rules", "cellspacing", "cellpadding");
434    xml.enter("table");
435    processChildren(xml, n);
436    xml.exit("table");
437  }
438
439  private void processTBody(IXMLWriter xml, XhtmlNode n) throws IOException, FHIRException {
440    processAttributes(n, xml, "id", "language", "styleCode", "align", "char", "charoff", "valign");
441    xml.enter("tbody");
442    processChildren(xml, n);
443    xml.exit("tbody");
444  }
445
446  private void processTd(IXMLWriter xml, XhtmlNode n) throws IOException, FHIRException {
447    processAttributes(n, xml, "id", "language", "styleCode", "abbr", "axis", "headers", "scope", "rowspan", "colspan", "align", "char", "charoff", "valign");
448    xml.enter("td");
449    processChildren(xml, n);
450    xml.exit("td");
451  }
452
453  private void processTFoot(IXMLWriter xml, XhtmlNode n) {
454    throw new Error("element "+n.getName()+" not handled yet");
455  }
456
457  private void processTh(IXMLWriter xml, XhtmlNode n) throws IOException, FHIRException {
458    processAttributes(n, xml, "id", "language", "styleCode", "abbr", "axis", "headers", "scope", "rowspan", "colspan", "align", "char", "charoff", "valign");
459    xml.enter("th");
460    processChildren(xml, n);
461    xml.exit("th");
462  }
463
464  private void processTHead(IXMLWriter xml, XhtmlNode n) throws IOException, FHIRException {
465    processAttributes(n, xml, "id", "language", "styleCode", "align", "char", "charoff", "valign");
466    xml.enter("thead");
467    processChildren(xml, n);
468    xml.exit("thead");
469  }
470
471  private void processTr(IXMLWriter xml, XhtmlNode n) throws IOException, FHIRException {
472    processAttributes(n, xml, "id", "language", "styleCode", "align", "char", "charoff", "valign");
473    xml.enter("tr");
474    processChildren(xml, n);
475    xml.exit("tr");
476  }
477
478  private void processAttributes(XhtmlNode xn, IXMLWriter xml, String... names) throws IOException {
479    for (String n : names) {
480      if (xn.hasAttribute(n)) {
481        String v = xn.getAttribute(n);
482        if (n.equals("id"))
483          xml.attribute("ID", v);
484        else
485          xml.attribute(n, v);
486      }
487    }
488  }
489
490
491}