001    /**
002     * Implements the Cold Fusion Function getmetadata
003     */
004    package railo.runtime.functions.other;
005    
006    import railo.runtime.Component;
007    import railo.runtime.ComponentPro;
008    import railo.runtime.PageContext;
009    import railo.runtime.exp.FunctionException;
010    import railo.runtime.exp.PageException;
011    import railo.runtime.ext.function.Function;
012    import railo.runtime.img.Image;
013    import railo.runtime.java.JavaObject;
014    import railo.runtime.op.Caster;
015    import railo.runtime.type.ObjectWrap;
016    import railo.runtime.type.Query;
017    import railo.runtime.type.Struct;
018    import railo.runtime.type.StructImpl;
019    import railo.runtime.type.UDF;
020    import railo.runtime.type.scope.UndefinedImpl;
021    
022    public final class GetMetaData implements Function {
023            // TODO support enties more deeply
024            public static Object call(PageContext pc ) throws PageException {
025            Component ac = pc.getActiveComponent();
026            if(ac!=null) {
027                    return call(pc , ac);
028                }
029                
030                    return new StructImpl();
031            }
032            
033            public static Object call(PageContext pc , Object object) throws PageException {
034                    return call(pc, object, false);
035            }
036            
037            public static Object call(PageContext pc , Object object,boolean source) throws PageException {
038                    if(object instanceof JavaObject){
039                            return call(pc,((JavaObject)object).getClazz(),source);
040                    }
041                    else if(object instanceof ObjectWrap){
042                            return call(pc,((ObjectWrap)object).getEmbededObject(),source);
043                    }
044                    
045                    if(!source){
046                            // Component
047                            if(object instanceof Component) {
048                                    return getMetaData((ComponentPro)object,pc);
049                                    //return ((Component)object).getMetaData(pc);
050                            }
051                            // UDF
052                            if(object instanceof UDF) {
053                                    return ((UDF)object).getMetaData(pc);
054                            }
055                            // Query
056                    else if(object instanceof Query) {
057                        return ((Query)object).getMetaDataSimple();
058                    }
059                            // Image
060                    else if(object instanceof Image) {
061                        return ((Image)object).info();
062                    }
063                            
064                            return object.getClass();
065                    }
066                    String str = Caster.toString(object,null);
067                    if(str==null)throw new FunctionException(pc,"GetMetaData",1,"object","must be a string when second argument is true");
068            return ((UndefinedImpl)pc.undefinedScope()).getScope(str);
069                    
070            }
071    
072            public static Struct getMetaData(ComponentPro cfc, PageContext pc) throws PageException {
073                    return cfc.getMetaData(pc);
074            }
075    
076            /*private static Map<String,Struct> datas=new ReferenceMap(ReferenceMap.SOFT,ReferenceMap.SOFT);
077            
078            public static Struct getMetaData(ComponentPro cfc,PageContext pc) throws PageException {
079                    String key=createKey(cfc.getPageSource());
080                    Struct meta = key==null?null:datas.get(key);
081                    if(meta==null){
082                            meta=cfc.getMetaData(pc);
083                            datas.put(key, meta);
084                    }
085                    return meta;
086            }
087            public static String createKey(PageSource ps) throws PageException {
088                    Page p = ((PageSourceImpl)ps).getPage();
089                    if(p==null) return null;
090                    String key=ps.getMapping().getConfig().getId()+":"+p.getSourceLastModified()+":"+ps.getDisplayPath();
091                    return key;
092            }
093    
094            public static Struct getMetaData(PageSource ps) throws PageException {
095                    String key=createKey(ps);
096                    return key==null?null:datas.get(key);
097            }*/
098    }