1 16 package org.apache.commons.jxpath.servlet; 17 18 import javax.servlet.ServletContext ; 19 import javax.servlet.ServletRequest ; 20 import javax.servlet.http.HttpServletRequest ; 21 import javax.servlet.http.HttpSession ; 22 import javax.servlet.jsp.PageContext ; 23 import org.apache.commons.jxpath.JXPathContext; 24 import org.apache.commons.jxpath.JXPathContextFactory; 25 import org.apache.commons.jxpath.JXPathIntrospector; 26 27 66 public final class JXPathServletContexts { 67 68 private static JXPathContextFactory factory; 69 70 static { 71 JXPathIntrospector.registerDynamicClass( 72 PageScopeContext.class, 73 PageScopeContextHandler.class); 74 JXPathIntrospector.registerDynamicClass( 75 PageContext .class, 76 PageContextHandler.class); 77 JXPathIntrospector.registerDynamicClass( 78 ServletContext .class, 79 ServletContextHandler.class); 80 JXPathIntrospector.registerDynamicClass( 81 ServletRequestAndContext.class, 82 ServletRequestHandler.class); 83 JXPathIntrospector.registerDynamicClass( 84 HttpSessionAndServletContext.class, 85 HttpSessionHandler.class); 86 factory = JXPathContextFactory.newInstance(); 87 } 88 89 93 public static JXPathContext getPageContext(PageContext pageContext) { 94 JXPathContext context = 95 (JXPathContext) pageContext.getAttribute(Constants.JXPATH_CONTEXT); 96 if (context == null) { 97 JXPathContext parentContext = 98 getRequestContext( 99 pageContext.getRequest(), 100 pageContext.getServletContext()); 101 context = factory.newContext(parentContext, pageContext); 102 context.setVariables( 103 new KeywordVariables( 104 Constants.PAGE_SCOPE, 105 new PageScopeContext(pageContext))); 106 pageContext.setAttribute(Constants.JXPATH_CONTEXT, context); 107 } 108 return context; 109 } 110 111 115 public static JXPathContext getRequestContext( 116 ServletRequest request, 117 ServletContext servletContext) 118 { 119 JXPathContext context = 120 (JXPathContext) request.getAttribute(Constants.JXPATH_CONTEXT); 121 if (context != null) { 125 ServletRequestAndContext handle = 126 (ServletRequestAndContext) context.getContextBean(); 127 if (handle.getServletRequest() == request) { 128 return context; 129 } 130 } 131 132 JXPathContext parentContext = null; 133 if (request instanceof HttpServletRequest ) { 134 HttpSession session = 135 ((HttpServletRequest ) request).getSession(false); 136 if (session != null) { 137 parentContext = getSessionContext(session, servletContext); 138 } 139 else { 140 parentContext = getApplicationContext(servletContext); 141 } 142 } 143 ServletRequestAndContext handle = 144 new ServletRequestAndContext(request, servletContext); 145 context = factory.newContext(parentContext, handle); 146 context.setVariables( 147 new KeywordVariables(Constants.REQUEST_SCOPE, handle)); 148 request.setAttribute(Constants.JXPATH_CONTEXT, context); 149 return context; 150 } 151 152 156 public static JXPathContext getSessionContext( 157 HttpSession session, 158 ServletContext servletContext) 159 { 160 JXPathContext context = 161 (JXPathContext) session.getAttribute(Constants.JXPATH_CONTEXT); 162 if (context == null) { 163 JXPathContext parentContext = getApplicationContext(servletContext); 164 HttpSessionAndServletContext handle = 165 new HttpSessionAndServletContext(session, servletContext); 166 context = factory.newContext(parentContext, handle); 167 context.setVariables( 168 new KeywordVariables(Constants.SESSION_SCOPE, handle)); 169 session.setAttribute(Constants.JXPATH_CONTEXT, context); 170 } 171 return context; 172 } 173 174 178 public static JXPathContext getApplicationContext( 179 ServletContext servletContext) 180 { 181 JXPathContext context = 182 (JXPathContext) servletContext.getAttribute( 183 Constants.JXPATH_CONTEXT); 184 if (context == null) { 185 context = factory.newContext(null, servletContext); 186 context.setVariables( 187 new KeywordVariables( 188 Constants.APPLICATION_SCOPE, 189 servletContext)); 190 servletContext.setAttribute(Constants.JXPATH_CONTEXT, context); 191 } 192 return context; 193 } 194 } 195 | Popular Tags |