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 **/
019/**
020 * Implements the CFML Function writeoutput
021 */
022package lucee.runtime.functions.other;
023
024
025import java.io.PrintStream;
026
027import lucee.commons.lang.ExceptionUtil;
028import lucee.commons.lang.StringUtil;
029import lucee.runtime.PageContext;
030import lucee.runtime.exp.PageException;
031import lucee.runtime.ext.function.Function;
032import lucee.runtime.functions.dynamicEvaluation.Serialize;
033import lucee.runtime.op.Caster;
034import lucee.runtime.op.Decision;
035
036public final class SystemOutput implements Function {
037    public static boolean call(PageContext pc , Object obj) throws PageException {
038        return call(pc, obj, false,false);
039    }
040    public static boolean call(PageContext pc , Object obj, boolean addNewLine) throws PageException {
041        return call(pc, obj, addNewLine, false);
042    }
043    public static boolean call(PageContext pc , Object obj, boolean addNewLine,boolean doErrorStream) throws PageException {
044        String string;
045        if(Decision.isSimpleValue(obj))string=Caster.toString(obj);
046        else {
047                try{
048                        string=Serialize.call(pc, obj);
049                }
050                catch(Throwable t){
051                        ExceptionUtil.rethrowIfNecessary(t);
052                        string=obj.toString();
053                }
054        }
055        PrintStream stream = System.out;
056        //string+=":"+Thread.currentThread().getId();
057        if(doErrorStream) stream = System.err;
058        if(string!=null) {
059                if(StringUtil.indexOfIgnoreCase(string,"<print-stack-trace>")!=-1){
060                        String st = ExceptionUtil.getStacktrace(new Exception("Stack trace"), false);
061                        string=StringUtil.replace(string, "<print-stack-trace>", "\n"+st+"\n", true).trim();
062                }
063                if(StringUtil.indexOfIgnoreCase(string,"<hash-code>")!=-1){
064                        String st = obj.hashCode()+"";
065                        string=StringUtil.replace(string, "<hash-code>", st, true).trim();
066                }
067        }
068        if(addNewLine)stream.println(string);
069        else stream.print(string);
070        
071        return true;
072    }
073}