001/* 002Copyright (c) 2011+, HL7, Inc 003All rights reserved. 004 005Redistribution and use in source and binary forms, with or without modification, 006are permitted provided that the following conditions are met: 007 008 * Redistributions of source code must retain the above copyright notice, this 009 list of conditions and the following disclaimer. 010 * Redistributions in binary form must reproduce the above copyright notice, 011 this list of conditions and the following disclaimer in the documentation 012 and/or other materials provided with the distribution. 013 * Neither the name of HL7 nor the names of its contributors may be used to 014 endorse or promote products derived from this software without specific 015 prior written permission. 016 017THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 018ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 019WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 020IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 021INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 022NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 023PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 024WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 025ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 026POSSIBILITY OF SUCH DAMAGE. 027 028 */ 029package org.hl7.fhir.utilities.xhtml; 030 031/*- 032 * #%L 033 * org.hl7.fhir.utilities 034 * %% 035 * Copyright (C) 2014 - 2019 Health Level 7 036 * %% 037 * Licensed under the Apache License, Version 2.0 (the "License"); 038 * you may not use this file except in compliance with the License. 039 * You may obtain a copy of the License at 040 * 041 * http://www.apache.org/licenses/LICENSE-2.0 042 * 043 * Unless required by applicable law or agreed to in writing, software 044 * distributed under the License is distributed on an "AS IS" BASIS, 045 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 046 * See the License for the specific language governing permissions and 047 * limitations under the License. 048 * #L% 049 */ 050 051 052import java.io.IOException; 053import java.util.ArrayList; 054import java.util.HashMap; 055import java.util.List; 056import java.util.Map; 057 058import org.hl7.fhir.instance.model.api.IBaseXhtml; 059import org.hl7.fhir.utilities.Utilities; 060 061import ca.uhn.fhir.model.primitive.XhtmlDt; 062 063import static org.apache.commons.lang3.StringUtils.isNotBlank; 064 065@ca.uhn.fhir.model.api.annotation.DatatypeDef(name="xhtml") 066public class XhtmlNode implements IBaseXhtml { 067 private static final long serialVersionUID = -4362547161441436492L; 068 069 070 public static class Location { 071 private int line; 072 private int column; 073 public Location(int line, int column) { 074 super(); 075 this.line = line; 076 this.column = column; 077 } 078 public int getLine() { 079 return line; 080 } 081 public int getColumn() { 082 return column; 083 } 084 @Override 085 public String toString() { 086 return "Line "+Integer.toString(line)+", column "+Integer.toString(column); 087 } 088 } 089 090 public static final String NBSP = Character.toString((char)0xa0); 091 private static final String DECL_XMLNS = " xmlns=\"http://www.w3.org/1999/xhtml\""; 092 093 094 private Location location; 095 private NodeType nodeType; 096 private String name; 097 private Map<String, String> attributes = new HashMap<String, String>(); 098 private List<XhtmlNode> childNodes = new ArrayList<XhtmlNode>(); 099 private String content; 100 private boolean notPretty; 101 102 public XhtmlNode() { 103 super(); 104 } 105 106 107 public XhtmlNode(NodeType nodeType, String name) { 108 super(); 109 this.nodeType = nodeType; 110 this.name = name; 111 } 112 113 public XhtmlNode(NodeType nodeType) { 114 super(); 115 this.nodeType = nodeType; 116 } 117 118 public NodeType getNodeType() { 119 return nodeType; 120 } 121 122 public void setNodeType(NodeType nodeType) { 123 this.nodeType = nodeType; 124 } 125 126 public String getName() { 127 return name; 128 } 129 130 public XhtmlNode setName(String name) { 131 assert name.contains(":") == false : "Name should not contain any : but was " + name; 132 this.name = name; 133 return this; 134 } 135 136 public Map<String, String> getAttributes() { 137 return attributes; 138 } 139 140 public List<XhtmlNode> getChildNodes() { 141 return childNodes; 142 } 143 144 public String getContent() { 145 return content; 146 } 147 148 public XhtmlNode setContent(String content) { 149 if (!(nodeType != NodeType.Text || nodeType != NodeType.Comment)) 150 throw new Error("Wrong node type"); 151 this.content = content; 152 return this; 153 } 154 155 public XhtmlNode addTag(String name) 156 { 157 158 if (!(nodeType == NodeType.Element || nodeType == NodeType.Document)) 159 throw new Error("Wrong node type. is "+nodeType.toString()); 160 XhtmlNode node = new XhtmlNode(NodeType.Element); 161 node.setName(name); 162 childNodes.add(node); 163 return node; 164 } 165 166 public XhtmlNode addTag(int index, String name) 167 { 168 169 if (!(nodeType == NodeType.Element || nodeType == NodeType.Document)) 170 throw new Error("Wrong node type. is "+nodeType.toString()); 171 XhtmlNode node = new XhtmlNode(NodeType.Element); 172 node.setName(name); 173 childNodes.add(index, node); 174 return node; 175 } 176 177 public XhtmlNode addComment(String content) 178 { 179 if (!(nodeType == NodeType.Element || nodeType == NodeType.Document)) 180 throw new Error("Wrong node type"); 181 XhtmlNode node = new XhtmlNode(NodeType.Comment); 182 node.setContent(content); 183 childNodes.add(node); 184 return node; 185 } 186 187 public XhtmlNode addDocType(String content) 188 { 189 if (!(nodeType == NodeType.Document)) 190 throw new Error("Wrong node type"); 191 XhtmlNode node = new XhtmlNode(NodeType.DocType); 192 node.setContent(content); 193 childNodes.add(node); 194 return node; 195 } 196 197 public XhtmlNode addInstruction(String content) 198 { 199 if (!(nodeType == NodeType.Document)) 200 throw new Error("Wrong node type"); 201 XhtmlNode node = new XhtmlNode(NodeType.Instruction); 202 node.setContent(content); 203 childNodes.add(node); 204 return node; 205 } 206 207 208 209 210 public XhtmlNode addText(String content) 211 { 212 if (!(nodeType == NodeType.Element || nodeType == NodeType.Document)) 213 throw new Error("Wrong node type"); 214 if (content != null) { 215 XhtmlNode node = new XhtmlNode(NodeType.Text); 216 node.setContent(content); 217 childNodes.add(node); 218 return node; 219 } else 220 return null; 221 } 222 223 public XhtmlNode addText(int index, String content) 224 { 225 if (!(nodeType == NodeType.Element || nodeType == NodeType.Document)) 226 throw new Error("Wrong node type"); 227 if (content == null) 228 throw new Error("Content cannot be null"); 229 230 XhtmlNode node = new XhtmlNode(NodeType.Text); 231 node.setContent(content); 232 childNodes.add(index, node); 233 return node; 234 } 235 236 public boolean allChildrenAreText() 237 { 238 boolean res = true; 239 for (XhtmlNode n : childNodes) 240 res = res && n.getNodeType() == NodeType.Text; 241 return res; 242 } 243 244 public XhtmlNode getElement(String name) { 245 for (XhtmlNode n : childNodes) 246 if (n.getNodeType() == NodeType.Element && name.equals(n.getName())) 247 return n; 248 return null; 249 } 250 251 public XhtmlNode getFirstElement() { 252 for (XhtmlNode n : childNodes) 253 if (n.getNodeType() == NodeType.Element) 254 return n; 255 return null; 256 } 257 258 public String allText() { 259 if (childNodes == null || childNodes.isEmpty()) 260 return getContent(); 261 262 StringBuilder b = new StringBuilder(); 263 for (XhtmlNode n : childNodes) 264 if (n.getNodeType() == NodeType.Text) 265 b.append(n.getContent()); 266 else if (n.getNodeType() == NodeType.Element) 267 b.append(n.allText()); 268 return b.toString(); 269 } 270 271 public XhtmlNode attribute(String name, String value) { 272 if (!(nodeType == NodeType.Element || nodeType == NodeType.Document)) 273 throw new Error("Wrong node type"); 274 if (name == null) 275 throw new Error("name is null"); 276 if (value == null) 277 throw new Error("value is null"); 278 attributes.put(name, value); 279 return this; 280 } 281 282 public boolean hasAttribute(String name) { 283 return getAttributes().containsKey(name); 284 } 285 286 public String getAttribute(String name) { 287 return getAttributes().get(name); 288 } 289 290 public XhtmlNode setAttribute(String name, String value) { 291 getAttributes().put(name, value); 292 return this; 293 } 294 295 public XhtmlNode copy() { 296 XhtmlNode dst = new XhtmlNode(nodeType); 297 dst.name = name; 298 for (String n : attributes.keySet()) { 299 dst.attributes.put(n, attributes.get(n)); 300 } 301 for (XhtmlNode n : childNodes) 302 dst.childNodes.add(n.copy()); 303 dst.content = content; 304 return dst; 305 } 306 307 @Override 308 public boolean isEmpty() { 309 return (childNodes == null || childNodes.isEmpty()) && content == null; 310 } 311 312 public boolean equalsDeep(XhtmlNode other) { 313 if (other == null) { 314 return false; 315 } 316 317 if (!(nodeType == other.nodeType) || !compare(name, other.name) || !compare(content, other.content)) 318 return false; 319 if (attributes.size() != other.attributes.size()) 320 return false; 321 for (String an : attributes.keySet()) 322 if (!attributes.get(an).equals(other.attributes.get(an))) 323 return false; 324 if (childNodes.size() != other.childNodes.size()) 325 return false; 326 for (int i = 0; i < childNodes.size(); i++) { 327 if (!compareDeep(childNodes.get(i), other.childNodes.get(i))) 328 return false; 329 } 330 return true; 331 } 332 333 private boolean compare(String s1, String s2) { 334 if (s1 == null && s2 == null) 335 return true; 336 if (s1 == null || s2 == null) 337 return false; 338 return s1.equals(s2); 339 } 340 341 private static boolean compareDeep(XhtmlNode e1, XhtmlNode e2) { 342 if (e1 == null && e2 == null) 343 return true; 344 if (e1 == null || e2 == null) 345 return false; 346 return e1.equalsDeep(e2); 347 } 348 349 public String getNsDecl() { 350 for (String an : attributes.keySet()) { 351 if (an.equals("xmlns")) { 352 return attributes.get(an); 353 } 354 } 355 return null; 356 } 357 358 359 @Override 360 public String getValueAsString() { 361 if (isEmpty()) { 362 return null; 363 } 364 try { 365 String retVal = new XhtmlComposer(XhtmlComposer.XML).compose(this); 366 retVal = XhtmlDt.preprocessXhtmlNamespaceDeclaration(retVal); 367 return retVal; 368 } catch (Exception e) { 369 // TODO: composer shouldn't throw exception like this 370 throw new RuntimeException(e); 371 } 372 } 373 374 @Override 375 public void setValueAsString(String theValue) throws IllegalArgumentException { 376 this.attributes = null; 377 this.childNodes = null; 378 this.content = null; 379 this.name = null; 380 this.nodeType= null; 381 if (theValue == null || theValue.length() == 0) { 382 return; 383 } 384 385 String val = theValue.trim(); 386 387 if (!val.startsWith("<")) { 388 val = "<div" + DECL_XMLNS +">" + val + "</div>"; 389 } 390 if (val.startsWith("<?") && val.endsWith("?>")) { 391 return; 392 } 393 394 val = XhtmlDt.preprocessXhtmlNamespaceDeclaration(val); 395 396 try { 397 // TODO: this is ugly 398 XhtmlNode fragment = new XhtmlParser().parseFragment(val); 399 this.attributes = fragment.attributes; 400 this.childNodes = fragment.childNodes; 401 this.content = fragment.content; 402 this.name = fragment.name; 403 this.nodeType= fragment.nodeType; 404 } catch (Exception e) { 405 // TODO: composer shouldn't throw exception like this 406 throw new RuntimeException(e); 407 } 408 409 } 410 411 public XhtmlNode getElementByIndex(int i) { 412 int c = 0; 413 for (XhtmlNode n : childNodes) 414 if (n.getNodeType() == NodeType.Element) { 415 if (c == i) 416 return n; 417 else 418 c++; 419 } 420 return null; 421 } 422 423 @Override 424 public String getValue() { 425 return getValueAsString(); 426 } 427 428 public boolean hasValue() { 429 return isNotBlank(getValueAsString()); 430 } 431 432 @Override 433 public XhtmlNode setValue(String theValue) throws IllegalArgumentException { 434 setValueAsString(theValue); 435 return this; 436 } 437 438 /** 439 * Returns false 440 */ 441 public boolean hasFormatComment() { 442 return false; 443 } 444 445 /** 446 * NOT SUPPORTED - Throws {@link UnsupportedOperationException} 447 */ 448 public List<String> getFormatCommentsPre() { 449 throw new UnsupportedOperationException(); 450 } 451 452 /** 453 * NOT SUPPORTED - Throws {@link UnsupportedOperationException} 454 */ 455 public List<String> getFormatCommentsPost() { 456 throw new UnsupportedOperationException(); 457 } 458 459 /** 460 * NOT SUPPORTED - Throws {@link UnsupportedOperationException} 461 */ 462 public Object getUserData(String theName) { 463 throw new UnsupportedOperationException(); 464 } 465 466 /** 467 * NOT SUPPORTED - Throws {@link UnsupportedOperationException} 468 */ 469 public void setUserData(String theName, Object theValue) { 470 throw new UnsupportedOperationException(); 471 } 472 473 474 public Location getLocation() { 475 return location; 476 } 477 478 479 public void setLocation(Location location) { 480 this.location = location; 481 } 482 483 // xhtml easy adders ----------------------------------------------- 484 public XhtmlNode h1() { 485 return addTag("h1"); 486 } 487 488 public XhtmlNode h2() { 489 return addTag("h2"); 490 } 491 492 public XhtmlNode h3() { 493 return addTag("h3"); 494 } 495 496 public XhtmlNode h4() { 497 return addTag("h4"); 498 } 499 500 public XhtmlNode table(String clss) { 501 XhtmlNode res = addTag("table"); 502 if (!Utilities.noString(clss)) 503 res.setAttribute("class", clss); 504 return res; 505 } 506 507 public XhtmlNode tr() { 508 return addTag("tr"); 509 } 510 511 public XhtmlNode th() { 512 return addTag("th"); 513 } 514 515 public XhtmlNode td() { 516 return addTag("td"); 517 } 518 519 public XhtmlNode colspan(String n) { 520 return setAttribute("colspan", n); 521 } 522 523 public XhtmlNode para() { 524 return addTag("p"); 525 } 526 527 public XhtmlNode pre() { 528 return addTag("pre"); 529 } 530 531 public void br() { 532 addTag("br"); 533 } 534 535 public void hr() { 536 addTag("hr"); 537 } 538 539 public XhtmlNode ul() { 540 return addTag("ul"); 541 } 542 543 public XhtmlNode li() { 544 return addTag("li"); 545 } 546 547 public XhtmlNode b() { 548 return addTag("b"); 549 } 550 551 public XhtmlNode i() { 552 return addTag("i"); 553 } 554 public XhtmlNode tx(String cnt) { 555 return addText(cnt); 556 } 557 public XhtmlNode ah(String href) { 558 return addTag("a").attribute("href", href); 559 } 560 561 public void an(String href) { 562 addTag("a").attribute("name", href).tx(" "); 563 } 564 565 public XhtmlNode span(String style, String title) { 566 XhtmlNode res = addTag("span"); 567 if (!Utilities.noString(style)) 568 res.attribute("style", style); 569 if (!Utilities.noString(title)) 570 res.attribute("title", title); 571 return res; 572 } 573 574 575 public XhtmlNode code(String text) { 576 return addTag("code").tx(text); 577 } 578 579 public XhtmlNode code() { 580 return addTag("code"); 581 } 582 583 584 public XhtmlNode blockquote() { 585 return addTag("blockquote"); 586 } 587 588 589 @Override 590 public String toString() { 591 switch (nodeType) { 592 case Document: 593 case Element: 594 try { 595 return new XhtmlComposer(XhtmlComposer.HTML).compose(this); 596 } catch (IOException e) { 597 return super.toString(); 598 } 599 case Text: 600 return this.content; 601 case Comment: 602 return "<!-- "+this.content+" -->"; 603 case DocType: 604 return "<? "+this.content+" />"; 605 case Instruction: 606 return "<? "+this.content+" />"; 607 } 608 return super.toString(); 609 } 610 611 612 public XhtmlNode getNextElement(XhtmlNode c) { 613 boolean f = false; 614 for (XhtmlNode n : childNodes) { 615 if (n == c) 616 f = true; 617 else if (f && n.getNodeType() == NodeType.Element) 618 return n; 619 } 620 return null; 621 } 622 623 624 public XhtmlNode notPretty() { 625 notPretty = true; 626 return this; 627 } 628 629 630 public boolean isNoPretty() { 631 return notPretty; 632 } 633 634 635 public XhtmlNode style(String style) { 636 setAttribute("style", style); 637 return this; 638 } 639 640 641 public XhtmlNode nbsp() { 642 return addText(NBSP); 643 } 644 645 646 public XhtmlNode para(String text) { 647 XhtmlNode p = para(); 648 p.addText(text); 649 return p; 650 651 } 652 653 public XhtmlNode add(XhtmlNode n) { 654 getChildNodes().add(n); 655 return this; 656 } 657 658}