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