KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > common > Writer


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  * Paul Mahar
21  *
22  */

23
24 package org.enhydra.kelp.common;
25
26 // Kelp imports
27
import org.enhydra.kelp.common.event.WriteEvent;
28 import org.enhydra.kelp.common.event.WriteListener;
29
30 // Standard imports
31
import java.io.StringWriter JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Arrays JavaDoc;
34
35
36
37 /**
38  * Class declaration
39  *
40  */

41 public class Writer extends StringWriter JavaDoc {
42     private WriteListener[] writeListeners = new WriteListener[0];
43     private boolean buffered = false;
44     private boolean newLine = false;
45     private StringBuffer JavaDoc lineBuffer = new StringBuffer JavaDoc();
46
47     /**
48      * Constructor declaration
49      *
50      */

51     public Writer() {}
52
53     public void setLineBuffered(boolean b) {
54         buffered = b;
55     }
56
57     public boolean isLineBuffered() {
58         return buffered;
59     }
60
61     /**
62      * Method declaration
63      *
64      *
65      * @param c
66      */

67     public synchronized void write(int c) {
68         char[] ca = new char[1];
69
70         ca[0] = (char) c;
71         String JavaDoc cs = new String JavaDoc(ca);
72
73         write(cs);
74     }
75
76     /**
77      * Method declaration
78      *
79      *
80      * @param str
81      */

82     public synchronized void write(String JavaDoc str) {
83
84         // if (isLineBuffered()) {
85
// if (str != null && str.trim().length() > 0) {
86
// write(str, 0, str.length());
87
// }
88
// } else {
89
write(str, 0, str.length());
90
91         // }
92
}
93
94     /**
95      * Method declaration
96      *
97      *
98      * @param str
99      */

100     public synchronized void writeln(String JavaDoc str) {
101         newLine = true;
102         write(str + '\n');
103         newLine = false;
104     }
105
106     /**
107      * Method declaration
108      *
109      *
110      * @param str
111      * @param off
112      * @param len
113      */

114     public synchronized void write(String JavaDoc str, int off, int len) {
115         String JavaDoc out = str.substring(off, (off + len));
116         boolean writeLine = false;
117
118         if (isLineBuffered()) {
119             if (out.length() > 1) {
120                 char end1 = out.charAt(out.length() - 2);
121                 char end2 = out.charAt(out.length() - 1);
122
123                 if ((int) end1 == 13 && (int) end2 == 10) {
124                     if (out.length() < 3) {
125                         out = new String JavaDoc();
126                     } else {
127                         out = out.substring(0, out.length() - 3);
128                     }
129                     writeLine = true;
130                 }
131                 if ((out.length() > 0)
132                         && (out.charAt(out.length() - 1) == '\n')) {
133                     writeLine = true;
134                 }
135             }
136             if (writeLine) {
137                 notifyWriteListeners(lineBuffer.toString() + out);
138                 lineBuffer = new StringBuffer JavaDoc();
139             } else {
140                 lineBuffer.append(out);
141             }
142         } else {
143             notifyWriteListeners(out);
144         }
145     }
146
147     // event methods
148

149     /**
150      * Method declaration
151      *
152      *
153      * @param l
154      */

155     public synchronized void addWriteListener(WriteListener l) {
156         ArrayList JavaDoc list = null;
157         list = new ArrayList JavaDoc(Arrays.asList(writeListeners));
158         if (! list.contains(l)) {
159             list.add(l);
160         }
161         list.trimToSize();
162         writeListeners = new WriteListener[list.size()];
163         writeListeners = (WriteListener[]) list.toArray(writeListeners);
164         list.clear();
165     }
166
167     /**
168      * Method declaration
169      *
170      *
171      * @param l
172      */

173     public synchronized void removeWriteListener(WriteListener l) {
174         ArrayList JavaDoc list = null;
175         list = new ArrayList JavaDoc(Arrays.asList(writeListeners));
176         if (list.contains(l)) {
177             list.remove(l);
178         }
179         list.trimToSize();
180         writeListeners = new WriteListener[list.size()];
181         writeListeners = (WriteListener[]) list.toArray(writeListeners);
182         list.clear();
183     }
184
185     public synchronized WriteListener[] getWriteListeners() {
186         return writeListeners;
187     }
188
189     /**
190      * Method declaration
191      *
192      *
193      * @param s
194      */

195     private synchronized void notifyWriteListeners(String JavaDoc s) {
196         WriteEvent event = new WriteEvent(this, WriteEvent.OUTPUT, s);
197
198         for (int i = 0; i < writeListeners.length; i++) {
199             writeListeners[i].onWrite(event);
200         }
201     }
202
203 }
204
Popular Tags