001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: * Paul Mahar
021: *
022: */
023:
024: package org.enhydra.kelp.common;
025:
026: // Kelp imports
027: import org.enhydra.kelp.common.event.WriteEvent;
028: import org.enhydra.kelp.common.event.WriteListener;
029:
030: // Standard imports
031: import java.io.StringWriter;
032: import java.util.ArrayList;
033: import java.util.Arrays;
034:
035: /**
036: * Class declaration
037: *
038: */
039: public class Writer extends StringWriter {
040: private WriteListener[] writeListeners = new WriteListener[0];
041: private boolean buffered = false;
042: private StringBuffer lineBuffer = new StringBuffer();
043:
044: /**
045: * Constructor declaration
046: *
047: */
048: public Writer() {
049: }
050:
051: public void setLineBuffered(boolean b) {
052: buffered = b;
053: }
054:
055: public boolean isLineBuffered() {
056: return buffered;
057: }
058:
059: /**
060: * Method declaration
061: *
062: *
063: * @param c
064: */
065: public synchronized void write(int c) {
066: char[] ca = new char[1];
067:
068: ca[0] = (char) c;
069: String cs = new String(ca);
070:
071: write(cs);
072: }
073:
074: /**
075: * Method declaration
076: *
077: *
078: * @param str
079: */
080: public synchronized void write(String str) {
081:
082: // if (isLineBuffered()) {
083: // if (str != null && str.trim().length() > 0) {
084: // write(str, 0, str.length());
085: // }
086: // } else {
087: write(str, 0, str.length());
088:
089: // }
090: }
091:
092: /**
093: * Method declaration
094: *
095: *
096: * @param str
097: */
098: public synchronized void writeln(String str) {
099: write(str + '\n');
100: }
101:
102: /**
103: * Method declaration
104: *
105: *
106: * @param str
107: * @param off
108: * @param len
109: */
110: public synchronized void write(String str, int off, int len) {
111: String out = str.substring(off, (off + len));
112: boolean writeLine = false;
113:
114: if (isLineBuffered()) {
115: if (out.length() > 1) {
116: char end1 = out.charAt(out.length() - 2);
117: char end2 = out.charAt(out.length() - 1);
118:
119: if ((int) end1 == 13 && (int) end2 == 10) {
120: if (out.length() < 3) {
121: out = new String();
122: } else {
123: out = out.substring(0, out.length() - 3);
124: }
125: writeLine = true;
126: }
127: if ((out.length() > 0)
128: && (out.charAt(out.length() - 1) == '\n')) {
129: writeLine = true;
130: }
131: }
132: if (writeLine) {
133: notifyWriteListeners(lineBuffer.toString() + out);
134: lineBuffer = new StringBuffer();
135: } else {
136: lineBuffer.append(out);
137: }
138: } else {
139: notifyWriteListeners(out);
140: }
141: }
142:
143: // event methods
144:
145: /**
146: * Method declaration
147: *
148: *
149: * @param l
150: */
151: public synchronized void addWriteListener(WriteListener l) {
152: ArrayList list = null;
153: list = new ArrayList(Arrays.asList(writeListeners));
154: if (!list.contains(l)) {
155: list.add(l);
156: }
157: list.trimToSize();
158: writeListeners = new WriteListener[list.size()];
159: writeListeners = (WriteListener[]) list.toArray(writeListeners);
160: list.clear();
161: }
162:
163: /**
164: * Method declaration
165: *
166: *
167: * @param l
168: */
169: public synchronized void removeWriteListener(WriteListener l) {
170: ArrayList list = null;
171: list = new ArrayList(Arrays.asList(writeListeners));
172: if (list.contains(l)) {
173: list.remove(l);
174: }
175: list.trimToSize();
176: writeListeners = new WriteListener[list.size()];
177: writeListeners = (WriteListener[]) list.toArray(writeListeners);
178: list.clear();
179: }
180:
181: public synchronized WriteListener[] getWriteListeners() {
182: return writeListeners;
183: }
184:
185: /**
186: * Method declaration
187: *
188: *
189: * @param s
190: */
191: private synchronized void notifyWriteListeners(String s) {
192: WriteEvent event = new WriteEvent(this , WriteEvent.OUTPUT, s);
193:
194: for (int i = 0; i < writeListeners.length; i++) {
195: writeListeners[i].onWrite(event);
196: }
197: }
198:
199: }
|