• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * $RCSfile$
3  * $Revision$
4  * $Date$
5  *
6  * Copyright 2003-2007 Jive Software.
7  *
8  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 package org.jivesoftware.smackx;
22 
23 import org.jivesoftware.smack.util.StringUtils;
24 
25 /**
26  * An XHTMLText represents formatted text. This class also helps to build valid
27  * XHTML tags.
28  *
29  * @author Gaston Dombiak
30  */
31 public class XHTMLText {
32 
33     private StringBuilder text = new StringBuilder(30);
34 
35     /**
36      * Creates a new XHTMLText with body tag params.
37      *
38      * @param style the XHTML style of the body
39      * @param lang the language of the body
40      */
XHTMLText(String style, String lang)41     public XHTMLText(String style, String lang) {
42         appendOpenBodyTag(style, lang);
43     }
44 
45     /**
46      * Appends a tag that indicates that an anchor section begins.
47      *
48      * @param href indicates the URL being linked to
49      * @param style the XHTML style of the anchor
50      */
appendOpenAnchorTag(String href, String style)51     public void appendOpenAnchorTag(String href, String style) {
52         StringBuilder sb = new StringBuilder("<a");
53         if (href != null) {
54             sb.append(" href=\"");
55             sb.append(href);
56             sb.append("\"");
57         }
58         if (style != null) {
59             sb.append(" style=\"");
60             sb.append(style);
61             sb.append("\"");
62         }
63         sb.append(">");
64         text.append(sb.toString());
65     }
66 
67     /**
68      * Appends a tag that indicates that an anchor section ends.
69      *
70      */
appendCloseAnchorTag()71     public void appendCloseAnchorTag() {
72         text.append("</a>");
73     }
74 
75     /**
76      * Appends a tag that indicates that a blockquote section begins.
77      *
78      * @param style the XHTML style of the blockquote
79      */
appendOpenBlockQuoteTag(String style)80     public void appendOpenBlockQuoteTag(String style) {
81         StringBuilder sb = new StringBuilder("<blockquote");
82         if (style != null) {
83             sb.append(" style=\"");
84             sb.append(style);
85             sb.append("\"");
86         }
87         sb.append(">");
88         text.append(sb.toString());
89     }
90 
91     /**
92      * Appends a tag that indicates that a blockquote section ends.
93      *
94      */
appendCloseBlockQuoteTag()95     public void appendCloseBlockQuoteTag() {
96         text.append("</blockquote>");
97     }
98 
99     /**
100      * Appends a tag that indicates that a body section begins.
101      *
102      * @param style the XHTML style of the body
103      * @param lang the language of the body
104      */
appendOpenBodyTag(String style, String lang)105     private void appendOpenBodyTag(String style, String lang) {
106         StringBuilder sb = new StringBuilder("<body");
107         if (style != null) {
108             sb.append(" style=\"");
109             sb.append(style);
110             sb.append("\"");
111         }
112         if (lang != null) {
113             sb.append(" xml:lang=\"");
114             sb.append(lang);
115             sb.append("\"");
116         }
117         sb.append(">");
118         text.append(sb.toString());
119     }
120 
121     /**
122      * Appends a tag that indicates that a body section ends.
123      *
124      */
closeBodyTag()125     private String closeBodyTag() {
126         return "</body>";
127     }
128 
129     /**
130      * Appends a tag that inserts a single carriage return.
131      *
132      */
appendBrTag()133     public void appendBrTag() {
134         text.append("<br/>");
135     }
136 
137     /**
138      * Appends a tag that indicates a reference to work, such as a book, report or web site.
139      *
140      */
appendOpenCiteTag()141     public void appendOpenCiteTag() {
142         text.append("<cite>");
143     }
144 
145     /**
146      * Appends a tag that indicates text that is the code for a program.
147      *
148      */
appendOpenCodeTag()149     public void appendOpenCodeTag() {
150         text.append("<code>");
151     }
152 
153     /**
154      * Appends a tag that indicates end of text that is the code for a program.
155      *
156      */
appendCloseCodeTag()157     public void appendCloseCodeTag() {
158         text.append("</code>");
159     }
160 
161     /**
162      * Appends a tag that indicates emphasis.
163      *
164      */
appendOpenEmTag()165     public void appendOpenEmTag() {
166         text.append("<em>");
167     }
168 
169     /**
170      * Appends a tag that indicates end of emphasis.
171      *
172      */
appendCloseEmTag()173     public void appendCloseEmTag() {
174         text.append("</em>");
175     }
176 
177     /**
178      * Appends a tag that indicates a header, a title of a section of the message.
179      *
180      * @param level the level of the Header. It should be a value between 1 and 3
181      * @param style the XHTML style of the blockquote
182      */
appendOpenHeaderTag(int level, String style)183     public void appendOpenHeaderTag(int level, String style) {
184         if (level > 3 || level < 1) {
185             return;
186         }
187         StringBuilder sb = new StringBuilder("<h");
188         sb.append(level);
189         if (style != null) {
190             sb.append(" style=\"");
191             sb.append(style);
192             sb.append("\"");
193         }
194         sb.append(">");
195         text.append(sb.toString());
196     }
197 
198     /**
199      * Appends a tag that indicates that a header section ends.
200      *
201      * @param level the level of the Header. It should be a value between 1 and 3
202      */
appendCloseHeaderTag(int level)203     public void appendCloseHeaderTag(int level) {
204         if (level > 3 || level < 1) {
205             return;
206         }
207         StringBuilder sb = new StringBuilder("</h");
208         sb.append(level);
209         sb.append(">");
210         text.append(sb.toString());
211     }
212 
213     /**
214      * Appends a tag that indicates an image.
215      *
216      * @param align how text should flow around the picture
217      * @param alt the text to show if you don't show the picture
218      * @param height how tall is the picture
219      * @param src where to get the picture
220      * @param width how wide is the picture
221      */
appendImageTag(String align, String alt, String height, String src, String width)222     public void appendImageTag(String align, String alt, String height, String src, String width) {
223         StringBuilder sb = new StringBuilder("<img");
224         if (align != null) {
225             sb.append(" align=\"");
226             sb.append(align);
227             sb.append("\"");
228         }
229         if (alt != null) {
230             sb.append(" alt=\"");
231             sb.append(alt);
232             sb.append("\"");
233         }
234         if (height != null) {
235             sb.append(" height=\"");
236             sb.append(height);
237             sb.append("\"");
238         }
239         if (src != null) {
240             sb.append(" src=\"");
241             sb.append(src);
242             sb.append("\"");
243         }
244         if (width != null) {
245             sb.append(" width=\"");
246             sb.append(width);
247             sb.append("\"");
248         }
249         sb.append(">");
250         text.append(sb.toString());
251     }
252 
253     /**
254      * Appends a tag that indicates the start of a new line item within a list.
255      *
256      * @param style the style of the line item
257      */
appendLineItemTag(String style)258     public void appendLineItemTag(String style) {
259         StringBuilder sb = new StringBuilder("<li");
260         if (style != null) {
261             sb.append(" style=\"");
262             sb.append(style);
263             sb.append("\"");
264         }
265         sb.append(">");
266         text.append(sb.toString());
267     }
268 
269     /**
270      * Appends a tag that creates an ordered list. "Ordered" means that the order of the items
271      * in the list is important. To show this, browsers automatically number the list.
272      *
273      * @param style the style of the ordered list
274      */
appendOpenOrderedListTag(String style)275     public void appendOpenOrderedListTag(String style) {
276         StringBuilder sb = new StringBuilder("<ol");
277         if (style != null) {
278             sb.append(" style=\"");
279             sb.append(style);
280             sb.append("\"");
281         }
282         sb.append(">");
283         text.append(sb.toString());
284     }
285 
286     /**
287      * Appends a tag that indicates that an ordered list section ends.
288      *
289      */
appendCloseOrderedListTag()290     public void appendCloseOrderedListTag() {
291         text.append("</ol>");
292     }
293 
294     /**
295      * Appends a tag that creates an unordered list. The unordered part means that the items
296      * in the list are not in any particular order.
297      *
298      * @param style the style of the unordered list
299      */
appendOpenUnorderedListTag(String style)300     public void appendOpenUnorderedListTag(String style) {
301         StringBuilder sb = new StringBuilder("<ul");
302         if (style != null) {
303             sb.append(" style=\"");
304             sb.append(style);
305             sb.append("\"");
306         }
307         sb.append(">");
308         text.append(sb.toString());
309     }
310 
311     /**
312      * Appends a tag that indicates that an unordered list section ends.
313      *
314      */
appendCloseUnorderedListTag()315     public void appendCloseUnorderedListTag() {
316         text.append("</ul>");
317     }
318 
319     /**
320      * Appends a tag that indicates the start of a new paragraph. This is usually rendered
321      * with two carriage returns, producing a single blank line in between the two paragraphs.
322      *
323      * @param style the style of the paragraph
324      */
appendOpenParagraphTag(String style)325     public void appendOpenParagraphTag(String style) {
326         StringBuilder sb = new StringBuilder("<p");
327         if (style != null) {
328             sb.append(" style=\"");
329             sb.append(style);
330             sb.append("\"");
331         }
332         sb.append(">");
333         text.append(sb.toString());
334     }
335 
336     /**
337      * Appends a tag that indicates the end of a new paragraph. This is usually rendered
338      * with two carriage returns, producing a single blank line in between the two paragraphs.
339      *
340      */
appendCloseParagraphTag()341     public void appendCloseParagraphTag() {
342         text.append("</p>");
343     }
344 
345     /**
346      * Appends a tag that indicates that an inlined quote section begins.
347      *
348      * @param style the style of the inlined quote
349      */
appendOpenInlinedQuoteTag(String style)350     public void appendOpenInlinedQuoteTag(String style) {
351         StringBuilder sb = new StringBuilder("<q");
352         if (style != null) {
353             sb.append(" style=\"");
354             sb.append(style);
355             sb.append("\"");
356         }
357         sb.append(">");
358         text.append(sb.toString());
359     }
360 
361     /**
362      * Appends a tag that indicates that an inlined quote section ends.
363      *
364      */
appendCloseInlinedQuoteTag()365     public void appendCloseInlinedQuoteTag() {
366         text.append("</q>");
367     }
368 
369     /**
370      * Appends a tag that allows to set the fonts for a span of text.
371      *
372      * @param style the style for a span of text
373      */
appendOpenSpanTag(String style)374     public void appendOpenSpanTag(String style) {
375         StringBuilder sb = new StringBuilder("<span");
376         if (style != null) {
377             sb.append(" style=\"");
378             sb.append(style);
379             sb.append("\"");
380         }
381         sb.append(">");
382         text.append(sb.toString());
383     }
384 
385     /**
386      * Appends a tag that indicates that a span section ends.
387      *
388      */
appendCloseSpanTag()389     public void appendCloseSpanTag() {
390         text.append("</span>");
391     }
392 
393     /**
394      * Appends a tag that indicates text which should be more forceful than surrounding text.
395      *
396      */
appendOpenStrongTag()397     public void appendOpenStrongTag() {
398         text.append("<strong>");
399     }
400 
401     /**
402      * Appends a tag that indicates that a strong section ends.
403      *
404      */
appendCloseStrongTag()405     public void appendCloseStrongTag() {
406         text.append("</strong>");
407     }
408 
409     /**
410      * Appends a given text to the XHTMLText.
411      *
412      * @param textToAppend the text to append
413      */
append(String textToAppend)414     public void append(String textToAppend) {
415         text.append(StringUtils.escapeForXML(textToAppend));
416     }
417 
418     /**
419      * Returns the text of the XHTMLText.
420      *
421      * Note: Automatically adds the closing body tag.
422      *
423      * @return the text of the XHTMLText
424      */
toString()425     public String toString() {
426         return text.toString().concat(closeBodyTag());
427     }
428 
429 }
430