001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Adam Megacz
028: */
029:
030: package com.caucho.xml.stream;
031:
032: import com.caucho.util.CharBuffer;
033: import com.caucho.vfs.WriteStream;
034:
035: import java.io.IOException;
036:
037: public class Escapifier {
038:
039: public static String escape(String s) {
040: if (s == null)
041: return "";
042:
043: CharBuffer cb = null;
044: int len = s.length();
045:
046: for (int i = 0; i < len; i++) {
047: char c = s.charAt(i);
048:
049: if (c >= 32 && c <= 127 && c != '&' && c != '<' && c != '>'
050: && c != '\"' || Character.isWhitespace(c)) {
051: if (cb != null)
052: cb.append(c);
053: continue;
054: }
055:
056: if (cb == null) {
057: cb = new CharBuffer();
058: cb.append(s.substring(0, i));
059: }
060: switch (c) {
061: case '&':
062: cb.append("&");
063: break;
064: case '<':
065: cb.append("<");
066: break;
067: case '>':
068: cb.append(">");
069: break;
070: // case '\'': cb.append("'"); break; // TCK compliance
071: case '\"':
072: cb.append(""");
073: break;
074: default:
075: cb.append("&#" + ((int) (c & 0xffff)) + ";");
076: break;
077: }
078: }
079:
080: if (cb == null)
081: return s;
082:
083: return cb.toString();
084: }
085:
086: public static void escape(String s, WriteStream ws)
087: throws IOException {
088: ws.print(escape(s));
089: }
090:
091: public static void escape(char[] buffer, int offset, int len,
092: WriteStream out) throws IOException {
093: for (int i = 0; i < len; i++) {
094: char ch = buffer[offset + i];
095:
096: switch (ch) {
097: case '&':
098: out.print("&");
099: break;
100: case '<':
101: out.print("<");
102: break;
103: case '>':
104: out.print(">");
105: break;
106: // case '\'': out.print("'"); break; // TCK compliance
107: case '\"':
108: out.print(""");
109: break;
110:
111: case ' ':
112: case '\t':
113: case '\r':
114: case '\n':
115: out.print(ch);
116: break;
117:
118: default:
119: if (32 <= ch && ch <= 127)
120: out.print(ch);
121: else
122: out.print("&#" + ((int) ch) + ";");
123: break;
124: }
125: }
126: }
127: }
|