XHTMLText.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.jivesoftware.smackx.xhtmlim;

  18. import org.jivesoftware.smack.packet.Message;
  19. import org.jivesoftware.smack.util.XmlStringBuilder;

  20. /**
  21.  * An XHTMLText represents formatted text. This class also helps to build valid
  22.  * XHTML tags.
  23.  *
  24.  * @author Gaston Dombiak
  25.  */
  26. public class XHTMLText {

  27.     public static final String NAMESPACE = "http://www.w3.org/1999/xhtml";

  28.     private final XmlStringBuilder text = new XmlStringBuilder();

  29.     /**
  30.      * Creates a new XHTMLText with body tag params.
  31.      *
  32.      * @param style the XHTML style of the body
  33.      * @param lang the language of the body
  34.      */
  35.     public XHTMLText(String style, String lang) {
  36.         appendOpenBodyTag(style, lang);
  37.     }

  38.     public static final String A = "a";
  39.     public static final String HREF = "href";
  40.     public static final String STYLE = "style";

  41.     /**
  42.      * Appends a tag that indicates that an anchor section begins.
  43.      *
  44.      * @param href indicates the URL being linked to
  45.      * @param style the XHTML style of the anchor
  46.      */
  47.     public XHTMLText appendOpenAnchorTag(String href, String style) {
  48.         text.halfOpenElement(A);
  49.         text.optAttribute(HREF, href);
  50.         text.optAttribute(STYLE, style);
  51.         text.rightAngleBracket();
  52.         return this;
  53.     }

  54.     /**
  55.      * Appends a tag that indicates that an anchor section ends.
  56.      *
  57.      */
  58.     public XHTMLText appendCloseAnchorTag() {
  59.         text.closeElement(A);
  60.         return this;
  61.     }

  62.     public static final String BLOCKQUOTE = "blockquote";
  63.     /**
  64.      * Appends a tag that indicates that a blockquote section begins.
  65.      *
  66.      * @param style the XHTML style of the blockquote
  67.      */
  68.     public XHTMLText appendOpenBlockQuoteTag(String style) {
  69.         text.halfOpenElement(BLOCKQUOTE);
  70.         text.optAttribute(STYLE, style);
  71.         text.rightAngleBracket();
  72.         return this;
  73.     }

  74.     /**
  75.      * Appends a tag that indicates that a blockquote section ends.
  76.      *
  77.      */
  78.     public XHTMLText appendCloseBlockQuoteTag() {
  79.         text.closeElement(BLOCKQUOTE);
  80.         return this;
  81.     }

  82.     /**
  83.      * Appends a tag that indicates that a body section begins.
  84.      *
  85.      * @param style the XHTML style of the body
  86.      * @param lang the language of the body
  87.      */
  88.     private XHTMLText appendOpenBodyTag(String style, String lang) {
  89.         text.halfOpenElement(Message.BODY);
  90.         text.xmlnsAttribute(NAMESPACE);
  91.         text.optElement(STYLE, style);
  92.         text.xmllangAttribute(lang);
  93.         text.rightAngleBracket();
  94.         return this;
  95.     }

  96.     public XHTMLText appendCloseBodyTag() {
  97.         text.closeElement(Message.BODY);
  98.         return this;
  99.     }

  100.     public static final String BR = "br";
  101.     public static final String CITE = "cite";
  102.     public static final String CODE = "code";

  103.     /**
  104.      * Appends a tag that inserts a single carriage return.
  105.      *
  106.      */
  107.     public XHTMLText appendBrTag() {
  108.         text.emptyElement(BR);
  109.         return this;
  110.     }

  111.     /**
  112.      * Appends a tag that indicates a reference to work, such as a book, report or web site.
  113.      *
  114.      */
  115.     public XHTMLText appendOpenCiteTag() {
  116.         text.openElement(CITE);
  117.         return this;
  118.     }

  119.     /**
  120.      * Appends a tag that indicates text that is the code for a program.
  121.      *
  122.      */
  123.     public XHTMLText appendOpenCodeTag() {
  124.         text.openElement(CODE);
  125.         return this;
  126.     }

  127.     /**
  128.      * Appends a tag that indicates end of text that is the code for a program.
  129.      *
  130.      */
  131.     public XHTMLText appendCloseCodeTag() {
  132.         text.closeElement(CODE);
  133.         return this;
  134.     }

  135.     public static final String EM = "em";

  136.     /**
  137.      * Appends a tag that indicates emphasis.
  138.      *
  139.      */
  140.     public XHTMLText appendOpenEmTag() {
  141.         text.openElement(EM);
  142.         return this;
  143.     }

  144.     /**
  145.      * Appends a tag that indicates end of emphasis.
  146.      *
  147.      */
  148.     public XHTMLText appendCloseEmTag() {
  149.         text.closeElement(EM);
  150.         return this;
  151.     }

  152.     public static final String H = "h";

  153.     /**
  154.      * Appends a tag that indicates a header, a title of a section of the message.
  155.      *
  156.      * @param level the level of the Header. It must be a value between 1 and 3
  157.      * @param style the XHTML style of the blockquote
  158.      */
  159.     public XHTMLText appendOpenHeaderTag(int level, String style) {
  160.         if (level > 3 || level < 1) {
  161.             throw new IllegalArgumentException("Level must be between 1 and 3");
  162.         }
  163.         text.halfOpenElement(H + Integer.toString(level));
  164.         text.optAttribute(STYLE, style);
  165.         text.rightAngleBracket();
  166.         return this;
  167.     }

  168.     /**
  169.      * Appends a tag that indicates that a header section ends.
  170.      *
  171.      * @param level the level of the Header. It must be a value between 1 and 3
  172.      */
  173.     public XHTMLText appendCloseHeaderTag(int level) {
  174.         if (level > 3 || level < 1) {
  175.             throw new IllegalArgumentException("Level must be between 1 and 3");
  176.         }
  177.         text.closeElement(H + Integer.toBinaryString(level));
  178.         return this;
  179.     }

  180.     public static final String IMG = "img";

  181.     /**
  182.      * Appends a tag that indicates an image.
  183.      *
  184.      * @param align how text should flow around the picture
  185.      * @param alt the text to show if you don't show the picture
  186.      * @param height how tall is the picture
  187.      * @param src where to get the picture
  188.      * @param width how wide is the picture
  189.      */
  190.     public XHTMLText appendImageTag(String align, String alt, String height, String src, String width) {
  191.         text.halfOpenElement(IMG);
  192.         text.optAttribute("align", align);
  193.         text.optAttribute("alt", alt);
  194.         text.optAttribute("height", height);
  195.         text.optAttribute("src", src);
  196.         text.optAttribute("width", width);
  197.         text.rightAngleBracket();
  198.         return this;
  199.     }

  200.     public static final String LI = "li";
  201.     public static final String OL = "ol";

  202.     /**
  203.      * Appends a tag that indicates the start of a new line item within a list.
  204.      *
  205.      * @param style the style of the line item
  206.      */
  207.     public XHTMLText appendLineItemTag(String style) {
  208.         text.halfOpenElement(LI);
  209.         text.optAttribute(STYLE, style);
  210.         text.rightAngleBracket();
  211.         return this;
  212.     }

  213.     /**
  214.      * Appends a tag that indicates that a line item section ends.
  215.      *
  216.      */
  217.     public XHTMLText appendCloseLineItemTag() {
  218.         text.closeElement(LI);
  219.         return this;
  220.     }

  221.     /**
  222.      * Appends a tag that creates an ordered list. "Ordered" means that the order of the items
  223.      * in the list is important. To show this, browsers automatically number the list.
  224.      *
  225.      * @param style the style of the ordered list
  226.      */
  227.     public XHTMLText appendOpenOrderedListTag(String style) {
  228.         text.halfOpenElement(OL);
  229.         text.optAttribute(STYLE, style);
  230.         text.rightAngleBracket();
  231.         return this;
  232.     }

  233.     /**
  234.      * Appends a tag that indicates that an ordered list section ends.
  235.      *
  236.      */
  237.     public XHTMLText appendCloseOrderedListTag() {
  238.         text.closeElement(OL);
  239.         return this;
  240.     }

  241.     public static final String UL = "ul";

  242.     /**
  243.      * Appends a tag that creates an unordered list. The unordered part means that the items
  244.      * in the list are not in any particular order.
  245.      *
  246.      * @param style the style of the unordered list
  247.      */
  248.     public XHTMLText appendOpenUnorderedListTag(String style) {
  249.         text.halfOpenElement(UL);
  250.         text.optAttribute(STYLE, style);
  251.         text.rightAngleBracket();
  252.         return this;
  253.     }

  254.     /**
  255.      * Appends a tag that indicates that an unordered list section ends.
  256.      *
  257.      */
  258.     public XHTMLText appendCloseUnorderedListTag() {
  259.         text.closeElement(UL);
  260.         return this;
  261.     }

  262.     public static final String P = "p";

  263.     /**
  264.      * Appends a tag that indicates the start of a new paragraph. This is usually rendered
  265.      * with two carriage returns, producing a single blank line in between the two paragraphs.
  266.      *
  267.      * @param style the style of the paragraph
  268.      */
  269.     public XHTMLText appendOpenParagraphTag(String style) {
  270.         text.halfOpenElement(P);
  271.         text.optAttribute(STYLE, style);
  272.         text.rightAngleBracket();
  273.         return this;
  274.     }

  275.     /**
  276.      * Appends a tag that indicates the end of a new paragraph. This is usually rendered
  277.      * with two carriage returns, producing a single blank line in between the two paragraphs.
  278.      *
  279.      */
  280.     public XHTMLText appendCloseParagraphTag() {
  281.         text.closeElement(P);
  282.         return this;
  283.     }

  284.     public static final String Q = "q";

  285.     /**
  286.      * Appends a tag that indicates that an inlined quote section begins.
  287.      *
  288.      * @param style the style of the inlined quote
  289.      */
  290.     public XHTMLText appendOpenInlinedQuoteTag(String style) {
  291.         text.halfOpenElement(Q);
  292.         text.optAttribute(STYLE, style);
  293.         text.rightAngleBracket();
  294.         return this;
  295.     }

  296.     /**
  297.      * Appends a tag that indicates that an inlined quote section ends.
  298.      *
  299.      */
  300.     public XHTMLText appendCloseInlinedQuoteTag() {
  301.         text.closeElement(Q);
  302.         return this;
  303.     }

  304.     public static final String SPAN = "span";

  305.     /**
  306.      * Appends a tag that allows to set the fonts for a span of text.
  307.      *
  308.      * @param style the style for a span of text
  309.      */
  310.     public XHTMLText appendOpenSpanTag(String style) {
  311.         text.halfOpenElement(SPAN);
  312.         text.optAttribute(STYLE, style);
  313.         text.rightAngleBracket();
  314.         return this;
  315.     }

  316.     /**
  317.      * Appends a tag that indicates that a span section ends.
  318.      *
  319.      */
  320.     public XHTMLText appendCloseSpanTag() {
  321.         text.closeElement(SPAN);
  322.         return this;
  323.     }

  324.     public static final String STRONG = "strong";

  325.     /**
  326.      * Appends a tag that indicates text which should be more forceful than surrounding text.
  327.      *
  328.      */
  329.     public XHTMLText appendOpenStrongTag() {
  330.         text.openElement(STRONG);
  331.         return this;
  332.     }

  333.     /**
  334.      * Appends a tag that indicates that a strong section ends.
  335.      *
  336.      */
  337.     public XHTMLText appendCloseStrongTag() {
  338.         text.closeElement(STRONG);
  339.         return this;
  340.     }

  341.     /**
  342.      * Appends a given text to the XHTMLText.
  343.      *
  344.      * @param textToAppend the text to append  
  345.      */
  346.     public XHTMLText append(String textToAppend) {
  347.         text.escape(textToAppend);
  348.         return this;
  349.     }

  350.     /**
  351.      * Returns the text of the XHTMLText.
  352.      *
  353.      * @return the text of the XHTMLText  
  354.      */
  355.     public String toString() {
  356.         return text.toString();
  357.     }

  358.     public XmlStringBuilder toXML() {
  359.         return text;
  360.     }
  361. }