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