View Javadoc

1   /*______________________________________________________________________________
2    * 
3    * Copyright 2006  Arnaud Bailly - 
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    * (1) Redistributions of source code must retain the above copyright
10   *     notice, this list of conditions and the following disclaimer.
11   *
12   * (2) Redistributions in binary form must reproduce the above copyright
13   *     notice, this list of conditions and the following disclaimer in
14   *     the documentation and/or other materials provided with the
15   *     distribution.
16   *
17   * (3) The name of the author may not be used to endorse or promote
18   *     products derived from this software without specific prior
19   *     written permission.
20   *
21   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24   * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27   *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31   * OF THE POSSIBILITY OF SUCH DAMAGE.
32   *
33   * Created on Tue Jul 11 2006
34   *
35   */
36  package oqube.muse.html;
37  
38  import java.io.PrintWriter;
39  import java.util.ArrayList;
40  import java.util.List;
41  import java.util.Iterator;
42  import java.io.OutputStream;
43  
44  import oqube.muse.MuseTagHandler;
45  import oqube.muse.MuseSink;
46  import oqube.muse.MuseLinker;
47  
48  /***
49   * 
50   * @author abailly@oqube.muse.com
51   * @version $Id: MuseHTMLSink.java 31 2006-09-18 14:59:29Z /CN=nono $
52   * @plexus.component role="oqube.muse.MuseSink" role-hint="xhtml"
53   */
54  public class MuseHTMLSink implements MuseSink {
55  
56    /***
57     * @plexus.requirement role="oqube.muse.MuseLinker" role-hint="xhtml"
58     */
59    private MuseLinker linker;
60  
61    public MuseLinker getLinker() {
62      return linker;
63    }
64  
65    public void setLinker(MuseLinker linker) {
66      this.linker = linker;
67    }
68  
69    /***
70     * @plexus.requirement role="oqube.muse.MuseTagHandler" role-hint="xhtml"
71     */
72    private MuseTagHandler tagHandler;
73  
74    public MuseTagHandler getTagHandler() {
75      return tagHandler;
76    }
77  
78    public void setTagHandler(MuseTagHandler tagHandler) {
79      this.tagHandler = tagHandler;
80    }
81  
82    /***
83     * @plexus.configuration default-value="UTF-8"
84     */
85    private String encoding = "UTF-8";
86  
87    /***
88     * @plexus.configuration default-value="html PUBLIC \"-//W3C//DTD XHTML 1.0
89     *                       Transitional//EN\"
90     *                       \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\""
91     */
92    private String doctype = "html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"";
93  
94    public String getEncoding() {
95      return encoding;
96    }
97  
98    public void setEncoding(String encoding) {
99      this.encoding = encoding;
100   }
101 
102   public String getDoctype() {
103     return doctype;
104   }
105 
106   public void setDoctype(String doctype) {
107     this.doctype = doctype;
108   }
109 
110   public void startDocument() {
111     out.println("<?xml  version=\"1.0\" encoding=\"" + getEncoding() + "\" ?>");
112     out.println("<!DOCTYPE " + getDoctype() + " >");
113     out.println("<html>");
114   }
115 
116   public void endDocument() {
117     out.println("</html>");
118   }
119 
120   public void startHeader() {
121     out.println("<head>");
122   }
123 
124   public void endHeader() {
125     out.println("</head>");
126   }
127 
128   public void startBody() {
129     out.println("<body>");
130   }
131 
132   public void endBody() {
133     out.println("</body>");
134   }
135 
136   public void startFooter() {
137   }
138 
139   public void endFooter() {
140   }
141 
142   /*
143    * the final output
144    */
145   private PrintWriter out;
146 
147   public PrintWriter getOut() {
148     return out;
149   }
150 
151   /***
152    * Sets the output to use for generating data.
153    */
154   public void setOut(PrintWriter out) {
155     this.out = out;
156   }
157 
158   public void setOut(OutputStream os) {
159     this.out = new PrintWriter(os);
160   }
161 
162   public void setLineWidth(int lw) {
163   }
164 
165   public void startPara() {
166     out.print("<p>");
167   }
168 
169   public void text(String txt) {
170     out.print(txt);
171   }
172 
173   public void rawText(String txt) {
174     out.print(txt);
175   }
176 
177   public void endPara() {
178     out.println("</p>");
179   }
180 
181   public void startList() {
182     out.print("<ul>");
183 
184   }
185 
186   public void endList() {
187     out.println("</ul>");
188   }
189 
190   public void startItem() {
191     out.print("<li>");
192   }
193 
194   public void endItem() {
195     out.println("</li>");
196   }
197 
198   public void startQuote() {
199     out.println("<quote>");
200   }
201 
202   public void endQuote() {
203     out.println("</quote>");
204   }
205 
206   public void startCenter() {
207     out.println("<center>");
208   }
209 
210   public void endCenter() {
211     out.println("</center>");
212   }
213 
214   public void startEnums() {
215     out.print("<ol>");
216   }
217 
218   public void endEnums() {
219     out.println("</ol>");
220   }
221 
222   public void startTitle1() {
223     out.print("<h1>");
224   }
225 
226   public void endTitle1() {
227     out.println("</h1>");
228   }
229 
230   public void startTitle2() {
231     out.print("<h2>");
232   }
233 
234   public void endTitle2() {
235     out.println("</h2>");
236   }
237 
238   public void startTitle3() {
239     out.print("<h3>");
240   }
241 
242   public void endTitle3() {
243     out.println("</h3>");
244   }
245 
246   public void startTitle4() {
247     out.print("<h4>");
248   }
249 
250   public void endTitle4() {
251     out.println("</h4>");
252   }
253 
254   public void startEmph() {
255     out.print("<em>");
256   }
257 
258   public void endEmph() {
259     out.print("</em>");
260   }
261 
262   public void startStrong() {
263     out.print("<strong>");
264   }
265 
266   public void endStrong() {
267     out.print("</strong>");
268   }
269 
270   public void startVerb() {
271     out.print("<code>");
272   }
273 
274   public void endVerb() {
275     out.print("</code>");
276   }
277 
278   public void startUline() {
279     out.print("<u>");
280   }
281 
282   public void endUline() {
283     out.print("</u>");
284   }
285 
286   /***
287    * Generic formatting method for arbitrary tags denoting block level elements.
288    * This method is called when parser encounter XML-style tags used in muse.
289    * 
290    * @param tag
291    *          the tag name
292    */
293   public void block(String tag, String content) {
294     if (tagHandler.block(this, tag, content))
295       return;
296     // Default is output tag as class name of div
297     out.println("<div class=\"" + tag + "\">");
298     out.println(content);
299     out.println("</div>");
300   }
301 
302   public void flow(String tag, String content) {
303     if (tagHandler.flow(this, tag, content))
304       return;
305     out.print("<span class=\"" + tag + "\">");
306     out.print(content);
307     out.print("</span>");
308   }
309 
310   public void link(String s, String t) {
311     if (linker != null)
312       linker.link(this, s, t);
313   }
314 
315   public void anchor(String a) {
316     if (linker != null)
317       linker.anchor(this, a);
318   }
319 
320   public void addMetadata(String s, String t) {
321     out.print("<meta name=\"" + s + "\" content=\"" + t + "\" />");
322   }
323 
324   public void separator() {
325     out.println("<hr />");
326   }
327 
328   /*
329    * (non-Javadoc)
330    * 
331    * @see oqube.muse.MuseSink#endTable()
332    */
333   public void endTable() {
334     out.println("</table>");
335 
336   }
337 
338   /*
339    * (non-Javadoc)
340    * 
341    * @see oqube.muse.MuseSink#endTableData()
342    */
343   public void endTableData() {
344     out.println("</td>");
345 
346   }
347 
348   /*
349    * (non-Javadoc)
350    * 
351    * @see oqube.muse.MuseSink#endTableHeader()
352    */
353   public void endTableHeader() {
354     out.println("</th>");
355 
356   }
357 
358   /*
359    * (non-Javadoc)
360    * 
361    * @see oqube.muse.MuseSink#startTable()
362    */
363   public void startTable() {
364     out.println("<table>");
365 
366   }
367 
368   /*
369    * (non-Javadoc)
370    * 
371    * @see oqube.muse.MuseSink#startTableData()
372    */
373   public void startTableData() {
374     out.print("<td>");
375 
376   }
377 
378   /*
379    * (non-Javadoc)
380    * 
381    * @see oqube.muse.MuseSink#startTableHeader()
382    */
383   public void startTableHeader() {
384     out.print("<th>");
385 
386   }
387 
388   /*
389    * (non-Javadoc)
390    * 
391    * @see oqube.muse.MuseSink#endTableRow()
392    */
393   public void endTableRow() {
394     out.println("</tr>");
395 
396   }
397 
398   /*
399    * (non-Javadoc)
400    * 
401    * @see oqube.muse.MuseSink#startTableRow()
402    */
403   public void startTableRow() {
404     out.println("<tr>");
405 
406   }
407 
408 }