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