001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.debug;
020
021import java.io.IOException;
022import java.io.OutputStream;
023import java.io.Writer;
024import java.util.ArrayList;
025import java.util.List;
026
027import lucee.commons.io.SystemUtil;
028import lucee.commons.io.SystemUtil.TemplateLine;
029import lucee.commons.lang.StringUtil;
030import lucee.runtime.cache.legacy.CacheItem;
031import lucee.runtime.op.Caster;
032import lucee.runtime.writer.CFMLWriter;
033
034public class DebugCFMLWriter extends CFMLWriter implements DebugOutputLog {
035
036        private CFMLWriter writer;
037        private List<DebugTextFragment> fragments=new ArrayList<DebugTextFragment>(); 
038
039        public DebugCFMLWriter(CFMLWriter writer) {
040                super(writer.getBufferSize(), writer.isAutoFlush());
041                this.writer=writer;
042        }
043
044        @Override
045        public int getBufferSize() {
046                return writer.getBufferSize();
047        }
048
049        @Override
050        public boolean isAutoFlush() {
051                return writer.isAutoFlush();
052        }
053
054        @Override
055        public Writer append(CharSequence csq) throws IOException {
056                log(csq.toString());
057                return writer.append(csq);
058        }
059
060        @Override
061        public Writer append(char c) throws IOException {
062                log(new String(new char[]{c}));
063                return writer.append(c);
064        }
065
066        @Override
067        public Writer append(CharSequence csq, int start, int end) throws IOException {
068                log(csq.subSequence(start, end).toString());
069                return writer.append(csq, start, end);
070        }
071
072        @Override
073        public void write(int i) throws IOException {
074                print(i);
075        }
076
077        @Override
078        public void write(char[] cbuf) throws IOException {
079                print(cbuf);
080        }
081
082        @Override
083        public void write(String str) throws IOException {
084                print(str);
085        }
086
087        @Override
088        public void write(String str, int off, int len) throws IOException {
089                log(StringUtil.substring(str,off,len));
090                writer.write(str, off, len);
091        }
092
093        @Override
094        public OutputStream getResponseStream() throws IOException {
095                return writer.getResponseStream();
096        }
097
098        @Override
099        public void setClosed(boolean b) {
100                writer.setClosed(b);
101        }
102
103        @Override
104        public void setBufferConfig(int interval, boolean b) throws IOException {
105                writer.setBufferConfig(interval, b);
106        }
107
108        @Override
109        public void appendHTMLBody(String text) throws IOException {
110                writer.appendHTMLBody(text);
111        }
112
113        @Override
114        public void writeHTMLBody(String text) throws IOException {
115                writer.writeHTMLBody(text);
116        }
117
118        @Override
119        public String getHTMLBody() throws IOException {
120                return writer.getHTMLBody();
121        }
122
123        @Override
124        public void flushHTMLBody() throws IOException {
125                writer.flushHTMLBody();
126        }
127
128        @Override
129        public void resetHTMLBody() throws IOException {
130                writer.resetHTMLBody();
131        }
132
133        @Override
134        public void appendHTMLHead(String text) throws IOException {
135                writer.appendHTMLHead(text);
136        }
137
138        @Override
139        public void writeHTMLHead(String text) throws IOException {
140                writer.writeHTMLHead(text);
141        }
142
143        @Override
144        public String getHTMLHead() throws IOException {
145                return writer.getHTMLHead();
146        }
147
148        @Override
149        public void flushHTMLHead() throws IOException {
150                writer.flushHTMLHead();
151        }
152
153        @Override
154        public void resetHTMLHead() throws IOException {
155                writer.resetHTMLHead();
156        }
157
158        @Override
159        public void writeRaw(String str) throws IOException {
160                print(str);
161        }
162
163        @Override
164        public void clear() throws IOException {
165                writer.clear();
166        }
167
168        @Override
169        public void clearBuffer() throws IOException {
170                writer.clearBuffer();
171        }
172
173        @Override
174        public void close() throws IOException {
175                writer.close();
176        }
177
178        @Override
179        public void flush() throws IOException {
180                writer.flush();
181        }
182
183        @Override
184        public int getRemaining() {
185                return writer.getRemaining();
186        }
187
188        @Override
189        public void newLine() throws IOException {
190                println();
191        }
192
193        @Override
194        public void print(boolean b) throws IOException {
195                writer.print(b);
196                log(b?"true":"false");
197        }
198
199        @Override
200        public void print(char c) throws IOException {
201                log(new String(new char[]{c}));
202                writer.write(c);
203        }
204
205        @Override
206        public void print(int i) throws IOException {
207                log(Caster.toString(i));
208                writer.write(i);
209        }
210
211        @Override
212        public void print(long l) throws IOException {
213                log(Caster.toString(l));
214                writer.print(l);
215        }
216
217        @Override
218        public void print(float f) throws IOException {
219                log(Caster.toString(f));
220                writer.print(f);
221        }
222
223        @Override
224        public void print(double d) throws IOException {
225                log(Caster.toString(d));
226                writer.print(d);
227        }
228
229        @Override
230        public void print(char[] carr) throws IOException {
231                log(new String(carr));
232                writer.write(carr);
233        }
234
235        @Override
236        public void print(String str) throws IOException {
237                log(str);
238                writer.write(str);
239        }
240
241        @Override
242        public void print(Object obj) throws IOException {
243                log(String.valueOf(obj));
244                writer.print(obj);
245        }
246
247        @Override
248        public void println() throws IOException {
249                print("\n");
250        }
251
252        @Override
253        public void println(boolean b) throws IOException {
254                print(b);
255                print("\n");
256        }
257
258        @Override
259        public void println(char c) throws IOException {
260                print(c);
261                print("\n");
262        }
263
264        @Override
265        public void println(int i) throws IOException {
266                print(i);
267                print("\n");
268        }
269
270        @Override
271        public void println(long l) throws IOException {
272                print(l);
273                print("\n");
274        }
275
276        @Override
277        public void println(float f) throws IOException {
278                print(f);
279                print("\n");
280        }
281
282        @Override
283        public void println(double d) throws IOException {
284                print(d);
285                print("\n");
286        }
287
288        @Override
289        public void println(char[] carr) throws IOException {
290                print(carr);
291                print("\n");
292        }
293
294        @Override
295        public void println(String str) throws IOException {
296                print(str);
297                print("\n");
298        }
299
300        @Override
301        public void println(Object obj) throws IOException {
302                print(obj);
303                print("\n");
304        }
305
306        @Override
307        public void write(char[] carr, int off, int len) throws IOException {
308                log(StringUtil.substring(new String(carr),off,len));
309                writer.write(carr,off,len);
310        }
311
312        private void log(String str) {
313                TemplateLine tl = SystemUtil.getCurrentContext();
314                if(tl!=null){
315                        fragments.add(new DebugTextFragment(str, tl));
316                }
317        }
318
319        @Override
320        public DebugTextFragment[] getFragments() {
321                return fragments.toArray(new DebugTextFragment[fragments.size()]);
322        }
323
324        @Override
325        public void setAllowCompression(boolean allowCompression) {
326                writer.setAllowCompression(allowCompression);
327        }
328
329        @Override
330        public void doCache(CacheItem ci) {
331                writer.doCache(ci);
332        }
333
334        @Override
335        public CacheItem getCacheItem() {
336                return writer.getCacheItem();
337        }
338
339}