aboutsummaryrefslogtreecommitdiff
path: root/src/org/jivesoftware/smackx/XHTMLText.java
blob: 201e5308acfca3f9c4dd435dc58c0bba862572d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
 * Copyright 2003-2007 Jive Software.
 *
 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jivesoftware.smackx;

import org.jivesoftware.smack.util.StringUtils;

/**
 * An XHTMLText represents formatted text. This class also helps to build valid 
 * XHTML tags.
 *
 * @author Gaston Dombiak
 */
public class XHTMLText {

    private StringBuilder text = new StringBuilder(30);

    /**
     * Creates a new XHTMLText with body tag params.
     * 
     * @param style the XHTML style of the body
     * @param lang the language of the body
     */
    public XHTMLText(String style, String lang) {
        appendOpenBodyTag(style, lang);
    }

    /**
     * Appends a tag that indicates that an anchor section begins.
     * 
     * @param href indicates the URL being linked to
     * @param style the XHTML style of the anchor
     */
    public void appendOpenAnchorTag(String href, String style) {
        StringBuilder sb = new StringBuilder("<a");
        if (href != null) {
            sb.append(" href=\"");
            sb.append(href);
            sb.append("\"");
        }
        if (style != null) {
            sb.append(" style=\"");
            sb.append(style);
            sb.append("\"");
        }
        sb.append(">");
        text.append(sb.toString());
    }

    /**
     * Appends a tag that indicates that an anchor section ends.
     * 
     */
    public void appendCloseAnchorTag() {
        text.append("</a>");
    }

    /**
     * Appends a tag that indicates that a blockquote section begins.
     * 
     * @param style the XHTML style of the blockquote
     */
    public void appendOpenBlockQuoteTag(String style) {
        StringBuilder sb = new StringBuilder("<blockquote");
        if (style != null) {
            sb.append(" style=\"");
            sb.append(style);
            sb.append("\"");
        }
        sb.append(">");
        text.append(sb.toString());
    }

    /**
     * Appends a tag that indicates that a blockquote section ends.
     * 
     */
    public void appendCloseBlockQuoteTag() {
        text.append("</blockquote>");
    }

    /**
     * Appends a tag that indicates that a body section begins.
     * 
     * @param style the XHTML style of the body
     * @param lang the language of the body
     */
    private void appendOpenBodyTag(String style, String lang) {
        StringBuilder sb = new StringBuilder("<body");
        if (style != null) {
            sb.append(" style=\"");
            sb.append(style);
            sb.append("\"");
        }
        if (lang != null) {
            sb.append(" xml:lang=\"");
            sb.append(lang);
            sb.append("\"");
        }
        sb.append(">");
        text.append(sb.toString());
    }

    /**
     * Appends a tag that indicates that a body section ends.
     * 
     */
    private String closeBodyTag() {
        return "</body>";
    }

    /**
     * Appends a tag that inserts a single carriage return.
     * 
     */
    public void appendBrTag() {
        text.append("<br/>");
    }

    /**
     * Appends a tag that indicates a reference to work, such as a book, report or web site.
     * 
     */
    public void appendOpenCiteTag() {
        text.append("<cite>");
    }

    /**
     * Appends a tag that indicates text that is the code for a program.
     * 
     */
    public void appendOpenCodeTag() {
        text.append("<code>");
    }

    /**
     * Appends a tag that indicates end of text that is the code for a program.
     * 
     */
    public void appendCloseCodeTag() {
        text.append("</code>");
    }

    /**
     * Appends a tag that indicates emphasis.
     * 
     */
    public void appendOpenEmTag() {
        text.append("<em>");
    }

    /**
     * Appends a tag that indicates end of emphasis.
     * 
     */
    public void appendCloseEmTag() {
        text.append("</em>");
    }

    /**
     * Appends a tag that indicates a header, a title of a section of the message.
     * 
     * @param level the level of the Header. It should be a value between 1 and 3
     * @param style the XHTML style of the blockquote
     */
    public void appendOpenHeaderTag(int level, String style) {
        if (level > 3 || level < 1) {
            return;
        }
        StringBuilder sb = new StringBuilder("<h");
        sb.append(level);
        if (style != null) {
            sb.append(" style=\"");
            sb.append(style);
            sb.append("\"");
        }
        sb.append(">");
        text.append(sb.toString());
    }

    /**
     * Appends a tag that indicates that a header section ends.
     * 
     * @param level the level of the Header. It should be a value between 1 and 3
     */
    public void appendCloseHeaderTag(int level) {
        if (level > 3 || level < 1) {
            return;
        }
        StringBuilder sb = new StringBuilder("</h");
        sb.append(level);
        sb.append(">");
        text.append(sb.toString());
    }

