001    package railo.runtime.debug;
002    
003    import java.io.IOException;
004    import java.io.OutputStream;
005    import java.io.Writer;
006    import java.util.ArrayList;
007    import java.util.List;
008    
009    import railo.commons.io.res.util.ResourceUtil;
010    import railo.commons.lang.StringUtil;
011    import railo.runtime.cache.legacy.CacheItem;
012    import railo.runtime.op.Caster;
013    import railo.runtime.writer.CFMLWriter;
014    
015    public class DebugCFMLWriter extends CFMLWriter implements DebugOutputLog {
016    
017            private CFMLWriter writer;
018            private List<DebugTextFragment> fragments=new ArrayList<DebugTextFragment>(); 
019    
020            public DebugCFMLWriter(CFMLWriter writer) {
021                    super(writer.getBufferSize(), writer.isAutoFlush());
022                    this.writer=writer;
023            }
024    
025            @Override
026            public int getBufferSize() {
027                    return writer.getBufferSize();
028            }
029    
030            @Override
031            public boolean isAutoFlush() {
032                    return writer.isAutoFlush();
033            }
034    
035            @Override
036            public Writer append(CharSequence csq) throws IOException {
037                    log(csq.toString());
038                    return writer.append(csq);
039            }
040    
041            @Override
042            public Writer append(char c) throws IOException {
043                    log(new String(new char[]{c}));
044                    return writer.append(c);
045            }
046    
047            @Override
048            public Writer append(CharSequence csq, int start, int end) throws IOException {
049                    log(csq.subSequence(start, end).toString());
050                    return writer.append(csq, start, end);
051            }
052    
053            @Override
054            public void write(int i) throws IOException {
055                    print(i);
056            }
057    
058            @Override
059            public void write(char[] cbuf) throws IOException {
060                    print(cbuf);
061            }
062    
063            @Override
064            public void write(String str) throws IOException {
065                    print(str);
066            }
067    
068            @Override
069            public void write(String str, int off, int len) throws IOException {
070                    log(StringUtil.substring(str,off,len));
071                    writer.write(str, off, len);
072            }
073    
074            @Override
075            public OutputStream getResponseStream() throws IOException {
076                    return writer.getResponseStream();
077            }
078    
079            @Override
080            public void setClosed(boolean b) {
081                    writer.setClosed(b);
082            }
083    
084            @Override
085            public void setBufferConfig(int interval, boolean b) throws IOException {
086                    writer.setBufferConfig(interval, b);
087            }
088    
089            @Override
090            public void appendHTMLHead(String text) throws IOException {
091                    writer.appendHTMLHead(text);
092            }
093    
094            @Override
095            public void writeHTMLHead(String text) throws IOException {
096                    writer.writeHTMLHead(text);
097            }
098    
099            @Override
100            public String getHTMLHead() throws IOException {
101                    return writer.getHTMLHead();
102            }
103    
104            @Override
105            public void resetHTMLHead() throws IOException {
106                    writer.resetHTMLHead();
107            }
108    
109            @Override
110            public void writeRaw(String str) throws IOException {
111                    print(str);
112            }
113    
114            @Override
115            public void clear() throws IOException {
116                    writer.clear();
117            }
118    
119            @Override
120            public void clearBuffer() throws IOException {
121                    writer.clearBuffer();
122            }
123    
124            @Override
125            public void close() throws IOException {
126                    writer.close();
127            }
128    
129            @Override
130            public void flush() throws IOException {
131                    writer.flush();
132            }
133    
134            @Override
135            public int getRemaining() {
136                    return writer.getRemaining();
137            }
138    
139            @Override
140            public void newLine() throws IOException {
141                    println();
142            }
143    
144            @Override
145            public void print(boolean b) throws IOException {
146                    writer.print(b);
147                    log(b?"true":"false");
148            }
149    
150            @Override
151            public void print(char c) throws IOException {
152                    log(new String(new char[]{c}));
153                    writer.write(c);
154            }
155    
156            @Override
157            public void print(int i) throws IOException {
158                    log(Caster.toString(i));
159                    writer.write(i);
160            }
161    
162            @Override
163            public void print(long l) throws IOException {
164                    log(Caster.toString(l));
165                    writer.print(l);
166            }
167    
168            @Override
169            public void print(float f) throws IOException {
170                    log(Caster.toString(f));
171                    writer.print(f);
172            }
173    
174            @Override
175            public void print(double d) throws IOException {
176                    log(Caster.toString(d));
177                    writer.print(d);
178            }
179    
180            @Override
181            public void print(char[] carr) throws IOException {
182                    log(new String(carr));
183                    writer.write(carr);
184            }
185    
186            @Override
187            public void print(String str) throws IOException {
188                    log(str);
189                    writer.write(str);
190            }
191    
192            @Override
193            public void print(Object obj) throws IOException {
194                    log(String.valueOf(obj));
195                    writer.print(obj);
196            }
197    
198            @Override
199            public void println() throws IOException {
200                    print("\n");
201            }
202    
203            @Override
204            public void println(boolean b) throws IOException {
205                    print(b);
206                    print("\n");
207            }
208    
209            @Override
210            public void println(char c) throws IOException {
211                    print(c);
212                    print("\n");
213            }
214    
215            @Override
216            public void println(int i) throws IOException {
217                    print(i);
218                    print("\n");
219            }
220    
221            @Override
222            public void println(long l) throws IOException {
223                    print(l);
224                    print("\n");
225            }
226    
227            @Override
228            public void println(float f) throws IOException {
229                    print(f);
230                    print("\n");
231            }
232    
233            @Override
234            public void println(double d) throws IOException {
235                    print(d);
236                    print("\n");
237            }
238    
239            @Override
240            public void println(char[] carr) throws IOException {
241                    print(carr);
242                    print("\n");
243            }
244    
245            @Override
246            public void println(String str) throws IOException {
247                    print(str);
248                    print("\n");
249            }
250    
251            @Override
252            public void println(Object obj) throws IOException {
253                    print(obj);
254                    print("\n");
255            }
256    
257            @Override
258            public void write(char[] carr, int off, int len) throws IOException {
259                    log(StringUtil.substring(new String(carr),off,len));
260                    writer.write(carr,off,len);
261            }
262    
263            private void log(String str) {
264                    StackTraceElement[] traces = new Throwable().getStackTrace();
265                    StackTraceElement trace;
266                    String template;
267                    int line;
268                    for(int i=0;i<traces.length;i++){
269                            trace=traces[i];
270                            template = trace.getFileName();
271                            line=trace.getLineNumber();
272                            if(line<=0 || template==null || ResourceUtil.getExtension(template,"").equals("java")) continue;
273                            fragments.add(new DebugTextFragment(str, template, line));
274                            break;
275                    }
276            }
277    
278            @Override
279            public DebugTextFragment[] getFragments() {
280                    return fragments.toArray(new DebugTextFragment[fragments.size()]);
281            }
282    
283            @Override
284            public void setAllowCompression(boolean allowCompression) {
285                    writer.setAllowCompression(allowCompression);
286            }
287    
288            @Override
289            public void doCache(CacheItem ci) {
290                    writer.doCache(ci);
291            }
292    
293            @Override
294            public CacheItem getCacheItem() {
295                    return writer.getCacheItem();
296            }
297    
298    }