01: /*
02: * Copyright 2001-2006 C:1 Financial Services GmbH
03: *
04: * This software is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License Version 2.1, as published by the Free Software Foundation.
07: *
08: * This software is distributed in the hope that it will be useful,
09: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11: * Lesser General Public License for more details.
12: *
13: * You should have received a copy of the GNU Lesser General Public
14: * License along with this library; if not, write to the Free Software
15: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
16: */
17:
18: package de.finix.contelligent.resource;
19:
20: import java.io.*;
21:
22: import de.finix.contelligent.logging.LoggingService;
23:
24: public class TextResource extends StringResource {
25: final static org.apache.log4j.Logger log = LoggingService
26: .getLogger(TextResource.class);
27:
28: private static final int BOM_SIZE = 4;
29:
30: public TextResource(String text, long lastModified) {
31: super (text, lastModified);
32: }
33:
34: public TextResource(InputStream in, long lastModified) {
35: super ("", lastModified);
36: String encoding = System.getProperty("file.encoding");
37: PushbackInputStream pbin = new PushbackInputStream(in, BOM_SIZE);
38: byte[] bom = new byte[BOM_SIZE];
39: int n, unread;
40: StringBuffer out = new StringBuffer();
41: try {
42: n = pbin.read(bom, 0, bom.length);
43: if ((bom[0] == (byte) 0xEF) && (bom[1] == (byte) 0xBB)
44: && (bom[2] == (byte) 0xBF)) {
45: encoding = "UTF-8";
46: unread = n - 3;
47: } else if ((bom[0] == (byte) 0xFE)
48: && (bom[1] == (byte) 0xFF)) {
49: encoding = "UTF-16BE";
50: unread = n - 2;
51: } else if ((bom[0] == (byte) 0xFF)
52: && (bom[1] == (byte) 0xFE)) {
53: encoding = "UTF-16LE";
54: unread = n - 2;
55: } else {
56: unread = n;
57: }
58: if (unread > 0)
59: pbin.unread(bom, (n - unread), unread);
60: byte[] b = new byte[4096];
61: for (; (n = pbin.read(b)) != -1;) {
62: out.append(new String(b, 0, n, encoding));
63: }
64: } catch (IOException ioe) {
65: log.error("Building resource failed.", ioe);
66: }
67: this .text = out.toString();
68: }
69:
70: public boolean isString() {
71: return false;
72: }
73:
74: public boolean isText() {
75: return true;
76: }
77: }
|