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 225 public Cell() { 226 227 } 228 public Cell(String prefix, String reference, String text, String hint, String suffix) { 229 super(); 230 if (!Utilities.noString(prefix)) 231 pieces.add(new Piece(null, prefix, null)); 232 pieces.add(new Piece(reference, text, hint)); 233 if (!Utilities.noString(suffix)) 234 pieces.add(new Piece(null, suffix, null)); 235 } 236 public List<Piece> getPieces() { 237 return pieces; 238 } 239 public Cell addPiece(Piece piece) { 240 pieces.add(piece); 241 return this; 242 } 243 public Cell addMarkdown(String md) { 244 try { 245 Parser parser = Parser.builder().build(); 246 Node document = parser.parse(md); 247 HtmlRenderer renderer = HtmlRenderer.builder().escapeHtml(true).build(); 248 String html = renderer.render(document); 249 pieces.addAll(htmlToParagraphPieces(html)); 250 } catch (Exception e) { 251 e.printStackTrace(); 252 } 253 return this; 254 } 255 256 private List<Piece> htmlToParagraphPieces(String html) { 257 List<Piece> myPieces = new ArrayList<Piece>(); 258 try { 259 XhtmlNode node = new XhtmlParser().parseFragment("<html>"+html+"</html>"); 260 boolean first = true; 261 for (XhtmlNode c : node.getChildNodes()) { 262 if (first) { 263 first = false; 264 } else { 265 myPieces.add(new Piece("br")); 266 myPieces.add(new Piece("br")); 267 } 268 if (c.getNodeType() == NodeType.Text) { 269 if (!Utilities.isWhitespace(c.getContent())) 270 addNode(myPieces, c); 271 } else if ("p".equals(c.getName())) { 272 for (XhtmlNode g : c.getChildNodes()) { 273 addNode(myPieces, g); 274 } 275 } else { 276 Piece x = new Piece(c.getName()); 277 x.getChildren().addAll(c.getChildNodes()); 278 myPieces.add(x); 279 } 280 } 281// String[] paragraphs = html.replace("<p>", "").split("<\\/p>|<br \\/>"); 282// for (int i=0;i<paragraphs.length;i++) { 283// if (!paragraphs[i].isEmpty()) { 284// if (i!=0) { 285// myPieces.add(new Piece("br")); 286// myPieces.add(new Piece("br")); 287// } 288// myPieces.addAll(htmlFormattingToPieces(paragraphs[i])); 289// } 290// } 291 } catch (Exception e) { 292 throw new FHIRException("Exception parsing html: "+e.getMessage()+" for "+html, e); 293 } 294 295 return myPieces; 296 } 297 298 private List<Piece> htmlFormattingToPieces(String html) throws IOException, FHIRException { 299 List<Piece> myPieces = new ArrayList<Piece>(); 300 if (html.contains(("<"))) { 301 XhtmlNode node = new XhtmlParser().parseFragment("<p>"+html+"</p>"); 302 for (XhtmlNode c : node.getChildNodes()) { 303 addNode(myPieces, c); 304 } 305 } else 306 myPieces.add(new Piece(null, html, null)); 307 return myPieces; 308 } 309 310 private void addNode(List<Piece> list, XhtmlNode c) { 311 if (c.getNodeType() == NodeType.Text) 312 list.add(new Piece(null, c.getContent(), null)); 313 else if (c.getNodeType() == NodeType.Element) { 314 if (c.getName().equals("a")) { 315 list.add(new Piece(c.getAttribute("href"), c.allText(), c.getAttribute("title"))); 316 } else if (c.getName().equals("b") || c.getName().equals("em") || c.getName().equals("strong")) { 317 list.add(new Piece(null, c.allText(), null).setStyle("font-face: bold")); 318 } else if (c.getName().equals("code")) { 319 list.add(new Piece(null, c.allText(), null).setStyle("padding: 2px 4px; color: #005c00; background-color: #f9f2f4; white-space: nowrap; border-radius: 4px")); 320 } else if (c.getName().equals("i")) { 321 list.add(new Piece(null, c.allText(), null).setStyle("font-style: italic")); 322 } else if (c.getName().equals("pre")) { 323 Piece p = new Piece(c.getName()).setStyle("white-space: pre; font-family: courier"); 324 list.add(p); 325 p.getChildren().addAll(c.getChildNodes()); 326 } else if (c.getName().equals("ul") || c.getName().equals("ol")) { 327 Piece p = new Piece(c.getName()); 328 list.add(p); 329 p.getChildren().addAll(c.getChildNodes()); 330 } else if (c.getName().equals("i")) { 331 list.add(new Piece(null, c.allText(), null).setStyle("font-style: italic")); 332 } else if (c.getName().equals("h1")||c.getName().equals("h2")||c.getName().equals("h3")||c.getName().equals("h4")) { 333 Piece p = new Piece(c.getName()); 334 list.add(p); 335 p.getChildren().addAll(c.getChildNodes()); 336 } else if (c.getName().equals("br")) { 337 list.add(new Piece(c.getName())); 338 } else { 339 340 throw new Error("Not handled yet: "+c.getName()); 341 } 342 } else 343 throw new Error("Unhandled type "+c.getNodeType().toString()); 344 345 } 346 public Cell addStyle(String style) { 347 for (Piece p : pieces) 348 p.addStyle(style); 349 return this; 350 } 351 public void addToHint(String text) { 352 for (Piece p : pieces) 353 p.addToHint(text); 354 } 355 public Piece addStyledText(String hint, String alt, String fgColor, String bgColor, String link, boolean border) { 356 Piece p = new Piece(link, alt, hint); 357 p.addStyle("padding-left: 3px"); 358 p.addStyle("padding-right: 3px"); 359 if (border) { 360 p.addStyle("border: 1px grey solid"); 361 p.addStyle("font-weight: bold"); 362 } 363 if (fgColor != null) { 364 p.addStyle("color: "+fgColor); 365 p.addStyle("background-color: "+bgColor); 366 } else { 367 p.addStyle("color: black"); 368 p.addStyle("background-color: "+bgColor != null ? bgColor : "white"); 369 } 370 pieces.add(p); 371 return p; 372 } 373 public String text() { 374 StringBuilder b = new StringBuilder(); 375 for (Piece p : pieces) 376 b.append(p.text); 377 return b.toString(); 378 } 379 @Override 380 public String toString() { 381 if (span != 1) { 382 return text()+" {"+span+"}"; 383 } else { 384 return text(); 385 } 386 } 387 public Cell setStyle(String value) { 388 cellStyle = value; 389 return this; 390 } 391 392 public Cell span(int value) { 393 span = value; 394 return this; 395 } 396 public Cell center() { 397 alignment = TextAlignment.CENTER; 398 return this; 399 } 400 401 402 } 403 404 public class Title extends Cell { 405 private int width; 406 407 public Title(String prefix, String reference, String text, String hint, String suffix, int width) { 408 super(prefix, reference, text, hint, suffix); 409 this.width = width; 410 } 411 412 public Title(String prefix, String reference, String text, String hint, String suffix, int width, int span) { 413 super(prefix, reference, text, hint, suffix); 414 this.width = width; 415 this.span = span; 416 } 417 418 public Title setStyle(String value) { 419 super.setStyle(value); 420 return this; 421 } 422 } 423 424 public class Row { 425 private List<Row> subRows = new ArrayList<HierarchicalTableGenerator.Row>(); 426 private List<Cell> cells = new ArrayList<HierarchicalTableGenerator.Cell>(); 427 private String icon; 428 private String anchor; 429 private String hint; 430 private String color; 431 private int lineColor; 432 private String id; 433 private String opacity; 434 435 public List<Row> getSubRows() { 436 return subRows; 437 } 438 public List<Cell> getCells() { 439 return cells; 440 } 441 public String getIcon() { 442 return icon; 443 } 444 public void setIcon(String icon, String hint) { 445 this.icon = icon; 446 this.hint = hint; 447 } 448 public String getAnchor() { 449 return anchor; 450 } 451 public void setAnchor(String anchor) { 452 this.anchor = anchor; 453 } 454 public String getHint() { 455 return hint; 456 } 457 public String getColor() { 458 return color; 459 } 460 public void setColor(String color) { 461 this.color = color; 462 } 463 public int getLineColor() { 464 return lineColor; 465 } 466 public void setLineColor(int lineColor) { 467 assert lineColor >= 0; 468 assert lineColor <= 2; 469 this.lineColor = lineColor; 470 } 471 public String getId() { 472 return id; 473 } 474 public void setId(String id) { 475 this.id = id; 476 } 477 public String getOpacity() { 478 return opacity; 479 } 480 public void setOpacity(String opacity) { 481 this.opacity = opacity; 482 } 483 484 } 485 486 public class TableModel { 487 private String id; 488 private boolean active; 489 private List<Title> titles = new ArrayList<HierarchicalTableGenerator.Title>(); 490 private List<Row> rows = new ArrayList<HierarchicalTableGenerator.Row>(); 491 private String docoRef; 492 private String docoImg; 493 private boolean alternating; 494 495 public TableModel(String id, boolean active) { 496 super(); 497 this.id = id; 498 this.active = active; 499 } 500 public List<Title> getTitles() { 501 return titles; 502 } 503 public List<Row> getRows() { 504 return rows; 505 } 506 public String getDocoRef() { 507 return docoRef; 508 } 509 public String getDocoImg() { 510 return docoImg; 511 } 512 public void setDocoRef(String docoRef) { 513 this.docoRef = docoRef; 514 } 515 public void setDocoImg(String docoImg) { 516 this.docoImg = docoImg; 517 } 518 public String getId() { 519 return id; 520 } 521 522 public void setId(String id) { 523 this.id = id; 524 } 525 public boolean isActive() { 526 return active && ACTIVE_TABLES; 527 } 528 public boolean isAlternating() { 529 return alternating; 530 } 531 public void setAlternating(boolean alternating) { 532 this.alternating = alternating; 533 } 534 535 } 536 537 538 private String dest; 539 private boolean makeTargets; 540 541 /** 542 * There are circumstances where the table has to present in the absence of a stable supporting infrastructure. 543 * and the file paths cannot be guaranteed. For these reasons, you can tell the builder to inline all the graphics 544 * (all the styles are inlined anyway, since the table fbuiler has even less control over the styling 545 * 546 */ 547 private boolean inLineGraphics; 548 549 public HierarchicalTableGenerator() { 550 super(); 551 } 552 553 public HierarchicalTableGenerator(String dest, boolean inlineGraphics) { 554 super(); 555 this.dest = dest; 556 this.inLineGraphics = inlineGraphics; 557 this.makeTargets = true; 558 } 559 560 public HierarchicalTableGenerator(String dest, boolean inlineGraphics, boolean makeTargets) { 561 super(); 562 this.dest = dest; 563 this.inLineGraphics = inlineGraphics; 564 this.makeTargets = makeTargets; 565 } 566 567 public TableModel initNormalTable(String prefix, boolean isLogical, boolean alternating, String id, boolean isActive) { 568 TableModel model = new TableModel(id, isActive); 569 570 model.setAlternating(alternating); 571 model.setDocoImg(prefix+"help16.png"); 572 model.setDocoRef(prefix+"formats.html#table"); 573 model.getTitles().add(new Title(null, model.getDocoRef(), translate("sd.head", "Name"), translate("sd.hint", "The logical name of the element"), null, 0)); 574 model.getTitles().add(new Title(null, model.getDocoRef(), translate("sd.head", "Flags"), translate("sd.hint", "Information about the use of the element"), null, 0)); 575 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)); 576 model.getTitles().add(new Title(null, model.getDocoRef(), translate("sd.head", "Type"), translate("sd.hint", "Reference to the type of the element"), null, 100)); 577 model.getTitles().add(new Title(null, model.getDocoRef(), translate("sd.head", "Description & Constraints"), translate("sd.hint", "Additional information about the element"), null, 0)); 578 if (isLogical) { 579 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)); 580 } 581 return model; 582 } 583 584 public TableModel initComparisonTable(String prefix, String id) { 585 TableModel model = new TableModel(id, true); 586 587 model.setAlternating(true); 588 model.setDocoImg(prefix+"help16.png"); 589 model.setDocoRef(prefix+"formats.html#table"); 590 model.getTitles().add(new Title(null, model.getDocoRef(), translate("sd.head", "Name"), translate("sd.hint", "The logical name of the element"), null, 0)); 591 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")); 592 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)); 593 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)); 594 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")); 595 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")); 596 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)); 597 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)); 598 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")); 599 model.getTitles().add(new Title(null, model.getDocoRef(), translate("sd.head", "Comments"), translate("sd.hint", "Comments about the comparison"), null, 0)); 600 return model; 601 } 602 603 604 605 public TableModel initGridTable(String prefix, String id) { 606 TableModel model = new TableModel(id, false); 607 608 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)); 609 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)); 610 model.getTitles().add(new Title(null, model.getDocoRef(), translate("sd.head", "Type"), translate("sd.hint", "Reference to the type of the element"), null, 100)); 611 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)); 612 return model; 613 } 614 615 public XhtmlNode generate(TableModel model, String imagePath, int border, Set<String> outputTracker) throws IOException, FHIRException { 616 checkModel(model); 617 XhtmlNode table = new XhtmlNode(NodeType.Element, "table").setAttribute("border", Integer.toString(border)).setAttribute("cellspacing", "0").setAttribute("cellpadding", "0"); 618 619 if (model.isActive()) { 620 table.setAttribute("id", model.getId()); 621 } 622 table.setAttribute("style", "border: " + border + "px #F0F0F0 solid; font-size: 11px; font-family: verdana; vertical-align: top;"); 623 XhtmlNode tr = table.addTag("tr"); 624 tr.setAttribute("style", "border: " + Integer.toString(1 + border) + "px #F0F0F0 solid; font-size: 11px; font-family: verdana; vertical-align: top"); 625 XhtmlNode tc = null; 626 for (Title t : model.getTitles()) { 627 tc = renderCell(tr, t, "th", null, null, null, false, null, "white", 0, imagePath, border, outputTracker, model, null); 628 if (t.width != 0) 629 tc.setAttribute("style", "width: "+Integer.toString(t.width)+"px"); 630 } 631 if (tc != null && model.getDocoRef() != null) { 632 XhtmlNode img = tc.addTag("span").setAttribute("style", "float: right").addTag("a").setAttribute("title", "Legend for this format").setAttribute("href", model.getDocoRef()).addTag("img"); 633 img.setAttribute("alt", "doco").setAttribute("style", "background-color: inherit").setAttribute("src", model.getDocoImg()); 634 if (model.isActive()) { 635 img.setAttribute("onload", "fhirTableInit(this)"); 636 } 637 } 638 639 Counter counter = new Counter(); 640 for (Row r : model.getRows()) { 641 renderRow(table, r, 0, new ArrayList<Integer>(), imagePath, border, outputTracker, counter, model); 642 } 643 if (model.getDocoRef() != null) { 644 tr = table.addTag("tr"); 645 tc = tr.addTag("td"); 646 tc.setAttribute("class", "hierarchy"); 647 tc.setAttribute("colspan", Integer.toString(model.getTitles().size())); 648 tc.addTag("br"); 649 XhtmlNode a = tc.addTag("a").setAttribute("title", translate("sd.doco", "Legend for this format")).setAttribute("href", model.getDocoRef()); 650 if (model.getDocoImg() != null) 651 a.addTag("img").setAttribute("alt", "doco").setAttribute("style", "background-color: inherit").setAttribute("src", model.getDocoImg()); 652 a.addText(" "+translate("sd.doco", "Documentation for this format")); 653 } 654 return table; 655 } 656 657 658 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 { 659 counter.row(); 660 XhtmlNode tr = table.addTag("tr"); 661 String color = "white"; 662 if (r.getColor() != null) 663 color = r.getColor(); 664 else if (model.isAlternating() && counter.isOdd()) 665 color = BACKGROUND_ALT_COLOR; 666 667 tr.setAttribute("style", "border: " + border + "px #F0F0F0 solid; padding:0px; vertical-align: top; background-color: "+color+(r.getOpacity() == null ? "" : "; opacity: "+r.getOpacity())); 668 if (model.isActive()) { 669 tr.setAttribute("id", r.getId()); 670 } 671 boolean first = true; 672 for (Cell t : r.getCells()) { 673 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); 674 first = false; 675 } 676 table.addText("\r\n"); 677 678 for (int i = 0; i < r.getSubRows().size(); i++) { 679 Row c = r.getSubRows().get(i); 680 List<Integer> ind = new ArrayList<Integer>(); 681 ind.addAll(indents); 682 if (i == r.getSubRows().size() - 1) { 683 ind.add(r.getLineColor()*2); 684 } else { 685 ind.add(r.getLineColor()*2+1); 686 } 687 renderRow(table, c, indent+1, ind, imagePath, border, outputTracker, counter, model); 688 } 689 } 690 691 692 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 { 693 XhtmlNode tc = tr.addTag(name); 694 tc.setAttribute("class", "hierarchy"); 695 if (c.span > 1) { 696 tc.colspan(Integer.toString(c.span)); 697 } 698 if (indents != null) { 699 tc.addTag("img").setAttribute("src", srcFor(imagePath, "tbl_spacer.png")).setAttribute("style", "background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", "."); 700 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 : "")); 701 for (int i = 0; i < indents.size()-1; i++) { 702 switch (indents.get(i)) { 703 case NEW_REGULAR: 704 case NEW_SLICER: 705 case NEW_SLICE: 706 tc.addTag("img").setAttribute("src", srcFor(imagePath, "tbl_blank.png")).setAttribute("style", "background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", "."); 707 break; 708 case CONTINUE_REGULAR: 709 tc.addTag("img").setAttribute("src", srcFor(imagePath, "tbl_vline.png")).setAttribute("style", "background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", "."); 710 break; 711 case CONTINUE_SLICER: 712 tc.addTag("img").setAttribute("src", srcFor(imagePath, "tbl_vline_slicer.png")).setAttribute("style", "background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", "."); 713 break; 714 case CONTINUE_SLICE: 715 tc.addTag("img").setAttribute("src", srcFor(imagePath, "tbl_vline_slice.png")).setAttribute("style", "background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", "."); 716 break; 717 default: 718 throw new Error("Unrecognized indent level: " + indents.get(i)); 719 } 720 } 721 if (!indents.isEmpty()) { 722 String sfx = table.isActive() && hasChildren ? "-open" : ""; 723 XhtmlNode img = tc.addTag("img"); 724 switch (indents.get(indents.size()-1)) { 725 case NEW_REGULAR: 726 img.setAttribute("src", srcFor(imagePath, "tbl_vjoin_end"+sfx+".png")).setAttribute("style", "background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", "."); 727 break; 728 case NEW_SLICER: 729 img.setAttribute("src", srcFor(imagePath, "tbl_vjoin_end_slicer"+sfx+".png")).setAttribute("style", "background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", "."); 730 break; 731 case NEW_SLICE: 732 img.setAttribute("src", srcFor(imagePath, "tbl_vjoin_end_slice"+sfx+".png")).setAttribute("style", "background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", "."); 733 break; 734 case CONTINUE_REGULAR: 735 img.setAttribute("src", srcFor(imagePath, "tbl_vjoin"+sfx+".png")).setAttribute("style", "background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", "."); 736 break; 737 case CONTINUE_SLICER: 738 img.setAttribute("src", srcFor(imagePath, "tbl_vjoin_slicer"+sfx+".png")).setAttribute("style", "background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", "."); 739 break; 740 case CONTINUE_SLICE: 741 img.setAttribute("src", srcFor(imagePath, "tbl_vjoin_slice"+sfx+".png")).setAttribute("style", "background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", "."); 742 break; 743 default: 744 throw new Error("Unrecognized indent level: " + indents.get(indents.size()-1)); 745 } 746 if (table.isActive() && hasChildren) { 747 img.setAttribute("onClick", "tableRowAction(this)"); 748 } 749 } 750 } 751 else 752 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 : "")); 753 if (!Utilities.noString(icon)) { 754 XhtmlNode img = tc.addTag("img").setAttribute("src", srcFor(imagePath, icon)).setAttribute("class", "hierarchy").setAttribute("style", "background-color: "+color+"; background-color: inherit").setAttribute("alt", "."); 755 if (hint != null) 756 img.setAttribute("title", hint); 757 tc.addText(" "); 758 } 759 for (Piece p : c.pieces) { 760 if (!Utilities.noString(p.getTag())) { 761 XhtmlNode tag = tc.addTag(p.getTag()); 762 if (p.attributes != null) 763 for (String n : p.attributes.keySet()) 764 tag.setAttribute(n, p.attributes.get(n)); 765 if (p.getHint() != null) 766 tag.setAttribute("title", p.getHint()); 767 addStyle(tag, p); 768 if (p.hasChildren()) 769 tag.getChildNodes().addAll(p.getChildren()); 770 } else if (!Utilities.noString(p.getReference())) { 771 XhtmlNode a = addStyle(tc.addTag("a"), p); 772 a.setAttribute("href", p.getReference()); 773 if (!Utilities.noString(p.getHint())) 774 a.setAttribute("title", p.getHint()); 775 if (p.getText() != null) { 776 a.addText(p.getText()); 777 } else { 778 a.addChildren(p.getChildren()); 779 } 780 addStyle(a, p); 781 } else { 782 if (!Utilities.noString(p.getHint())) { 783 XhtmlNode s = addStyle(tc.addTag("span"), p); 784 s.setAttribute("title", p.getHint()); 785 s.addText(p.getText()); 786 } else if (p.getStyle() != null) { 787 XhtmlNode s = addStyle(tc.addTag("span"), p); 788 s.addText(p.getText()); 789 } else 790 tc.addText(p.getText()); 791 } 792 } 793 if (makeTargets && !Utilities.noString(anchor)) 794 tc.addTag("a").setAttribute("name", nmTokenize(anchor)).addText(" "); 795 return tc; 796 } 797 798 799 private XhtmlNode addStyle(XhtmlNode node, Piece p) { 800 if (p.getStyle() != null) 801 node.setAttribute("style", p.getStyle()); 802 return node; 803 } 804 805 private String nmTokenize(String anchor) { 806 return anchor.replace("[", "_").replace("]", "_"); 807 } 808 809 private String srcFor(String corePrefix, String filename) throws IOException { 810 if (inLineGraphics) { 811 if (files.containsKey(filename)) 812 return files.get(filename); 813 StringBuilder b = new StringBuilder(); 814 b.append("data:image/png;base64,"); 815 byte[] bytes; 816 File file = new File(Utilities.path(dest, filename)); 817 if (!file.exists()) // because sometime this is called real early before the files exist. it will be built again later because of this 818 bytes = new byte[0]; 819 else 820 bytes = FileUtils.readFileToByteArray(file); 821 b.append(new String(Base64.encodeBase64(bytes))); 822// files.put(filename, b.toString()); 823 return b.toString(); 824 } else 825 return corePrefix+filename; 826 } 827 828 829 private void checkModel(TableModel model) throws FHIRException { 830 check(!model.getRows().isEmpty(), "Must have rows"); 831 check(!model.getTitles().isEmpty(), "Must have titles"); 832 int tc = 0; 833 for (Cell c : model.getTitles()) { 834 check(c); 835 tc = tc + c.span; 836 } 837 int i = 0; 838 for (Row r : model.getRows()) { 839 check(r, "rows", tc, "", i, model.getRows().size()); 840 i++; 841 } 842 } 843 844 845 private void check(Cell c) throws FHIRException { 846 boolean hasText = false; 847 for (Piece p : c.pieces) 848 if (!Utilities.noString(p.getText())) 849 hasText = true; 850 check(hasText, "Title cells must have text"); 851 } 852 853 854 private void check(Row r, String string, int size, String path, int index, int total) throws FHIRException { 855 String id = Integer.toString(index)+"."; 856 if (total <= 26) { 857 char c = (char) ('a'+index); 858 id = Character.toString(c); 859 } 860 path = path + id; 861 r.setId(path); 862 int tc = 0; 863 for (Cell c : r.getCells()) { 864 tc = tc + c.span; 865 } 866 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()); 867 int i = 0; 868 for (Row c : r.getSubRows()) { 869 check(c, "rows", size, path, i, r.getSubRows().size()); 870 i++; 871 } 872 } 873 874 875 private String checkExists(List<Integer> indents, boolean hasChildren, int lineColor, Set<String> outputTracker) throws IOException { 876 String filename = makeName(indents); 877 878 StringBuilder b = new StringBuilder(); 879 if (inLineGraphics) { 880 if (files.containsKey(filename)) 881 return files.get(filename); 882 ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 883 genImage(indents, hasChildren, lineColor, bytes); 884 b.append("data:image/png;base64,"); 885 byte[] encodeBase64 = Base64.encodeBase64(bytes.toByteArray()); 886 b.append(new String(encodeBase64)); 887 files.put(filename, b.toString()); 888 return b.toString(); 889 } else { 890 b.append("tbl_bck"); 891 for (Integer i : indents) 892 b.append(Integer.toString(i)); 893 int indent = lineColor*2 + (hasChildren?1:0); 894 b.append(Integer.toString(indent)); 895 b.append(".png"); 896 String file = Utilities.path(dest, b.toString()); 897 if (!new File(file).exists()) { 898 File newFile = new File(file); 899 newFile.getParentFile().mkdirs(); 900 newFile.createNewFile(); 901 FileOutputStream stream = new FileOutputStream(file); 902 genImage(indents, hasChildren, lineColor, stream); 903 if (outputTracker!=null) 904 outputTracker.add(file); 905 } 906 return b.toString(); 907 } 908 } 909 910 911 private void genImage(List<Integer> indents, boolean hasChildren, int lineColor, OutputStream stream) throws IOException { 912 BufferedImage bi = new BufferedImage(800, 2, BufferedImage.TYPE_INT_ARGB); 913 // i have no idea why this works to make these pixels transparent. It defies logic. 914 // But this combination of INT_ARGB and filling with grey magically worked when nothing else did. So it stays as is. 915 Color grey = new Color(99,99,99,0); 916 for (int i = 0; i < 800; i++) { 917 bi.setRGB(i, 0, grey.getRGB()); 918 bi.setRGB(i, 1, grey.getRGB()); 919 } 920 Color black = new Color(0, 0, 0); 921 Color green = new Color(14,209,69); 922 Color gold = new Color(212,168,21); 923 for (int i = 0; i < indents.size(); i++) { 924 int indent = indents.get(i).intValue(); 925 if (indent == CONTINUE_REGULAR) 926 bi.setRGB(12+(i*16), 0, black.getRGB()); 927 else if (indent == CONTINUE_SLICER) 928 bi.setRGB(12+(i*16), 0, green.getRGB()); 929 else if (indent == CONTINUE_SLICE) 930 bi.setRGB(12+(i*16), 0, gold.getRGB()); 931 } 932 if (hasChildren) { 933 if (lineColor==0) 934 bi.setRGB(12+(indents.size()*16), 0, black.getRGB()); 935 else if (lineColor==1) 936 bi.setRGB(12+(indents.size()*16), 0, green.getRGB()); 937 else if (lineColor==2) 938 bi.setRGB(12+(indents.size()*16), 0, gold.getRGB()); 939 } 940 ImageIO.write(bi, "PNG", stream); 941 } 942 943 private String makeName(List<Integer> indents) { 944 StringBuilder b = new StringBuilder(); 945 b.append("indents:"); 946 for (Integer i : indents) 947 b.append(Integer.toString(i)); 948 return b.toString(); 949 } 950 951 private void check(boolean check, String message) throws FHIRException { 952 if (!check) 953 throw new FHIRException(message); 954 } 955 956 public void emptyRow(TableModel model, int cellCount) { 957 Row r = new Row(); 958 model.rows.add(r); 959 for (int i = 0; i < cellCount; i++) { 960 r.getCells().add(new Cell()); 961 } 962 } 963}