KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > grammar > converter > impl > Text2HtmlConverterImpl


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.services.grammar.converter.impl;
6
7 import org.exoplatform.services.grammar.converter.Text2HtmlConverter ;
8 /**
9  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
10  * @since Jan 4, 2005
11  * @version $Id$
12  */

13 public class Text2HtmlConverterImpl implements Text2HtmlConverter {
14
15   public String JavaDoc toHtml(String JavaDoc javaCode) {
16     return toHtml(javaCode.toCharArray());
17   }
18
19   public String JavaDoc toHtml(char[] javaCodeBuf) {
20     StringBuffer JavaDoc b = new StringBuffer JavaDoc() ;
21     toHtml(javaCodeBuf, b) ;
22     return b.toString() ;
23   }
24
25   public void toHtml(char[] buf, StringBuffer JavaDoc b) {
26     b.append("<div class='text'>") ;
27     for(int i = 0; i < buf.length; i++) {
28       switch(buf[i]) {
29         case '\n' : b.append("<br/>") ; break ;
30         case '\r' : break ;
31         case '&' : b.append("&amp;") ; break ;
32         case '>' : b.append("&gt;") ; break ;
33         case '<' : b.append("&lt;") ; break ;
34         case ' ' : {
35           b.append(" ") ;
36           i++ ;
37           while(i < buf.length && buf[i] == ' ') {
38             b.append("&nbsp;") ;
39             i++ ;
40           }
41           i-- ;
42           break ;
43         }
44         default: b.append(buf[i]) ; break ;
45       }
46     }
47     b.append("</div>") ;
48   }
49 }
50
Popular Tags