KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xml > stream > Escapifier


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Adam Megacz
28  */

29
30 package com.caucho.xml.stream;
31
32 import com.caucho.util.CharBuffer;
33 import com.caucho.vfs.WriteStream;
34
35 import java.io.IOException JavaDoc;
36
37 public class Escapifier {
38
39   public static String JavaDoc escape(String JavaDoc s)
40   {
41     if (s == null)
42       return "";
43     
44     CharBuffer cb = null;
45     int len = s.length();
46
47     for (int i = 0; i < len; i++) {
48       char c = s.charAt(i);
49
50       if (c >= 32 && c <= 127 && c != '&' && c!='<' && c!='>' && c!='\"'
51           || Character.isWhitespace(c)) {
52         if (cb != null) cb.append(c);
53         continue;
54       }
55       
56       if (cb == null) {
57         cb = new CharBuffer();
58         cb.append(s.substring(0, i));
59       }
60       switch(c) {
61       case '&': cb.append("&amp;"); break;
62       case '<': cb.append("&lt;"); break;
63       case '>': cb.append("&gt;"); break;
64       // case '\'': cb.append("&apos;"); break; // TCK compliance
65
case '\"': cb.append("&quot;"); break;
66       default: cb.append("&#"+((int)(c & 0xffff))+";"); break;
67       }
68     }
69
70     if (cb == null)
71       return s;
72
73     return cb.toString();
74   }
75
76   public static void escape(String JavaDoc s, WriteStream ws)
77     throws IOException JavaDoc
78   {
79     ws.print(escape(s));
80   }
81
82   public static void escape(char []buffer, int offset, int len,
83                 WriteStream out)
84     throws IOException JavaDoc
85   {
86     for (int i = 0; i < len; i++) {
87       char ch = buffer[offset + i];
88       
89       switch (ch) {
90       case '&':
91     out.print("&amp;");
92     break;
93       case '<':
94     out.print("&lt;");
95     break;
96       case '>':
97     out.print("&gt;");
98     break;
99       // case '\'': out.print("&apos;"); break; // TCK compliance
100
case '\"':
101     out.print("&quot;");
102     break;
103
104       case ' ': case '\t': case '\r': case '\n':
105     out.print(ch);
106     break;
107     
108       default:
109     if (32 <= ch && ch <= 127)
110       out.print(ch);
111     else
112       out.print("&#" + ((int) ch) + ";");
113     break;
114       }
115     }
116   }
117 }
118
Popular Tags