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.packet.Message;
021import org.jivesoftware.smack.util.XmlStringBuilder;
022
023/**
024 * An XHTMLText represents formatted text. This class also helps to build valid
025 * XHTML tags.
026 *
027 * @author Gaston Dombiak
028 */
029public class XHTMLText {
030
031    public static final String NAMESPACE = "http://www.w3.org/1999/xhtml";
032
033    private final XmlStringBuilder text = new XmlStringBuilder();
034
035    /**
036     * Creates a new XHTMLText with body tag params.
037     *
038     * @param style the XHTML style of the body
039     * @param lang the language of the body
040     */
041    public XHTMLText(String style, String lang) {
042        appendOpenBodyTag(style, lang);
043    }
044
045    public static final String A = "a";
046    public static final String HREF = "href";
047    public static final String STYLE = "style";
048
049    /**
050     * Appends a tag that indicates that an anchor section begins.
051     *
052     * @param href indicates the URL being linked to
053     * @param style the XHTML style of the anchor
054     * @return this.
055     */
056    public XHTMLText appendOpenAnchorTag(String href, String style) {
057        text.halfOpenElement(A);
058        text.optAttribute(HREF, href);
059        text.optAttribute(STYLE, style);
060        text.rightAngleBracket();
061        return this;
062    }
063
064    /**
065     * Appends a tag that indicates that an anchor section ends.
066     *
067     * @return this.
068     */
069    public XHTMLText appendCloseAnchorTag() {
070        text.closeElement(A);
071        return this;
072    }
073
074    public static final String BLOCKQUOTE = "blockquote";
075    /**
076     * Appends a tag that indicates that a blockquote section begins.
077     *
078     * @param style the XHTML style of the blockquote
079     * @return this.
080     */
081    public XHTMLText appendOpenBlockQuoteTag(String style) {
082        text.halfOpenElement(BLOCKQUOTE);
083        text.optAttribute(STYLE, style);
084        text.rightAngleBracket();
085        return this;
086    }
087
088    /**
089     * Appends a tag that indicates that a blockquote section ends.
090     *
091     * @return this.
092     */
093    public XHTMLText appendCloseBlockQuoteTag() {
094        text.closeElement(BLOCKQUOTE);
095        return this;
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     * @return this.
104     */
105    private XHTMLText appendOpenBodyTag(String style, String lang) {
106        text.halfOpenElement(Message.BODY);
107        text.xmlnsAttribute(NAMESPACE);
108        text.optAttribute(STYLE, style);
109        text.xmllangAttribute(lang);
110        text.rightAngleBracket();
111        return this;
112    }
113
114    /**
115     * Appends a tag that indicates that the body section ends.
116     *
117     * @return this.
118     */
119    public XHTMLText appendCloseBodyTag() {
120        text.closeElement(Message.BODY);
121        return this;
122    }
123
124    public static final String BR = "br";
125    public static final String CITE = "cite";
126    public static final String CODE = "code";
127
128    /**
129     * Appends a tag that inserts a single carriage return.
130     *
131     * @return this.
132     */
133    public XHTMLText appendBrTag() {
134        text.emptyElement(BR);
135        return this;
136    }
137
138    /**
139     * Appends a tag that indicates a reference to work, such as a book, report or web site.
140     *
141     * @return this.
142     */
143    public XHTMLText appendOpenCiteTag() {
144        text.openElement(CITE);
145        return this;
146    }
147
148    /**
149     * Appends a tag that indicates text that is the code for a program.
150     *
151     * @return this.
152     */
153    public XHTMLText appendOpenCodeTag() {
154        text.openElement(CODE);
155        return this;
156    }
157
158    /**
159     * Appends a tag that indicates end of text that is the code for a program.
160     *
161     * @return this.
162     */
163    public XHTMLText appendCloseCodeTag() {
164        text.closeElement(CODE);
165        return this;
166    }
167
168    public static final String EM = "em";
169
170    /**
171     * Appends a tag that indicates emphasis.
172     *
173     * @return this.
174     */
175    public XHTMLText appendOpenEmTag() {
176        text.openElement(EM);
177        return this;
178    }
179
180    /**
181     * Appends a tag that indicates end of emphasis.
182     *
183     * @return this.
184     */
185    public XHTMLText appendCloseEmTag() {
186        text.closeElement(EM);
187        return this;
188    }
189
190    public static final String H = "h";
191
192    /**
193     * Appends a tag that indicates a header, a title of a section of the message.
194     *
195     * @param level the level of the Header. It must be a value between 1 and 3
196     * @param style the XHTML style of the blockquote
197     *
198     * @return this.
199     */
200    public XHTMLText appendOpenHeaderTag(int level, String style) {
201        if (level > 3 || level < 1) {
202            throw new IllegalArgumentException("Level must be between 1 and 3");
203        }
204        text.halfOpenElement(H + Integer.toString(level));
205        text.optAttribute(STYLE, style);
206        text.rightAngleBracket();
207        return this;
208    }
209
210    /**
211     * Appends a tag that indicates that a header section ends.
212     *
213     * @param level the level of the Header. It must be a value between 1 and 3
214     * @return this.
215     */
216    public XHTMLText appendCloseHeaderTag(int level) {
217        if (level > 3 || level < 1) {
218            throw new IllegalArgumentException("Level must be between 1 and 3");
219        }
220        text.closeElement(H + Integer.toBinaryString(level));
221        return this;
222    }
223
224    public static final String IMG = "img";
225
226    /**
227     * Appends a tag that indicates an image.
228     *
229     * @param align how text should flow around the picture
230     * @param alt the text to show if you don't show the picture
231     * @param height how tall is the picture
232     * @param src where to get the picture
233     * @param width how wide is the picture
234     *
235     * @return this.
236     */
237    public XHTMLText appendImageTag(String align, String alt, String height, String src, String width) {
238        text.halfOpenElement(IMG);
239        text.optAttribute("align", align);
240        text.optAttribute("alt", alt);
241        text.optAttribute("height", height);
242        text.optAttribute("src", src);
243        text.optAttribute("width", width);
244        text.rightAngleBracket();
245        return this;
246    }
247
248    public static final String LI = "li";
249    public static final String OL = "ol";
250
251    /**
252     * Appends a tag that indicates the start of a new line item within a list.
253     *
254     * @param style the style of the line item
255     *
256     * @return this.
257     */
258    public XHTMLText appendLineItemTag(String style) {
259        text.halfOpenElement(LI);
260        text.optAttribute(STYLE, style);
261        text.rightAngleBracket();
262        return this;
263    }
264
265    /**
266     * Appends a tag that indicates that a line item section ends.
267     *
268     * @return this.
269     */
270    public XHTMLText appendCloseLineItemTag() {
271        text.closeElement(LI);
272        return this;
273    }
274
275    /**
276     * Appends a tag that creates an ordered list. "Ordered" means that the order of the items
277     * in the list is important. To show this, browsers automatically number the list.
278     *
279     * @param style the style of the ordered list
280     *
281     * @return this.
282     */
283    public XHTMLText appendOpenOrderedListTag(String style) {
284        text.halfOpenElement(OL);
285        text.optAttribute(STYLE, style);
286        text.rightAngleBracket();
287        return this;
288    }
289
290    /**
291     * Appends a tag that indicates that an ordered list section ends.
292     *
293     * @return this.
294     */
295    public XHTMLText appendCloseOrderedListTag() {
296        text.closeElement(OL);
297        return this;
298    }
299
300    public static final String UL = "ul";
301
302    /**
303     * Appends a tag that creates an unordered list. The unordered part means that the items
304     * in the list are not in any particular order.
305     *
306     * @param style the style of the unordered list
307     *
308     * @return this.
309     */
310    public XHTMLText appendOpenUnorderedListTag(String style) {
311        text.halfOpenElement(UL);
312        text.optAttribute(STYLE, style);
313        text.rightAngleBracket();
314        return this;
315    }
316
317    /**
318     * Appends a tag that indicates that an unordered list section ends.
319     *
320     * @return this.
321     */
322    public XHTMLText appendCloseUnorderedListTag() {
323        text.closeElement(UL);
324        return this;
325    }
326
327    public static final String P = "p";
328
329    /**
330     * Appends a tag that indicates the start of a new paragraph. This is usually rendered
331     * with two carriage returns, producing a single blank line in between the two paragraphs.
332     *
333     * @param style the style of the paragraph
334     *
335     * @return this.
336     */
337    public XHTMLText appendOpenParagraphTag(String style) {
338        text.halfOpenElement(P);
339        text.optAttribute(STYLE, style);
340        text.rightAngleBracket();
341        return this;
342    }
343
344    /**
345     * Appends a tag that indicates the end of a new paragraph. This is usually rendered
346     * with two carriage returns, producing a single blank line in between the two paragraphs.
347     *
348     * @return this.
349     */
350    public XHTMLText appendCloseParagraphTag() {
351        text.closeElement(P);
352        return this;
353    }
354
355    public static final String Q = "q";
356
357    /**
358     * Appends a tag that indicates that an inlined quote section begins.
359     *
360     * @param style the style of the inlined quote
361     *
362     * @return this.
363     */
364    public XHTMLText appendOpenInlinedQuoteTag(String style) {
365        text.halfOpenElement(Q);
366        text.optAttribute(STYLE, style);
367        text.rightAngleBracket();
368        return this;
369    }
370
371    /**
372     * Appends a tag that indicates that an inlined quote section ends.
373     *
374     * @return this.
375     */
376    public XHTMLText appendCloseInlinedQuoteTag() {
377        text.closeElement(Q);
378        return this;
379    }
380
381    public static final String SPAN = "span";
382
383    /**
384     * Appends a tag that allows to set the fonts for a span of text.
385     *
386     * @param style the style for a span of text
387     *
388     * @return this.
389     */
390    public XHTMLText appendOpenSpanTag(String style) {
391        text.halfOpenElement(SPAN);
392        text.optAttribute(STYLE, style);
393        text.rightAngleBracket();
394        return this;
395    }
396
397    /**
398     * Appends a tag that indicates that a span section ends.
399     *
400     * @return this.
401     */
402    public XHTMLText appendCloseSpanTag() {
403        text.closeElement(SPAN);
404        return this;
405    }
406
407    public static final String STRONG = "strong";
408
409    /**
410     * Appends a tag that indicates text which should be more forceful than surrounding text.
411     *
412     * @return this.
413     */
414    public XHTMLText appendOpenStrongTag() {
415        text.openElement(STRONG);
416        return this;
417    }
418
419    /**
420     * Appends a tag that indicates that a strong section ends.
421     *
422     * @return this.
423     */
424    public XHTMLText appendCloseStrongTag() {
425        text.closeElement(STRONG);
426        return this;
427    }
428
429    /**
430     * Appends a given text to the XHTMLText.
431     *
432     * @param textToAppend the text to append
433     *
434     * @return this.
435     */
436    public XHTMLText append(String textToAppend) {
437        text.escape(textToAppend);
438        return this;
439    }
440
441    /**
442     * Returns the text of the XHTMLText.
443     *
444     * @return the text of the XHTMLText
445     */
446    @Override
447    public String toString() {
448        return text.toString();
449    }
450
451    public XmlStringBuilder toXML() {
452        return text;
453    }
454}