001package org.hl7.fhir.utilities.xhtml;
002
003/*
004  Copyright (c) 2011+, HL7, Inc.
005  All rights reserved.
006  
007  Redistribution and use in source and binary forms, with or without modification, 
008  are permitted provided that the following conditions are met:
009    
010   * Redistributions of source code must retain the above copyright notice, this 
011     list of conditions and the following disclaimer.
012   * Redistributions in binary form must reproduce the above copyright notice, 
013     this list of conditions and the following disclaimer in the documentation 
014     and/or other materials provided with the distribution.
015   * Neither the name of HL7 nor the names of its contributors may be used to 
016     endorse or promote products derived from this software without specific 
017     prior written permission.
018  
019  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
020  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
021  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
022  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
023  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
024  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
025  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
026  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
027  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
028  POSSIBILITY OF SUCH DAMAGE.
029  
030 */
031
032
033
034/*
035Copyright (c) 2011+, HL7, Inc
036All rights reserved.
037
038Redistribution and use in source and binary forms, with or without modification, 
039are permitted provided that the following conditions are met:
040
041 * Redistributions of source code must retain the above copyright notice, this 
042   list of conditions and the following disclaimer.
043 * Redistributions in binary form must reproduce the above copyright notice, 
044   this list of conditions and the following disclaimer in the documentation 
045   and/or other materials provided with the distribution.
046 * Neither the name of HL7 nor the names of its contributors may be used to 
047   endorse or promote products derived from this software without specific 
048   prior written permission.
049
050THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
051ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
052WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
053IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
054INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
055NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
056PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
057WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
058ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
059POSSIBILITY OF SUCH DAMAGE.
060
061*/
062
063import java.awt.Color;
064import java.awt.image.BufferedImage;
065import java.io.ByteArrayOutputStream;
066import java.io.File;
067import java.io.FileOutputStream;
068import java.io.IOException;
069import java.io.OutputStream;
070import java.util.ArrayList;
071import java.util.HashMap;
072import java.util.List;
073import java.util.Map;
074import java.util.Set;
075
076import javax.imageio.ImageIO;
077
078import org.apache.commons.codec.binary.Base64;
079import org.apache.commons.io.FileUtils;
080import org.commonmark.node.Node;
081import org.commonmark.parser.Parser;
082import org.commonmark.renderer.html.HtmlRenderer;
083import org.hl7.fhir.exceptions.FHIRException;
084import org.hl7.fhir.utilities.TranslatingUtilities;
085import org.hl7.fhir.utilities.Utilities;
086
087
088public class HierarchicalTableGenerator extends TranslatingUtilities {
089  public static final String TEXT_ICON_REFERENCE = "Reference to another Resource";
090  public static final String TEXT_ICON_PRIMITIVE = "Primitive Data Type";
091  public static final String TEXT_ICON_DATATYPE = "Data Type";
092  public static final String TEXT_ICON_RESOURCE = "Resource";
093  public static final String TEXT_ICON_ELEMENT = "Element";
094  public static final String TEXT_ICON_REUSE = "Reference to another Element";
095  public static final String TEXT_ICON_EXTENSION = "Extension";
096  public static final String TEXT_ICON_CHOICE = "Choice of Types";
097  public static final String TEXT_ICON_SLICE = "Slice Definition";
098  public static final String TEXT_ICON_SLICE_ITEM = "Slice Item";
099  public static final String TEXT_ICON_FIXED = "Fixed Value";
100  public static final String TEXT_ICON_EXTENSION_SIMPLE = "Simple Extension";
101  public static final String TEXT_ICON_PROFILE = "Profile";
102  public static final String TEXT_ICON_EXTENSION_COMPLEX = "Complex Extension";
103
104  public static final int NEW_REGULAR = 0;
105  public static final int CONTINUE_REGULAR = 1;
106  public static final int NEW_SLICER = 2;
107  public static final int CONTINUE_SLICER = 3;
108  public static final int NEW_SLICE = 4;
109  public static final int CONTINUE_SLICE = 5;
110  private static final String BACKGROUND_ALT_COLOR = "#F7F7F7";
111  public static boolean ACTIVE_TABLES = false;
112    
113  public enum TextAlignment {
114    LEFT, CENTER, RIGHT;  
115  }
116  
117  private static Map<String, String> files = new HashMap<String, String>();
118
119  private class Counter {
120    private int count = -1;
121    private void row() {
122      count++;
123    }
124    private boolean isOdd() {
125      return count % 2 == 1;
126    }
127  }
128  public class Piece {
129    private String tag;
130    private String reference;
131    private String text;
132    private String hint;
133    private String style;
134    private Map<String, String> attributes;
135    private List<XhtmlNode> children;
136    
137    public Piece(String tag) {
138      super();
139      this.tag = tag;
140    }
141    
142    public Piece(String reference, String text, String hint) {
143      super();
144      this.reference = reference;
145      this.text = text;
146      this.hint = hint;
147    }
148    public String getReference() {
149      return reference;
150    }
151    public void setReference(String value) {
152      reference = value;
153    }
154    public String getText() {
155      return text;
156    }
157    public String getHint() {
158      return hint;
159    }
160
161    public String getTag() {
162      return tag;
163    }
164
165    public String getStyle() {
166      return style;
167    }
168
169    public void setTag(String tag) {
170      this.tag = tag;
171    }
172
173    public Piece setText(String text) {
174      this.text = text;
175      return this;
176    }
177
178    public void setHint(String hint) {
179      this.hint = hint;
180    }
181
182    public Piece setStyle(String style) {
183      this.style = style;
184      return this;
185    }
186
187    public Piece addStyle(String style) {
188      if (this.style != null)
189        this.style = this.style+"; "+style;
190      else
191        this.style = style;
192      return this;
193    }
194
195    public void addToHint(String text) {
196      if (this.hint == null)
197        this.hint = text;
198      else
199        this.hint += (this.hint.endsWith(".") || this.hint.endsWith("?") ? " " : ". ")+text;
200    }
201    
202    public boolean hasChildren() {
203      return children != null && !children.isEmpty();
204    }
205
206    public List<XhtmlNode> getChildren() {
207      if (children == null)
208        children = new ArrayList<XhtmlNode>();
209      return children;
210    }
211
212    public Piece addHtml(XhtmlNode x) {
213      getChildren().add(x);
214      return this;
215    }
216    
217  }
218  
219  public class Cell {
220    private List<Piece> pieces = new ArrayList<HierarchicalTableGenerator.Piece>();
221    private String cellStyle;
222    protected int span = 1;
223    private TextAlignment alignment = TextAlignment.LEFT;
224    private String id;
225 
226    public Cell() {
227      
228    }
229    public Cell(String prefix, String reference, String text, String hint, String suffix) {
230      super();
231      if (!Utilities.noString(prefix))
232        pieces.add(new Piece(null, prefix, null));
233      pieces.add(new Piece(reference, text, hint));
234      if (!Utilities.noString(suffix))
235        pieces.add(new Piece(null, suffix, null));
236    }
237    public List<Piece> getPieces() {
238      return pieces;
239    }
240    public Cell addPiece(Piece piece) {
241      pieces.add(piece);
242      return this;
243    }
244
245
246    
247    public Cell addMarkdown(String md) {
248      if (!Utilities.noString(md)) {
249        try {
250          Parser parser = Parser.builder().build();
251          Node document = parser.parse(md);
252          HtmlRenderer renderer = HtmlRenderer.builder().escapeHtml(true).build();
253          String html = renderer.render(document);  
254          pieces.addAll(htmlToParagraphPieces(html, null));
255        } catch (Exception e) {
256          e.printStackTrace();
257        }
258      }
259      return this;
260    }
261    
262    public Cell addMarkdownNoPara(String md) {
263      return addMarkdownNoPara(md, null);
264    }
265    
266    public Cell addMarkdownNoPara(String md, String style) {
267      try {
268        Parser parser = Parser.builder().build();
269        Node document = parser.parse(md);
270        HtmlRenderer renderer = HtmlRenderer.builder().escapeHtml(true).build();
271        String html = renderer.render(document);  
272        pieces.addAll(htmlToParagraphPieces(html, style));
273      } catch (Exception e) {
274        e.printStackTrace();
275      }
276      return this;
277    }
278    
279    private List<Piece> htmlToParagraphPieces(String html, String style)  {
280      List<Piece> myPieces = new ArrayList<Piece>();
281      try {
282        XhtmlNode node = new XhtmlParser().parseFragment("<html>"+html+"</html>");
283        boolean first = true;
284        for (XhtmlNode c : node.getChildNodes()) {
285          if (first) {
286            first = false;
287          } else {
288            myPieces.add(new Piece("br"));
289            myPieces.add(new Piece("br"));            
290          }
291          if (c.getNodeType() == NodeType.Text) {
292            if (!Utilities.isWhitespace(c.getContent()))
293              addNode(myPieces, c, style);
294          } else if ("p".equals(c.getName())) {
295            for (XhtmlNode g : c.getChildNodes()) {
296              addNode(myPieces, g, style);
297            }
298          } else {
299           Piece x = new Piece(c.getName());
300           x.getChildren().addAll(c.getChildNodes());
301           if (style != null) {
302             x.addStyle(style);
303           }
304           myPieces.add(x);            
305          }
306        }
307//        String[] paragraphs = html.replace("<p>", "").split("<\\/p>|<br  \\/>");
308//        for (int i=0;i<paragraphs.length;i++) {
309//          if (!paragraphs[i].isEmpty()) {
310//            if (i!=0) {
311//              myPieces.add(new Piece("br"));
312//              myPieces.add(new Piece("br"));
313//            }
314//            myPieces.addAll(htmlFormattingToPieces(paragraphs[i]));
315//          }
316//        }
317      } catch (Exception e) {
318        throw new FHIRException("Exception parsing html: "+e.getMessage()+" for "+html, e);
319      }
320
321      return myPieces;
322    }
323    
324    private List<Piece> htmlFormattingToPieces(String html) throws IOException, FHIRException {
325      List<Piece> myPieces = new ArrayList<Piece>();
326      if (html.contains(("<"))) {
327        XhtmlNode node = new XhtmlParser().parseFragment("<p>"+html+"</p>");
328        for (XhtmlNode c : node.getChildNodes()) {
329          addNode(myPieces, c, null);
330        }
331      } else
332        myPieces.add(new Piece(null, html, null));        
333      return myPieces;
334    }
335    
336    private void addNode(List<Piece> list, XhtmlNode c, String style) {
337      if (c.getNodeType() == NodeType.Text)
338        list.add(styleIt(new Piece(null, c.getContent(), null), style));
339      else if (c.getNodeType() == NodeType.Element) {
340        if (c.getName().equals("a")) {
341          list.add(styleIt(new Piece(c.getAttribute("href"), c.allText(), c.getAttribute("title")), style));                    
342        } else if (c.getName().equals("b") || c.getName().equals("em") || c.getName().equals("strong")) {
343          list.add(styleIt(new Piece(null, c.allText(), null).setStyle("font-face: bold"), style));                    
344        } else if (c.getName().equals("code")) {
345          list.add(styleIt(new Piece(null, c.allText(), null).setStyle("padding: 2px 4px; color: #005c00; background-color: #f9f2f4; white-space: nowrap; border-radius: 4px"), style));                    
346        } else if (c.getName().equals("i")) {
347          list.add(styleIt(new Piece(null, c.allText(), null).setStyle("font-style: italic"), style));
348        } else if (c.getName().equals("pre")) {
349          Piece p = styleIt(new Piece(c.getName()).setStyle("white-space: pre; font-family: courier"), style);
350          list.add(p);
351          p.getChildren().addAll(c.getChildNodes());
352        } else if (c.getName().equals("ul") || c.getName().equals("ol")) {
353          Piece p = styleIt(new Piece(c.getName()), style);
354          list.add(p);
355          p.getChildren().addAll(c.getChildNodes());
356        } else if (c.getName().equals("i")) {
357          list.add(styleIt(new Piece(null, c.allText(), null).setStyle("font-style: italic"), style));                    
358        } else if (c.getName().equals("h1")||c.getName().equals("h2")||c.getName().equals("h3")||c.getName().equals("h4")) {
359          Piece p = styleIt(new Piece(c.getName()), style);
360          list.add(p);
361          p.getChildren().addAll(c.getChildNodes());
362        } else if (c.getName().equals("br")) {
363          list.add(styleIt(new Piece(c.getName()), style));
364        } else {
365          throw new Error("Not handled yet: "+c.getName());
366        }
367      } else
368        throw new Error("Unhandled type "+c.getNodeType().toString());
369    }
370    
371    
372    private Piece styleIt(Piece piece, String style) {
373      if (style != null) {
374        piece.addStyle(style);
375      }
376      return piece;
377    }
378    
379    public Cell addStyle(String style) {
380      for (Piece p : pieces)
381        p.addStyle(style);
382      return this;
383    }
384    public void addToHint(String text) {
385      for (Piece p : pieces)
386        p.addToHint(text);            
387    }
388    public Piece addStyledText(String hint, String alt, String fgColor, String bgColor, String link, boolean border) {
389      Piece p = new Piece(link, alt, hint);
390      p.addStyle("padding-left: 3px");
391      p.addStyle("padding-right: 3px");
392      if (border) {
393        p.addStyle("border: 1px grey solid");
394        p.addStyle("font-weight: bold");
395      }
396      if (fgColor != null) {
397        p.addStyle("color: "+fgColor);
398        p.addStyle("background-color: "+bgColor);
399      } else {
400        p.addStyle("color: black");
401        p.addStyle("background-color: "+bgColor != null ? bgColor : "white");       
402      }
403      pieces.add(p);
404      return p;
405    }
406    public String text() {
407      StringBuilder b = new StringBuilder();
408      for (Piece p : pieces)
409        b.append(p.text);
410      return b.toString();
411    }
412    @Override
413    public String toString() {
414      if (span != 1) {
415        return text()+" {"+span+"}";
416      } else {
417        return text();
418      }
419    }
420    public Cell setStyle(String value) {
421      cellStyle = value;
422      return this;
423    }
424    
425    public Cell span(int value) {
426      span = value;
427      return this;
428    }
429    public Cell center() {
430      alignment = TextAlignment.CENTER;
431      return this;
432    }
433    
434    public String getId() {
435      return id;
436    }
437    public void setId(String id) {
438      this.id = id;
439    }
440
441  }
442
443  public class Title extends Cell {
444    private int width;
445
446    public Title(String prefix, String reference, String text, String hint, String suffix, int width) {
447      super(prefix, reference, text, hint, suffix);
448      this.width = width;
449    }
450
451    public Title(String prefix, String reference, String text, String hint, String suffix, int width, int span) {
452      super(prefix, reference, text, hint, suffix);
453      this.width = width;
454      this.span = span;
455    }
456
457    public Title setStyle(String value) {
458      super.setStyle(value);
459      return this;
460    }
461  }
462  
463  public class Row {
464    private List<Row> subRows = new ArrayList<HierarchicalTableGenerator.Row>();
465    private List<Cell> cells = new ArrayList<HierarchicalTableGenerator.Cell>();
466    private String icon;
467    private String anchor;
468    private String hint;
469    private String color;
470    private int lineColor;
471    private String id;
472    private String opacity;
473    
474    public List<Row> getSubRows() {
475      return subRows;
476    }
477    public List<Cell> getCells() {
478      return cells;
479    }
480    public String getIcon() {
481      return icon;
482    }
483    public void setIcon(String icon, String hint) {
484      this.icon = icon;
485      this.hint = hint;
486    }
487    public String getAnchor() {
488      return anchor;
489    }
490    public void setAnchor(String anchor) {
491      this.anchor = anchor;
492    }
493    public String getHint() {
494      return hint;
495    }
496    public String getColor() {
497      return color;
498    }
499    public void setColor(String color) {
500      this.color = color;
501    }
502    public int getLineColor() {
503      return lineColor;
504    }
505    public void setLineColor(int lineColor) {
506      assert lineColor >= 0;
507      assert lineColor <= 2;
508      this.lineColor = lineColor;
509    }
510    public String getId() {
511      return id;
512    }
513    public void setId(String id) {
514      this.id = id;
515    }
516    public String getOpacity() {
517      return opacity;
518    }
519    public void setOpacity(String opacity) {
520      this.opacity = opacity;
521    }
522    
523  }
524
525  public class TableModel {
526    private String id;
527    private boolean active;
528    private List<Title> titles = new ArrayList<HierarchicalTableGenerator.Title>();
529    private List<Row> rows = new ArrayList<HierarchicalTableGenerator.Row>();
530    private String docoRef;
531    private String docoImg;
532    private boolean alternating;
533        
534    public TableModel(String id, boolean active) {
535      super();
536      this.id = id;
537      this.active = active;
538    }
539    public List<Title> getTitles() {
540      return titles;
541    }
542    public List<Row> getRows() {
543      return rows;
544    }
545    public String getDocoRef() {
546      return docoRef;
547    }
548    public String getDocoImg() {
549      return docoImg;
550    }
551    public void setDocoRef(String docoRef) {
552      this.docoRef = docoRef;
553    }
554    public void setDocoImg(String docoImg) {
555      this.docoImg = docoImg;
556    }
557    public String getId() {
558      return id;
559    }
560    
561    public void setId(String id) {
562      this.id = id;
563    }
564    public boolean isActive() {
565      return active && ACTIVE_TABLES;
566    }
567    public boolean isAlternating() {
568      return alternating;
569    }
570    public void setAlternating(boolean alternating) {
571      this.alternating = alternating;
572    }
573    
574  }
575
576
577  private String dest;
578  private boolean makeTargets;
579  
580  /**
581   * There are circumstances where the table has to present in the absence of a stable supporting infrastructure.
582   * and the file paths cannot be guaranteed. For these reasons, you can tell the builder to inline all the graphics
583   * (all the styles are inlined anyway, since the table fbuiler has even less control over the styling
584   *  
585   */
586  private boolean inLineGraphics;  
587  
588  public HierarchicalTableGenerator() {
589    super();
590  }
591
592  public HierarchicalTableGenerator(String dest, boolean inlineGraphics) {
593    super();
594    this.dest = dest;
595    this.inLineGraphics = inlineGraphics;
596    this.makeTargets = true;
597  }
598
599  public HierarchicalTableGenerator(String dest, boolean inlineGraphics, boolean makeTargets) {
600    super();
601    this.dest = dest;
602    this.inLineGraphics = inlineGraphics;
603    this.makeTargets = makeTargets;
604  }
605
606  public TableModel initNormalTable(String prefix, boolean isLogical, boolean alternating, String id, boolean isActive) {
607    TableModel model = new TableModel(id, isActive);
608    
609    model.setAlternating(alternating);
610    model.setDocoImg(Utilities.pathURL(prefix, "help16.png"));
611    model.setDocoRef(Utilities.pathURL(prefix, "formats.html#table"));
612    model.getTitles().add(new Title(null, model.getDocoRef(), translate("sd.head", "Name"), translate("sd.hint", "The logical name of the element"), null, 0));
613    model.getTitles().add(new Title(null, model.getDocoRef(), translate("sd.head", "Flags"), translate("sd.hint", "Information about the use of the element"), null, 0));
614    model.getTitles().add(new Title(null, model.getDocoRef(), translate("sd.head", "Card."), translate("sd.hint", "Minimum and Maximum # of times the the element can appear in the instance"), null, 0));
615    model.getTitles().add(new Title(null, model.getDocoRef(), translate("sd.head", "Type"), translate("sd.hint", "Reference to the type of the element"), null, 100));
616    model.getTitles().add(new Title(null, model.getDocoRef(), translate("sd.head", "Description & Constraints"), translate("sd.hint", "Additional information about the element"), null, 0));
617    if (isLogical) {
618      model.getTitles().add(new Title(null, prefix+"structuredefinition.html#logical", "Implemented As", "How this logical data item is implemented in a concrete resource", null, 0));
619    }
620    return model;
621  }
622
623  public TableModel initComparisonTable(String prefix, String id) {
624    TableModel model = new TableModel(id, true);
625    
626    model.setAlternating(true);
627    model.setDocoImg(Utilities.pathURL(prefix, "help16.png"));
628    model.setDocoRef(Utilities.pathURL(prefix, "formats.html#table"));    
629    model.getTitles().add(new Title(null, model.getDocoRef(), translate("sd.head", "Name"), translate("sd.hint", "The logical name of the element"), null, 0));
630    model.getTitles().add(new Title(null, model.getDocoRef(), translate("sd.head", "L Flags"), translate("sd.hint", "Information about the use of the element - Left Structure"), null, 0).setStyle("border-left: 1px grey solid"));
631    model.getTitles().add(new Title(null, model.getDocoRef(), translate("sd.head", "L Card."), translate("sd.hint", "Minimum and Maximum # of times the the element can appear in the instance - Left Structure"), null, 0));
632    model.getTitles().add(new Title(null, model.getDocoRef(), translate("sd.head", "L Type"), translate("sd.hint", "Reference to the type of the element - Left Structure"), null, 100));
633    model.getTitles().add(new Title(null, model.getDocoRef(), translate("sd.head", "L Description & Constraints"), translate("sd.hint", "Additional information about the element - Left Structure"), null, 0).setStyle("border-right: 1px grey solid"));
634    model.getTitles().add(new Title(null, model.getDocoRef(), translate("sd.head", "R Flags"), translate("sd.hint", "Information about the use of the element - Left Structure"), null, 0).setStyle("border-left: 1px grey solid"));
635    model.getTitles().add(new Title(null, model.getDocoRef(), translate("sd.head", "R Card."), translate("sd.hint", "Minimum and Maximum # of times the the element can appear in the instance - Left Structure"), null, 0));
636    model.getTitles().add(new Title(null, model.getDocoRef(), translate("sd.head", "L Type"), translate("sd.hint", "Reference to the type of the element - Left Structure"), null, 100));
637    model.getTitles().add(new Title(null, model.getDocoRef(), translate("sd.head", "L Description & Constraints"), translate("sd.hint", "Additional information about the element - Left Structure"), null, 0).setStyle("border-right: 1px grey solid"));
638    model.getTitles().add(new Title(null, model.getDocoRef(), translate("sd.head", "Comments"), translate("sd.hint", "Comments about the comparison"), null, 0));
639    return model;
640  }
641
642
643
644  public TableModel initGridTable(String prefix, String id) {
645    TableModel model = new TableModel(id, false);
646    
647    model.getTitles().add(new Title(null, model.getDocoRef(), translate("sd.head", "Name"), translate("sd.hint", "The name of the element (Slice name in brackets).  Mouse-over provides definition"), null, 0));
648    model.getTitles().add(new Title(null, model.getDocoRef(), translate("sd.head", "Card."), translate("sd.hint", "Minimum and Maximum # of times the the element can appear in the instance. Super-scripts indicate additional constraints on appearance"), null, 0));
649    model.getTitles().add(new Title(null, model.getDocoRef(), translate("sd.head", "Type"), translate("sd.hint", "Reference to the type of the element"), null, 100));
650    model.getTitles().add(new Title(null, model.getDocoRef(), translate("sd.head", "Constraints and Usage"), translate("sd.hint", "Fixed values, length limits, vocabulary bindings and other usage notes"), null, 0));
651    return model;
652  }
653
654  public XhtmlNode generate(TableModel model, String imagePath, int border, Set<String> outputTracker) throws IOException, FHIRException  {
655    checkModel(model);
656    XhtmlNode table = new XhtmlNode(NodeType.Element, "table").setAttribute("border", Integer.toString(border)).setAttribute("cellspacing", "0").setAttribute("cellpadding", "0");
657    
658    if (model.isActive()) {      
659      table.setAttribute("id", model.getId());
660    }
661    table.setAttribute("style", "border: " + border + "px #F0F0F0 solid; font-size: 11px; font-family: verdana; vertical-align: top;");
662    XhtmlNode tr = table.addTag("tr");
663    tr.setAttribute("style", "border: " + Integer.toString(1 + border) + "px #F0F0F0 solid; font-size: 11px; font-family: verdana; vertical-align: top");
664    XhtmlNode tc = null;
665    for (Title t : model.getTitles()) {
666      tc = renderCell(tr, t, "th", null, null, null, false, null, "white", 0, imagePath, border, outputTracker, model, null);
667      if (t.width != 0)
668        tc.setAttribute("style", "width: "+Integer.toString(t.width)+"px");
669    }
670    if (tc != null && model.getDocoRef() != null) {
671      XhtmlNode img = tc.addTag("span").setAttribute("style", "float: right").addTag("a").setAttribute("title", "Legend for this format").setAttribute("href", model.getDocoRef()).addTag("img");
672      img.setAttribute("alt", "doco").setAttribute("style", "background-color: inherit").setAttribute("src", model.getDocoImg());
673      if (model.isActive()) {
674        img.setAttribute("onload", "fhirTableInit(this)");
675      }
676    }
677      
678    Counter counter = new Counter();
679    for (Row r : model.getRows()) {
680      renderRow(table, r, 0, new ArrayList<Integer>(), imagePath, border, outputTracker, counter, model);
681    }
682    if (model.getDocoRef() != null) {
683      tr = table.addTag("tr");
684      tc = tr.addTag("td");
685      tc.setAttribute("class", "hierarchy");
686      tc.setAttribute("colspan", Integer.toString(model.getTitles().size()));
687      tc.addTag("br");
688      XhtmlNode a = tc.addTag("a").setAttribute("title", translate("sd.doco", "Legend for this format")).setAttribute("href", model.getDocoRef());
689      if (model.getDocoImg() != null)
690        a.addTag("img").setAttribute("alt", "doco").setAttribute("style", "background-color: inherit").setAttribute("src", model.getDocoImg());
691      a.addText(" "+translate("sd.doco", "Documentation for this format"));
692    }
693    return table;
694  }
695
696
697  private void renderRow(XhtmlNode table, Row r, int indent, List<Integer> indents, String imagePath, int border, Set<String> outputTracker, Counter counter, TableModel model) throws IOException  {
698    counter.row();
699    XhtmlNode tr = table.addTag("tr");
700    String color = "white";
701    if (r.getColor() != null)
702      color = r.getColor();
703    else if (model.isAlternating()  && counter.isOdd())
704      color = BACKGROUND_ALT_COLOR;
705    
706    tr.setAttribute("style", "border: " + border + "px #F0F0F0 solid; padding:0px; vertical-align: top; background-color: "+color+(r.getOpacity() == null ? "" : "; opacity: "+r.getOpacity()));
707    if (model.isActive()) {
708      tr.setAttribute("id", r.getId());
709    }
710    boolean first = true;
711    for (Cell t : r.getCells()) {
712      renderCell(tr, t, "td", first ? r.getIcon() : null, first ? r.getHint() : null, first ? indents : null, !r.getSubRows().isEmpty(), first ? r.getAnchor() : null, color, r.getLineColor(), imagePath, border, outputTracker, model, r);
713      first = false;
714    }
715    table.addText("\r\n");
716    
717    for (int i = 0; i < r.getSubRows().size(); i++) {
718      Row c = r.getSubRows().get(i);
719      List<Integer> ind = new ArrayList<Integer>();
720      ind.addAll(indents);
721      if (i == r.getSubRows().size() - 1) {
722        ind.add(r.getLineColor()*2);
723      } else {
724        ind.add(r.getLineColor()*2+1);
725      }
726      renderRow(table, c, indent+1, ind, imagePath, border, outputTracker, counter, model);
727    }
728  }
729
730
731  private XhtmlNode renderCell(XhtmlNode tr, Cell c, String name, String icon, String hint, List<Integer> indents, boolean hasChildren, String anchor, String color, int lineColor, String imagePath, int border, Set<String> outputTracker, TableModel table, Row row) throws IOException  {
732    XhtmlNode tc = tr.addTag(name);
733    tc.setAttribute("class", "hierarchy");
734    if (c.span > 1) {
735      tc.colspan(Integer.toString(c.span));
736    }
737    if (c.getId() != null) {
738      tc.setAttribute("id", c.getId());
739    }
740
741    if (indents != null) {
742      tc.addTag("img").setAttribute("src", srcFor(imagePath, "tbl_spacer.png")).setAttribute("style", "background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", ".");
743      tc.setAttribute("style", "vertical-align: top; text-align : left; "+(c.cellStyle != null  && c.cellStyle.contains("background-color") ? "" : "background-color: "+color+"; ")+"border: "+ border +"px #F0F0F0 solid; padding:0px 4px 0px 4px; white-space: nowrap; background-image: url("+imagePath+checkExists(indents, hasChildren, lineColor, outputTracker)+")"+(c.cellStyle != null ? ";"+c.cellStyle : ""));
744      for (int i = 0; i < indents.size()-1; i++) {
745        switch (indents.get(i)) {
746          case NEW_REGULAR:
747          case NEW_SLICER:
748          case NEW_SLICE:
749            tc.addTag("img").setAttribute("src", srcFor(imagePath, "tbl_blank.png")).setAttribute("style", "background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", ".");
750            break;
751          case CONTINUE_REGULAR:
752            tc.addTag("img").setAttribute("src", srcFor(imagePath, "tbl_vline.png")).setAttribute("style", "background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", ".");
753            break;
754          case CONTINUE_SLICER:
755            tc.addTag("img").setAttribute("src", srcFor(imagePath, "tbl_vline_slicer.png")).setAttribute("style", "background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", ".");
756            break;
757          case CONTINUE_SLICE:
758            tc.addTag("img").setAttribute("src", srcFor(imagePath, "tbl_vline_slice.png")).setAttribute("style", "background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", ".");
759            break;
760          default:
761            throw new Error("Unrecognized indent level: " + indents.get(i));
762        }
763      }
764      if (!indents.isEmpty()) {
765        String sfx = table.isActive() && hasChildren ? "-open" : "";
766        XhtmlNode img = tc.addTag("img");
767        switch (indents.get(indents.size()-1)) {
768        case NEW_REGULAR:
769          img.setAttribute("src", srcFor(imagePath, "tbl_vjoin_end"+sfx+".png")).setAttribute("style", "background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", ".");
770          break;
771        case NEW_SLICER:
772          img.setAttribute("src", srcFor(imagePath, "tbl_vjoin_end_slicer"+sfx+".png")).setAttribute("style", "background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", ".");
773          break;
774        case NEW_SLICE:
775          img.setAttribute("src", srcFor(imagePath, "tbl_vjoin_end_slice"+sfx+".png")).setAttribute("style", "background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", ".");
776          break;
777        case CONTINUE_REGULAR:
778          img.setAttribute("src", srcFor(imagePath, "tbl_vjoin"+sfx+".png")).setAttribute("style", "background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", ".");
779          break;
780        case CONTINUE_SLICER:
781          img.setAttribute("src", srcFor(imagePath, "tbl_vjoin_slicer"+sfx+".png")).setAttribute("style", "background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", ".");
782          break;
783        case CONTINUE_SLICE:
784          img.setAttribute("src", srcFor(imagePath, "tbl_vjoin_slice"+sfx+".png")).setAttribute("style", "background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", ".");
785          break;
786        default:
787          throw new Error("Unrecognized indent level: " + indents.get(indents.size()-1));
788        }
789        if (table.isActive() && hasChildren) {
790          img.setAttribute("onClick", "tableRowAction(this)");
791        }
792      }
793    }
794    else
795      tc.setAttribute("style", "vertical-align: top; text-align : left; "+(c.cellStyle != null  && c.cellStyle.contains("background-color") ? "" : "background-color: "+color+"; ")+"border: "+ border +"px #F0F0F0 solid; padding:0px 4px 0px 4px"+(c.cellStyle != null ? ";"+c.cellStyle : ""));
796    if (!Utilities.noString(icon)) {
797      XhtmlNode img = tc.addTag("img").setAttribute("src", srcFor(imagePath, icon)).setAttribute("class", "hierarchy").setAttribute("style", "background-color: "+color+"; background-color: inherit").setAttribute("alt", ".");
798      if (hint != null)
799        img.setAttribute("title", hint);
800      tc.addText(" ");
801    }
802    for (Piece p : c.pieces) {
803      if (!Utilities.noString(p.getTag())) {
804        XhtmlNode tag = tc.addTag(p.getTag());
805        if (p.attributes != null)
806          for (String n : p.attributes.keySet())
807            tag.setAttribute(n, p.attributes.get(n));
808        if (p.getHint() != null)
809          tag.setAttribute("title", p.getHint());
810        addStyle(tag, p);
811        if (p.hasChildren()) {
812          tag.getChildNodes().addAll(p.getChildren());
813        }
814      } else if (!Utilities.noString(p.getReference())) {
815        XhtmlNode a = addStyle(tc.addTag("a"), p);
816        a.setAttribute("href", p.getReference());
817        if (!Utilities.noString(p.getHint()))
818          a.setAttribute("title", p.getHint());
819        if (p.getText() != null) {
820          a.addText(p.getText());
821        } else {
822          a.addChildren(p.getChildren());
823        }
824        addStyle(a, p);
825      } else { 
826        if (!Utilities.noString(p.getHint())) {
827          XhtmlNode s = addStyle(tc.addTag("span"), p);
828          s.setAttribute("title", p.getHint());
829          s.addText(p.getText());
830        } else if (p.getStyle() != null) {
831          XhtmlNode s = addStyle(tc.addTag("span"), p);
832          s.addText(p.getText());
833        } else
834          tc.addText(p.getText());
835        if (p.hasChildren()) {
836          tc.getChildNodes().addAll(p.getChildren());
837        }
838      }
839    }
840    if (makeTargets && !Utilities.noString(anchor))
841      tc.addTag("a").setAttribute("name", nmTokenize(anchor)).addText(" ");
842    return tc;
843  }
844
845
846  private XhtmlNode addStyle(XhtmlNode node, Piece p) {
847    if (p.getStyle() != null)
848      node.setAttribute("style", p.getStyle());
849    return node;
850  }
851
852  private String nmTokenize(String anchor) {
853    return anchor.replace("[", "_").replace("]", "_");
854  }
855  
856  private String srcFor(String corePrefix, String filename) throws IOException {
857    if (inLineGraphics) {
858      if (files.containsKey(filename))
859        return files.get(filename);
860      StringBuilder b = new StringBuilder();
861      b.append("data:image/png;base64,");
862      byte[] bytes;
863      File file = new File(Utilities.path(dest, filename));
864      if (!file.exists()) // because sometime this is called real early before the files exist. it will be built again later because of this
865        bytes = new byte[0]; 
866      else
867        bytes = FileUtils.readFileToByteArray(file);
868      b.append(new String(Base64.encodeBase64(bytes)));
869//      files.put(filename, b.toString());
870      return b.toString();
871    } else
872      return corePrefix+filename;
873  }
874
875
876  private void checkModel(TableModel model) throws FHIRException  {
877    check(!model.getTitles().isEmpty(), "Must have titles");
878    int tc = 0;
879    for (Cell c : model.getTitles()) {
880      check(c);
881      tc = tc + c.span;
882    }
883    int i = 0;
884    for (Row r : model.getRows()) { 
885      check(r, "rows", tc, "", i, model.getRows().size());
886      i++;
887    }
888  }
889
890
891  private void check(Cell c) throws FHIRException  {  
892    boolean hasText = false;
893    for (Piece p : c.pieces)
894      if (!Utilities.noString(p.getText()))
895        hasText = true;
896    check(hasText, "Title cells must have text");    
897  }
898
899
900  private void check(Row r, String string, int size, String path, int index, int total) throws FHIRException  {
901    String id = Integer.toString(index)+".";
902    if (total <= 26) {
903      char c = (char) ('a'+index);
904      id = Character.toString(c);
905    }
906    path = path + id;
907    r.setId(path);
908    int tc = 0;
909    for (Cell c : r.getCells()) {
910      tc = tc + c.span;
911    }
912    check(tc == size, "All rows must have the same number of columns as the titles  ("+Integer.toString(size)+") but row "+path+" doesn't - it has "+tc+" ("+r.getCells().get(0).text()+"): "+r.getCells());
913    int i = 0;
914    for (Row c : r.getSubRows()) {
915      check(c, "rows", size, path, i, r.getSubRows().size());
916      i++;
917    }
918  }
919
920
921  private String checkExists(List<Integer> indents, boolean hasChildren, int lineColor, Set<String> outputTracker) throws IOException  {
922    String filename = makeName(indents);
923    
924    StringBuilder b = new StringBuilder();
925    if (inLineGraphics) {
926      if (files.containsKey(filename))
927        return files.get(filename);
928      ByteArrayOutputStream bytes = new ByteArrayOutputStream();
929      genImage(indents, hasChildren, lineColor, bytes);
930      b.append("data:image/png;base64,");
931      byte[] encodeBase64 = Base64.encodeBase64(bytes.toByteArray());
932      b.append(new String(encodeBase64));
933      files.put(filename, b.toString());
934      return b.toString();
935    } else {
936      b.append("tbl_bck");
937      for (Integer i : indents)
938        b.append(Integer.toString(i));
939      int indent = lineColor*2 + (hasChildren?1:0);
940      b.append(Integer.toString(indent));
941      b.append(".png");
942      String file = Utilities.path(dest, b.toString());
943      if (!new File(file).exists()) {
944        File newFile = new File(file);
945        newFile.getParentFile().mkdirs();
946        newFile.createNewFile();
947        FileOutputStream stream = new FileOutputStream(file);
948        genImage(indents, hasChildren, lineColor, stream);
949        if (outputTracker!=null)
950          outputTracker.add(file);
951      }
952      return b.toString();
953    }
954  }
955
956
957  private void genImage(List<Integer> indents, boolean hasChildren, int lineColor, OutputStream stream) throws IOException {
958    BufferedImage bi = new BufferedImage(800, 2, BufferedImage.TYPE_INT_ARGB);
959    // i have no idea why this works to make these pixels transparent. It defies logic. 
960    // But this combination of INT_ARGB and filling with grey magically worked when nothing else did. So it stays as is.
961    Color grey = new Color(99,99,99,0); 
962    for (int i = 0; i < 800; i++) {
963      bi.setRGB(i, 0, grey.getRGB());
964      bi.setRGB(i, 1, grey.getRGB());
965    }
966    Color black = new Color(0, 0, 0);
967    Color green = new Color(14,209,69);
968    Color gold = new Color(212,168,21);
969    for (int i = 0; i < indents.size(); i++) {
970      int indent = indents.get(i).intValue();
971      if (indent == CONTINUE_REGULAR)
972        bi.setRGB(12+(i*16), 0, black.getRGB());
973      else if (indent == CONTINUE_SLICER)
974        bi.setRGB(12+(i*16), 0, green.getRGB());
975      else if (indent == CONTINUE_SLICE)
976        bi.setRGB(12+(i*16), 0, gold.getRGB());
977    }
978    if (hasChildren) {
979      if (lineColor==0)
980        bi.setRGB(12+(indents.size()*16), 0, black.getRGB());
981      else if (lineColor==1)
982        bi.setRGB(12+(indents.size()*16), 0, green.getRGB());
983      else if (lineColor==2)
984        bi.setRGB(12+(indents.size()*16), 0, gold.getRGB());
985    }
986    ImageIO.write(bi, "PNG", stream);
987  }
988
989  private String makeName(List<Integer> indents) {
990    StringBuilder b = new StringBuilder();
991    b.append("indents:");
992    for (Integer i : indents)
993      b.append(Integer.toString(i));
994    return b.toString();
995  }
996
997  private void check(boolean check, String message) throws FHIRException  {
998    if (!check)
999      throw new FHIRException(message);
1000  }
1001
1002  public void emptyRow(TableModel model, int cellCount) {
1003    Row r = new Row();
1004    model.rows.add(r);
1005    for (int i = 0; i < cellCount; i++) {
1006      r.getCells().add(new Cell());
1007    }
1008  }
1009}