01: package org.gomba.utils.xml;
02:
03: import java.io.Reader;
04: import java.io.IOException;
05:
06: /**
07: * This Reader implementation removes invalid xml characters.
08: *
09: * not in a smart way :-(
10: *
11: * @author Daniele Galdi
12: * @version $Id: XMLTextReader.java,v 1.1 2004/11/10 16:44:27 flaviotordini Exp $
13: * @see http://www.w3.org/TR/REC-xml/#charsets
14: */
15: public class XMLTextReader extends Reader {
16:
17: Reader reader;
18:
19: public XMLTextReader(Reader reader) {
20: this .reader = reader;
21: }
22:
23: public int read() throws IOException {
24: return this .reader.read();
25: }
26:
27: public int read(char[] cbuf) throws IOException {
28: char[] tmp = new char[cbuf.length];
29: int result = this .reader.read(tmp);
30: int newLenght = 0;
31:
32: for (int i = 0; i < result; i++) {
33: if (isXMLValid(tmp[i])) {
34: cbuf[newLenght] = tmp[i];
35: newLenght++;
36: }
37: }
38:
39: return (result < 0 ? result : newLenght);
40: }
41:
42: public void close() throws IOException {
43: this .reader.close();
44: }
45:
46: public int read(char[] chars, int i, int i1) throws IOException {
47: return read(chars, i, i1);
48: }
49:
50: boolean isXMLValid(int i) {
51: return !((i >= 0 && i <= 8) || i == 11 || i == 12 || (i >= 14 && i <= 31));
52: }
53: }
|