    /**
     * Appends a tag that indicates an image.
     * 
     * @param align how text should flow around the picture
     * @param alt the text to show if you don't show the picture
     * @param height how tall is the picture
     * @param src where to get the picture
     * @param width how wide is the picture
     */
    public void appendImageTag(String align, String alt, String height, String src, String width) {
        StringBuilder sb = new StringBuilder("<img");
        if (align != null) {
            sb.append(" align=\"");
            sb.append(align);
            sb.append("\"");
        }
        if (alt != null) {
            sb.append(" alt=\"");
            sb.append(alt);
            sb.append("\"");
        }
        if (height != null) {
            sb.append(" height=\"");
            sb.append(height);
            sb.append("\"");
        }
        if (src != null) {
            sb.append(" src=\"");
            sb.append(src);
            sb.append("\"");
        }
        if (width != null) {
            sb.append(" width=\"");
            sb.append(width);
            sb.append("\"");
        }
        sb.append(">");
        text.append(sb.toString());
    }

    /**
     * Appends a tag that indicates the start of a new line item within a list.
     * 
     * @param style the style of the line item
     */
    public void appendLineItemTag(String style) {
        StringBuilder sb = new StringBuilder("<li");
        if (style != null) {
            sb.append(" style=\"");
            sb.append(style);
            sb.append("\"");
        }
        sb.append(">");
        text.append(sb.toString());
    }

    /**
     * Appends a tag that creates an ordered list. "Ordered" means that the order of the items 
     * in the list is important. To show this, browsers automatically number the list. 
     * 
     * @param style the style of the ordered list
     */
    public void appendOpenOrderedListTag(String style) {
        StringBuilder sb = new StringBuilder("<ol");
        if (style != null) {
            sb.append(" style=\"");
            sb.append(style);
            sb.append("\"");
        }
        sb.append(">");
        text.append(sb.toString());
    }

    /**
     * Appends a tag that indicates that an ordered list section ends.
     * 
     */
    public void appendCloseOrderedListTag() {
        text.append("</ol>");
    }

    /**
     * Appends a tag that creates an unordered list. The unordered part means that the items 
     * in the list are not in any particular order.
     * 
     * @param style the style of the unordered list
     */
    public void appendOpenUnorderedListTag(String style) {
        StringBuilder sb = new StringBuilder("<ul");
        if (style != null) {
            sb.append(" style=\"");
            sb.append(style);
            sb.append("\"");
        }
        sb.append(">");
        text.append(sb.toString());
    }

    /**
     * Appends a tag that indicates that an unordered list section ends.
     * 
     */
    public void appendCloseUnorderedListTag() {
        text.append("</ul>");
    }

    /**
     * Appends a tag that indicates the start of a new paragraph. This is usually rendered 
     * with two carriage returns, producing a single blank line in between the two paragraphs.
     * 
     * @param style the style of the paragraph
     */
    public void appendOpenParagraphTag(String style) {
        StringBuilder sb = new StringBuilder("<p");
        if (style != null) {
            sb.append(" style=\"");
            sb.append(style);
            sb.append("\"");
        }
        sb.append(">");
        text.append(sb.toString());
    }

    /**
     * Appends a tag that indicates the end of a new paragraph. This is usually rendered 
     * with two carriage returns, producing a single blank line in between the two paragraphs.
     * 
     */
    public void appendCloseParagraphTag() {
        text.append("</p>");
    }

    /**
     * Appends a tag that indicates that an inlined quote section begins.
     * 
     * @param style the style of the inlined quote
     */
    public void appendOpenInlinedQuoteTag(String style) {
        StringBuilder sb = new StringBuilder("<q");
        if (style != null) {
            sb.append(" style=\"");
            sb.append(style);
            sb.append("\"");
        }
        sb.append(">");
        text.append(sb.toString());
    }

    /**
     * Appends a tag that indicates that an inlined quote section ends.
     * 
     */
    public void appendCloseInlinedQuoteTag() {
        text.append("</q>");
    }

    /**
     * Appends a tag that allows to set the fonts for a span of text.
     * 
     * @param style the style for a span of text
     */
    public void appendOpenSpanTag(String style) {
        StringBuilder sb = new StringBuilder("<span");
        if (style != null) {
            sb.append(" style=\"");
            sb.append(style);
            sb.append("\"");
        }
        sb.append(">");
        text.append(sb.toString());
    }

    /**
     * Appends a tag that indicates that a span section ends.
     * 
     */
    public void appendCloseSpanTag() {
        text.append("</span>");
    }

    /**
     * Appends a tag that indicates text which should be more forceful than surrounding text.
     * 
     */
    public void appendOpenStrongTag() {
        text.append("<strong>");
    }

    /**
     * Appends a tag that indicates that a strong section ends.
     * 
     */
    public void appendCloseStrongTag() {
        text.append("</strong>");
    }

    /**
     * Appends a given text to the XHTMLText.
     * 
     * @param textToAppend the text to append   
     */
    public void append(String textToAppend) {
        text.append(StringUtils.escapeForXML(textToAppend));
    }

    /**
     * Returns the text of the XHTMLText.
     * 
     * Note: Automatically adds the closing body tag.
     * 
     * @return the text of the XHTMLText   
     */
    public String toString() {
        return text.toString().concat(closeBodyTag());
    }

}