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.intergral.fusiondebug.server.util;
020
021import lucee.commons.lang.ExceptionUtil;
022import lucee.intergral.fusiondebug.server.type.coll.FDCollection;
023import lucee.intergral.fusiondebug.server.type.coll.FDUDF;
024import lucee.intergral.fusiondebug.server.type.nat.FDNative;
025import lucee.intergral.fusiondebug.server.type.qry.FDQuery;
026import lucee.intergral.fusiondebug.server.type.simple.FDSimpleValue;
027import lucee.runtime.converter.ScriptConverter;
028import lucee.runtime.op.Caster;
029import lucee.runtime.op.Decision;
030import lucee.runtime.type.Collection;
031import lucee.runtime.type.Query;
032import lucee.runtime.type.UDF;
033
034import com.intergral.fusiondebug.server.IFDStackFrame;
035import com.intergral.fusiondebug.server.IFDValue;
036
037public class FDCaster {
038        
039
040        public static IFDValue toFDValue(IFDStackFrame frame,String name,Object value) {
041                if(value instanceof UDF)
042                        return new FDUDF(frame,name, (UDF)value);
043                if(value instanceof Query)
044                        return new FDQuery(frame, (Query)value);
045                //if(value instanceof Array)
046                //      return new FDArray(frame,name,(Array)value);
047                if(value instanceof Collection)
048                        return new FDCollection(frame,name,(Collection)value);
049                if(Decision.isCastableToString(value))
050                        return new FDSimpleValue(null,Caster.toString(value,null));
051                return new FDNative(frame,name,value);
052        }
053        
054        public static IFDValue toFDValue(IFDStackFrame frame,Object value) {
055                return toFDValue(frame,"", value);
056        }
057
058        /**
059         * translate a object to its string representation
060         * @param object
061         * @return
062         */
063        public static String serialize(Object object) {
064                if(object==null) return "[null]";
065                try {
066                        return new ScriptConverter().serialize(object);
067                } 
068                catch (Throwable t) {
069                ExceptionUtil.rethrowIfNecessary(t);
070                        return object.toString();
071                }
072        }
073
074        public static Object unserialize(String value) {
075                // TODO
076                return value;
077        }
078}