KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > jcr > impl > util > StringConverter


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
6 package org.exoplatform.services.jcr.impl.util;
7
8 /**
9  * Created by The eXo Platform SARL .
10  *
11  * @author <a HREF="mailto:geaz@users.sourceforge.net">Gennady Azarenkov</a>
12  * @version $Id: StringConverter.java,v 1.2 2004/07/08 23:36:50 benjmestrallet Exp $
13  */

14
15 public class StringConverter {
16
17   /**
18    * Normalizes and prints the given string.
19    */

20   public static String JavaDoc normalizeString(String JavaDoc s, boolean canonical) {
21
22     StringBuffer JavaDoc strBuf = new StringBuffer JavaDoc();
23     int len = (s != null) ? s.length() : 0;
24     for (int i = 0; i < len; i++) {
25       char c = s.charAt(i);
26       strBuf.append(normalizeChar(c, canonical));
27     }
28     return new String JavaDoc(strBuf);
29
30   } // normalizeAndPrint(String)
31

32   /**
33    * Normalizes and print the given character.
34    */

35   public static String JavaDoc normalizeChar(char c, boolean canonical) {
36
37     switch (c) {
38       case '<':
39         return "&lt;";
40       case '>':
41         return "&gt;";
42       case '&':
43         return "&amp;";
44       case '"':
45         return "&quot;";
46       case '\r':
47       case '\n':
48         if (canonical) {
49           return "&#" + Integer.toString(c) + ";";
50         }
51         // else, default print char
52
default:
53         return "" + c;
54     }
55
56   }
57
58
59 }
60
Popular Tags