001package org.hl7.fhir.utilities.xhtml; 002 003import java.io.IOException; 004 005import org.hl7.fhir.exceptions.FHIRException; 006import org.hl7.fhir.exceptions.FHIRFormatError; 007import org.hl7.fhir.utilities.MarkDownProcessor; 008import org.hl7.fhir.utilities.Utilities; 009import org.hl7.fhir.utilities.MarkDownProcessor.Dialect; 010 011public abstract class XhtmlFluent { 012 013 protected abstract XhtmlNode addTag(String string); 014 protected abstract XhtmlNode addText(String cnt); 015 protected abstract void addChildren(XhtmlNodeList childNodes); 016 017 public XhtmlNode h1() { 018 return addTag("h1"); 019 } 020 021 022 public XhtmlNode h2() { 023 return addTag("h2"); 024 } 025 026 public XhtmlNode h(int level) { 027 if (level < 1 || level > 6) { 028 throw new FHIRException("Illegal Header level "+level); 029 } 030 return addTag("h"+Integer.toString(level)); 031 } 032 033 public XhtmlNode h3() { 034 return addTag("h3"); 035 } 036 037 public XhtmlNode h4() { 038 return addTag("h4"); 039 } 040 041 public XhtmlNode table(String clss) { 042 XhtmlNode res = addTag("table"); 043 if (!Utilities.noString(clss)) 044 res.setAttribute("class", clss); 045 return res; 046 } 047 048 public XhtmlNode tr() { 049 return addTag("tr"); 050 } 051 052 public XhtmlNode th() { 053 return addTag("th"); 054 } 055 056 public XhtmlNode td() { 057 return addTag("td"); 058 } 059 060 public XhtmlNode td(String clss) { 061 return addTag("td").attribute("class", clss); 062 } 063 064 public XhtmlNode div() { 065 return addTag("div"); 066 } 067 068 public XhtmlNode para() { 069 return addTag("p"); 070 } 071 072 public XhtmlNode pre() { 073 return addTag("pre"); 074 } 075 076 public XhtmlNode pre(String clss) { 077 return addTag("pre").setAttribute("class", clss); 078 } 079 080 public void br() { 081 addTag("br"); 082 } 083 084 public void hr() { 085 addTag("hr"); 086 } 087 088 public XhtmlNode ul() { 089 return addTag("ul"); 090 } 091 092 public XhtmlNode li() { 093 return addTag("li"); 094 } 095 096 public XhtmlNode b() { 097 return addTag("b"); 098 } 099 100 public XhtmlNode i() { 101 return addTag("i"); 102 } 103 104 public XhtmlNode tx(String cnt) { 105 return addText(cnt); 106 } 107 108 public XhtmlNode tx(int cnt) { 109 return addText(Integer.toString(cnt)); 110 } 111 112 public XhtmlNode ah(String href) { 113 return addTag("a").attribute("href", href); 114 } 115 116 public XhtmlNode ah(String href, String title) { 117 XhtmlNode x = addTag("a").attribute("href", href); 118 if (title != null) { 119 x.attribute("title", title); 120 } 121 return x; 122 } 123 124 public XhtmlNode img(String src, String alt) { 125 return addTag("img").attribute("src", src).attribute("alt", alt); 126 } 127 128 public XhtmlNode img(String src, String alt, String title) { 129 return addTag("img").attribute("src", src).attribute("alt", alt).attribute("title", title); 130 } 131 132 public XhtmlNode an(String href) { 133 return an(href, " "); 134 } 135 136 public XhtmlNode an(String href, String tx) { 137 XhtmlNode a = addTag("a").attribute("name", href); 138 a.tx(tx); 139 return a; 140 } 141 142 public XhtmlNode span(String style, String title) { 143 XhtmlNode res = addTag("span"); 144 if (!Utilities.noString(style)) 145 res.attribute("style", style); 146 if (!Utilities.noString(title)) 147 res.attribute("title", title); 148 return res; 149 } 150 151 152 public XhtmlNode code(String text) { 153 return addTag("code").tx(text); 154 } 155 156 public XhtmlNode code() { 157 return addTag("code"); 158 } 159 160 161 public XhtmlNode blockquote() { 162 return addTag("blockquote"); 163 } 164 165 166 public void markdown(String md, String source) throws IOException { 167 if (md != null) { 168 String s = new MarkDownProcessor(Dialect.COMMON_MARK).process(md, source); 169 XhtmlParser p = new XhtmlParser(); 170 XhtmlNode m; 171 try { 172 m = p.parse("<div>"+s+"</div>", "div"); 173 } catch (org.hl7.fhir.exceptions.FHIRFormatError e) { 174 throw new FHIRFormatError(e.getMessage(), e); 175 } 176 addChildren(m.getChildNodes()); 177 } 178 } 179 180 public void innerHTML(String html) throws IOException { 181 if (html != null) { 182 XhtmlParser p = new XhtmlParser(); 183 XhtmlNode m; 184 try { 185 m = p.parse("<div>"+html+"</div>", "div"); 186 } catch (org.hl7.fhir.exceptions.FHIRFormatError e) { 187 throw new FHIRFormatError(e.getMessage(), e); 188 } 189 addChildren(m.getChildNodes()); 190 } 191 } 192 193 194 195}