01: /*
02: */
03:
04: /*
05: * Copyright 2000,2005 wingS development team.
06: *
07: * This file is part of wingS (http://wingsframework.org).
08: *
09: * wingS is free software; you can redistribute it and/or modify
10: * it under the terms of the GNU Lesser General Public License
11: * as published by the Free Software Foundation; either version 2.1
12: * of the License, or (at your option) any later version.
13: *
14: * Please see COPYING for the complete licence.
15: */
16: package org.wings.externalizer;
17:
18: import org.wings.io.Device;
19: import org.wings.resource.HttpHeader;
20:
21: import java.io.Reader;
22: import java.io.StringReader;
23: import java.util.Collection;
24:
25: public class TextExternalizer implements Externalizer<String> {
26:
27: private static final Class[] SUPPORTED_CLASSES = { String.class };
28:
29: protected String extension;
30: protected String mimeType;
31: protected final String[] supportedMimeTypes = new String[1];
32:
33: public TextExternalizer(String mimeType, String extension) {
34: this .mimeType = mimeType;
35: this .extension = extension;
36:
37: supportedMimeTypes[0] = mimeType;
38: }
39:
40: public TextExternalizer(String mimeType) {
41: this (mimeType, "txt");
42: }
43:
44: public TextExternalizer() {
45: this ("text/plain", "txt");
46: }
47:
48: public void setExtension(String extension) {
49: this .extension = extension;
50: }
51:
52: public String getId(String obj) {
53: return null;
54: }
55:
56: public String getExtension(String obj) {
57: return extension;
58: }
59:
60: public void setMimeType(String mimeType) {
61: this .mimeType = mimeType;
62: }
63:
64: public String getMimeType(String obj) {
65: return mimeType;
66: }
67:
68: public boolean isFinal(String obj) {
69: return true;
70: }
71:
72: public int getLength(String obj) {
73: return -1;
74: }
75:
76: public void write(Object obj, Device out)
77: throws java.io.IOException {
78: Reader reader = new StringReader((String) obj);
79: char[] buffer = new char[2048];
80: int num;
81: while ((num = reader.read(buffer)) > 0) {
82: out.print(buffer, 0, num);
83: }
84: reader.close();
85: }
86:
87: public Class[] getSupportedClasses() {
88: return SUPPORTED_CLASSES;
89: }
90:
91: public String[] getSupportedMimeTypes() {
92: return supportedMimeTypes;
93: }
94:
95: public Collection<HttpHeader> getHeaders(String obj) {
96: return null;
97: }
98: }
|