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 getmetadata
021 */
022package lucee.runtime.functions.other;
023
024import lucee.runtime.Component;
025import lucee.runtime.PageContext;
026import lucee.runtime.exp.FunctionException;
027import lucee.runtime.exp.PageException;
028import lucee.runtime.ext.function.Function;
029import lucee.runtime.img.Image;
030import lucee.runtime.java.JavaObject;
031import lucee.runtime.op.Caster;
032import lucee.runtime.type.KeyImpl;
033import lucee.runtime.type.ObjectWrap;
034import lucee.runtime.type.Query;
035import lucee.runtime.type.Struct;
036import lucee.runtime.type.StructImpl;
037import lucee.runtime.type.UDF;
038
039public final class GetMetaData implements Function {
040
041        private static final long serialVersionUID = -3787469574373656167L;
042
043        // TODO support enties more deeply
044        public static Object call(PageContext pc ) throws PageException {
045        Component ac = pc.getActiveComponent();
046        if(ac!=null) {
047                return call(pc , ac);
048            }
049            
050                return new StructImpl();
051        }
052        
053        public static Object call(PageContext pc , Object object) throws PageException {
054                return call(pc, object, false);
055        }
056        
057        public static Object call(PageContext pc , Object object,boolean source) throws PageException {
058                if(object instanceof JavaObject){
059                        return call(pc,((JavaObject)object).getClazz(),source);
060                }
061                else if(object instanceof ObjectWrap){
062                        return call(pc,((ObjectWrap)object).getEmbededObject(),source);
063                }
064                
065                if(!source){
066                        // Component
067                        if(object instanceof Component) {
068                                return getMetaData((Component)object,pc);
069                                //return ((Component)object).getMetaData(pc);
070                        }
071                        // UDF
072                        if(object instanceof UDF) {
073                                return ((UDF)object).getMetaData(pc);
074                        }
075                        // Query
076                else if(object instanceof Query) {
077                    return ((Query)object).getMetaDataSimple();
078                }
079                        // Image
080                else if(object instanceof Image) {
081                    return ((Image)object).info();
082                }
083                        if(object==null) throw new FunctionException(pc,"GetMetaData",1,"object","value is null");
084                        return object.getClass();
085                }
086                
087                String str = Caster.toString(object,null);
088                if(str==null)throw new FunctionException(pc,"GetMetaData",1,"object","must be a string when second argument is true");
089        return pc.undefinedScope().getScope(KeyImpl.init(str));
090                
091        }
092
093        public static Struct getMetaData(Component cfc, PageContext pc) throws PageException {
094                return cfc.getMetaData(pc);
095        }
096}