Line 0
Link Here
|
|
|
1 |
/* |
2 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
3 |
* contributor license agreements. See the NOTICE file distributed with |
4 |
* this work for additional information regarding copyright ownership. |
5 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
6 |
* (the "License"); you may not use this file except in compliance with |
7 |
* the License. You may obtain a copy of the License at |
8 |
* |
9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
10 |
* |
11 |
* Unless required by applicable law or agreed to in writing, software |
12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 |
* See the License for the specific language governing permissions and |
15 |
* limitations under the License. |
16 |
*/ |
17 |
package org.apache.taglibs.standard.xpath; |
18 |
|
19 |
import javax.servlet.http.Cookie; |
20 |
import javax.servlet.http.HttpServletRequest; |
21 |
import javax.servlet.jsp.PageContext; |
22 |
import javax.xml.XMLConstants; |
23 |
import javax.xml.namespace.NamespaceContext; |
24 |
import javax.xml.namespace.QName; |
25 |
import javax.xml.xpath.XPathVariableResolver; |
26 |
import java.util.HashMap; |
27 |
import java.util.Iterator; |
28 |
import java.util.Map; |
29 |
|
30 |
/** |
31 |
*/ |
32 |
public class JSTLVariableResolver implements XPathVariableResolver, NamespaceContext { |
33 |
|
34 |
private static enum Scope { |
35 |
JSP, PARAM, HEADER, COOKIE, INITPARAM, PAGE, REQUEST, SESSION, APPLICATION |
36 |
} |
37 |
|
38 |
// Namespace URIs for JSTL implicit variables |
39 |
private static final String PARAM_NS_URL = "http://java.sun.com/jstl/xpath/param"; |
40 |
private static final String HEADER_NS_URL = "http://java.sun.com/jstl/xpath/header"; |
41 |
private static final String COOKIE_NS_URL = "http://java.sun.com/jstl/xpath/cookie"; |
42 |
private static final String INITPARAM_NS_URL = "http://java.sun.com/jstl/xpath/initParam"; |
43 |
private static final String PAGE_NS_URL = "http://java.sun.com/jstl/xpath/page"; |
44 |
private static final String REQUEST_NS_URL = "http://java.sun.com/jstl/xpath/request"; |
45 |
private static final String SESSION_NS_URL = "http://java.sun.com/jstl/xpath/session"; |
46 |
private static final String APP_NS_URL = "http://java.sun.com/jstl/xpath/app"; |
47 |
|
48 |
// Prefixes for JSTL implicit variables |
49 |
private static final String PARAM_PREFIX = "param"; |
50 |
private static final String HEADER_PREFIX = "header"; |
51 |
private static final String COOKIE_PREFIX = "cookie"; |
52 |
private static final String INITPARAM_PREFIX = "initParam"; |
53 |
private static final String PAGE_PREFIX = "pageScope"; |
54 |
private static final String REQUEST_PREFIX = "requestScope"; |
55 |
private static final String SESSION_PREFIX = "sessionScope"; |
56 |
private static final String APP_PREFIX = "applicationScope"; |
57 |
|
58 |
// map prefixes to namespaces |
59 |
private static final Map<String, String> NAMESPACES; |
60 |
|
61 |
static { |
62 |
NAMESPACES = new HashMap<String, String>(8); |
63 |
NAMESPACES.put(XMLConstants.DEFAULT_NS_PREFIX, XMLConstants.NULL_NS_URI); |
64 |
NAMESPACES.put(XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI); |
65 |
NAMESPACES.put(XMLConstants.XMLNS_ATTRIBUTE, XMLConstants.XMLNS_ATTRIBUTE_NS_URI); |
66 |
NAMESPACES.put(PARAM_PREFIX, PARAM_NS_URL); |
67 |
NAMESPACES.put(HEADER_PREFIX, HEADER_NS_URL); |
68 |
NAMESPACES.put(COOKIE_PREFIX, COOKIE_NS_URL); |
69 |
NAMESPACES.put(INITPARAM_PREFIX, INITPARAM_NS_URL); |
70 |
NAMESPACES.put(PAGE_PREFIX, PAGE_NS_URL); |
71 |
NAMESPACES.put(REQUEST_PREFIX, REQUEST_NS_URL); |
72 |
NAMESPACES.put(SESSION_PREFIX, SESSION_NS_URL); |
73 |
NAMESPACES.put(APP_PREFIX, APP_NS_URL); |
74 |
} |
75 |
|
76 |
// map namespaces to scopes |
77 |
private static final Map<String, Scope> SCOPES; |
78 |
|
79 |
static { |
80 |
SCOPES = new HashMap<String, Scope>(8); |
81 |
SCOPES.put(XMLConstants.NULL_NS_URI, Scope.JSP); |
82 |
SCOPES.put(PARAM_NS_URL, Scope.PARAM); |
83 |
SCOPES.put(HEADER_NS_URL, Scope.HEADER); |
84 |
SCOPES.put(COOKIE_NS_URL, Scope.COOKIE); |
85 |
SCOPES.put(INITPARAM_NS_URL, Scope.INITPARAM); |
86 |
SCOPES.put(PAGE_NS_URL, Scope.PAGE); |
87 |
SCOPES.put(REQUEST_NS_URL, Scope.REQUEST); |
88 |
SCOPES.put(SESSION_NS_URL, Scope.SESSION); |
89 |
SCOPES.put(APP_NS_URL, Scope.APPLICATION); |
90 |
} |
91 |
|
92 |
private PageContext pageContext; |
93 |
|
94 |
public void setPageContext(PageContext pageContext) { |
95 |
this.pageContext = pageContext; |
96 |
} |
97 |
|
98 |
public Object resolveVariable(QName variableName) { |
99 |
String name = variableName.getLocalPart(); |
100 |
Scope scope = SCOPES.get(variableName.getNamespaceURI()); |
101 |
if (scope == null) { |
102 |
return null; |
103 |
} |
104 |
switch (scope) { |
105 |
case JSP: |
106 |
return pageContext.findAttribute(name); |
107 |
case PARAM: |
108 |
return pageContext.getRequest().getParameter(name); |
109 |
case HEADER: |
110 |
return ((HttpServletRequest) pageContext.getRequest()).getHeader(name); |
111 |
case COOKIE: |
112 |
Cookie[] cookies = ((HttpServletRequest) pageContext.getRequest()).getCookies(); |
113 |
if (cookies != null) { |
114 |
for (Cookie cookie : cookies) { |
115 |
if (cookie.getName().equals(name)) { |
116 |
return cookie.getValue(); |
117 |
} |
118 |
} |
119 |
} |
120 |
return null; |
121 |
case INITPARAM: |
122 |
return pageContext.getServletContext().getInitParameter(name); |
123 |
case PAGE: |
124 |
return pageContext.getAttribute(name, PageContext.PAGE_SCOPE); |
125 |
case REQUEST: |
126 |
return pageContext.getAttribute(name, PageContext.REQUEST_SCOPE); |
127 |
case SESSION: |
128 |
return pageContext.getAttribute(name, PageContext.SESSION_SCOPE); |
129 |
case APPLICATION: |
130 |
return pageContext.getAttribute(name, PageContext.APPLICATION_SCOPE); |
131 |
default: |
132 |
throw new AssertionError(); |
133 |
} |
134 |
} |
135 |
|
136 |
public String getNamespaceURI(String prefix) { |
137 |
if (prefix == null) { |
138 |
throw new IllegalArgumentException("prefix is null"); |
139 |
} |
140 |
String uri = NAMESPACES.get(prefix); |
141 |
if (uri != null) { |
142 |
return uri; |
143 |
} |
144 |
return XMLConstants.NULL_NS_URI; |
145 |
} |
146 |
|
147 |
public String getPrefix(String namespaceURI) { |
148 |
throw new UnsupportedOperationException(); |
149 |
} |
150 |
|
151 |
public Iterator getPrefixes(String namespaceURI) { |
152 |
throw new UnsupportedOperationException(); |
153 |
} |
154 |
} |
155 |
native |