KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > tag > common > xml > JSTLXPathVariableResolver


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  *
21  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
22  *
23  * Portions Copyright Apache Software Foundation.
24  */

25 package org.apache.taglibs.standard.tag.common.xml;
26
27 import javax.xml.xpath.XPathVariableResolver JavaDoc;
28 import javax.xml.namespace.QName JavaDoc;
29
30 import javax.servlet.http.Cookie JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.jsp.PageContext JavaDoc;
33
34
35 /**
36   * The XPathVariableResolver implementation provides access to user
37   * XPath variables.
38   */

39 public class JSTLXPathVariableResolver implements XPathVariableResolver JavaDoc {
40
41     //*********************************************************************
42
// Support for XPath evaluation
43

44     private PageContext JavaDoc pageContext;
45
46     // The URLs
47
private static final String JavaDoc PAGE_NS_URL =
48             "http://java.sun.com/jstl/xpath/page";
49     private static final String JavaDoc REQUEST_NS_URL =
50             "http://java.sun.com/jstl/xpath/request";
51     private static final String JavaDoc SESSION_NS_URL =
52             "http://java.sun.com/jstl/xpath/session";
53     private static final String JavaDoc APP_NS_URL =
54             "http://java.sun.com/jstl/xpath/app";
55     private static final String JavaDoc PARAM_NS_URL =
56             "http://java.sun.com/jstl/xpath/param";
57     private static final String JavaDoc INITPARAM_NS_URL =
58             "http://java.sun.com/jstl/xpath/initParam";
59     private static final String JavaDoc COOKIE_NS_URL =
60             "http://java.sun.com/jstl/xpath/cookie";
61     private static final String JavaDoc HEADER_NS_URL =
62             "http://java.sun.com/jstl/xpath/header";
63     
64     //*********************************************************************
65
// Constructor
66

67     public JSTLXPathVariableResolver(PageContext JavaDoc pc) {
68         pageContext = pc;
69     }
70
71     /**
72      * Find variable in set of variables
73      *
74      * @param QName variable name
75      *
76      * @return QName variables value
77      *
78      * @throws NullPointerException if variable name is null
79      */

80     public Object JavaDoc resolveVariable(QName JavaDoc qname) throws NullPointerException JavaDoc {
81
82         Object JavaDoc varObject = null;
83
84         if (qname == null) {
85             throw new NullPointerException JavaDoc("Cannot resolve null variable");
86         }
87
88         String JavaDoc namespace = qname.getNamespaceURI();
89         String JavaDoc prefix = qname.getPrefix();
90         String JavaDoc localName = qname.getLocalPart();
91         // p("[resolveVariable] namespace: " + namespace + " prefix: " + prefix + " localName: " + localName);
92

93         try {
94             varObject = getVariableValue(namespace, prefix, localName);
95         } catch (UnresolvableException ue) {
96             System.out.println("JSTLXpathVariableResolver.resolveVariable threw UnresolvableException: " + ue);
97         }
98
99         // p("[resolveVariable] varObject: " + varObject);
100
return varObject;
101     }
102
103     /**
104      * Retrieve an XPath's variable value using JSTL's custom
105      * variable-mapping rules
106      */

107     protected Object JavaDoc getVariableValue(String JavaDoc namespace,
108                                       String JavaDoc prefix,
109                                       String JavaDoc localName)
110                                       throws UnresolvableException {
111         // p("resolving: ns=" + namespace + " prefix=" + prefix + " localName=" + localName);
112
// We can match on namespace with Xalan but leaving as is
113
// [ I 'd prefer to match on namespace, but this doesn't appear
114
// to work in Jaxen]
115
if (namespace == null || namespace.equals("")) {
116             return notNull(
117             pageContext.findAttribute(localName),
118             namespace,
119             localName);
120         } else if (namespace.equals(PAGE_NS_URL)) {
121             return notNull(
122             pageContext.getAttribute(localName,PageContext.PAGE_SCOPE),
123             namespace,
124             localName);
125         } else if (namespace.equals(REQUEST_NS_URL)) {
126             return notNull(
127             pageContext.getAttribute(localName,
128             PageContext.REQUEST_SCOPE),
129             namespace,
130             localName);
131         } else if (namespace.equals(SESSION_NS_URL)) {
132             return notNull(
133             pageContext.getAttribute(localName,
134             PageContext.SESSION_SCOPE),
135             namespace,
136             localName);
137         } else if (namespace.equals(APP_NS_URL)) {
138             return notNull(
139             pageContext.getAttribute(localName,
140             PageContext.APPLICATION_SCOPE),
141             namespace,
142             localName);
143         } else if (namespace.equals(PARAM_NS_URL)) {
144             return notNull(
145             pageContext.getRequest().getParameter(localName),
146             namespace,
147             localName);
148         } else if (namespace.equals(INITPARAM_NS_URL)) {
149             return notNull(
150             pageContext.getServletContext().
151             getInitParameter(localName),
152             namespace,
153             localName);
154         } else if (namespace.equals(HEADER_NS_URL)) {
155             HttpServletRequest JavaDoc hsr =
156             (HttpServletRequest JavaDoc) pageContext.getRequest();
157             return notNull(
158             hsr.getHeader(localName),
159             namespace,
160             localName);
161         } else if (namespace.equals(COOKIE_NS_URL)) {
162             HttpServletRequest JavaDoc hsr =
163             (HttpServletRequest JavaDoc) pageContext.getRequest();
164             Cookie JavaDoc[] c = hsr.getCookies();
165             for (int i = 0; i < c.length; i++)
166                 if (c[i].getName().equals(localName))
167                     return c[i].getValue();
168             throw new UnresolvableException("$" + namespace + ":" + localName);
169         } else {
170             throw new UnresolvableException("$" + namespace + ":" + localName);
171         }
172     }
173
174     /**
175      * Validate that the Object returned is not null. If it is
176      * null, throw an exception.
177      */

178     private Object JavaDoc notNull(Object JavaDoc o, String JavaDoc namespace, String JavaDoc localName)
179         throws UnresolvableException {
180         if (o == null) {
181             throw new UnresolvableException("$" + (namespace==null?"":namespace+":") + localName);
182         }
183         // p("resolved to: " + o);
184
return o;
185     }
186     
187     //*********************************************************************
188
// Utility methods
189

190     private static void p(String JavaDoc s) {
191         System.out.println("[JSTLXPathVariableResolver] " + s);
192     }
193
194 }
195
Popular Tags