001/**
002 *
003 * Copyright 2003-2007 Jive Software.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.jivesoftware.smackx.xhtmlim;
019
020import org.jivesoftware.smack.util.StringUtils;
021
022/**
023 * An XHTMLText represents formatted text. This class also helps to build valid 
024 * XHTML tags.
025 *
026 * @author Gaston Dombiak
027 */
028public class XHTMLText {
029
030    private static final String NAMESPACE = "http://www.w3.org/1999/xhtml";
031
032    private StringBuilder text = new StringBuilder(30);
033
034    /**
035     * Creates a new XHTMLText with body tag params.
036     * 
037     * @param style the XHTML style of the body
038     * @param lang the language of the body
039     */
040    public XHTMLText(String style, String lang) {
041        appendOpenBodyTag(style, lang);
042    }
043
044    /**
045     * Appends a tag that indicates that an anchor section begins.
046     * 
047     * @param href indicates the URL being linked to
048     * @param style the XHTML style of the anchor
049     */
050    public void appendOpenAnchorTag(String href, String style) {
051        StringBuilder sb = new StringBuilder("<a");
052        if (href != null) {
053            sb.append(" href=\"");
054            sb.append(href);
055            sb.append("\"");
056        }
057        if (style != null) {
058            sb.append(" style=\"");
059            sb.append(style);
060            sb.append("\"");
061        }
062        sb.append(">");
063        text.append(sb.toString());
064    }
065
066    /**
067     * Appends a tag that indicates that an anchor section ends.
068     * 
069     */
070    public void appendCloseAnchorTag() {
071        text.append("</a>");
072    }
073
074    /**
075     * Appends a tag that indicates that a blockquote section begins.
076     * 
077     * @param style the XHTML style of the blockquote
078     */
079    public void appendOpenBlockQuoteTag(String style) {
080        StringBuilder sb = new StringBuilder("<blockquote");
081        if (style != null) {
082            sb.append(" style=\"");
083            sb.append(style);
084            sb.append("\"");
085        }
086        sb.append(">");
087        text.append(sb.toString());
088    }
089
090    /**
091     * Appends a tag that indicates that a blockquote section ends.
092     * 
093     */
094    public void appendCloseBlockQuoteTag() {
095        text.append("</blockquote>");
096    }
097
098    /**
099     * Appends a tag that indicates that a body section begins.
100     * 
101     * @param style the XHTML style of the body
102     * @param lang the language of the body
103     */
104    private void appendOpenBodyTag(String style, String lang) {
105        StringBuilder sb = new StringBuilder("<body xmlns=\"" + NAMESPACE + "\"");
106        if (style != null) {
107            sb.append(" style=\"");
108            sb.append(style);
109            sb.append("\"");
110        }
111        if (lang != null) {
112            sb.append(" xml:lang=\"");
113            sb.append(lang);
114            sb.append("\"");
115        }
116        sb.append(">");
117        text.append(sb.toString());
118    }
119
120    /**
121     * Appends a tag that indicates that a body section ends.
122     * 
123     */
124    private String closeBodyTag() {
125        return "</body>";
126    }
127
128    /**
129     * Appends a tag that inserts a single carriage return.
130     * 
131     */
132    public void appendBrTag() {
133        text.append("<br/>");
134    }
135
136    /**
137     * Appends a tag that indicates a reference to work, such as a book, report or web site.
138     * 
139     */
140    public void appendOpenCiteTag() {
141        text.append("<cite>");
142    }
143
144    /**
145     * Appends a tag that indicates text that is the code for a program.
146     * 
147     */
148    public void appendOpenCodeTag() {
149        text.append("<code>");
150    }
151
152    /**
153     * Appends a tag that indicates end of text that is the code for a program.
154     * 
155     */
156    public void appendCloseCodeTag() {
157        text.append("</code>");
158    }
159
160    /**
161     * Appends a tag that indicates emphasis.
162     * 
163     */
164    public void appendOpenEmTag() {
165        text.append("<em>");
166    }
167
168    /**
169     * Appends a tag that indicates end of emphasis.
170     * 
171     */
172    public void appendCloseEmTag() {
173        text.append("</em>");
174    }
175
176    /**
177     * Appends a tag that indicates a header, a title of a section of the message.
178     * 
179     * @param level the level of the Header. It should be a value between 1 and 3
180     * @param style the XHTML style of the blockquote
181     */
182    public void appendOpenHeaderTag(int level, String style) {
183        if (level > 3 || level < 1) {
184            return;
185        }
186        StringBuilder sb = new StringBuilder("<h");
187        sb.append(level);
188        if (style != null) {
189            sb.append(" style=\"");
190            sb.append(style);
191            sb.append("\"");
192        }
193        sb.append(">");
194        text.append(sb.toString());
195    }
196
197    /**
198     * Appends a tag that indicates that a header section ends.
199     * 
200     * @param level the level of the Header. It should be a value between 1 and 3
201     */
202    public void appendCloseHeaderTag(int level) {
203        if (level > 3 || level < 1) {
204            return;
205        }
206        StringBuilder sb = new StringBuilder("</h");
207        sb.append(level);
208        sb.append(">");
209        text.append(sb.toString());
210    }
211
212    /**
213     * Appends a tag that indicates an image.
214     * 
215     * @param align how text should flow around the picture
216     * @param alt the text to show if you don't show the picture
217     * @param height how tall is the picture
218     * @param src where to get the picture
219     * @param width how wide is the picture
220     */
221    public void appendImageTag(String align, String alt, String height, String src, String width) {
222        StringBuilder sb = new StringBuilder("<img");
223        if (align != null) {
224            sb.append(" align=\"");
225            sb.append(align);
226            sb.append("\"");
227        }
228        if (alt != null) {
229            sb.append(" alt=\"");
230            sb.append(alt);
231            sb.append("\"");
232        }
233        if (height != null) {
234            sb.append(" height=\"");
235            sb.append(height);
236            sb.append("\"");
237        }
238        if (src != null) {
239            sb.append(" src=\"");
240            sb.append(src);
241            sb.append("\"");
242        }
243        if (width != null) {
244            sb.append(" width=\"");
245            sb.append(width);
246            sb.append("\"");
247        }
248        sb.append(">");
249        text.append(sb.toString());
250    }
251
252    /**
253     * Appends a tag that indicates the start of a new line item within a list.
254     * 
255     * @param style the style of the line item
256     */
257    public void appendLineItemTag(String style) {
258        StringBuilder sb = new StringBuilder("<li");
259        if (style != null) {
260            sb.append(" style=\"");
261            sb.append(style);
262            sb.append("\"");
263        }
264        sb.append(">");
265        text.append(sb.toString());
266    }
267
268    /**
269     * Appends a tag that creates an ordered list. "Ordered" means that the order of the items 
270     * in the list is important. To show this, browsers automatically number the list. 
271     * 
272     * @param style the style of the ordered list
273     */
274    public void appendOpenOrderedListTag(String style) {
275        StringBuilder sb = new StringBuilder("<ol");
276        if (style != null) {
277            sb.append(" style=\"");
278            sb.append(style);
279            sb.append("\"");
280        }
281        sb.append(">");
282        text.append(sb.toString());
283    }
284
285    /**
286     * Appends a tag that indicates that an ordered list section ends.
287     * 
288     */
289    public void appendCloseOrderedListTag() {
290        text.append("</ol>");
291    }
292
293    /**
294     * Appends a tag that creates an unordered list. The unordered part means that the items 
295     * in the list are not in any particular order.
296     * 
297     * @param style the style of the unordered list
298     */
299    public void appendOpenUnorderedListTag(String style) {
300        StringBuilder sb = new StringBuilder("<ul");
301        if (style != null) {
302            sb.append(" style=\"");
303            sb.append(style);
304            sb.append("\"");
305        }
306        sb.append(">");
307        text.append(sb.toString());
308    }
309
310    /**
311     * Appends a tag that indicates that an unordered list section ends.
312     * 
313     */
314    public void appendCloseUnorderedListTag() {
315        text.append("</ul>");
316    }
317
318    /**
319     * Appends a tag that indicates the start of a new paragraph. This is usually rendered 
320     * with two carriage returns, producing a single blank line in between the two paragraphs.
321     * 
322     * @param style the style of the paragraph
323     */
324    public void appendOpenParagraphTag(String style) {
325        StringBuilder sb = new StringBuilder("<p");
326        if (style != null) {
327            sb.append(" style=\"");
328            sb.append(style);
329            sb.append("\"");
330        }
331        sb.append(">");
332        text.append(sb.toString());
333    }
334
335    /**
336     * Appends a tag that indicates the end of a new paragraph. This is usually rendered 
337     * with two carriage returns, producing a single blank line in between the two paragraphs.
338     * 
339     */
340    public void appendCloseParagraphTag() {
341        text.append("</p>");
342    }
343
344    /**
345     * Appends a tag that indicates that an inlined quote section begins.
346     * 
347     * @param style the style of the inlined quote
348     */
349    public void appendOpenInlinedQuoteTag(String style) {
350        StringBuilder sb = new StringBuilder("<q");
351        if (style != null) {
352            sb.append(" style=\"");
353            sb.append(style);
354            sb.append("\"");
355        }
356        sb.append(">");
357        text.append(sb.toString());
358    }
359
360    /**
361     * Appends a tag that indicates that an inlined quote section ends.
362     * 
363     */
364    public void appendCloseInlinedQuoteTag() {
365        text.append("</q>");
366    }
367
368    /**
369     * Appends a tag that allows to set the fonts for a span of text.
370     * 
371     * @param style the style for a span of text
372     */
373    public void appendOpenSpanTag(String style) {
374        StringBuilder sb = new StringBuilder("<span");
375        if (style != null) {
376            sb.append(" style=\"");
377            sb.append(style);
378            sb.append("\"");
379        }
380        sb.append(">");
381        text.append(sb.toString());
382    }
383
384    /**
385     * Appends a tag that indicates that a span section ends.
386     * 
387     */
388    public void appendCloseSpanTag() {
389        text.append("</span>");
390    }
391
392    /**
393     * Appends a tag that indicates text which should be more forceful than surrounding text.
394     * 
395     */
396    public void appendOpenStrongTag() {
397        text.append("<strong>");
398    }
399
400    /**
401     * Appends a tag that indicates that a strong section ends.
402     * 
403     */
404    public void appendCloseStrongTag() {
405        text.append("</strong>");
406    }
407
408    /**
409     * Appends a given text to the XHTMLText.
410     * 
411     * @param textToAppend the text to append   
412     */
413    public void append(String textToAppend) {
414        text.append(StringUtils.escapeForXML(textToAppend));
415    }
416
417    /**
418     * Returns the text of the XHTMLText.
419     * 
420     * Note: Automatically adds the closing body tag.
421     * 
422     * @return the text of the XHTMLText   
423     */
424    public String toString() {
425        return text.toString().concat(closeBodyTag());
426    }
427
428}