001    package railo.runtime.functions.system;
002    
003    import railo.commons.io.res.util.ResourceUtil;
004    import railo.runtime.PageContext;
005    import railo.runtime.ext.function.Function;
006    import railo.runtime.type.Array;
007    import railo.runtime.type.ArrayImpl;
008    import railo.runtime.type.Struct;
009    import railo.runtime.type.StructImpl;
010    
011    /**
012     * returns the root of this actuell Page Context
013     */
014    public final class GetCurrentContext implements Function {
015            
016            public static Array call(PageContext pc) {
017                    Array arr=new ArrayImpl();
018                    _getTagContext(pc, arr, new Exception("Stack trace"));
019                    return arr;
020            }
021            
022            public static void _getTagContext(PageContext pc, Array tagContext, Throwable t) {
023                    //Throwable root = t.getRootCause();
024                    Throwable cause = t.getCause(); 
025                    if(cause!=null)_getTagContext(pc, tagContext, cause);
026                    StackTraceElement[] traces = t.getStackTrace();
027                    
028            int line=0;
029                    String template;
030                    Struct item;
031                    StackTraceElement trace=null;
032                    for(int i=0;i<traces.length;i++) {
033                            trace=traces[i];
034                            template=trace.getFileName();
035                            if(trace.getLineNumber()<=0 || template==null || ResourceUtil.getExtension(template,"").equals("java")) continue;
036                            
037                            item=new StructImpl();
038                            line=trace.getLineNumber();
039                            item.setEL("template",template);
040                            item.setEL("line",new Double(line));
041                            tagContext.appendEL(item);
042                    }
043            }
044    }