/smack/source/org/jivesoftware/smackx/XHTMLText.java

https://github.com/joechen2010/IM · Java · 429 lines · 215 code · 36 blank · 178 comment · 38 complexity · 68e1b86f1022a5021a2b01ba7da834c6 MD5 · raw file

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