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.InputStream; 070import java.io.OutputStream; 071import java.util.ArrayList; 072import java.util.HashMap; 073import java.util.List; 074import java.util.Map; 075import java.util.Set; 076 077import javax.imageio.ImageIO; 078 079import org.apache.commons.codec.binary.Base64; 080import org.apache.commons.io.FileUtils; 081import org.apache.commons.lang3.StringUtils; 082import org.commonmark.node.Node; 083import org.commonmark.parser.Parser; 084import org.commonmark.renderer.html.HtmlRenderer; 085import org.hl7.fhir.exceptions.FHIRException; 086import org.hl7.fhir.utilities.CommaSeparatedStringBuilder; 087import org.hl7.fhir.utilities.FileUtilities; 088import org.hl7.fhir.utilities.UUIDUtilities; 089import org.hl7.fhir.utilities.Utilities; 090import org.hl7.fhir.utilities.filesystem.ManagedFileAccess; 091import org.hl7.fhir.utilities.http.ManagedWebAccess; 092import org.hl7.fhir.utilities.i18n.RenderingI18nContext; 093 094 095public class HierarchicalTableGenerator { 096 public enum TableGenerationMode { 097 XML, XHTML 098 } 099 100 // used in older versions, where translation is not set up 101 public static final String TEXT_ICON_REFERENCE = "Reference to another Resource"; 102 public static final String TEXT_ICON_PRIMITIVE = "Primitive Data Type"; 103 public static final String TEXT_ICON_KEY = "JSON Key Value"; 104 public static final String TEXT_ICON_DATATYPE = "Data Type"; 105 public static final String TEXT_ICON_RESOURCE = "Resource"; 106 public static final String TEXT_ICON_ELEMENT = "Element"; 107 public static final String TEXT_ICON_OBJECT_BOX = "Object"; 108 public static final String TEXT_ICON_REUSE = "Reference to another Element"; 109 public static final String TEXT_ICON_EXTENSION = "Extension"; 110 public static final String TEXT_ICON_CHOICE = "Choice of Types"; 111 public static final String TEXT_ICON_SLICE = "Slice Definition"; 112 public static final String TEXT_ICON_SLICE_ITEM = "Slice Item"; 113 public static final String TEXT_ICON_FIXED = "Fixed Value"; 114 public static final String TEXT_ICON_EXTENSION_SIMPLE = "Simple Extension"; 115 public static final String TEXT_ICON_PROFILE = "Profile"; 116 public static final String TEXT_ICON_EXTENSION_COMPLEX = "Complex Extension"; 117 118 public static final int NEW_REGULAR = 0; 119 public static final int CONTINUE_REGULAR = 1; 120 public static final int NEW_SLICER = 2; 121 public static final int CONTINUE_SLICER = 3; 122 public static final int NEW_SLICE = 4; 123 public static final int CONTINUE_SLICE = 5; 124 private static final String BACKGROUND_ALT_COLOR = "#F7F7F7"; 125 public static boolean ACTIVE_TABLES = false; 126 public static String uuid = UUIDUtilities.makeUuidLC(); 127 private static Set<String> KNOWN_ROLES = Set.of("binding", "constraint", "obligation");; 128 129 public enum TextAlignment { 130 LEFT, CENTER, RIGHT; 131 } 132 133 private static Map<String, String> files = new HashMap<String, String>(); 134 135 private class Counter { 136 private int count = -1; 137 private void row() { 138 count++; 139 } 140 private boolean isOdd() { 141 return count % 2 == 1; 142 } 143 } 144 public class Piece { 145 private String tag; 146 private String reference; 147 private String text; 148 private String hint; 149 private String style; 150 private String tagImg; 151 private Map<String, String> attributes; 152 private XhtmlNodeList children; 153 154 public Piece(String tag) { 155 super(); 156 this.tag = tag; 157 } 158 159 public Piece(String reference, String text, String hint) { 160 super(); 161 this.reference = reference; 162 this.text = text; 163 this.hint = hint; 164 } 165 public Piece(String role, String tag) { 166 super(); 167 this.tag = tag; 168 this.setRole(role); 169 } 170 171 public Piece(String role, String reference, String text, String hint) { 172 super(); 173 this.reference = reference; 174 this.text = text; 175 this.hint = hint; 176 this.setRole(role); 177 } 178 public String getReference() { 179 return reference; 180 } 181 public void setReference(String value) { 182 reference = value; 183 } 184 public String getText() { 185 return text; 186 } 187 public String getHint() { 188 return hint; 189 } 190 191 public String getTag() { 192 return tag; 193 } 194 195 public String getStyle() { 196 return style; 197 } 198 199 public String getRole() { 200 if (attributes == null) { 201 return null; 202 } else { 203 for (String s : attributes.get("class").split("\\ ")) { 204 if (KNOWN_ROLES.contains(s)) { 205 return s; 206 } 207 } 208 return null; 209 } 210 } 211 212 public Piece setTag(String tag) { 213 this.tag = tag; 214 return this; 215 } 216 217 public Piece setText(String text) { 218 this.text = text; 219 return this; 220 } 221 222 public void setHint(String hint) { 223 this.hint = hint; 224 } 225 226 public void setRole(String role) { 227 if (!KNOWN_ROLES.contains(role)) { 228 throw new Error("Unknown role "+role); 229 } 230 setClass(role); 231 } 232 233 public Piece setClass(String role) { 234 if (attributes == null) { 235 attributes = new HashMap<String, String>(); 236 } 237 if (attributes.containsKey("class")) { 238 attributes.put("class", attributes.get("class") + " "+ role); 239 } else { 240 attributes.put("class", role); 241 } 242 return this; 243 } 244 245 public Piece setStyle(String style) { 246 this.style = style; 247 return this; 248 } 249 250 public Piece addStyle(String style) { 251 if (this.style != null) 252 this.style = this.style+"; "+style; 253 else 254 this.style = style; 255 return this; 256 } 257 258 public void addToHint(String text) { 259 if (this.hint == null) 260 this.hint = text; 261 else 262 this.hint += (this.hint.endsWith(".") || this.hint.endsWith("?") ? " " : ". ")+text; 263 } 264 265 public boolean hasChildren() { 266 return children != null && !children.isEmpty(); 267 } 268 269 public XhtmlNodeList getChildren() { 270 if (children == null) 271 children = new XhtmlNodeList(); 272 return children; 273 } 274 275 public Piece addHtml(XhtmlNode x) { 276 getChildren().add(x); 277 return this; 278 } 279 280 public Piece attr(String name, String value) { 281 if (attributes == null) { 282 attributes = new HashMap<>(); 283 } 284 attributes.put(name, value); 285 return this; 286 } 287 288 public String getTagImg() { 289 return tagImg; 290 } 291 292 public Piece setTagImg(String tagImg) { 293 this.tagImg = tagImg; 294 return this; 295 } 296 297 public boolean hasAttributes() { 298 return attributes != null && attributes.size() > 0; 299 } 300 301 } 302 303 public class Cell { 304 private List<Piece> pieces = new ArrayList<HierarchicalTableGenerator.Piece>(); 305 private String cellStyle; 306 protected int span = 1; 307 private boolean innerTable; // if you want a multiline left cell, you have to set this to true 308 private TextAlignment alignment = TextAlignment.LEFT; 309 private String id; 310 311 public Cell() { 312 313 } 314 public Cell(String prefix, String reference, String text, String hint, String suffix) { 315 super(); 316 if (!Utilities.noString(prefix)) 317 pieces.add(new Piece(null, prefix, null)); 318 pieces.add(new Piece(reference, text, hint)); 319 if (!Utilities.noString(suffix)) 320 pieces.add(new Piece(null, suffix, null)); 321 } 322 public List<Piece> getPieces() { 323 return pieces; 324 } 325 public Cell addPiece(Piece piece) { 326 pieces.add(piece); 327 return this; 328 } 329 330 public Cell addMarkdown(String md) { 331 return addMarkdown(md, null); 332 } 333 334 public Cell addMarkdown(String md, String style) { 335 if (!Utilities.noString(md)) { 336 try { 337 Parser parser = Parser.builder().build(); 338 Node document = parser.parse(md); 339 HtmlRenderer renderer = HtmlRenderer.builder().escapeHtml(true).build(); 340 String html = renderer.render(document); 341 pieces.addAll(htmlToParagraphPieces(html, style)); 342 } catch (Exception e) { 343 e.printStackTrace(); 344 } 345 } 346 return this; 347 } 348 349 public Cell addMarkdownNoPara(String md) { 350 return addMarkdownNoPara(md, null); 351 } 352 353 public Cell addMarkdownNoPara(String md, String style) { 354 try { 355 Parser parser = Parser.builder().build(); 356 Node document = parser.parse(md); 357 HtmlRenderer renderer = HtmlRenderer.builder().escapeHtml(true).build(); 358 String html = renderer.render(document); 359 pieces.addAll(htmlToParagraphPieces(html, style)); 360 } catch (Exception e) { 361 e.printStackTrace(); 362 } 363 return this; 364 } 365 366 public Cell addMarkdownNoPara(String role, String md, String style) { 367 try { 368 Parser parser = Parser.builder().build(); 369 Node document = parser.parse(md); 370 HtmlRenderer renderer = HtmlRenderer.builder().escapeHtml(true).build(); 371 String html = renderer.render(document); 372 List<Piece> hp = htmlToParagraphPieces(html, style); 373 // Trim unwanted trailing line-breaks 374 while (!hp.isEmpty() && hp.get(hp.size()-1).getTag() != null && hp.get(hp.size()-1).getTag().equals("br")) 375 hp.remove(hp.size()-1); 376 for (Piece p : hp) { 377 p.setRole(role); 378 } 379 pieces.addAll(hp); 380 } catch (Exception e) { 381 e.printStackTrace(); 382 } 383 return this; 384 } 385 386 private List<Piece> htmlToParagraphPieces(String html, String style) { 387 List<Piece> myPieces = new ArrayList<Piece>(); 388 try { 389 XhtmlNode node = new XhtmlParser().parseFragment("<html>"+html+"</html>"); 390 boolean first = true; 391 for (XhtmlNode c : node.getChildNodes()) { 392 if (first) { 393 first = false; 394 } else { 395 myPieces.add(new Piece("br")); 396 myPieces.add(new Piece("br")); 397 } 398 if (c.getNodeType() == NodeType.Text) { 399 if (!StringUtils.isWhitespace(c.getContent())) 400 addNode(myPieces, c, style); 401 } else if ("p".equals(c.getName())) { 402 for (XhtmlNode g : c.getChildNodes()) { 403 addNode(myPieces, g, style); 404 } 405 } else { 406 Piece x = new Piece(c.getName()); 407 x.getChildren().addAll(c.getChildNodes()); 408 if (style != null) { 409 x.addStyle(style); 410 } 411 myPieces.add(x); 412 } 413 } 414// String[] paragraphs = html.replace("<p>", "").split("<\\/p>|<br \\/>"); 415// for (int i=0;i<paragraphs.length;i++) { 416// if (!paragraphs[i].isEmpty()) { 417// if (i!=0) { 418// myPieces.add(new Piece("br")); 419// myPieces.add(new Piece("br")); 420// } 421// myPieces.addAll(htmlFormattingToPieces(paragraphs[i])); 422// } 423// } 424 } catch (Exception e) { 425 throw new FHIRException("Exception parsing html: "+e.getMessage()+" for "+html, e); 426 } 427 428 return myPieces; 429 } 430 431 private List<Piece> htmlFormattingToPieces(String html) throws IOException, FHIRException { 432 List<Piece> myPieces = new ArrayList<Piece>(); 433 if (html.contains(("<"))) { 434 XhtmlNode node = new XhtmlParser().parseFragment("<p>"+html+"</p>"); 435 for (XhtmlNode c : node.getChildNodes()) { 436 addNode(myPieces, c, null); 437 } 438 } else 439 myPieces.add(new Piece(null, html, null)); 440 return myPieces; 441 } 442 443 private void addNode(List<Piece> list, XhtmlNode c, String style) { 444 if (c.getNodeType() == NodeType.Text) 445 list.add(styleIt(new Piece(null, c.getContent(), null), style)); 446 else if (c.getNodeType() == NodeType.Element) { 447 if (c.getName().equals("a")) { 448 list.add(styleIt(new Piece(c.getAttribute("href"), c.allText(), c.getAttribute("title")), style)); 449 } else if (c.getName().equals("b") || c.getName().equals("em") || c.getName().equals("strong")) { 450 list.add(styleIt(new Piece(null, c.allText(), null).setStyle("font-face: bold"), style)); 451 } else if (c.getName().equals("code")) { 452 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)); 453 } else if (c.getName().equals("i")) { 454 list.add(styleIt(new Piece(null, c.allText(), null).setStyle("font-style: italic"), style)); 455 } else if (c.getName().equals("pre")) { 456 Piece p = styleIt(new Piece(c.getName()).setStyle("white-space: pre; font-family: courier"), style); 457 list.add(p); 458 p.getChildren().addAll(c.getChildNodes()); 459 } else if (c.getName().equals("ul") || c.getName().equals("ol")) { 460 Piece p = styleIt(new Piece(c.getName()), style); 461 list.add(p); 462 p.getChildren().addAll(c.getChildNodes()); 463 } else if (c.getName().equals("i")) { 464 list.add(styleIt(new Piece(null, c.allText(), null).setStyle("font-style: italic"), style)); 465 } else if (c.getName().equals("h1")||c.getName().equals("h2")||c.getName().equals("h3")||c.getName().equals("h4")) { 466 Piece p = styleIt(new Piece(c.getName()), style); 467 list.add(p); 468 p.getChildren().addAll(c.getChildNodes()); 469 } else if (c.getName().equals("br")) { 470 list.add(styleIt(new Piece(c.getName()), style)); 471 } else { 472 throw new Error("Not handled yet: "+c.getName()); 473 } 474 } else 475 throw new Error("Unhandled type "+c.getNodeType().toString()); 476 } 477 478 479 private Piece styleIt(Piece piece, String style) { 480 if (style != null) { 481 piece.addStyle(style); 482 } 483 return piece; 484 } 485 486 public Cell addStyle(String style) { 487 for (Piece p : pieces) 488 p.addStyle(style); 489 return this; 490 } 491 492 public Cell addCellStyle(String style) { 493 if (cellStyle == null) { 494 cellStyle = style; 495 } else { 496 cellStyle = cellStyle+"; "+style; 497 } 498 return this; 499 } 500 501 public void addToHint(String text) { 502 for (Piece p : pieces) 503 p.addToHint(text); 504 } 505 public Piece addStyledText(String hint, String alt, String fgColor, String bgColor, String link, boolean border) { 506 Piece p = new Piece(link, alt, hint); 507 p.addStyle("padding-left: 3px"); 508 p.addStyle("padding-right: 3px"); 509 if (border) { 510 p.addStyle("border: 1px grey solid"); 511 p.addStyle("font-weight: bold"); 512 } 513 if (fgColor != null) { 514 p.addStyle("color: "+fgColor); 515 p.addStyle("background-color: "+bgColor); 516 } else { 517 p.addStyle("color: black"); 518 p.addStyle("background-color: "+bgColor != null ? bgColor : "white"); 519 } 520 pieces.add(p); 521 return p; 522 } 523 public Piece addText(String text) { 524 Piece p = new Piece(null, text, null); 525 pieces.add(p); 526 return p; 527 } 528 public String text() { 529 StringBuilder b = new StringBuilder(); 530 for (Piece p : pieces) 531 b.append(p.text); 532 return b.toString(); 533 } 534 @Override 535 public String toString() { 536 if (span != 1) { 537 return text()+" {"+span+"}"; 538 } else { 539 return text(); 540 } 541 } 542 public Cell setStyle(String value) { 543 cellStyle = value; 544 return this; 545 } 546 547 public Cell span(int value) { 548 span = value; 549 return this; 550 } 551 public Cell center() { 552 alignment = TextAlignment.CENTER; 553 return this; 554 } 555 556 public String getId() { 557 return id; 558 } 559 public void setId(String id) { 560 this.id = id; 561 } 562 public Piece addImg(String icon, String hint, String link) { 563 Piece p = new Piece("img"); 564 p.attr("src", icon); 565 p.hint = hint; 566 p.reference = link; 567 pieces.add(p); 568 return p; 569 } 570 public void addXhtml(XhtmlNode div) { 571 Piece p = new Piece(null); 572 pieces.add(p); 573 p.children = div.childNodes; 574 } 575 public boolean isInnerTable() { 576 return innerTable; 577 } 578 public void setInnerTable(boolean innerTable) { 579 this.innerTable = innerTable; 580 } 581 582 } 583 584 public class Title extends Cell { 585 private int width; 586 private boolean filter; 587 private Map<String, String> checkboxes = new HashMap<String, String>(); 588 589 public Title(String prefix, String reference, String text, String hint, String suffix, int width) { 590 super(prefix, reference, text, hint, suffix); 591 this.width = width; 592 } 593 594 public Title(String prefix, String reference, String text, String hint, String suffix, int width, int span) { 595 super(prefix, reference, text, hint, suffix); 596 this.width = width; 597 this.span = span; 598 } 599 600 public Title setStyle(String value) { 601 super.setStyle(value); 602 return this; 603 } 604 605 public boolean isFilter() { 606 return filter; 607 } 608 609 public void setFilter(boolean filter) { 610 this.filter = filter; 611 } 612 613 public Map<String, String> getCheckboxes() { 614 return checkboxes; 615 } 616 617 public void setCheckboxes(Map<String, String> checkboxes) { 618 this.checkboxes = checkboxes; 619 } 620 621 622 } 623 624 public class Row { 625 private List<Row> subRows = new ArrayList<HierarchicalTableGenerator.Row>(); 626 private List<Cell> cells = new ArrayList<HierarchicalTableGenerator.Cell>(); 627 private String icon; 628 private String anchor; 629 private String hint; 630 private String color; 631 private int lineColor; 632 private String id; 633 private String opacity; 634 private String topLine; 635 private boolean partnerRow; 636 637 public List<Row> getSubRows() { 638 return subRows; 639 } 640 public List<Cell> getCells() { 641 return cells; 642 } 643 public String getIcon() { 644 return icon; 645 } 646 public void setIcon(String icon, String hint) { 647 this.icon = icon; 648 this.hint = hint; 649 } 650 public String getAnchor() { 651 return anchor; 652 } 653 public void setAnchor(String anchor) { 654 this.anchor = anchor; 655 } 656 public String getHint() { 657 return hint; 658 } 659 public String getColor() { 660 return color; 661 } 662 public void setColor(String color) { 663 this.color = color; 664 } 665 public int getLineColor() { 666 return lineColor; 667 } 668 public void setLineColor(int lineColor) { 669 assert lineColor >= 0; 670 assert lineColor <= 2; 671 this.lineColor = lineColor; 672 } 673 public String getId() { 674 return id; 675 } 676 public void setId(String id) { 677 this.id = id; 678 } 679 public String getOpacity() { 680 return opacity; 681 } 682 public void setOpacity(String opacity) { 683 this.opacity = opacity; 684 } 685 public String text() { 686 CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder(); 687 for (Cell c : cells) { 688 b.append(c.text()); 689 } 690 return b.toString(); 691 } 692 public String getTopLine() { 693 return topLine; 694 } 695 public void setTopLine(String topLine) { 696 this.topLine = topLine; 697 } 698 699 } 700 701 public class TableModel { 702 private String id; 703 private boolean active; 704 private List<Title> titles = new ArrayList<HierarchicalTableGenerator.Title>(); 705 private List<Row> rows = new ArrayList<HierarchicalTableGenerator.Row>(); 706 private String docoRef; 707 private String docoImg; 708 private boolean alternating; 709 private boolean showHeadings = true; 710 private boolean border = false; 711 712 public TableModel(String id, boolean active) { 713 super(); 714 this.id = id; 715 this.active = active; 716 } 717 public List<Title> getTitles() { 718 return titles; 719 } 720 public List<Row> getRows() { 721 return rows; 722 } 723 public String getDocoRef() { 724 return docoRef; 725 } 726 public String getDocoImg() { 727 return docoImg; 728 } 729 public void setDocoRef(String docoRef) { 730 this.docoRef = docoRef; 731 } 732 public void setDocoImg(String docoImg) { 733 this.docoImg = docoImg; 734 } 735 public String getId() { 736 return id; 737 } 738 739 public void setId(String id) { 740 this.id = id; 741 } 742 public boolean isActive() { 743 return active && ACTIVE_TABLES; 744 } 745 public boolean isAlternating() { 746 return alternating; 747 } 748 public void setAlternating(boolean alternating) { 749 this.alternating = alternating; 750 } 751 public boolean isShowHeadings() { 752 return showHeadings; 753 } 754 public void setShowHeadings(boolean showHeadings) { 755 this.showHeadings = showHeadings; 756 } 757 public boolean isBorder() { 758 return border; 759 } 760 public void setBorder(boolean border) { 761 this.border = border; 762 } 763 764 } 765 766 767 private String dest; 768 private boolean makeTargets; 769 private String defPath = ""; 770 771 /** 772 * There are circumstances where the table has to present in the absence of a stable supporting infrastructure. 773 * and the file paths cannot be guaranteed. For these reasons, you can tell the builder to inline all the graphics 774 * (all the styles are inlined anyway, since the table fbuiler has even less control over the styling 775 * 776 */ 777 private boolean inLineGraphics; 778 779 private TableGenerationMode mode; 780 private RenderingI18nContext i18n; 781 private String uniqueLocalPrefix; 782 private boolean treelines = true; 783 784 public HierarchicalTableGenerator(RenderingI18nContext i18n) { 785 super(); 786 this.i18n = i18n; 787 } 788 789 public HierarchicalTableGenerator(RenderingI18nContext i18n, String uniqueLocalPrefix) { 790 super(); 791 this.i18n = i18n; 792 this.uniqueLocalPrefix = uniqueLocalPrefix; 793 } 794 795 public HierarchicalTableGenerator(RenderingI18nContext i18n, String dest, boolean inlineGraphics) { 796 super(); 797 this.i18n = i18n; 798 this.dest = dest; 799 this.inLineGraphics = inlineGraphics; 800 this.makeTargets = true; 801 checkSetup(); 802 } 803 804 public HierarchicalTableGenerator(RenderingI18nContext i18n, String dest, boolean inlineGraphics, String uniqueLocalPrefix) { 805 super(); 806 this.i18n = i18n; 807 this.dest = dest; 808 this.inLineGraphics = inlineGraphics; 809 this.makeTargets = true; 810 this.uniqueLocalPrefix = uniqueLocalPrefix; 811 checkSetup(); 812 } 813 814 public HierarchicalTableGenerator(RenderingI18nContext i18n, String dest, boolean inlineGraphics, boolean makeTargets, String defPath, String uniqueLocalPrefix) { 815 super(); 816 this.i18n = i18n; 817 this.dest = dest; 818 this.inLineGraphics = inlineGraphics; 819 this.makeTargets = makeTargets; 820 this.defPath = defPath; 821 this.uniqueLocalPrefix = uniqueLocalPrefix; 822 checkSetup(); 823 } 824 825 public HierarchicalTableGenerator(RenderingI18nContext i18n, String dest, boolean inlineGraphics, boolean makeTargets, String uniqueLocalPrefix) { 826 super(); 827 this.i18n = i18n; 828 this.dest = dest; 829 this.inLineGraphics = inlineGraphics; 830 this.makeTargets = makeTargets; 831 this.uniqueLocalPrefix = uniqueLocalPrefix; 832 checkSetup(); 833 } 834 835 public HierarchicalTableGenerator(RenderingI18nContext i18n, String dest, boolean inlineGraphics, boolean makeTargets) { 836 super(); 837 this.i18n = i18n; 838 this.dest = dest; 839 this.inLineGraphics = inlineGraphics; 840 this.makeTargets = makeTargets; 841 checkSetup(); 842 } 843 844 private void checkSetup() { 845 if (dest == null) { 846 throw new Error("what"); 847 } 848 } 849 850 public String getDefPath() { 851 return defPath; 852 } 853 854 public TableModel initNormalTable(String prefix, boolean isLogical, boolean alternating, String id, boolean isActive, TableGenerationMode mode) throws IOException { 855 this.mode = mode; 856 857 TableModel model = new TableModel(id, isActive); 858 859 model.setAlternating(alternating); 860 if (mode == TableGenerationMode.XML) { 861 model.setDocoImg(help16AsData()); 862 } else { 863 model.setDocoImg(Utilities.pathURL(ManagedWebAccess.makeSecureRef(prefix), "help16.png")); 864 } 865 model.setDocoRef(Utilities.pathURL("https://build.fhir.org/ig/FHIR/ig-guidance", "readingIgs.html#table-views")); 866 model.getTitles().add(new Title(null, model.getDocoRef(), i18n.formatPhrase(RenderingI18nContext.GENERAL_NAME), i18n.formatPhrase(RenderingI18nContext.GENERAL_LOGICAL_NAME), null, 0)); 867 model.getTitles().add(new Title(null, model.getDocoRef(), i18n.formatPhrase(RenderingI18nContext.GENERAL_FLAGS), i18n.formatPhrase(RenderingI18nContext.SD_HEAD_FLAGS_DESC), null, 0)); 868 model.getTitles().add(new Title(null, model.getDocoRef(), i18n.formatPhrase(RenderingI18nContext.GENERAL_CARD), i18n.formatPhrase(RenderingI18nContext.SD_HEAD_CARD_DESC), null, 0)); 869 model.getTitles().add(new Title(null, model.getDocoRef(), i18n.formatPhrase(RenderingI18nContext.GENERAL_TYPE), i18n.formatPhrase(RenderingI18nContext.SD_GRID_HEAD_TYPE_DESC), null, 100)); 870 Title t = new Title(null, model.getDocoRef(), i18n.formatPhrase(RenderingI18nContext.GENERAL_DESC_CONST), i18n.formatPhrase(RenderingI18nContext.SD_HEAD_DESC_DESC), null, 0); 871 t.setFilter(true); 872 t.checkboxes.put(i18n.formatPhrase(RenderingI18nContext.GENERAL_OBLIGATIONS), "obligation"); 873 t.checkboxes.put(i18n.formatPhrase(RenderingI18nContext.GENERAL_CONSTRAINTS), "constraint"); 874 t.checkboxes.put(i18n.formatPhrase(RenderingI18nContext.GENERAL_BINDINGS), "binding"); 875 model.getTitles().add(t); 876 if (isLogical) { 877 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)); 878 } 879 return model; 880 } 881 882 883 public TableModel initComparisonTable(String prefix, String id) throws IOException { 884 TableModel model = new TableModel(id, true); 885 886 model.setAlternating(true); 887 if (mode == TableGenerationMode.XML) { 888 model.setDocoImg(help16AsData()); 889 } else { 890 model.setDocoImg(Utilities.pathURL(ManagedWebAccess.makeSecureRef(prefix), "help16.png")); 891 } 892 model.setDocoRef(Utilities.pathURL(ManagedWebAccess.makeSecureRef(prefix), "formats.html#table")); 893 model.getTitles().add(new Title(null, model.getDocoRef(), i18n.formatPhrase(RenderingI18nContext.GENERAL_NAME), i18n.formatPhrase(RenderingI18nContext.GENERAL_LOGICAL_NAME), null, 0)); 894 model.getTitles().add(new Title(null, model.getDocoRef(), i18n.formatPhrase(RenderingI18nContext.SD_COMP_HEAD_FLAGS_L), i18n.formatPhrase(RenderingI18nContext.SD_COMP_HEAD_FLAGS_L_DESC), null, 0).setStyle("border-left: 1px grey solid")); 895 model.getTitles().add(new Title(null, model.getDocoRef(), i18n.formatPhrase(RenderingI18nContext.SD_COMP_HEAD_CARD_L), i18n.formatPhrase(RenderingI18nContext.SD_COMP_HEAD_CARD_L_DESC), null, 0)); 896 model.getTitles().add(new Title(null, model.getDocoRef(), i18n.formatPhrase(RenderingI18nContext.SD_COMP_HEAD_TYPE_L), i18n.formatPhrase(RenderingI18nContext.SD_COMP_HEAD_TYPE_L_DESC), null, 100)); 897 model.getTitles().add(new Title(null, model.getDocoRef(), i18n.formatPhrase(RenderingI18nContext.SD_COMP_HEAD_DESC_L), i18n.formatPhrase(RenderingI18nContext.SD_COMP_HEAD_DESC_L_DESC), null, 0).setStyle("border-right: 1px grey solid")); 898 model.getTitles().add(new Title(null, model.getDocoRef(), i18n.formatPhrase(RenderingI18nContext.SD_COMP_HEAD_FLAGS_R), i18n.formatPhrase(RenderingI18nContext.SD_COMP_HEAD_FLAGS_R_DESC), null, 0).setStyle("border-left: 1px grey solid")); 899 model.getTitles().add(new Title(null, model.getDocoRef(), i18n.formatPhrase(RenderingI18nContext.SD_COMP_HEAD_CARD_R), i18n.formatPhrase(RenderingI18nContext.SD_COMP_HEAD_CARD_R_DESC), null, 0)); 900 model.getTitles().add(new Title(null, model.getDocoRef(), i18n.formatPhrase(RenderingI18nContext.SD_COMP_HEAD_TYPE_R), i18n.formatPhrase(RenderingI18nContext.SD_COMP_HEAD_TYPE_R_DESC), null, 100)); 901 model.getTitles().add(new Title(null, model.getDocoRef(), i18n.formatPhrase(RenderingI18nContext.SD_COMP_HEAD_DESC_R), i18n.formatPhrase(RenderingI18nContext.SD_COMP_HEAD_DESC_R_DESC), null, 0).setStyle("border-right: 1px grey solid")); 902 model.getTitles().add(new Title(null, model.getDocoRef(), i18n.formatPhrase(RenderingI18nContext.GENERAL_COMMENTS), i18n.formatPhrase(RenderingI18nContext.SD_COMP_HEAD_COMP_DESC), null, 0)); 903 return model; 904 } 905 906 907 908 public TableModel initGridTable(String prefix, String id) { 909 TableModel model = new TableModel(id, false); 910 model.getTitles().add(new Title(null, model.getDocoRef(), i18n.formatPhrase(RenderingI18nContext.GENERAL_NAME), i18n.formatPhrase(RenderingI18nContext.SD_GRID_HEAD_NAME_DESC), null, 0)); 911 model.getTitles().add(new Title(null, model.getDocoRef(), i18n.formatPhrase(RenderingI18nContext.GENERAL_CARD), i18n.formatPhrase(RenderingI18nContext.SD_GRID_HEAD_CARD_DESC), null, 0)); 912 model.getTitles().add(new Title(null, model.getDocoRef(), i18n.formatPhrase(RenderingI18nContext.GENERAL_TYPE), i18n.formatPhrase(RenderingI18nContext.SD_GRID_HEAD_TYPE_DESC), null, 100)); 913 model.getTitles().add(new Title(null, model.getDocoRef(), i18n.formatPhrase(RenderingI18nContext.SD_GRID_HEAD_DESC), i18n.formatPhrase(RenderingI18nContext.SD_GRID_HEAD_DESC_DESC), null, 0)); 914 915 return model; 916 } 917 918 919 public String treeFilterJS(String mid, Map<String, String> checkboxes) { 920 String js= " // "+uuid+"\n"; 921 922 for (String s : Utilities.sorted(checkboxes.keySet())) { 923 String id = "cb"+mid+"-"+checkboxes.get(s); 924 js += "document.getElementById('"+id+"').checked = 'false' != localStorage.getItem('ht-table-states-"+checkboxes.get(s)+"');\n"; 925 js += "filterDesc(document.getElementById('"+mid+"'), '"+checkboxes.get(s)+"', document.getElementById('cb"+mid+"-"+checkboxes.get(s)+"').checked, document.getElementById('pp"+mid+"'));\n"; 926 } 927 928 return js; 929 } 930 931 public XhtmlNode generate(TableModel model, String imagePath, int border, Set<String> outputTracker) throws IOException, FHIRException { 932 checkModel(model); 933 boolean script = false; 934 Map<String, String> checkboxes = null; 935 for (Title t : model.getTitles()) { 936 script = script || t.isFilter() || t.getCheckboxes().size() > 0; 937 if (t.getCheckboxes().size() > 0) { 938 checkboxes = t.getCheckboxes(); 939 } 940 } 941 XhtmlNode table = new XhtmlNode(NodeType.Element, "table").setAttribute("border", Integer.toString(border)).setAttribute("cellspacing", "0").setAttribute("cellpadding", "0"); 942 if (model.active) { 943 table.setAttribute("fhir", "generated-heirarchy"); // deprecated - will be removed once everyone has dealt with the change 944 table.setAttribute("data-fhir", "generated-heirarchy"); 945 } 946 if (model.isActive()) { 947 table.setAttribute("id", model.getId()); 948 } 949 if (model.isBorder()) { 950 table.style("border: 2px black solid; font-size: 11px; font-family: verdana; vertical-align: top;"); 951 } else { 952 table.style("border: " + border + "px #F0F0F0 solid; font-size: 11px; font-family: verdana; vertical-align: top;"); 953 } 954 if (model.isShowHeadings()) { 955 XhtmlNode tr = table.addTag("tr"); 956 if (model.active) { 957 tr.setAttribute("fhir", "generated-heirarchy"); // deprecated - will be removed once everyone has dealt with the change 958 tr.setAttribute("data-fhir", "generated-heirarchy"); 959 } 960 tr.style("border: " + Integer.toString(1 + border) + "px #F0F0F0 solid; font-size: 11px; font-family: verdana; vertical-align: top"); 961 XhtmlNode tc = null; 962 for (Title t : model.getTitles()) { 963 tc = renderCell(tr, t, "th", null, null, null, false, null, "white", 0, imagePath, border, outputTracker, model, null, true, model.active && t.isFilter(), model.getId(), t.getCheckboxes()); 964 if (t.width != 0) 965 tc.style("width: "+Integer.toString(t.width)+"px"); 966 } 967 if (tc != null && model.getDocoRef() != null) { 968 XhtmlNode a = tc.addTag("span").style("float: right").addTag("a").setAttribute("title", "Legend for this format").setAttribute("href", model.getDocoRef()); 969 if (mode == TableGenerationMode.XHTML) { 970 a.setAttribute("no-external", "true"); // deprecated - will be removed once everyone has dealt with the change 971 a.setAttribute("data-no-external", "true"); 972 } 973 XhtmlNode img = a.addTag("img"); 974 img.setAttribute("alt", "doco").style("background-color: inherit").setAttribute("src", model.getDocoImg()); 975 if (model.isActive()) { 976 img.setAttribute("onLoad", "fhirTableInit(this)"); 977 } 978 } 979 } 980 Counter counter = new Counter(); 981 for (Row r : model.getRows()) { 982 renderRow(table, r, 0, new ArrayList<Integer>(), imagePath, border, outputTracker, counter, model); 983 } 984 if (model.getDocoRef() != null) { 985 XhtmlNode tr = table.addTag("tr"); 986 if (model.active) { 987 tr.setAttribute("fhir", "generated-heirarchy"); // deprecated - will be removed once everyone has dealt with the change 988 tr.setAttribute("data-fhir", "generated-heirarchy"); 989 } 990 XhtmlNode tc = tr.addTag("td"); 991 tc.setAttribute("class", "hierarchy"); 992 tc.setAttribute("colspan", Integer.toString(model.getTitles().size())); 993 tc.addTag("br"); 994 XhtmlNode a = tc.addTag("a").setAttribute("title", i18n.formatPhrase(RenderingI18nContext.SD_LEGEND)).setAttribute("href", model.getDocoRef()); 995 if (model.getDocoImg() != null) 996 a.addTag("img").setAttribute("alt", "doco").style("background-color: inherit").setAttribute("src", model.getDocoImg()); 997 a.addText(" "+i18n.formatPhrase(RenderingI18nContext.SD_DOCO)); 998 } 999 if (model.active && script) { 1000 table.addTag("script").setAttribute("type", "text/javascript").tx(treeFilterJS(model.getId(), checkboxes)); 1001 } 1002 return table; 1003 } 1004 1005 1006 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 { 1007 if (!r.partnerRow) { 1008 counter.row(); 1009 } 1010 XhtmlNode tr = table.addTag("tr"); 1011 if (model.active) { 1012 tr.setAttribute("fhir", "generated-heirarchy"); // deprecated - will be removed once everyone has dealt with the change 1013 tr.setAttribute("data-fhir", "generated-heirarchy"); 1014 } 1015 1016 String color = "white"; 1017 if (r.getColor() != null) 1018 color = r.getColor(); 1019 else if (model.isAlternating() && counter.isOdd()) 1020 color = BACKGROUND_ALT_COLOR; 1021 1022 String lineStyle = r.getTopLine() == null ? "" : "; border-top: 1px solid "+r.getTopLine(); 1023 1024 tr.style("border: " + border + "px #F0F0F0 solid; padding:0px; vertical-align: top; background-color: "+color+(r.getOpacity() == null ? "" : "; opacity: "+r.getOpacity())+lineStyle); 1025 if (model.isActive()) { 1026 tr.setAttribute("id", r.getId()); 1027 } 1028 boolean first = true; 1029 for (Cell t : r.getCells()) { 1030 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, first, false, model.getId(), null); 1031 first = false; 1032 } 1033 table.addText("\r\n"); 1034 1035 for (int i = 0; i < r.getSubRows().size(); i++) { 1036 Row c = r.getSubRows().get(i); 1037 List<Integer> ind = new ArrayList<Integer>(); 1038 ind.addAll(indents); 1039 if (i == r.getSubRows().size() - 1) { 1040 ind.add(r.getLineColor()*2); 1041 } else { 1042 ind.add(r.getLineColor()*2+1); 1043 } 1044 renderRow(table, c, indent+1, ind, imagePath, border, outputTracker, counter, model); 1045 } 1046 } 1047 1048 1049 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, boolean suppressExternals, boolean filter, String mid, Map<String, String> checkboxes) throws IOException { 1050 XhtmlNode tc = tr.addTag(name); 1051 tc.setAttribute("class", "hierarchy"); 1052 if (c.span > 1) { 1053 tc.colspan(Integer.toString(c.span)); 1054 } 1055 if (c.getId() != null) { 1056 tc.setAttribute("id", c.getId()); 1057 } 1058 String lineStyle = row != null && row.getTopLine() == null ? "" : "; padding-top: 3px; padding-bottom: 3px"; 1059 1060 XhtmlNode itc = tc; 1061 XhtmlNode itr = null; 1062 if (c.innerTable) { 1063 itr = tc.table("none", true).tr(); 1064 itc = itr.td(); 1065 } 1066 1067 if (indents != null) { 1068 itc.addTag("img").setAttribute("src", srcFor(imagePath, "tbl_spacer.png")).style("background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", "."); 1069 tc.style("vertical-align: top; text-align : var(--ig-left,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"+(treelines ? "; background-image: url("+imagePath+checkExists(indents, hasChildren, lineColor, outputTracker)+")" : "")+(c.cellStyle != null ? ";"+c.cellStyle : "")+lineStyle); 1070 for (int i = 0; i < indents.size()-1; i++) { 1071 switch (indents.get(i)) { 1072 case NEW_REGULAR: 1073 case NEW_SLICER: 1074 case NEW_SLICE: 1075 itc.addTag("img").setAttribute("src", srcFor(imagePath, "tbl_blank.png")).style("background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", "."); 1076 break; 1077 case CONTINUE_REGULAR: 1078 itc.addTag("img").setAttribute("src", srcFor(imagePath, "tbl_vline.png")).style("background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", "."); 1079 break; 1080 case CONTINUE_SLICER: 1081 itc.addTag("img").setAttribute("src", srcFor(imagePath, "tbl_vline_slicer.png")).style("background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", "."); 1082 break; 1083 case CONTINUE_SLICE: 1084 itc.addTag("img").setAttribute("src", srcFor(imagePath, "tbl_vline_slice.png")).style("background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", "."); 1085 break; 1086 default: 1087 throw new Error("Unrecognized indent level: " + indents.get(i)); 1088 } 1089 } 1090 if (!indents.isEmpty()) { 1091 String sfx = table.isActive() && hasChildren ? "-open" : ""; 1092 XhtmlNode img = itc.addTag("img"); 1093 switch (indents.get(indents.size()-1)) { 1094 case NEW_REGULAR: 1095 img.setAttribute("src", srcFor(imagePath, "tbl_vjoin_end"+sfx+".png")).style("background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", "."); 1096 break; 1097 case NEW_SLICER: 1098 img.setAttribute("src", srcFor(imagePath, "tbl_vjoin_end_slicer"+sfx+".png")).style("background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", "."); 1099 break; 1100 case NEW_SLICE: 1101 img.setAttribute("src", srcFor(imagePath, "tbl_vjoin_end_slice"+sfx+".png")).style("background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", "."); 1102 break; 1103 case CONTINUE_REGULAR: 1104 img.setAttribute("src", srcFor(imagePath, "tbl_vjoin"+sfx+".png")).style("background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", "."); 1105 break; 1106 case CONTINUE_SLICER: 1107 img.setAttribute("src", srcFor(imagePath, "tbl_vjoin_slicer"+sfx+".png")).style("background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", "."); 1108 break; 1109 case CONTINUE_SLICE: 1110 img.setAttribute("src", srcFor(imagePath, "tbl_vjoin_slice"+sfx+".png")).style("background-color: inherit").setAttribute("class", "hierarchy").setAttribute("alt", "."); 1111 break; 1112 default: 1113 throw new Error("Unrecognized indent level: " + indents.get(indents.size()-1)); 1114 } 1115 if (table.isActive() && hasChildren) { 1116 img.setAttribute("onClick", "tableRowAction(this)"); 1117 } 1118 } 1119 } 1120 else { 1121 tc.style("vertical-align: top; text-align : var(--ig-left,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 : "")+lineStyle); 1122 } 1123 if (c.innerTable) { 1124 itc = itr.td(); 1125 } 1126 if (!Utilities.noString(icon)) { 1127 XhtmlNode img = itc.addTag("img").setAttribute("alt", "icon").setAttribute("src", srcFor(imagePath, icon)).setAttribute("class", "hierarchy").style("background-color: "+color+"; background-color: inherit").setAttribute("alt", "."); 1128 if (hint != null) 1129 img.setAttribute("title", hint); 1130 itc.addText(" "); 1131 } 1132 for (Piece p : c.pieces) { 1133 if (!Utilities.noString(p.getTag())) { 1134 XhtmlNode tag = itc.addTag(p.getTag()); 1135 if (p.attributes != null) 1136 for (String n : p.attributes.keySet()) 1137 tag.setAttribute(n, p.attributes.get(n)); 1138 if (p.getHint() != null) 1139 tag.setAttribute("title", p.getHint()); 1140 addStyle(tag, p); 1141 if (p.hasChildren()) { 1142 tag.addChildNodes(p.getChildren()); 1143 } 1144 } else if (!Utilities.noString(p.getReference())) { 1145 XhtmlNode a = addStyle(itc.addTag("a"), p); 1146 if (p.attributes != null) 1147 for (String n : p.attributes.keySet()) 1148 a.setAttribute(n, p.attributes.get(n)); 1149 a.setAttribute("href", prefixLocalHref(p.getReference())); 1150 if (mode == TableGenerationMode.XHTML && suppressExternals) { 1151 a.setAttribute("no-external", "true"); // deprecated - will be removed once everyone has dealt with the change 1152 a.setAttribute("data-no-external", "true"); 1153 } 1154 if (!Utilities.noString(p.getHint())) 1155 a.setAttribute("title", p.getHint()); 1156 if (p.getText() != null) { 1157 a.addText(p.getText()); 1158 } else { 1159 a.addChildren(p.getChildren()); 1160 } 1161 addStyle(a, p); 1162 if (p.getTagImg() != null) { 1163 a.tx(" "); 1164 a.img(p.getTagImg(), null); 1165 } 1166 1167 if (p.hasChildren()) { 1168 itc.addChildNodes(p.getChildren()); 1169 } 1170 } else { 1171 if (!Utilities.noString(p.getHint()) || p.hasAttributes()) { 1172 XhtmlNode s = addStyle(itc.addTag("span"), p); 1173 if (p.attributes != null) 1174 for (String n : p.attributes.keySet()) 1175 s.setAttribute(n, p.attributes.get(n)); 1176 s.setAttribute("title", p.getHint()); 1177 s.addText(p.getText()); 1178 } else if (p.getStyle() != null) { 1179 XhtmlNode s = addStyle(itc.addTag("span"), p); 1180 if (p.attributes != null) 1181 for (String n : p.attributes.keySet()) 1182 s.setAttribute(n, p.attributes.get(n)); 1183 s.addText(p.getText()); 1184 } else { 1185 itc.addText(p.getText()); 1186 } 1187 if (p.hasChildren()) { 1188 itc.addChildNodes(p.getChildren()); 1189 } 1190 if (p.getTagImg() != null) { 1191 itc.tx(" "); 1192 itc.img(p.getTagImg(), null); 1193 } 1194 } 1195 } 1196 if (makeTargets && !Utilities.noString(anchor)) { 1197 tc.addTag("a").setAttribute("name", prefixAnchor(nmTokenize(anchor))).addText(" "); 1198 } 1199 if (filter) { 1200 itc.nbsp();itc.nbsp();itc.nbsp();itc.nbsp(); 1201 XhtmlNode span = itc.span(); 1202 span.style("font-weight: normal"); 1203 span.tx("Filter: "); 1204 XhtmlNode input = span.input("filter", "text", null, 10); 1205 input.style("border: 1px #F0F0F0 solid; background-color: rgb(254, 254, 231);"); 1206 input.setAttribute("onInput", "filterTree(document.getElementById('"+mid+"'), event.target.value)"); 1207 if (checkboxes != null) { 1208 span.tx(" "); 1209 span.img("tree-filter.png", "Filters").setAttribute("onClick", "showPanel(event.target, document.getElementById('"+mid+"'), document.getElementById('pp"+mid+"'))"); 1210 XhtmlNode popupPanel = span.div(); 1211 popupPanel.attribute("id", "pp"+mid); 1212 popupPanel.style("display: none; position: fixed; opacity : 1.0; background-color: rgb(254, 254, 231); border: 1px solid #ccc; padding: 10px; "+ 1213 "boxShadow: 0 2px 5px rgba(0,0,0,0.2); zIndex: 1000; borderRadius: 4px"); 1214 for (String s : Utilities.sorted(checkboxes.keySet())) { 1215 String v = checkboxes.get(s); 1216 popupPanel.tx(s); 1217 popupPanel.tx(" "); 1218 input = popupPanel.input(v, "checkbox", null, 1); 1219 input.setAttribute("id", "cb"+mid+"-"+checkboxes.get(s)); 1220 input.setAttribute("checked", "true"); 1221 input.setAttribute("onClick", "filterDesc(document.getElementById('"+mid+"'), '"+v+"',event.target.checked, document.getElementById('pp"+mid+"'))"); 1222 popupPanel.br(); 1223 } 1224 } 1225 } 1226 return tc; 1227 } 1228 1229 1230 /** 1231 * this is used to generate a simplified form while reusing all the massive amount of code that goes into the generation of the table 1232 * 1233 * no heirarchy, no headings, no border 1234 */ 1235 public XhtmlNode generateAttributeTable(TableModel model, String imagePath, int border, Set<String> outputTracker) throws IOException, FHIRException { 1236 checkModel(model); 1237 boolean script = false; 1238 XhtmlNode table = new XhtmlNode(NodeType.Element, "table").setAttribute("border", Integer.toString(border)).setAttribute("cellspacing", "0").setAttribute("cellpadding", "0"); 1239 Counter counter = new Counter(); 1240 for (Row r : model.getRows().get(0).getSubRows()) { 1241 renderAttributeRow(table, r, 0, imagePath, border, outputTracker, counter, model); 1242 } 1243 return table; 1244 } 1245 1246 1247 private void renderAttributeRow(XhtmlNode table, Row r, int indent, String imagePath, int border, Set<String> outputTracker, Counter counter, TableModel model) throws IOException { 1248 if (!r.partnerRow) { 1249 counter.row(); 1250 } 1251 XhtmlNode tr = table.addTag("tr"); 1252 String color = "white"; 1253 if (r.getColor() != null) 1254 color = r.getColor(); 1255 else if (model.isAlternating() && counter.isOdd()) 1256 color = BACKGROUND_ALT_COLOR; 1257 1258 String lineStyle = r.getTopLine() == null ? "" : "; border-top: 1px solid "+r.getTopLine(); 1259 1260 tr.style("border: " + border + "px #F0F0F0 solid; padding:0px; vertical-align: top; background-color: "+color+(r.getOpacity() == null ? "" : "; opacity: "+r.getOpacity())+lineStyle); 1261 boolean first = true; 1262 for (Cell t : r.getCells()) { 1263 renderCell(tr, t, "td", first ? r.getIcon() : null, first ? r.getHint() : null, null, !r.getSubRows().isEmpty(), first ? r.getAnchor() : null, color, r.getLineColor(), imagePath, border, outputTracker, model, r, first, false, model.getId(), null); 1264 first = false; 1265 } 1266 table.addText("\r\n"); 1267 } 1268 1269 1270 1271 private XhtmlNode addStyle(XhtmlNode node, Piece p) { 1272 if (p.getStyle() != null) 1273 node.style(p.getStyle()); 1274 return node; 1275 } 1276 1277 private String nmTokenize(String anchor) { 1278 return anchor.replace("[", "_").replace("]", "_"); 1279 } 1280 1281 private String srcFor(String corePrefix, String filename) throws IOException { 1282 if (!treelines && filename.startsWith("tbl")) { 1283 if (filename.contains("-open")) { 1284 filename = "tbl-open.png"; 1285 } else if (filename.contains("-closed")) { 1286 filename = "tbl-closed.png"; 1287 } else { 1288 filename = "tbl_blank.png"; 1289 } 1290 } 1291 if (inLineGraphics) { 1292 if (files.containsKey(filename)) 1293 return files.get(filename); 1294 StringBuilder b = new StringBuilder(); 1295 b.append("data:image/png;base64,"); 1296 byte[] bytes; 1297 File file = ManagedFileAccess.file(Utilities.path(dest, filename)); 1298 if (!file.exists()) // because sometime this is called real early before the files exist. it will be built again later because of this 1299 bytes = new byte[0]; 1300 else 1301 bytes = FileUtils.readFileToByteArray(file); 1302 b.append(new String(Base64.encodeBase64(bytes))); 1303 // files.put(filename, b.toString()); 1304 return b.toString(); 1305 } else { 1306 return corePrefix+filename; 1307 } 1308 } 1309 1310 public static String help16AsData() throws IOException { 1311 ClassLoader classLoader = HierarchicalTableGenerator.class.getClassLoader(); 1312 InputStream help = classLoader.getResourceAsStream("help16.png"); 1313 StringBuilder b = new StringBuilder(); 1314 b.append("data:image/png;base64,"); 1315 byte[] bytes = FileUtilities.streamToBytes(help); 1316 b.append(new String(Base64.encodeBase64(bytes))); 1317 return b.toString(); 1318 } 1319 1320 private void checkModel(TableModel model) throws FHIRException { 1321 check(!model.getTitles().isEmpty(), "Must have titles"); 1322 int tc = 0; 1323 for (Cell c : model.getTitles()) { 1324 check(c); 1325 tc = tc + c.span; 1326 } 1327 int i = 0; 1328 for (Row r : model.getRows()) { 1329 check(r, "rows", tc, "", i, model.getRows().size()); 1330 i++; 1331 } 1332 } 1333 1334 1335 private void check(Cell c) throws FHIRException { 1336 boolean hasText = false; 1337 for (Piece p : c.pieces) 1338 if (!Utilities.noString(p.getText())) 1339 hasText = true; 1340 check(hasText, "Title cells must have text"); 1341 } 1342 1343 1344 private void check(Row r, String string, int size, String path, int index, int total) throws FHIRException { 1345 String id = Integer.toString(index)+"."; 1346 if (total <= 26) { 1347 char c = (char) ('a'+index); 1348 id = Character.toString(c); 1349 } 1350 path = path + id; 1351 r.setId(path); 1352 int tc = 0; 1353 for (Cell c : r.getCells()) { 1354 tc = tc + c.span; 1355 } 1356 if (tc != size) { 1357 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().size() > 0 ? "??" : r.text())+"): "+r.getCells()); 1358 } 1359 int i = 0; 1360 for (Row c : r.getSubRows()) { 1361 check(c, "rows", size, path, i, r.getSubRows().size()); 1362 i++; 1363 } 1364 } 1365 1366 1367 private String checkExists(List<Integer> indents, boolean hasChildren, int lineColor, Set<String> outputTracker) throws IOException { 1368 String filename = makeName(indents); 1369 1370 StringBuilder b = new StringBuilder(); 1371 if (inLineGraphics) { 1372 if (files.containsKey(filename)) 1373 return files.get(filename); 1374 ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 1375 genImage(indents, hasChildren, lineColor, bytes); 1376 b.append("data:image/png;base64,"); 1377 byte[] encodeBase64 = Base64.encodeBase64(bytes.toByteArray()); 1378 b.append(new String(encodeBase64)); 1379 files.put(filename, b.toString()); 1380 return b.toString(); 1381 } else if (treelines) { 1382 b.append("tbl_bck"); 1383 for (Integer i : indents) 1384 b.append(Integer.toString(i)); 1385 int indent = lineColor*2 + (hasChildren?1:0); 1386 b.append(Integer.toString(indent)); 1387 b.append(".png"); 1388 String file = Utilities.path(dest, b.toString()); 1389 if (!ManagedFileAccess.file(file).exists()) { 1390 File newFile = ManagedFileAccess.file(file); 1391 if (newFile.getParentFile() == null) { 1392 throw new Error("No source directory provided. ("+file+")"); 1393 } else { 1394 newFile.getParentFile().mkdirs(); 1395 } 1396 newFile.createNewFile(); 1397 FileOutputStream stream = ManagedFileAccess.outStream(file); 1398 try { 1399 genImage(indents, hasChildren, lineColor, stream); 1400 if (outputTracker!=null) 1401 outputTracker.add(file); 1402 } finally { 1403 stream.close(); 1404 } 1405 } 1406 return b.toString(); 1407 } else { 1408 return "tbl_bck0.png"; 1409 } 1410 } 1411 1412 1413 private void genImage(List<Integer> indents, boolean hasChildren, int lineColor, OutputStream stream) throws IOException { 1414 BufferedImage bi = new BufferedImage(800, 2, BufferedImage.TYPE_INT_ARGB); 1415 // i have no idea why this works to make these pixels transparent. It defies logic. 1416 // But this combination of INT_ARGB and filling with grey magically worked when nothing else did. So it stays as is. 1417 Color grey = new Color(99,99,99,0); 1418 for (int i = 0; i < 800; i++) { 1419 bi.setRGB(i, 0, grey.getRGB()); 1420 bi.setRGB(i, 1, grey.getRGB()); 1421 } 1422 Color black = new Color(0, 0, 0); 1423 Color green = new Color(14,209,69); 1424 Color gold = new Color(212,168,21); 1425 for (int i = 0; i < indents.size(); i++) { 1426 int indent = indents.get(i).intValue(); 1427 if (indent == CONTINUE_REGULAR) 1428 bi.setRGB(12+(i*16), 0, black.getRGB()); 1429 else if (indent == CONTINUE_SLICER) 1430 bi.setRGB(12+(i*16), 0, green.getRGB()); 1431 else if (indent == CONTINUE_SLICE) 1432 bi.setRGB(12+(i*16), 0, gold.getRGB()); 1433 } 1434 if (hasChildren) { 1435 if (lineColor==0) 1436 bi.setRGB(12+(indents.size()*16), 0, black.getRGB()); 1437 else if (lineColor==1) 1438 bi.setRGB(12+(indents.size()*16), 0, green.getRGB()); 1439 else if (lineColor==2) 1440 bi.setRGB(12+(indents.size()*16), 0, gold.getRGB()); 1441 } 1442 ImageIO.write(bi, "PNG", stream); 1443 } 1444 1445 private String makeName(List<Integer> indents) { 1446 StringBuilder b = new StringBuilder(); 1447 b.append("indents:"); 1448 for (Integer i : indents) 1449 b.append(Integer.toString(i)); 1450 return b.toString(); 1451 } 1452 1453 private void check(boolean check, String message) throws FHIRException { 1454 if (!check) 1455 throw new FHIRException(message); 1456 } 1457 1458 public void emptyRow(TableModel model, int cellCount) { 1459 Row r = new Row(); 1460 model.rows.add(r); 1461 for (int i = 0; i < cellCount; i++) { 1462 r.getCells().add(new Cell()); 1463 } 1464 } 1465 1466 1467 public String getUniqueLocalPrefix() { 1468 return uniqueLocalPrefix; 1469 } 1470 1471 public void setUniqueLocalPrefix(String uniqueLocalPrefix) { 1472 if (Utilities.noString(uniqueLocalPrefix)) { 1473 throw new Error("what?"); 1474 } 1475 this.uniqueLocalPrefix = uniqueLocalPrefix; 1476 } 1477 1478 public String prefixAnchor(String anchor) { 1479 return Utilities.noString(uniqueLocalPrefix) ? anchor : uniqueLocalPrefix+"-" + anchor; 1480 } 1481 1482 public String prefixLocalHref(String url) { 1483 if (url == null || Utilities.noString(uniqueLocalPrefix) || !url.startsWith("#")) { 1484 return url; 1485 } 1486 return "#"+uniqueLocalPrefix+"-"+url.substring(1); 1487 } 1488 1489 public boolean isTreelines() { 1490 return treelines; 1491 } 1492 1493 public void setTreelines(boolean treelines) { 1494 this.treelines = treelines; 1495 } 1496 1497 public static void forTesting() { 1498 uuid = "d5a880ec-5909-47f0-8053-be62dc5dc2b0"; 1499 } 1500 1501}