001    package com.mockrunner.mock.web;
002    
003    import javax.servlet.Servlet;
004    import javax.servlet.ServletContext;
005    import javax.servlet.ServletRequest;
006    import javax.servlet.ServletResponse;
007    import javax.servlet.jsp.JspApplicationContext;
008    import javax.servlet.jsp.JspEngineInfo;
009    import javax.servlet.jsp.JspFactory;
010    import javax.servlet.jsp.PageContext;
011    
012    import org.apache.jasper.runtime.JspApplicationContextImpl;
013    
014    /**
015     * This implementation of <code>JspFactory</code>
016     * provides full support of the <b>Unified Expression Language</b>
017     * API. Expression evaluation is available only for the <b>Unified 
018     * Expression Language</b> API, not for the pre JSP 2.1
019     * <code>javax.servlet.jsp.el</code> Expression Language API.
020     */
021    public class JasperJspFactory extends JspFactory
022    {
023        private WebMockObjectFactory mockFactory;
024        private JspEngineInfo engineInfo;
025        private JspApplicationContextImpl applicationContext;
026    
027        public JasperJspFactory()
028        {
029            engineInfo = new MockJspEngineInfo();
030        }
031        
032        /**
033         * Configures this implementation for EL support.
034         * Use
035         * <code>getWebMockObjectFactory().setDefaultJspFactory(new JasperJspFactory().configure(getWebMockObjectFactory()));</code>
036         * to set this implementation as the default factory.
037         * @return this instance for convenience
038         */
039        public JasperJspFactory configure(WebMockObjectFactory mockFactory)
040        {
041            this.mockFactory = mockFactory;
042            applicationContext = JspApplicationContextImpl.getInstance(mockFactory.getMockServletContext());
043            mockFactory.getMockPageContext().setELContext(applicationContext.createELContext(mockFactory.getMockPageContext()));
044            return this;
045        }
046        
047        /**
048         * Set the <code>JspEngineInfo</code>. Per default, 
049         * {@link MockJspEngineInfo} is used.
050         * @param engineInfo the <code>JspEngineInfo</code>
051         */
052        public void setEngineInfo(JspEngineInfo engineInfo)
053        {
054            this.engineInfo = engineInfo;
055        }
056        
057        public JspEngineInfo getEngineInfo()
058        {
059            return engineInfo;
060        }
061        
062        public JspApplicationContext getJspApplicationContext(ServletContext context)
063        {
064            return applicationContext;
065        }
066    
067        public PageContext getPageContext(Servlet servlet, ServletRequest request, ServletResponse response, String errorPageURL, boolean needsSession, int buffer, boolean autoflush)
068        {
069            return mockFactory.getMockPageContext();
070        }
071    
072        public void releasePageContext(PageContext pageContext)
073        {
074    
075        }
076    }