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.MarkDownProcessor.Dialect; 009import org.hl7.fhir.utilities.Utilities; 010 011public abstract class XhtmlFluent { 012 013 protected abstract XhtmlNode addTag(String string); 014 protected abstract XhtmlNode addTag(int index, String string); 015 protected abstract XhtmlNode addText(String cnt); 016 protected abstract void addChildren(XhtmlNodeList childNodes); 017 protected abstract int indexOfNode(XhtmlNode node); 018 019 public XhtmlNode h1() { 020 return addTag("h1"); 021 } 022 023 024 public XhtmlNode h2() { 025 return addTag("h2"); 026 } 027 028 public XhtmlNode h(int level) { 029 if (level < 1 || level > 6) { 030 throw new FHIRException("Illegal Header level "+level); 031 } 032 return addTag("h"+Integer.toString(level)); 033 } 034 035 /* Add header with an ID */ 036 public XhtmlNode h(int level, String id) { 037 if (level < 1 || level > 6) { 038 throw new FHIRException("Illegal Header level "+level); 039 } 040 XhtmlNode res = addTag("h"+Integer.toString(level)); 041 if (!Utilities.noString(id)) { 042 res.attribute("id", id); 043 } 044 return res; 045 } 046 047 public XhtmlNode h3() { 048 return addTag("h3"); 049 } 050 051 public XhtmlNode h4() { 052 return addTag("h4"); 053 } 054 055 public XhtmlNode table(String clss) { 056 return table(clss, false); 057 } 058 059 public XhtmlNode table(String clss, boolean forPresentation) { 060 XhtmlNode res = addTag("table"); 061 if (!Utilities.noString(clss)) 062 res.setAttribute("class", clss); 063 if (forPresentation) { 064 res.setAttribute("role", "presentation"); 065 } 066 return res; 067 } 068 069 public XhtmlNode tr() { 070 return addTag("tr"); 071 } 072 073 public XhtmlNode tr(XhtmlNode tr) { 074 return addTag(indexOfNode(tr)+1, "tr"); 075 } 076 077 public XhtmlNode th(int index) { 078 return addTag(index, "th"); 079 } 080 081 public XhtmlNode th() { 082 return addTag("th"); 083 } 084 085 public XhtmlNode td() { 086 XhtmlNode x = addTag("td"); 087 return x; 088 } 089 090 public XhtmlNode td(String clss) { 091 return addTag("td").attribute("class", clss); 092 } 093 094 public XhtmlNode div() { 095 return addTag("div"); 096 } 097 098 public XhtmlNode div(String style) { 099 XhtmlNode x = addTag("div"); 100 if (!Utilities.noString(style)) 101 x.attribute("style", style); 102 return x; 103 } 104 105 public XhtmlNode para() { 106 return addTag("p"); 107 } 108 109 public XhtmlNode pre() { 110 return addTag("pre"); 111 } 112 113 public XhtmlNode pre(String clss) { 114 XhtmlNode res = addTag("pre"); 115 if (clss != null) { 116 res.setAttribute("class", clss); 117 } 118 return res; 119 } 120 121 public void br() { 122 addTag("br"); 123 } 124 125 public void hr() { 126 addTag("hr"); 127 } 128 129 public XhtmlNode ul() { 130 return addTag("ul"); 131 } 132 133 public XhtmlNode ol() { 134 return addTag("ol"); 135 } 136 137 public XhtmlNode li() { 138 return addTag("li"); 139 } 140 141 public XhtmlNode b() { 142 return addTag("b"); 143 } 144 145 public XhtmlNode i() { 146 return addTag("i"); 147 } 148 149 public XhtmlNode tx(String cnt) { 150 return addText(cnt); 151 } 152 153 public XhtmlNode tx(int cnt) { 154 return addText(Integer.toString(cnt)); 155 } 156 157 public XhtmlNode ah(String href) { 158 if (href == null) { 159 if (this instanceof XhtmlNode) { 160 return (XhtmlNode) this; 161 } else { 162 return addTag("span"); 163 } 164 } else { 165 return addTag("a").attribute("href", href); 166 } 167 } 168 169 public XhtmlNode ah(String href, String title) { 170 XhtmlNode x = addTag("a").attribute("href", href); 171 if (title != null) { 172 x.attribute("title", title); 173 } 174 return x; 175 } 176 177 public XhtmlNode ahWithText(String preText, String href, String title, String text, String postText) { 178 tx(preText); 179 XhtmlNode x = addTag("a").attribute("href", href); 180 if (title != null) { 181 x.attribute("title", title); 182 } 183 x.tx(text); 184 tx(postText); 185 return x; 186 } 187 188 /** 189 * make it a code if it's not a link 190 * @param href 191 * @param title 192 * @return 193 */ 194 public XhtmlNode ahOrCode(String href, String title) { 195 if (href != null) { 196 return ah(href, title); 197 } else if (title != null) { 198 return code().setAttribute("title", title); 199 } else { 200 return code(); 201 } 202 } 203 204 public XhtmlNode ahOrCode(String href) { 205 return ahOrCode(href, null); 206 } 207 208 public XhtmlNode img(String src, String alt) { 209 if (alt == null) { 210 return addTag("img").attribute("src", src).attribute("alt", "."); 211 } else { 212 return addTag("img").attribute("src", src).attribute("alt", alt); 213 } 214 } 215 216 public XhtmlNode imgT(String src, String alt) { 217 if (alt == null) { 218 return addTag("img").attribute("src", src).attribute("alt", "."); 219 } else { 220 return addTag("img").attribute("src", src).attribute("alt", alt).attribute("title", alt); 221 } 222 } 223 224 public XhtmlNode img(String src, String alt, String title) { 225 return addTag("img").attribute("src", src).attribute("alt", alt).attribute("title", title); 226 } 227 228 public XhtmlNode an(String href) { 229 return an(href, " "); 230 } 231 232 public XhtmlNode an(String href, String tx) { 233 XhtmlNode a = addTag("a").attribute("name", href); 234 a.tx(tx); 235 return a; 236 } 237 238 public XhtmlNode span(String style, String title) { 239 XhtmlNode res = addTag("span"); 240 if (!Utilities.noString(style)) 241 res.attribute("style", style); 242 if (!Utilities.noString(title)) 243 res.attribute("title", title); 244 return res; 245 } 246 247 public XhtmlNode span(String style) { 248 XhtmlNode res = addTag("span"); 249 if (!Utilities.noString(style)) 250 res.attribute("style", style); 251 return res; 252 } 253 254 public XhtmlNode span() { 255 return addTag("span"); 256 } 257 258 public XhtmlNode spanClss(String clssName) { 259 XhtmlNode res = addTag("span"); 260 if (!Utilities.noString(clssName)) 261 res.attribute("class", clssName); 262 return res; 263 } 264 265 public void codeWithText(String preText, String text, String postText) { 266 tx(preText); 267 XhtmlNode code = addTag("code"); 268 code.tx(text); 269 tx(postText); 270 } 271 272 273 public XhtmlNode code(String text) { 274 XhtmlNode code = addTag("code"); 275 code.tx(text); 276 return code; 277 } 278 279 public XhtmlNode code() { 280 return addTag("code"); 281 } 282 283 284 public XhtmlNode blockquote() { 285 return addTag("blockquote"); 286 } 287 288 289 public void markdown(String md, String source) throws IOException { 290 if (md != null) { 291 String s = new MarkDownProcessor(Dialect.COMMON_MARK).process(md, source); 292 XhtmlParser p = new XhtmlParser(); 293 XhtmlNode m; 294 try { 295 m = p.parse("<div>"+s+"</div>", "div"); 296 } catch (org.hl7.fhir.exceptions.FHIRFormatError e) { 297 throw new FHIRFormatError(e.getMessage(), e); 298 } 299 addChildren(m.getChildNodes()); 300 } 301 } 302 303 public void innerHTML(String html) throws IOException { 304 if (html != null) { 305 XhtmlParser p = new XhtmlParser(); 306 XhtmlNode m; 307 try { 308 m = p.parse("<div>"+html+"</div>", "div"); 309 } catch (org.hl7.fhir.exceptions.FHIRFormatError e) { 310 throw new FHIRFormatError(e.getMessage(), e); 311 } 312 addChildren(m.getChildNodes()); 313 } 314 } 315 316 // differs from tx because it returns the owner node, not the created text 317 public XhtmlFluent txN(String cnt) { 318 addText(cnt); 319 return this; 320 } 321 322 323 public XhtmlFluent iff(boolean test) { 324 if (test) { 325 return this; 326 } else { 327 return new XhtmlNode(NodeType.Element, "span"); // which will never be connected 328 } 329 } 330}