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;
020
021import java.io.IOException;
022import java.io.OutputStream;
023import java.io.PrintStream;
024import java.io.Reader;
025import java.lang.reflect.InvocationTargetException;
026import java.sql.ResultSet;
027import java.util.Collection;
028import java.util.Date;
029import java.util.Enumeration;
030import java.util.Iterator;
031import java.util.List;
032import java.util.ListIterator;
033import java.util.Map;
034import java.util.Set;
035
036import javax.servlet.http.Cookie;
037
038import lucee.commons.io.IOUtil;
039import lucee.commons.io.SystemUtil;
040import lucee.commons.io.res.Resource;
041import lucee.commons.io.res.ResourcesImpl;
042import lucee.commons.io.res.util.ResourceUtil;
043import lucee.runtime.exp.PageException;
044import lucee.runtime.op.Caster;
045import lucee.runtime.type.QueryImpl;
046
047import org.w3c.dom.Attr;
048import org.w3c.dom.NamedNodeMap;
049import org.w3c.dom.Node;
050import org.xml.sax.InputSource;
051
052/**
053 *  
054 */
055public class aprint {
056
057        
058
059        public static void date(String value) {
060                long millis=System.currentTimeMillis();
061        o(
062                        new Date(millis)
063                        +"-"
064                        +(millis-(millis/1000*1000))
065                        +" "+value);
066        }
067
068        public static void ds(boolean useOutStream) {
069                new Exception("Stack trace").printStackTrace(useOutStream?System.out:System.err);
070        }
071        
072        public static void ds(Object label,boolean useOutStream) {
073                _oe(useOutStream?System.out:System.err, label);
074                ds(useOutStream);
075        }
076        
077        public static void ds() {ds(false);}
078        public static void ds(Object label) {ds(label,false);}
079        public static void dumpStack() {ds(false);}
080        public static void dumpStack(boolean useOutStream) {ds(useOutStream);}
081        public static void dumpStack(String label) {ds(label,false);}
082        public static void dumpStack(String label,boolean useOutStream) {ds(label,useOutStream);}
083    
084    public static void err(boolean o) { 
085                System.err.println(o);
086        }
087    public static void err(double d) {
088                System.err.println(d);
089        }
090    public static void err(long d) {
091                System.err.println(d);
092        }
093    public static void err(float d) {
094                System.err.println(d);
095        }
096    public static void err(int d) {
097                System.err.println(d);
098        }
099    public static void err(short d) {
100                System.err.println(d);
101        }
102
103    public static void out(Object o1,Object o2,Object o3) {
104                System.out.print(o1);
105                System.out.print(o2);
106                System.out.println(o3);
107        }
108        
109        public static void out(Object o1,Object o2) {
110                System.out.print(o1);
111                System.out.println(o2);
112        }
113
114        public static void out(Object o,long l) {
115                System.out.print(o);
116                System.out.println(l);
117        }
118        
119        public static void out(Object o,double d) {
120                System.out.print(o);
121                System.out.println(d);
122        }
123    
124    
125    public static void out(byte[] arr, int offset, int len) {
126        System.out.print("byte[]{");
127        for(int i=offset;i<len+offset;i++) {
128            if(i>0)System.out.print(',');
129            System.out.print(arr[i]);
130        }
131        System.out.println("}");
132    }
133    
134    
135
136    
137
138        
139        public static void out(double o) {
140                System.out.println(o);
141        }
142        
143        public static void out(float o) {
144                System.out.println(o);
145        }
146        public static void out(long o) {
147                System.out.println(o);
148        }
149        public static void out(int o) {
150                System.out.println(o);
151        }
152        public static void out(char o) {
153                System.out.println(o);
154        }
155        public static void out(boolean o) {
156                System.out.println(o);
157        }
158        public static void out() {
159                System.out.println();
160        }
161
162        public static void printST(Throwable t) {
163                if(t instanceof InvocationTargetException){
164                        t=((InvocationTargetException)t).getTargetException();
165                }
166                err(t.getClass().getName());
167                t.printStackTrace();
168                
169        }
170        
171        public static void printST(Throwable t,PrintStream ps) {
172                if(t instanceof InvocationTargetException){
173                        t=((InvocationTargetException)t).getTargetException();
174                }
175                err(t.getClass().getName());
176                t.printStackTrace(ps);
177                
178        }
179        
180        
181
182        public static void out(Object o) {
183                _oe(System.out, o);
184        }
185        public static void err(Object o) {
186                _oe(System.err, o);
187        }
188
189        public static void writeTemp(String name,Object o, boolean addStackTrace) {
190                //write(SystemUtil.getTempDirectory().getRealResource(name+".log"), o);
191                write(SystemUtil.getHomeDirectory().getRealResource(name+".log"), o,addStackTrace);
192        }
193        public static void writeHome(String name,Object o, boolean addStackTrace) {
194                write(SystemUtil.getHomeDirectory().getRealResource(name+".log"), o,addStackTrace);
195        }
196        public static void writeCustom(String path,Object o, boolean addStackTrace) {
197                write(ResourcesImpl.getFileResourceProvider().getResource(path), o,addStackTrace);
198        }
199
200        public static void write(Resource res,Object o, boolean addStackTrace) {
201                OutputStream os=null;
202                PrintStream ps=null;
203                try{
204                        ResourceUtil.touch(res);
205                        os = res.getOutputStream(true);
206                        ps = new PrintStream(os);
207                        _oe(ps, o);
208                        if(addStackTrace) new Exception("Stack trace").printStackTrace(ps);
209                }
210                catch(IOException ioe){
211                        ioe.printStackTrace();
212                }
213                finally{
214                        IOUtil.closeEL(ps);
215                        IOUtil.closeEL(os);
216                }
217        }
218        public static void _oe(Object o, boolean d) {
219                _oe(System.out, o);
220        }
221        public static void o(Object o) {
222                _oe(System.out, o);
223        }
224        public static void e(Object o) {
225                _oe(System.err, o);
226        }
227        public static void oe(Object o, boolean valid) {
228                _oe(valid?System.out:System.err, o);
229        }
230        
231        public static void dateO(String value) {
232                _date(System.out, value);
233        }
234        
235        public static void dateE(String value) {
236                _date(System.err, value);
237        }
238
239        private static void _date(PrintStream ps,String value) {
240                long millis = System.currentTimeMillis();
241                _oe(ps,
242                new Date(millis)
243                +"-"
244                +(millis-(millis/1000*1000))
245                +" "+value);
246        }
247        
248        
249        
250        private static void _oe(PrintStream ps,Object o) {
251                if(o instanceof Enumeration) _oe(ps,(Enumeration)o);
252        else if(o instanceof Object[]) _oe(ps,(Object[])o);
253        else if(o instanceof boolean[]) _oe(ps,(boolean[])o);
254        else if(o instanceof byte[]) _oe(ps,(byte[])o);
255        else if(o instanceof int[]) _oe(ps,(int[])o);
256        else if(o instanceof float[]) _oe(ps,(float[])o);
257        else if(o instanceof long[]) _oe(ps,(long[])o);
258        else if(o instanceof double[]) _oe(ps,(double[])o);
259        else if(o instanceof char[]) _oe(ps,(char[])o);
260        else if(o instanceof short[]) _oe(ps,(short[])o);
261        else if(o instanceof Set) _oe(ps,(Set)o);
262        else if(o instanceof List) _oe(ps,(List)o);
263        else if(o instanceof Map) _oe(ps,(Map)o);
264        else if(o instanceof Collection) _oe(ps,(Collection)o);
265        else if(o instanceof Iterator) _oe(ps,(Iterator)o);
266        else if(o instanceof NamedNodeMap) _oe(ps,(NamedNodeMap)o);
267        else if(o instanceof ResultSet) _oe(ps,(ResultSet)o);
268        else if(o instanceof Node) _oe(ps,(Node)o);
269        else if(o instanceof Throwable) _oe(ps,(Throwable)o);
270        else if(o instanceof Cookie) {
271                Cookie c=(Cookie) o;
272                ps.println("Cookie(name:"+c.getName()+";domain:"+c.getDomain()+";maxage:"+c.getMaxAge()+";path:"+c.getPath()+";value:"+c.getValue()+";version:"+c.getVersion()+";secure:"+c.getSecure()+")");
273        }
274        else if(o instanceof InputSource) {
275                InputSource is=(InputSource) o;
276                Reader r = is.getCharacterStream();
277                try {
278                                ps.println(IOUtil.toString(is.getCharacterStream()));
279                        } catch (IOException e) {}
280                        finally {
281                                IOUtil.closeEL(r);
282                        }
283        }
284        
285        else ps.println(o);
286    }
287        
288        
289        
290        
291        private static void _oe(PrintStream ps,Object[] arr) {
292        if(arr==null){
293                ps.println("null");
294                return;
295        }
296        ps.print(arr.getClass().getComponentType().getName()+"[]{");
297        for(int i=0;i<arr.length;i++) {
298            if(i>0) {
299                ps.print("\t,");
300            }
301            _oe(ps,arr[i]);
302        }
303        ps.println("}");
304    }
305    
306    private static void _oe(PrintStream ps,int[] arr) {
307        ps.print("int[]{");
308        for(int i=0;i<arr.length;i++) {
309            if(i>0)ps.print(',');
310            ps.print(arr[i]);
311        }
312        ps.println("}");
313    }
314    
315    private static void _oe(PrintStream ps,byte[] arr) {
316        ps.print("byte[]{");
317        for(int i=0;i<arr.length;i++) {
318            if(i>0)ps.print(',');
319            ps.print(arr[i]);
320        }
321        ps.println("}");
322    }
323    
324    private static void _oe(PrintStream ps,boolean[] arr) {
325        ps.print("boolean[]{");
326        for(int i=0;i<arr.length;i++) {
327            if(i>0)ps.print(',');
328            ps.print(arr[i]);
329        }
330        ps.println("}");
331    }
332    
333    private static void _oe(PrintStream ps,char[] arr) {
334        ps.print("char[]{");
335        for(int i=0;i<arr.length;i++) {
336            if(i>0)ps.print(',');
337            ps.print(arr[i]);
338        }
339        ps.println("}");
340    }
341    
342    private static void _oe(PrintStream ps,short[] arr) {
343        ps.print("short[]{");
344        for(int i=0;i<arr.length;i++) {
345            if(i>0)ps.print(',');
346            ps.print(arr[i]);
347        }
348        ps.println("}");
349    }
350    
351    private static void _oe(PrintStream ps,float[] arr) {
352        ps.print("float[]{");
353        for(int i=0;i<arr.length;i++) {
354            if(i>0)ps.print(',');
355            ps.print(arr[i]);
356        }
357        ps.println("}");
358    }
359    
360    private static void _oe(PrintStream ps,long[] arr) {
361        ps.print("long[]{");
362        for(int i=0;i<arr.length;i++) {
363            if(i>0)ps.print(',');
364            ps.print(arr[i]);
365        }
366        ps.println("}");
367    }
368    
369    private static void _oe(PrintStream ps,double[] arr) {
370        ps.print("double[]{");
371        for(int i=0;i<arr.length;i++) {
372            if(i>0)ps.print(',');
373            ps.print(arr[i]);
374        }
375        ps.println("}");
376    }
377    
378
379        private static void _oe(PrintStream ps,Node n) {
380                ps.print(Caster.toString(n,null));
381        }
382        
383        
384        private static void _oe(PrintStream ps,Throwable t) {
385        t.printStackTrace(ps);
386    }
387    
388
389    private static void _oe(PrintStream ps,Enumeration en) {
390        
391        _oe(ps,en.getClass().getName()+" [");
392        while(en.hasMoreElements()) {
393                ps.print(en.nextElement());
394            ps.println(",");
395        }
396        _oe(ps,"]");
397    }
398    
399    private static void _oe(PrintStream ps,List list) {
400        ListIterator it = list.listIterator();
401        _oe(ps,list.getClass().getName()+" {");
402        while(it.hasNext()) {
403            int index = it.nextIndex();
404            it.next();
405            ps.print(index);
406            ps.print(":");
407            ps.print(list.get(index));
408            ps.println(";");
409        }
410        _oe(ps,"}");
411    }
412    
413    private static void _oe(PrintStream ps,Collection coll) {
414        Iterator it = coll.iterator();
415        _oe(ps,coll.getClass().getName()+" {");
416        while(it.hasNext()) {
417                _oe(ps, it.next());
418        }
419        _oe(ps,"}");
420    }
421    
422    private static void _oe(PrintStream ps,Iterator it) {
423        
424        _oe(ps,it.getClass().getName()+" {");
425        while(it.hasNext()) {
426            ps.print(it.next());
427            ps.println(";");
428        }
429        _oe(ps,"}");
430    }
431    
432    
433    private static void _oe(PrintStream ps,Set set) {
434        Iterator it = set.iterator();
435        ps.println(set.getClass().getName()+" {");
436        boolean first=true;
437        while(it.hasNext()) {
438                if(!first){
439                        ps.println();
440                        ps.print(",");
441                }
442            _oe(ps,it.next());
443            first=false;
444        }
445        _oe(ps,"}");
446    }
447    
448    private static void _oe(PrintStream ps,ResultSet res) {
449        try {
450                        _oe(ps, new QueryImpl(res,"query",null).toString());
451                } catch (PageException e) {
452                        _oe(ps, res.toString());
453                }
454    }
455
456    private static void _oe(PrintStream ps,Map map) {
457        if(map==null) {
458                ps.println("null");
459                return;
460        }
461        Iterator it = map.keySet().iterator();
462        
463        if(map.size()<2) {
464                ps.print(map.getClass().getName()+" {");
465            while(it.hasNext()) {
466                Object key = it.next();
467
468                _oe(ps,key);
469                ps.print(":");
470                _oe(ps,map.get(key));
471            }
472            ps.println("}");
473        } 
474        else {
475                ps.println(map.getClass().getName()+" {");
476                while(it.hasNext()) {
477                    Object key = it.next();
478                    ps.print("  ");
479                    _oe(ps,key);
480                    ps.print(":");
481                    _oe(ps,map.get(key));
482                    ps.println(";");
483                }
484                ps.println("}");
485        }
486    }
487
488    private static void _oe(PrintStream ps,NamedNodeMap map) {
489        if(map==null) {
490                ps.println("null");
491                return;
492        }
493        int len = map.getLength();
494        ps.print(map.getClass().getName()+" {");
495        Attr attr;
496        for(int i=0;i<len;i++) {
497                attr=(Attr)map.item(i);
498
499                ps.print(attr.getName());
500                ps.print(":");
501                ps.print(attr.getValue());
502            ps.println(";");
503        }
504        ps.println("}");
505    }
506    
507    
508
509}