KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > util > XMLUtils


1 package com.dotmarketing.util;
2
3 import java.lang.reflect.Method JavaDoc;
4
5 public class XMLUtils {
6
7     /**
8      * This will take the three pre-defined entities in XML 1.0 (used
9      * specifically in XML elements) and convert their character representation
10      * to the appropriate entity reference, suitable for XML element content.
11      *
12      * @param str
13      * <code>String</code> input to escape.
14      * @return <code>String</code> with escaped content.
15      */

16     public static String JavaDoc xmlEscape(String JavaDoc str) {
17
18         if (str == null) {
19             return null;
20         }
21         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
22         char ch;
23
24         for (int i = 0; i < str.length(); i++) {
25             ch = str.charAt(i);
26             if (ch == 0)
27                 continue;
28             else if (ch == '<')
29                 sb.append("&lt;");
30
31             else if (ch == '>')
32                 sb.append("&gt;");
33
34             else if (ch == '&')
35                 sb.append("&amp;");
36
37             else if (ch == '\r')
38                 sb.append("&#xD;");
39
40             else if (ch == '\n')
41                 sb.append("\r\n");
42
43             else if (ch > 256)
44                 sb.append("&#x" + Integer.toHexString(ch) + ";");
45             else
46                 sb.append(ch);
47
48         }
49
50         return sb.toString();
51     }
52 }
53
Free Books   Free Magazines  
Popular Tags