Lines 57-101
Link Here
|
57 |
// would ideally be a base class, but some of our user handlers already |
57 |
// would ideally be a base class, but some of our user handlers already |
58 |
// have their own parents |
58 |
// have their own parents |
59 |
public class XPathUtil { |
59 |
public class XPathUtil { |
60 |
|
60 |
|
61 |
//********************************************************************* |
61 |
//********************************************************************* |
62 |
// Constructor |
62 |
// Constructor |
63 |
|
63 |
|
64 |
/** |
64 |
/** |
65 |
* Constructs a new XPathUtil object associated with the given |
65 |
* Constructs a new XPathUtil object associated with the given |
66 |
* PageContext. |
66 |
* PageContext. |
67 |
*/ |
67 |
*/ |
68 |
public XPathUtil(PageContext pc) { |
68 |
public XPathUtil(PageContext pc) { |
69 |
pageContext = pc; |
69 |
pageContext = pc; |
70 |
} |
70 |
} |
71 |
|
|
|
72 |
int globalVarSize=0; |
73 |
public Vector getVariableQNames ( ) { |
74 |
|
71 |
|
|
|
72 |
int globalVarSize = 0; |
73 |
|
74 |
public Vector getVariableQNames() { |
75 |
|
75 |
globalVarSize = 0; |
76 |
globalVarSize = 0; |
76 |
Vector variableVector = new Vector ( ); |
77 |
Vector variableVector = new Vector(); |
77 |
// Now construct attributes in different scopes |
78 |
// Now construct attributes in different scopes |
78 |
Enumeration enum_ = pageContext.getAttributeNamesInScope( |
79 |
Enumeration enum_ = pageContext.getAttributeNamesInScope( |
79 |
PageContext.PAGE_SCOPE ); |
80 |
PageContext.PAGE_SCOPE); |
80 |
while ( enum_.hasMoreElements() ) { |
81 |
while (enum_.hasMoreElements()) { |
81 |
String varName = (String)enum_.nextElement(); |
82 |
String varName = (String) enum_.nextElement(); |
82 |
QName varQName = new QName ( PAGE_NS_URL, PAGE_P, varName); |
83 |
QName varQName = new QName(PAGE_NS_URL, PAGE_P, varName); |
83 |
//Adding both namespace qualified QName and just localName |
84 |
//Adding both namespace qualified QName and just localName |
84 |
variableVector.addElement( varQName ); |
85 |
variableVector.addElement(varQName); |
85 |
globalVarSize++; |
86 |
globalVarSize++; |
86 |
|
87 |
|
87 |
variableVector.addElement( new QName(null, varName ) ); |
88 |
variableVector.addElement(new QName(null, varName)); |
88 |
globalVarSize++; |
89 |
globalVarSize++; |
89 |
} |
90 |
} |
90 |
enum_ = pageContext.getAttributeNamesInScope( |
91 |
enum_ = pageContext.getAttributeNamesInScope( |
91 |
PageContext.REQUEST_SCOPE ); |
92 |
PageContext.REQUEST_SCOPE); |
92 |
while ( enum_.hasMoreElements() ) { |
93 |
while (enum_.hasMoreElements()) { |
93 |
String varName = (String)enum_.nextElement(); |
94 |
String varName = (String) enum_.nextElement(); |
94 |
QName varQName = new QName ( REQUEST_NS_URL,REQUEST_P, varName); |
95 |
QName varQName = new QName(REQUEST_NS_URL, REQUEST_P, varName); |
95 |
//Adding both namespace qualified QName and just localName |
96 |
//Adding both namespace qualified QName and just localName |
96 |
variableVector.addElement( varQName ); |
97 |
variableVector.addElement(varQName); |
97 |
globalVarSize++; |
98 |
globalVarSize++; |
98 |
variableVector.addElement( new QName(null, varName ) ); |
99 |
variableVector.addElement(new QName(null, varName)); |
99 |
globalVarSize++; |
100 |
globalVarSize++; |
100 |
} |
101 |
} |
101 |
|
102 |
|
Lines 103-192
Link Here
|
103 |
// we may have a page directive preventing session creation/access |
104 |
// we may have a page directive preventing session creation/access |
104 |
// do not attempt to retrieve attribute names in session scope |
105 |
// do not attempt to retrieve attribute names in session scope |
105 |
// @see [ http://issues.apache.org/bugzilla/show_bug.cgi?id=35216 ] |
106 |
// @see [ http://issues.apache.org/bugzilla/show_bug.cgi?id=35216 ] |
106 |
enum_ = pageContext.getAttributeNamesInScope( |
107 |
enum_ = pageContext.getAttributeNamesInScope( |
107 |
PageContext.SESSION_SCOPE ); |
108 |
PageContext.SESSION_SCOPE); |
108 |
while ( enum_.hasMoreElements() ) { |
109 |
while (enum_.hasMoreElements()) { |
109 |
String varName = (String)enum_.nextElement(); |
110 |
String varName = (String) enum_.nextElement(); |
110 |
QName varQName = new QName ( SESSION_NS_URL, SESSION_P,varName); |
111 |
QName varQName = new QName(SESSION_NS_URL, SESSION_P, varName); |
111 |
//Adding both namespace qualified QName and just localName |
112 |
//Adding both namespace qualified QName and just localName |
112 |
variableVector.addElement( varQName ); |
113 |
variableVector.addElement(varQName); |
113 |
globalVarSize++; |
114 |
globalVarSize++; |
114 |
variableVector.addElement( new QName(null, varName ) ); |
115 |
variableVector.addElement(new QName(null, varName)); |
115 |
globalVarSize++; |
116 |
globalVarSize++; |
116 |
} |
117 |
} |
117 |
} |
118 |
} |
118 |
|
119 |
|
119 |
enum_ = pageContext.getAttributeNamesInScope( |
120 |
enum_ = pageContext.getAttributeNamesInScope( |
120 |
PageContext.APPLICATION_SCOPE ); |
121 |
PageContext.APPLICATION_SCOPE); |
121 |
while ( enum_.hasMoreElements() ) { |
122 |
while (enum_.hasMoreElements()) { |
122 |
String varName = (String)enum_.nextElement(); |
123 |
String varName = (String) enum_.nextElement(); |
123 |
QName varQName = new QName ( APP_NS_URL, APP_P,varName ); |
124 |
QName varQName = new QName(APP_NS_URL, APP_P, varName); |
124 |
//Adding both namespace qualified QName and just localName |
125 |
//Adding both namespace qualified QName and just localName |
125 |
variableVector.addElement( varQName ); |
126 |
variableVector.addElement(varQName); |
126 |
globalVarSize++; |
127 |
globalVarSize++; |
127 |
variableVector.addElement( new QName(null, varName ) ); |
128 |
variableVector.addElement(new QName(null, varName)); |
128 |
globalVarSize++; |
129 |
globalVarSize++; |
129 |
} |
130 |
} |
130 |
enum_ = pageContext.getRequest().getParameterNames(); |
131 |
enum_ = pageContext.getRequest().getParameterNames(); |
131 |
while ( enum_.hasMoreElements() ) { |
132 |
while (enum_.hasMoreElements()) { |
132 |
String varName = (String)enum_.nextElement(); |
133 |
String varName = (String) enum_.nextElement(); |
133 |
QName varQName = new QName ( PARAM_NS_URL, PARAM_P,varName ); |
134 |
QName varQName = new QName(PARAM_NS_URL, PARAM_P, varName); |
134 |
//Adding both namespace qualified QName and just localName |
135 |
//Adding both namespace qualified QName and just localName |
135 |
variableVector.addElement( varQName ); |
136 |
variableVector.addElement(varQName); |
136 |
globalVarSize++; |
137 |
globalVarSize++; |
137 |
} |
138 |
} |
138 |
enum_ = pageContext.getServletContext().getInitParameterNames(); |
139 |
enum_ = pageContext.getServletContext().getInitParameterNames(); |
139 |
while ( enum_.hasMoreElements() ) { |
140 |
while (enum_.hasMoreElements()) { |
140 |
String varName = (String)enum_.nextElement(); |
141 |
String varName = (String) enum_.nextElement(); |
141 |
QName varQName = new QName ( INITPARAM_NS_URL, INITPARAM_P,varName ); |
142 |
QName varQName = new QName(INITPARAM_NS_URL, INITPARAM_P, varName); |
142 |
//Adding both namespace qualified QName and just localName |
143 |
//Adding both namespace qualified QName and just localName |
143 |
variableVector.addElement( varQName ); |
144 |
variableVector.addElement(varQName); |
144 |
globalVarSize++; |
145 |
globalVarSize++; |
145 |
} |
146 |
} |
146 |
enum_ = ((HttpServletRequest)pageContext.getRequest()).getHeaderNames(); |
147 |
enum_ = ((HttpServletRequest) pageContext.getRequest()).getHeaderNames(); |
147 |
while ( enum_.hasMoreElements() ) { |
148 |
while (enum_.hasMoreElements()) { |
148 |
String varName = (String)enum_.nextElement(); |
149 |
String varName = (String) enum_.nextElement(); |
149 |
QName varQName = new QName ( HEADER_NS_URL, HEADER_P,varName ); |
150 |
QName varQName = new QName(HEADER_NS_URL, HEADER_P, varName); |
150 |
//Adding namespace qualified QName |
151 |
//Adding namespace qualified QName |
151 |
variableVector.addElement( varQName ); |
152 |
variableVector.addElement(varQName); |
152 |
globalVarSize++; |
153 |
globalVarSize++; |
153 |
} |
154 |
} |
154 |
Cookie[] c= ((HttpServletRequest)pageContext.getRequest()).getCookies(); |
155 |
Cookie[] c = ((HttpServletRequest) pageContext.getRequest()).getCookies(); |
155 |
if ( c!= null ) { |
156 |
if (c != null) { |
156 |
for (int i = 0; i < c.length; i++) { |
157 |
for (int i = 0; i < c.length; i++) { |
157 |
String varName = c[i].getName(); |
158 |
String varName = c[i].getName(); |
158 |
QName varQName = new QName ( COOKIE_NS_URL, COOKIE_P,varName ); |
159 |
QName varQName = new QName(COOKIE_NS_URL, COOKIE_P, varName); |
159 |
//Adding namespace qualified QName |
160 |
//Adding namespace qualified QName |
160 |
variableVector.addElement( varQName ); |
161 |
variableVector.addElement(varQName); |
161 |
globalVarSize++; |
162 |
globalVarSize++; |
162 |
} |
163 |
} |
163 |
} |
164 |
} |
164 |
|
165 |
|
165 |
return variableVector; |
166 |
return variableVector; |
166 |
|
167 |
|
167 |
} |
168 |
} |
168 |
|
169 |
|
169 |
//********************************************************************* |
170 |
//********************************************************************* |
170 |
// Support for JSTL variable resolution |
171 |
// Support for JSTL variable resolution |
171 |
|
172 |
|
172 |
// The URLs |
173 |
// The URLs |
173 |
private static final String PAGE_NS_URL |
174 |
private static final String PAGE_NS_URL |
174 |
= "http://java.sun.com/jstl/xpath/page"; |
175 |
= "http://java.sun.com/jstl/xpath/page"; |
175 |
private static final String REQUEST_NS_URL |
176 |
private static final String REQUEST_NS_URL |
176 |
= "http://java.sun.com/jstl/xpath/request"; |
177 |
= "http://java.sun.com/jstl/xpath/request"; |
177 |
private static final String SESSION_NS_URL |
178 |
private static final String SESSION_NS_URL |
178 |
= "http://java.sun.com/jstl/xpath/session"; |
179 |
= "http://java.sun.com/jstl/xpath/session"; |
179 |
private static final String APP_NS_URL |
180 |
private static final String APP_NS_URL |
180 |
= "http://java.sun.com/jstl/xpath/app"; |
181 |
= "http://java.sun.com/jstl/xpath/app"; |
181 |
private static final String PARAM_NS_URL |
182 |
private static final String PARAM_NS_URL |
182 |
= "http://java.sun.com/jstl/xpath/param"; |
183 |
= "http://java.sun.com/jstl/xpath/param"; |
183 |
private static final String INITPARAM_NS_URL |
184 |
private static final String INITPARAM_NS_URL |
184 |
= "http://java.sun.com/jstl/xpath/initParam"; |
185 |
= "http://java.sun.com/jstl/xpath/initParam"; |
185 |
private static final String COOKIE_NS_URL |
186 |
private static final String COOKIE_NS_URL |
186 |
= "http://java.sun.com/jstl/xpath/cookie"; |
187 |
= "http://java.sun.com/jstl/xpath/cookie"; |
187 |
private static final String HEADER_NS_URL |
188 |
private static final String HEADER_NS_URL |
188 |
= "http://java.sun.com/jstl/xpath/header"; |
189 |
= "http://java.sun.com/jstl/xpath/header"; |
189 |
|
190 |
|
190 |
// The prefixes |
191 |
// The prefixes |
191 |
private static final String PAGE_P = "pageScope"; |
192 |
private static final String PAGE_P = "pageScope"; |
192 |
private static final String REQUEST_P = "requestScope"; |
193 |
private static final String REQUEST_P = "requestScope"; |
Lines 196-389
Link Here
|
196 |
private static final String INITPARAM_P = "initParam"; |
197 |
private static final String INITPARAM_P = "initParam"; |
197 |
private static final String COOKIE_P = "cookie"; |
198 |
private static final String COOKIE_P = "cookie"; |
198 |
private static final String HEADER_P = "header"; |
199 |
private static final String HEADER_P = "header"; |
199 |
|
200 |
|
200 |
/** |
201 |
/** |
201 |
* org.apache.xpath.VariableStack defines a class to keep track of a stack |
202 |
* org.apache.xpath.VariableStack defines a class to keep track of a stack |
202 |
* for template arguments and variables. |
203 |
* for template arguments and variables. |
203 |
* JstlVariableContext customizes it so it handles JSTL custom |
204 |
* JstlVariableContext customizes it so it handles JSTL custom |
204 |
* variable-mapping rules. |
205 |
* variable-mapping rules. |
205 |
*/ |
206 |
*/ |
206 |
protected class JstlVariableContext extends org.apache.xpath.VariableStack { |
207 |
protected class JstlVariableContext extends org.apache.xpath.VariableStack { |
207 |
|
208 |
|
208 |
public JstlVariableContext( ) { |
209 |
public JstlVariableContext() { |
209 |
super(); |
210 |
super(); |
210 |
} |
211 |
} |
211 |
|
212 |
|
212 |
/** |
213 |
/** |
213 |
* Get a variable as an XPath object based on it's qualified name. |
214 |
* Get a variable as an XPath object based on it's qualified name. |
214 |
* We override the base class method so JSTL's custom variable-mapping |
215 |
* We override the base class method so JSTL's custom variable-mapping |
215 |
* rules can be applied. |
216 |
* rules can be applied. |
216 |
* |
217 |
* |
217 |
* @param xctxt The XPath context. @@@ we don't use it... |
218 |
* @param xctxt The XPath context. @@@ we don't use it... |
218 |
* (from xalan: which must be passed in order to lazy evaluate variables.) |
219 |
* (from xalan: which must be passed in order to lazy evaluate variables.) |
219 |
* @param qname The qualified name of the variable. |
220 |
* @param qname The qualified name of the variable. |
220 |
*/ |
221 |
*/ |
221 |
@Override |
222 |
@Override |
222 |
public XObject getVariableOrParam( |
223 |
public XObject getVariableOrParam( |
223 |
XPathContext xctxt, |
224 |
XPathContext xctxt, |
224 |
org.apache.xml.utils.QName qname) |
225 |
org.apache.xml.utils.QName qname) |
225 |
throws javax.xml.transform.TransformerException, UnresolvableException |
226 |
throws javax.xml.transform.TransformerException, UnresolvableException { |
226 |
{ |
|
|
227 |
//p( "***********************************getVariableOrParam begin****"); |
227 |
//p( "***********************************getVariableOrParam begin****"); |
228 |
String namespace = qname.getNamespaceURI(); |
228 |
String namespace = qname.getNamespaceURI(); |
229 |
String prefix = qname.getPrefix(); |
229 |
String prefix = qname.getPrefix(); |
230 |
String localName = qname.getLocalName(); |
230 |
String localName = qname.getLocalName(); |
231 |
|
231 |
|
232 |
//p("namespace:prefix:localname=>"+ namespace |
232 |
//p("namespace:prefix:localname=>"+ namespace |
233 |
// + ":" + prefix +":" + localName ); |
233 |
// + ":" + prefix +":" + localName ); |
234 |
|
234 |
|
235 |
try { |
235 |
try { |
236 |
Object varObject = getVariableValue(namespace,prefix,localName); |
236 |
Object varObject = getVariableValue(namespace, prefix, localName); |
237 |
|
237 |
|
238 |
|
238 |
|
239 |
//XObject varObject = myvs.getVariableOrParam( xpathSupport, varQName); |
239 |
//XObject varObject = myvs.getVariableOrParam( xpathSupport, varQName); |
240 |
XObject newXObject = new XObject( varObject); |
240 |
XObject newXObject = new XObject(varObject); |
241 |
|
241 |
|
242 |
if ( Class.forName("org.w3c.dom.Document").isInstance( varObject) ) { |
242 |
if (Class.forName("org.w3c.dom.Document").isInstance(varObject)) { |
243 |
|
243 |
|
244 |
NodeList nl= ((Document)varObject).getChildNodes(); |
244 |
NodeList nl = ((Document) varObject).getChildNodes(); |
245 |
// To allow non-welformed document |
245 |
// To allow non-welformed document |
246 |
Vector nodeVector = new Vector(); |
246 |
Vector nodeVector = new Vector(); |
247 |
for ( int i=0; i<nl.getLength(); i++ ) { |
247 |
for (int i = 0; i < nl.getLength(); i++) { |
248 |
Node currNode = nl.item(i); |
248 |
Node currNode = nl.item(i); |
249 |
if ( currNode.getNodeType() == Node.ELEMENT_NODE ) { |
249 |
if (currNode.getNodeType() == Node.ELEMENT_NODE) { |
250 |
nodeVector.addElement( currNode); |
250 |
nodeVector.addElement(currNode); |
251 |
} |
251 |
} |
252 |
} |
252 |
} |
253 |
JSTLNodeList jstlNodeList = new JSTLNodeList( nodeVector); |
253 |
JSTLNodeList jstlNodeList = new JSTLNodeList(nodeVector); |
254 |
newXObject = new XNodeSetForDOM( jstlNodeList, xctxt ); |
254 |
newXObject = new XNodeSetForDOM(jstlNodeList, xctxt); |
255 |
|
255 |
|
256 |
return newXObject; |
256 |
return newXObject; |
257 |
|
257 |
|
258 |
} |
258 |
} |
259 |
if ( Class.forName( |
259 |
if (Class.forName( |
260 |
"org.apache.taglibs.standard.tag.common.xml.JSTLNodeList").isInstance( |
260 |
"org.apache.taglibs.standard.tag.common.xml.JSTLNodeList").isInstance( |
261 |
varObject) ) { |
261 |
varObject)) { |
262 |
JSTLNodeList jstlNodeList = (JSTLNodeList)varObject; |
262 |
JSTLNodeList jstlNodeList = (JSTLNodeList) varObject; |
263 |
if ( ( jstlNodeList.getLength() == 1 ) && |
263 |
if ((jstlNodeList.getLength() == 1) && |
264 |
(!Class.forName("org.w3c.dom.Node").isInstance( jstlNodeList.elementAt(0) ) ) ) { |
264 |
(!Class.forName("org.w3c.dom.Node").isInstance(jstlNodeList.elementAt(0)))) { |
265 |
varObject = jstlNodeList.elementAt(0); |
265 |
varObject = jstlNodeList.elementAt(0); |
266 |
//Now we need to allow this primitive type to be coverted |
266 |
//Now we need to allow this primitive type to be coverted |
267 |
// to type which Xalan XPath understands |
267 |
// to type which Xalan XPath understands |
268 |
} else { |
268 |
} else { |
269 |
return new XNodeSetForDOM ( jstlNodeList ,xctxt ); |
269 |
return new XNodeSetForDOM(jstlNodeList, xctxt); |
270 |
} |
270 |
} |
271 |
} |
271 |
} |
272 |
if (Class.forName("org.w3c.dom.Node").isInstance( varObject)) { |
272 |
if (Class.forName("org.w3c.dom.Node").isInstance(varObject)) { |
273 |
newXObject = new XNodeSetForDOM ( new JSTLNodeList( (Node)varObject ),xctxt ); |
273 |
newXObject = new XNodeSetForDOM(new JSTLNodeList((Node) varObject), xctxt); |
274 |
} else if ( Class.forName("java.lang.String").isInstance( varObject)){ |
274 |
} else if (Class.forName("java.lang.String").isInstance(varObject)) { |
275 |
newXObject = new XString ( (String)varObject ); |
275 |
newXObject = new XString((String) varObject); |
276 |
} else if ( Class.forName("java.lang.Boolean").isInstance( varObject) ) { |
276 |
} else if (Class.forName("java.lang.Boolean").isInstance(varObject)) { |
277 |
newXObject = new XBoolean ( (Boolean)varObject ); |
277 |
newXObject = new XBoolean((Boolean) varObject); |
278 |
} else if ( Class.forName("java.lang.Number").isInstance( varObject) ) { |
278 |
} else if (Class.forName("java.lang.Number").isInstance(varObject)) { |
279 |
newXObject = new XNumber ( (Number)varObject ); |
279 |
newXObject = new XNumber((Number) varObject); |
280 |
} |
280 |
} |
281 |
|
281 |
|
282 |
return newXObject; |
282 |
return newXObject; |
283 |
// myvs.setGlobalVariable( i, newXObject ); |
283 |
// myvs.setGlobalVariable( i, newXObject ); |
284 |
} catch ( ClassNotFoundException cnfe ) { |
284 |
} catch (ClassNotFoundException cnfe) { |
285 |
// This shouldn't happen (TODO: LOG) |
285 |
// This shouldn't happen (TODO: LOG) |
286 |
System.out.println("CLASS NOT FOUND EXCEPTION :" + cnfe ); |
286 |
System.out.println("CLASS NOT FOUND EXCEPTION :" + cnfe); |
287 |
} |
287 |
} |
288 |
//System.out.println("*****getVariableOrParam returning *null*" ); |
288 |
//System.out.println("*****getVariableOrParam returning *null*" ); |
289 |
return null ; |
289 |
return null; |
290 |
} |
290 |
} |
291 |
|
291 |
|
292 |
/** |
292 |
/** |
293 |
* Retrieve an XPath's variable value using JSTL's custom |
293 |
* Retrieve an XPath's variable value using JSTL's custom |
294 |
* variable-mapping rules |
294 |
* variable-mapping rules |
295 |
*/ |
295 |
*/ |
296 |
public Object getVariableValue( |
296 |
public Object getVariableValue( |
297 |
String namespace, |
297 |
String namespace, |
298 |
String prefix, |
298 |
String prefix, |
299 |
String localName) |
299 |
String localName) |
300 |
throws UnresolvableException |
300 |
throws UnresolvableException { |
301 |
{ |
|
|
302 |
// p("resolving: ns=" + namespace + " prefix=" + prefix + " localName=" + localName); |
301 |
// p("resolving: ns=" + namespace + " prefix=" + prefix + " localName=" + localName); |
303 |
// We can match on namespace with Xalan but leaving as is |
302 |
// We can match on namespace with Xalan but leaving as is |
304 |
// [ I 'd prefer to match on namespace, but this doesn't appear |
303 |
// [ I 'd prefer to match on namespace, but this doesn't appear |
305 |
// to work in Jaxen] |
304 |
// to work in Jaxen] |
306 |
if (prefix == null || prefix.equals("")) { |
305 |
if (prefix == null || prefix.equals("")) { |
307 |
return notNull( |
306 |
return notNull( |
308 |
pageContext.findAttribute(localName), |
307 |
pageContext.findAttribute(localName), |
309 |
prefix, |
308 |
prefix, |
310 |
localName); |
309 |
localName); |
311 |
} else if (prefix.equals(PAGE_P)) { |
310 |
} else if (prefix.equals(PAGE_P)) { |
312 |
return notNull( |
311 |
return notNull( |
313 |
pageContext.getAttribute(localName,PageContext.PAGE_SCOPE), |
312 |
pageContext.getAttribute(localName, PageContext.PAGE_SCOPE), |
314 |
prefix, |
313 |
prefix, |
315 |
localName); |
314 |
localName); |
316 |
} else if (prefix.equals(REQUEST_P)) { |
315 |
} else if (prefix.equals(REQUEST_P)) { |
317 |
return notNull( |
316 |
return notNull( |
318 |
pageContext.getAttribute(localName, |
317 |
pageContext.getAttribute(localName, |
319 |
PageContext.REQUEST_SCOPE), |
318 |
PageContext.REQUEST_SCOPE), |
320 |
prefix, |
319 |
prefix, |
321 |
localName); |
320 |
localName); |
322 |
} else if (prefix.equals(SESSION_P)) { |
321 |
} else if (prefix.equals(SESSION_P)) { |
323 |
return notNull( |
322 |
return notNull( |
324 |
pageContext.getAttribute(localName, |
323 |
pageContext.getAttribute(localName, |
325 |
PageContext.SESSION_SCOPE), |
324 |
PageContext.SESSION_SCOPE), |
326 |
prefix, |
325 |
prefix, |
327 |
localName); |
326 |
localName); |
328 |
} else if (prefix.equals(APP_P)) { |
327 |
} else if (prefix.equals(APP_P)) { |
329 |
return notNull( |
328 |
return notNull( |
330 |
pageContext.getAttribute(localName, |
329 |
pageContext.getAttribute(localName, |
331 |
PageContext.APPLICATION_SCOPE), |
330 |
PageContext.APPLICATION_SCOPE), |
332 |
prefix, |
331 |
prefix, |
333 |
localName); |
332 |
localName); |
334 |
} else if (prefix.equals(PARAM_P)) { |
333 |
} else if (prefix.equals(PARAM_P)) { |
335 |
return notNull( |
334 |
return notNull( |
336 |
pageContext.getRequest().getParameter(localName), |
335 |
pageContext.getRequest().getParameter(localName), |
337 |
prefix, |
336 |
prefix, |
338 |
localName); |
337 |
localName); |
339 |
} else if (prefix.equals(INITPARAM_P)) { |
338 |
} else if (prefix.equals(INITPARAM_P)) { |
340 |
return notNull( |
339 |
return notNull( |
341 |
pageContext.getServletContext(). |
340 |
pageContext.getServletContext(). |
342 |
getInitParameter(localName), |
341 |
getInitParameter(localName), |
343 |
prefix, |
342 |
prefix, |
344 |
localName); |
343 |
localName); |
345 |
} else if (prefix.equals(HEADER_P)) { |
344 |
} else if (prefix.equals(HEADER_P)) { |
346 |
HttpServletRequest hsr = |
345 |
HttpServletRequest hsr = |
347 |
(HttpServletRequest) pageContext.getRequest(); |
346 |
(HttpServletRequest) pageContext.getRequest(); |
348 |
return notNull( |
347 |
return notNull( |
349 |
hsr.getHeader(localName), |
348 |
hsr.getHeader(localName), |
350 |
prefix, |
349 |
prefix, |
351 |
localName); |
350 |
localName); |
352 |
} else if (prefix.equals(COOKIE_P)) { |
351 |
} else if (prefix.equals(COOKIE_P)) { |
353 |
HttpServletRequest hsr = |
352 |
HttpServletRequest hsr = |
354 |
(HttpServletRequest) pageContext.getRequest(); |
353 |
(HttpServletRequest) pageContext.getRequest(); |
355 |
Cookie[] c = hsr.getCookies(); |
354 |
Cookie[] c = hsr.getCookies(); |
356 |
for (int i = 0; i < c.length; i++) |
355 |
for (int i = 0; i < c.length; i++) { |
357 |
if (c[i].getName().equals(localName)) |
356 |
if (c[i].getName().equals(localName)) { |
358 |
return c[i].getValue(); |
357 |
return c[i].getValue(); |
|
|
358 |
} |
359 |
} |
359 |
throw new UnresolvableException("$" + prefix + ":" + localName); |
360 |
throw new UnresolvableException("$" + prefix + ":" + localName); |
360 |
} else { |
361 |
} else { |
361 |
throw new UnresolvableException("$" + prefix + ":" + localName); |
362 |
throw new UnresolvableException("$" + prefix + ":" + localName); |
362 |
} |
363 |
} |
363 |
} |
364 |
} |
364 |
|
365 |
|
365 |
/** |
366 |
/** |
366 |
* Validate that the Object returned is not null. If it is |
367 |
* Validate that the Object returned is not null. If it is |
367 |
* null, throw an exception. |
368 |
* null, throw an exception. |
368 |
*/ |
369 |
*/ |
369 |
private Object notNull(Object o, String prefix, String localName) |
370 |
private Object notNull(Object o, String prefix, String localName) |
370 |
throws UnresolvableException { |
371 |
throws UnresolvableException { |
371 |
if (o == null) { |
372 |
if (o == null) { |
372 |
throw new UnresolvableException("$" + (prefix==null?"":prefix+":") + localName); |
373 |
throw new UnresolvableException("$" + (prefix == null ? "" : prefix + ":") + localName); |
373 |
} |
374 |
} |
374 |
//p("resolved to: " + o); |
375 |
//p("resolved to: " + o); |
375 |
return o; |
376 |
return o; |
376 |
} |
377 |
} |
377 |
} |
378 |
} |
378 |
|
379 |
|
379 |
//********************************************************************* |
380 |
//********************************************************************* |
380 |
// Support for XPath evaluation |
381 |
// Support for XPath evaluation |
381 |
|
382 |
|
382 |
private PageContext pageContext; |
383 |
private PageContext pageContext; |
383 |
private static HashMap exprCache; |
384 |
private static HashMap exprCache; |
384 |
private static JSTLPrefixResolver jstlPrefixResolver = null; |
385 |
private static JSTLPrefixResolver jstlPrefixResolver = null; |
385 |
|
386 |
|
386 |
/** Initialize globally useful data. */ |
387 |
/** |
|
|
388 |
* Initialize globally useful data. |
389 |
*/ |
387 |
private synchronized static void staticInit() { |
390 |
private synchronized static void staticInit() { |
388 |
if (jstlPrefixResolver == null) { |
391 |
if (jstlPrefixResolver == null) { |
389 |
// register supported namespaces |
392 |
// register supported namespaces |
Lines 396-443
Link Here
|
396 |
jstlPrefixResolver.addNamespace("initParam", INITPARAM_NS_URL); |
399 |
jstlPrefixResolver.addNamespace("initParam", INITPARAM_NS_URL); |
397 |
jstlPrefixResolver.addNamespace("header", HEADER_NS_URL); |
400 |
jstlPrefixResolver.addNamespace("header", HEADER_NS_URL); |
398 |
jstlPrefixResolver.addNamespace("cookie", COOKIE_NS_URL); |
401 |
jstlPrefixResolver.addNamespace("cookie", COOKIE_NS_URL); |
399 |
|
402 |
|
400 |
|
403 |
|
401 |
// create a HashMap to cache the expressions |
404 |
// create a HashMap to cache the expressions |
402 |
exprCache = new HashMap(); |
405 |
exprCache = new HashMap(); |
403 |
} |
406 |
} |
404 |
} |
407 |
} |
405 |
|
408 |
|
406 |
static DocumentBuilderFactory dbf = null; |
409 |
static DocumentBuilderFactory dbf = null; |
407 |
static DocumentBuilder db = null; |
410 |
static DocumentBuilder db = null; |
408 |
static Document d = null; |
411 |
static Document d = null; |
409 |
|
412 |
|
410 |
static Document getDummyDocument( ) { |
413 |
static Document getDummyDocument() { |
411 |
try { |
414 |
try { |
412 |
if ( dbf == null ) { |
415 |
if (dbf == null) { |
413 |
dbf = DocumentBuilderFactory.newInstance(); |
416 |
dbf = DocumentBuilderFactory.newInstance(); |
414 |
dbf.setNamespaceAware( true ); |
417 |
dbf.setNamespaceAware(true); |
415 |
dbf.setValidating( false ); |
418 |
dbf.setValidating(false); |
416 |
} |
419 |
} |
417 |
db = dbf.newDocumentBuilder(); |
420 |
db = dbf.newDocumentBuilder(); |
418 |
|
421 |
|
419 |
DOMImplementation dim = db.getDOMImplementation(); |
422 |
DOMImplementation dim = db.getDOMImplementation(); |
420 |
d = dim.createDocument("http://java.sun.com/jstl", "dummyroot", null); |
423 |
d = dim.createDocument("http://java.sun.com/jstl", "dummyroot", null); |
421 |
//d = db.newDocument(); |
424 |
//d = db.newDocument(); |
422 |
return d; |
425 |
return d; |
423 |
} catch ( Exception e ) { |
426 |
} catch (Exception e) { |
424 |
e.printStackTrace(); |
427 |
e.printStackTrace(); |
425 |
} |
428 |
} |
426 |
return null; |
429 |
return null; |
427 |
} |
430 |
} |
428 |
|
431 |
|
429 |
static Document getDummyDocumentWithoutRoot( ) { |
432 |
static Document getDummyDocumentWithoutRoot() { |
430 |
try { |
433 |
try { |
431 |
if ( dbf == null ) { |
434 |
if (dbf == null) { |
432 |
dbf = DocumentBuilderFactory.newInstance(); |
435 |
dbf = DocumentBuilderFactory.newInstance(); |
433 |
dbf.setNamespaceAware( true ); |
436 |
dbf.setNamespaceAware(true); |
434 |
dbf.setValidating( false ); |
437 |
dbf.setValidating(false); |
435 |
} |
438 |
} |
436 |
db = dbf.newDocumentBuilder(); |
439 |
db = dbf.newDocumentBuilder(); |
437 |
|
440 |
|
438 |
d = db.newDocument(); |
441 |
d = db.newDocument(); |
439 |
return d; |
442 |
return d; |
440 |
} catch ( Exception e ) { |
443 |
} catch (Exception e) { |
441 |
e.printStackTrace(); |
444 |
e.printStackTrace(); |
442 |
} |
445 |
} |
443 |
return null; |
446 |
return null; |
Lines 449-545
Link Here
|
449 |
doc.appendChild(importedNode); |
452 |
doc.appendChild(importedNode); |
450 |
return doc; |
453 |
return doc; |
451 |
} |
454 |
} |
452 |
|
455 |
|
453 |
// The following variable is used for holding the modified xpath string |
456 |
// The following variable is used for holding the modified xpath string |
454 |
// when adapting parameter for Xalan XPath engine, where we need to have |
457 |
// when adapting parameter for Xalan XPath engine, where we need to have |
455 |
// a Non null context node. |
458 |
// a Non null context node. |
456 |
String modifiedXPath = null; |
459 |
String modifiedXPath = null; |
457 |
|
|
|
458 |
|
460 |
|
459 |
|
461 |
|
460 |
|
|
|
461 |
|
462 |
/** |
462 |
/** |
463 |
* Evaluate an XPath expression to a String value. |
463 |
* Evaluate an XPath expression to a String value. |
464 |
*/ |
464 |
*/ |
465 |
public String valueOf(Node n, String xpath) throws JspTagException { |
465 |
public String valueOf(Node n, String xpath) throws JspTagException { |
466 |
//p("******** valueOf(" + n + ", " + xpath + ")"); |
466 |
//p("******** valueOf(" + n + ", " + xpath + ")"); |
467 |
staticInit(); |
467 |
staticInit(); |
468 |
// @@@ but where do we set the Pag4eContext for the varaiblecontext? |
468 |
// @@@ but where do we set the Pag4eContext for the varaiblecontext? |
469 |
JstlVariableContext vs = new JstlVariableContext(); |
469 |
JstlVariableContext vs = new JstlVariableContext(); |
470 |
XPathContext xpathSupport = new XPathContext(); |
470 |
XPathContext xpathSupport = new XPathContext(); |
471 |
xpathSupport.setVarStack( vs); |
471 |
xpathSupport.setVarStack(vs); |
472 |
|
472 |
|
473 |
Vector varVector = fillVarStack(vs, xpathSupport); |
473 |
Vector varVector = fillVarStack(vs, xpathSupport); |
474 |
|
474 |
|
475 |
Node contextNode = adaptParamsForXalan( vs, n, xpath.trim() ); |
475 |
Node contextNode = adaptParamsForXalan(vs, n, xpath.trim()); |
476 |
|
476 |
|
477 |
xpath = modifiedXPath; |
477 |
xpath = modifiedXPath; |
478 |
|
478 |
|
479 |
//p("******** valueOf: modified xpath: " + xpath); |
479 |
//p("******** valueOf: modified xpath: " + xpath); |
480 |
|
480 |
|
481 |
XObject result = JSTLXPathAPI.eval( contextNode, xpath, |
481 |
XObject result = JSTLXPathAPI.eval(contextNode, xpath, |
482 |
jstlPrefixResolver,xpathSupport, varVector); |
482 |
jstlPrefixResolver, xpathSupport, varVector); |
483 |
|
483 |
|
484 |
|
484 |
|
485 |
//p("******Result TYPE => " + result.getTypeString() ); |
485 |
//p("******Result TYPE => " + result.getTypeString() ); |
486 |
|
486 |
|
487 |
String resultString = result.str(); |
487 |
String resultString = result.str(); |
488 |
//p("******** valueOf: after eval: " + resultString); |
488 |
//p("******** valueOf: after eval: " + resultString); |
489 |
|
489 |
|
490 |
return resultString; |
490 |
return resultString; |
491 |
|
|
|
492 |
} |
493 |
|
491 |
|
494 |
/** |
492 |
} |
495 |
* Evaluate an XPath expression to a boolean value. |
493 |
|
|
|
494 |
/** |
495 |
* Evaluate an XPath expression to a boolean value. |
496 |
*/ |
496 |
*/ |
497 |
public boolean booleanValueOf(Node n, String xpath) |
497 |
public boolean booleanValueOf(Node n, String xpath) |
498 |
throws JspTagException { |
498 |
throws JspTagException { |
499 |
|
499 |
|
500 |
staticInit(); |
500 |
staticInit(); |
501 |
JstlVariableContext vs = new JstlVariableContext(); |
501 |
JstlVariableContext vs = new JstlVariableContext(); |
502 |
XPathContext xpathSupport = new XPathContext(); |
502 |
XPathContext xpathSupport = new XPathContext(); |
503 |
xpathSupport.setVarStack( vs); |
503 |
xpathSupport.setVarStack(vs); |
504 |
|
504 |
|
505 |
Vector varVector = fillVarStack(vs, xpathSupport); |
505 |
Vector varVector = fillVarStack(vs, xpathSupport); |
506 |
|
506 |
|
507 |
Node contextNode = adaptParamsForXalan( vs, n, xpath.trim() ); |
507 |
Node contextNode = adaptParamsForXalan(vs, n, xpath.trim()); |
508 |
xpath = modifiedXPath; |
508 |
xpath = modifiedXPath; |
509 |
|
509 |
|
510 |
XObject result = JSTLXPathAPI.eval( contextNode, xpath, |
510 |
XObject result = JSTLXPathAPI.eval(contextNode, xpath, |
511 |
jstlPrefixResolver, xpathSupport, varVector); |
511 |
jstlPrefixResolver, xpathSupport, varVector); |
512 |
|
512 |
|
513 |
try { |
513 |
try { |
514 |
return result.bool(); |
514 |
return result.bool(); |
515 |
} catch (TransformerException ex) { |
515 |
} catch (TransformerException ex) { |
516 |
throw new JspTagException( |
516 |
throw new JspTagException( |
517 |
Resources.getMessage("XPATH_ERROR_XOBJECT", ex.toString()), ex); |
517 |
Resources.getMessage("XPATH_ERROR_XOBJECT", ex.toString()), ex); |
518 |
} |
518 |
} |
519 |
} |
519 |
} |
520 |
|
520 |
|
521 |
/** |
521 |
/** |
522 |
* Evaluate an XPath expression to a List of nodes. |
522 |
* Evaluate an XPath expression to a List of nodes. |
523 |
*/ |
523 |
*/ |
524 |
public List selectNodes(Node n, String xpath) throws JspTagException { |
524 |
public List selectNodes(Node n, String xpath) throws JspTagException { |
525 |
|
525 |
|
526 |
staticInit(); |
526 |
staticInit(); |
527 |
JstlVariableContext vs = new JstlVariableContext(); |
527 |
JstlVariableContext vs = new JstlVariableContext(); |
528 |
XPathContext xpathSupport = new XPathContext(); |
528 |
XPathContext xpathSupport = new XPathContext(); |
529 |
xpathSupport.setVarStack( vs); |
529 |
xpathSupport.setVarStack(vs); |
530 |
|
|
|
531 |
Vector varVector = fillVarStack(vs, xpathSupport); |
532 |
|
530 |
|
533 |
Node contextNode = adaptParamsForXalan( vs, n, xpath.trim() ); |
531 |
Vector varVector = fillVarStack(vs, xpathSupport); |
|
|
532 |
|
533 |
Node contextNode = adaptParamsForXalan(vs, n, xpath.trim()); |
534 |
xpath = modifiedXPath; |
534 |
xpath = modifiedXPath; |
535 |
|
535 |
|
536 |
XObject result = JSTLXPathAPI.eval( contextNode, xpath, |
536 |
XObject result = JSTLXPathAPI.eval(contextNode, xpath, |
537 |
jstlPrefixResolver,xpathSupport, varVector); |
537 |
jstlPrefixResolver, xpathSupport, varVector); |
538 |
try { |
538 |
try { |
539 |
NodeList nl= JSTLXPathAPI.getNodeList(result); |
539 |
NodeList nl = JSTLXPathAPI.getNodeList(result); |
540 |
return new JSTLNodeList( nl ); |
540 |
return new JSTLNodeList(nl); |
541 |
} catch ( JspTagException e ) { |
541 |
} catch (JspTagException e) { |
542 |
try { |
542 |
try { |
543 |
//If result can't be converted to NodeList we receive exception |
543 |
//If result can't be converted to NodeList we receive exception |
544 |
// In this case we may have single primitive value as the result |
544 |
// In this case we may have single primitive value as the result |
545 |
// Populating List with this value ( String, Boolean or Number ) |
545 |
// Populating List with this value ( String, Boolean or Number ) |
Lines 547-700
Link Here
|
547 |
//System.out.println("JSTLXPathAPI.getNodeList thrown exception:"+ e); |
547 |
//System.out.println("JSTLXPathAPI.getNodeList thrown exception:"+ e); |
548 |
Vector vector = new Vector(); |
548 |
Vector vector = new Vector(); |
549 |
Object resultObject = null; |
549 |
Object resultObject = null; |
550 |
if ( result.getType()== XObject.CLASS_BOOLEAN ) { |
550 |
if (result.getType() == XObject.CLASS_BOOLEAN) { |
551 |
resultObject = new Boolean( result.bool()); |
551 |
resultObject = result.bool(); |
552 |
} else if ( result.getType()== XObject.CLASS_NUMBER ) { |
552 |
} else if (result.getType() == XObject.CLASS_NUMBER) { |
553 |
resultObject = new Double( result.num()); |
553 |
resultObject = result.num(); |
554 |
} else if ( result.getType()== XObject.CLASS_STRING ) { |
554 |
} else if (result.getType() == XObject.CLASS_STRING) { |
555 |
resultObject = result.str(); |
555 |
resultObject = result.str(); |
556 |
} |
556 |
} |
557 |
|
557 |
|
558 |
vector.add( resultObject ); |
558 |
vector.add(resultObject); |
559 |
return new JSTLNodeList ( vector ); |
559 |
return new JSTLNodeList(vector); |
560 |
} catch ( TransformerException te ) { |
560 |
} catch (TransformerException te) { |
561 |
throw new JspTagException(te.toString(), te); |
561 |
throw new JspTagException(te.toString(), te); |
562 |
} |
562 |
} |
563 |
} |
563 |
} |
564 |
|
564 |
|
565 |
|
565 |
|
566 |
|
|
|
567 |
} |
566 |
} |
568 |
|
567 |
|
569 |
/** |
568 |
/** |
570 |
* Evaluate an XPath expression to a single node. |
569 |
* Evaluate an XPath expression to a single node. |
571 |
*/ |
570 |
*/ |
572 |
public Node selectSingleNode(Node n, String xpath) |
571 |
public Node selectSingleNode(Node n, String xpath) |
573 |
throws JspTagException { |
572 |
throws JspTagException { |
574 |
//p("selectSingleNode of XPathUtil = passed node:" + |
573 |
//p("selectSingleNode of XPathUtil = passed node:" + |
575 |
// "xpath => " + n + " : " + xpath ); |
574 |
// "xpath => " + n + " : " + xpath ); |
576 |
|
575 |
|
577 |
staticInit(); |
576 |
staticInit(); |
578 |
JstlVariableContext vs = new JstlVariableContext(); |
577 |
JstlVariableContext vs = new JstlVariableContext(); |
579 |
XPathContext xpathSupport = new XPathContext(); |
578 |
XPathContext xpathSupport = new XPathContext(); |
580 |
xpathSupport.setVarStack( vs); |
579 |
xpathSupport.setVarStack(vs); |
581 |
|
|
|
582 |
Vector varVector = fillVarStack(vs, xpathSupport); |
583 |
|
580 |
|
584 |
Node contextNode = adaptParamsForXalan( vs, n, xpath.trim() ); |
581 |
Vector varVector = fillVarStack(vs, xpathSupport); |
|
|
582 |
|
583 |
Node contextNode = adaptParamsForXalan(vs, n, xpath.trim()); |
585 |
xpath = modifiedXPath; |
584 |
xpath = modifiedXPath; |
586 |
|
585 |
|
587 |
return (Node) JSTLXPathAPI.selectSingleNode( contextNode, xpath, |
586 |
return (Node) JSTLXPathAPI.selectSingleNode(contextNode, xpath, |
588 |
jstlPrefixResolver,xpathSupport ); |
587 |
jstlPrefixResolver, xpathSupport); |
589 |
} |
588 |
} |
590 |
|
589 |
|
591 |
/** Returns a locally appropriate context given a node. */ |
590 |
/** |
|
|
591 |
* Returns a locally appropriate context given a node. |
592 |
*/ |
592 |
private VariableStack getLocalContext() { |
593 |
private VariableStack getLocalContext() { |
593 |
// set up instance-specific contexts |
594 |
// set up instance-specific contexts |
594 |
VariableStack vc = new JstlVariableContext(); |
595 |
VariableStack vc = new JstlVariableContext(); |
595 |
return vc; |
596 |
return vc; |
596 |
} |
597 |
} |
597 |
|
598 |
|
598 |
//********************************************************************* |
599 |
//********************************************************************* |
599 |
// Adapt XPath expression for integration with Xalan |
600 |
// Adapt XPath expression for integration with Xalan |
600 |
|
601 |
|
601 |
/** |
602 |
/** |
602 |
* To evaluate an XPath expression using Xalan, we need |
603 |
* To evaluate an XPath expression using Xalan, we need |
603 |
* to create an XPath object, which wraps an expression object and provides |
604 |
* to create an XPath object, which wraps an expression object and provides |
604 |
* general services for execution of that expression. |
605 |
* general services for execution of that expression. |
605 |
* |
606 |
* <p/> |
606 |
* An XPath object can be instantiated with the following information: |
607 |
* An XPath object can be instantiated with the following information: |
607 |
* - XPath expression to evaluate |
608 |
* - XPath expression to evaluate |
608 |
* - SourceLocator |
609 |
* - SourceLocator |
609 |
* (reports where an error occurred in the XML source or |
610 |
* (reports where an error occurred in the XML source or |
610 |
* transformation instructions) |
611 |
* transformation instructions) |
611 |
* - PrefixResolver |
612 |
* - PrefixResolver |
612 |
* (resolve prefixes to namespace URIs) |
613 |
* (resolve prefixes to namespace URIs) |
613 |
* - type |
614 |
* - type |
614 |
* (one of SELECT or MATCH) |
615 |
* (one of SELECT or MATCH) |
615 |
* - ErrorListener |
616 |
* - ErrorListener |
616 |
* (customized error handling) |
617 |
* (customized error handling) |
617 |
* |
618 |
* <p/> |
618 |
* Execution of the XPath expression represented by an XPath object |
619 |
* Execution of the XPath expression represented by an XPath object |
619 |
* is done via method execute which takes the following parameters: |
620 |
* is done via method execute which takes the following parameters: |
620 |
* - XPathContext |
621 |
* - XPathContext |
621 |
* The execution context |
622 |
* The execution context |
622 |
* - Node contextNode |
623 |
* - Node contextNode |
623 |
* The node that "." expresses |
624 |
* The node that "." expresses |
624 |
* - PrefixResolver namespaceContext |
625 |
* - PrefixResolver namespaceContext |
625 |
* The context in which namespaces in the XPath are supposed to be |
626 |
* The context in which namespaces in the XPath are supposed to be |
626 |
* expanded. |
627 |
* expanded. |
627 |
* |
628 |
* <p/> |
628 |
* Given all of this, if no context node is set for the evaluation |
629 |
* Given all of this, if no context node is set for the evaluation |
629 |
* of the XPath expression, one must be set so Xalan |
630 |
* of the XPath expression, one must be set so Xalan |
630 |
* can successfully evaluate a JSTL XPath expression. |
631 |
* can successfully evaluate a JSTL XPath expression. |
631 |
* (it will not work if the context node is given as a varialbe |
632 |
* (it will not work if the context node is given as a varialbe |
632 |
* at the beginning of the expression) |
633 |
* at the beginning of the expression) |
633 |
* |
634 |
* |
634 |
* @@@ Provide more details... |
635 |
* @@@ Provide more details... |
635 |
*/ |
636 |
*/ |
636 |
protected Node adaptParamsForXalan( JstlVariableContext jvc, Node n, |
637 |
protected Node adaptParamsForXalan(JstlVariableContext jvc, Node n, |
637 |
String xpath ) { |
638 |
String xpath) { |
638 |
Node boundDocument = null; |
639 |
Node boundDocument = null; |
639 |
|
640 |
|
640 |
modifiedXPath = xpath; |
641 |
modifiedXPath = xpath; |
641 |
String origXPath = xpath ; |
642 |
String origXPath = xpath; |
642 |
boolean whetherOrigXPath = true; |
643 |
boolean whetherOrigXPath = true; |
643 |
|
644 |
|
644 |
// If contextNode is not null then just pass the values to Xalan XPath |
645 |
// If contextNode is not null then just pass the values to Xalan XPath |
645 |
// unless this is an expression that starts off with an xml document |
646 |
// unless this is an expression that starts off with an xml document |
646 |
if ( n != null && !xpath.startsWith("$") ) { |
647 |
if (n != null && !xpath.startsWith("$")) { |
647 |
return n; |
648 |
return n; |
648 |
} |
649 |
} |
649 |
|
650 |
|
650 |
if ( xpath.startsWith("$") ) { |
651 |
if (xpath.startsWith("$")) { |
651 |
// JSTL uses $scopePrefix:varLocalName/xpath expression |
652 |
// JSTL uses $scopePrefix:varLocalName/xpath expression |
652 |
|
653 |
|
653 |
String varQName= xpath.substring( xpath.indexOf("$")+1); |
654 |
String varQName = xpath.substring(xpath.indexOf("$") + 1); |
654 |
if ( varQName.indexOf("/") > 0 ) { |
655 |
if (varQName.indexOf("/") > 0) { |
655 |
varQName = varQName.substring( 0, varQName.indexOf("/")); |
656 |
varQName = varQName.substring(0, varQName.indexOf("/")); |
656 |
} |
657 |
} |
657 |
String varPrefix = null; |
658 |
String varPrefix = null; |
658 |
String varLocalName = varQName; |
659 |
String varLocalName = varQName; |
659 |
if ( varQName.indexOf( ":") >= 0 ) { |
660 |
if (varQName.indexOf(":") >= 0) { |
660 |
varPrefix = varQName.substring( 0, varQName.indexOf(":") ); |
661 |
varPrefix = varQName.substring(0, varQName.indexOf(":")); |
661 |
varLocalName = varQName.substring( varQName.indexOf(":")+1 ); |
662 |
varLocalName = varQName.substring(varQName.indexOf(":") + 1); |
662 |
} |
663 |
} |
663 |
|
664 |
|
664 |
if ( xpath.indexOf("/") > 0 ) { |
665 |
if (xpath.indexOf("/") > 0) { |
665 |
xpath = xpath.substring( xpath.indexOf("/")); |
666 |
xpath = xpath.substring(xpath.indexOf("/")); |
666 |
} else { |
667 |
} else { |
667 |
xpath = "/*"; |
668 |
xpath = "/*"; |
668 |
whetherOrigXPath = false; |
669 |
whetherOrigXPath = false; |
669 |
} |
670 |
} |
670 |
|
671 |
|
671 |
|
672 |
|
672 |
try { |
673 |
try { |
673 |
Object varObject=jvc.getVariableValue( null,varPrefix, |
674 |
Object varObject = jvc.getVariableValue(null, varPrefix, |
674 |
varLocalName); |
675 |
varLocalName); |
675 |
//System.out.println( "varObject => : its Class " +varObject + |
676 |
//System.out.println( "varObject => : its Class " +varObject + |
676 |
// ":" + varObject.getClass() ); |
677 |
// ":" + varObject.getClass() ); |
677 |
|
678 |
|
678 |
if ( Class.forName("org.w3c.dom.Document").isInstance( |
679 |
if (Class.forName("org.w3c.dom.Document").isInstance( |
679 |
varObject ) ) { |
680 |
varObject)) { |
680 |
//boundDocument = ((Document)varObject).getDocumentElement(); |
681 |
//boundDocument = ((Document)varObject).getDocumentElement(); |
681 |
boundDocument = ((Document)varObject); |
682 |
boundDocument = ((Document) varObject); |
682 |
} else { |
683 |
} else { |
683 |
|
684 |
|
684 |
//System.out.println("Creating a Dummy document to pass " + |
685 |
//System.out.println("Creating a Dummy document to pass " + |
685 |
// " onto as context node " ); |
686 |
// " onto as context node " ); |
686 |
|
687 |
|
687 |
if ( Class.forName("org.apache.taglibs.standard.tag.common.xml.JSTLNodeList").isInstance( varObject ) ) { |
688 |
if (Class.forName("org.apache.taglibs.standard.tag.common.xml.JSTLNodeList").isInstance(varObject)) { |
688 |
Document newDocument = getDummyDocument(); |
689 |
Document newDocument = getDummyDocument(); |
689 |
|
690 |
|
690 |
JSTLNodeList jstlNodeList = (JSTLNodeList)varObject; |
691 |
JSTLNodeList jstlNodeList = (JSTLNodeList) varObject; |
691 |
if ( jstlNodeList.getLength() == 1 ) { |
692 |
if (jstlNodeList.getLength() == 1) { |
692 |
if ( Class.forName("org.w3c.dom.Node").isInstance( |
693 |
if (Class.forName("org.w3c.dom.Node").isInstance( |
693 |
jstlNodeList.elementAt(0) ) ) { |
694 |
jstlNodeList.elementAt(0))) { |
694 |
Node node = (Node)jstlNodeList.elementAt(0); |
695 |
Node node = (Node) jstlNodeList.elementAt(0); |
695 |
boundDocument = getDocumentForNode(node); |
696 |
boundDocument = getDocumentForNode(node); |
696 |
if ( whetherOrigXPath ) { |
697 |
if (whetherOrigXPath) { |
697 |
xpath="/*" + xpath; |
698 |
xpath = "/*" + xpath; |
698 |
} |
699 |
} |
699 |
|
700 |
|
700 |
} else { |
701 |
} else { |
Lines 709-728
Link Here
|
709 |
|
710 |
|
710 |
//p("String value ( xpathwould be this) => " + xpath); |
711 |
//p("String value ( xpathwould be this) => " + xpath); |
711 |
boundDocument = newDocument; |
712 |
boundDocument = newDocument; |
712 |
} |
713 |
} |
713 |
|
714 |
|
714 |
} else { |
715 |
} else { |
715 |
|
716 |
|
716 |
Element dummyroot = newDocument.getDocumentElement(); |
717 |
Element dummyroot = newDocument.getDocumentElement(); |
717 |
for ( int i=0; i< jstlNodeList.getLength(); i++ ) { |
718 |
for (int i = 0; i < jstlNodeList.getLength(); i++) { |
718 |
Node currNode = (Node)jstlNodeList.item(i); |
719 |
Node currNode = (Node) jstlNodeList.item(i); |
719 |
|
720 |
|
720 |
Node importedNode = newDocument.importNode( |
721 |
Node importedNode = newDocument.importNode( |
721 |
currNode, true ); |
722 |
currNode, true); |
722 |
|
723 |
|
723 |
//printDetails ( newDocument); |
724 |
//printDetails ( newDocument); |
724 |
|
725 |
|
725 |
dummyroot.appendChild( importedNode ); |
726 |
dummyroot.appendChild(importedNode); |
726 |
|
727 |
|
727 |
//p( "Details of the document After importing"); |
728 |
//p( "Details of the document After importing"); |
728 |
//printDetails ( newDocument); |
729 |
//printDetails ( newDocument); |
Lines 733-743
Link Here
|
733 |
// to change the xpath expression.Hopefully this |
734 |
// to change the xpath expression.Hopefully this |
734 |
// won't change the result |
735 |
// won't change the result |
735 |
|
736 |
|
736 |
xpath = "/*" + xpath; |
737 |
xpath = "/*" + xpath; |
737 |
} |
738 |
} |
738 |
} else if ( Class.forName("org.w3c.dom.Node").isInstance( |
739 |
} else if (Class.forName("org.w3c.dom.Node").isInstance( |
739 |
varObject ) ) { |
740 |
varObject)) { |
740 |
boundDocument = getDocumentForNode((Node)varObject); |
741 |
boundDocument = getDocumentForNode((Node) varObject); |
741 |
if (whetherOrigXPath) { |
742 |
if (whetherOrigXPath) { |
742 |
xpath = "/*" + xpath; |
743 |
xpath = "/*" + xpath; |
743 |
} |
744 |
} |
Lines 745-891
Link Here
|
745 |
boundDocument = getDummyDocument(); |
746 |
boundDocument = getDummyDocument(); |
746 |
xpath = origXPath; |
747 |
xpath = origXPath; |
747 |
} |
748 |
} |
748 |
|
749 |
|
749 |
|
750 |
|
750 |
} |
751 |
} |
751 |
} catch ( UnresolvableException ue ) { |
752 |
} catch (UnresolvableException ue) { |
752 |
// TODO: LOG |
753 |
// TODO: LOG |
753 |
System.out.println("Variable Unresolvable :" + ue.getMessage()); |
754 |
System.out.println("Variable Unresolvable :" + ue.getMessage()); |
754 |
ue.printStackTrace(); |
755 |
ue.printStackTrace(); |
755 |
} catch ( ClassNotFoundException cnf ) { |
756 |
} catch (ClassNotFoundException cnf) { |
756 |
// Will never happen |
757 |
// Will never happen |
757 |
} |
758 |
} |
758 |
} else { |
759 |
} else { |
759 |
//System.out.println("Not encountered $ Creating a Dummydocument 2 "+ |
760 |
//System.out.println("Not encountered $ Creating a Dummydocument 2 "+ |
760 |
// "pass onto as context node " ); |
761 |
// "pass onto as context node " ); |
761 |
boundDocument = getDummyDocument(); |
762 |
boundDocument = getDummyDocument(); |
762 |
} |
763 |
} |
763 |
|
764 |
|
764 |
modifiedXPath = xpath; |
765 |
modifiedXPath = xpath; |
765 |
//System.out.println("Modified XPath::boundDocument =>" + modifiedXPath + |
766 |
//System.out.println("Modified XPath::boundDocument =>" + modifiedXPath + |
766 |
// "::" + boundDocument ); |
767 |
// "::" + boundDocument ); |
767 |
|
768 |
|
768 |
return boundDocument; |
769 |
return boundDocument; |
769 |
} |
770 |
} |
770 |
|
|
|
771 |
|
771 |
|
|
|
772 |
|
772 |
//********************************************************************* |
773 |
//********************************************************************* |
773 |
// |
774 |
// |
774 |
|
775 |
|
775 |
/** |
776 |
/** |
776 |
** @@@ why do we have to pass varVector in the varStack first, and then |
777 |
* * @@@ why do we have to pass varVector in the varStack first, and then |
777 |
* to XPath object? |
778 |
* to XPath object? |
778 |
*/ |
779 |
*/ |
779 |
private Vector fillVarStack(JstlVariableContext vs, XPathContext xpathSupport) |
780 |
private Vector fillVarStack(JstlVariableContext vs, XPathContext xpathSupport) |
780 |
throws JspTagException { |
781 |
throws JspTagException { |
781 |
org.apache.xpath.VariableStack myvs = xpathSupport.getVarStack(); |
782 |
org.apache.xpath.VariableStack myvs = xpathSupport.getVarStack(); |
782 |
Vector varVector = getVariableQNames(); |
783 |
Vector varVector = getVariableQNames(); |
783 |
for ( int i=0; i<varVector.size(); i++ ) { |
784 |
for (int i = 0; i < varVector.size(); i++) { |
784 |
|
|
|
785 |
QName varQName = (QName)varVector.elementAt(i); |
786 |
|
785 |
|
787 |
try { |
786 |
QName varQName = (QName) varVector.elementAt(i); |
788 |
XObject variableValue = vs.getVariableOrParam( xpathSupport, varQName ); |
787 |
|
|
|
788 |
try { |
789 |
XObject variableValue = vs.getVariableOrParam(xpathSupport, varQName); |
789 |
//p("&&&&Variable set to => " + variableValue.toString() ); |
790 |
//p("&&&&Variable set to => " + variableValue.toString() ); |
790 |
//p("&&&&Variable type => " + variableValue.getTypeString() ); |
791 |
//p("&&&&Variable type => " + variableValue.getTypeString() ); |
791 |
myvs.setGlobalVariable( i, variableValue ); |
792 |
myvs.setGlobalVariable(i, variableValue); |
792 |
|
793 |
|
793 |
} catch ( TransformerException te ) { |
794 |
} catch (TransformerException te) { |
794 |
throw new JspTagException(te.toString(), te); |
795 |
throw new JspTagException(te.toString(), te); |
795 |
} |
796 |
} |
796 |
|
797 |
|
797 |
} |
798 |
} |
798 |
return varVector; |
799 |
return varVector; |
799 |
} |
800 |
} |
800 |
|
801 |
|
801 |
|
802 |
|
802 |
|
|
|
803 |
|
804 |
|
805 |
//********************************************************************* |
803 |
//********************************************************************* |
806 |
// Static support for context retrieval from parent <forEach> tag |
804 |
// Static support for context retrieval from parent <forEach> tag |
807 |
|
805 |
|
808 |
public static Node getContext(Tag t) throws JspTagException { |
806 |
public static Node getContext(Tag t) throws JspTagException { |
809 |
ForEachTag xt = |
807 |
ForEachTag xt = |
810 |
(ForEachTag) TagSupport.findAncestorWithClass( |
808 |
(ForEachTag) TagSupport.findAncestorWithClass( |
811 |
t, ForEachTag.class); |
809 |
t, ForEachTag.class); |
812 |
if (xt == null) |
810 |
if (xt == null) { |
813 |
return null; |
811 |
return null; |
814 |
else |
812 |
} else { |
815 |
return (xt.getContext()); |
813 |
return (xt.getContext()); |
|
|
814 |
} |
816 |
} |
815 |
} |
817 |
|
816 |
|
818 |
//********************************************************************* |
817 |
//********************************************************************* |
819 |
// Utility methods |
818 |
// Utility methods |
820 |
|
819 |
|
821 |
private static void p(String s) { |
820 |
private static void p(String s) { |
822 |
System.out.println("[XPathUtil] " + s); |
821 |
System.out.println("[XPathUtil] " + s); |
823 |
} |
822 |
} |
824 |
|
823 |
|
825 |
public static void printDetails(Node n) { |
824 |
public static void printDetails(Node n) { |
826 |
System.out.println("\n\nDetails of Node = > " + n ) ; |
825 |
System.out.println("\n\nDetails of Node = > " + n); |
827 |
System.out.println("Name:Type:Node Value = > " + n.getNodeName() + |
826 |
System.out.println("Name:Type:Node Value = > " + n.getNodeName() + |
828 |
":" + n.getNodeType() + ":" + n.getNodeValue() ) ; |
827 |
":" + n.getNodeType() + ":" + n.getNodeValue()); |
829 |
System.out.println("Namespace URI : Prefix : localName = > " + |
828 |
System.out.println("Namespace URI : Prefix : localName = > " + |
830 |
n.getNamespaceURI() + ":" +n.getPrefix() + ":" + n.getLocalName()); |
829 |
n.getNamespaceURI() + ":" + n.getPrefix() + ":" + n.getLocalName()); |
831 |
System.out.println("\n Node has children => " + n.hasChildNodes() ); |
830 |
System.out.println("\n Node has children => " + n.hasChildNodes()); |
832 |
if ( n.hasChildNodes() ) { |
831 |
if (n.hasChildNodes()) { |
833 |
NodeList nl = n.getChildNodes(); |
832 |
NodeList nl = n.getChildNodes(); |
834 |
System.out.println("Number of Children => " + nl.getLength() ); |
833 |
System.out.println("Number of Children => " + nl.getLength()); |
835 |
for ( int i=0; i<nl.getLength(); i++ ) { |
834 |
for (int i = 0; i < nl.getLength(); i++) { |
836 |
Node childNode = nl.item(i); |
835 |
Node childNode = nl.item(i); |
837 |
printDetails( childNode ); |
836 |
printDetails(childNode); |
838 |
} |
837 |
} |
839 |
} |
838 |
} |
840 |
} |
839 |
} |
841 |
} |
840 |
} |
842 |
|
841 |
|
843 |
class JSTLNodeList extends Vector implements NodeList { |
842 |
class JSTLNodeList extends Vector implements NodeList { |
844 |
|
843 |
|
845 |
Vector nodeVector; |
844 |
Vector nodeVector; |
846 |
|
845 |
|
847 |
public JSTLNodeList ( Vector nodeVector ) { |
846 |
public JSTLNodeList(Vector nodeVector) { |
848 |
this.nodeVector = nodeVector; |
847 |
this.nodeVector = nodeVector; |
849 |
} |
848 |
} |
850 |
|
849 |
|
851 |
public JSTLNodeList ( NodeList nl ) { |
850 |
public JSTLNodeList(NodeList nl) { |
852 |
nodeVector = new Vector(); |
851 |
nodeVector = new Vector(); |
853 |
//System.out.println("[JSTLNodeList] nodelist details"); |
852 |
//System.out.println("[JSTLNodeList] nodelist details"); |
854 |
for ( int i=0; i<nl.getLength(); i++ ) { |
853 |
for (int i = 0; i < nl.getLength(); i++) { |
855 |
Node currNode = nl.item(i); |
854 |
Node currNode = nl.item(i); |
856 |
//XPathUtil.printDetails ( currNode ); |
855 |
//XPathUtil.printDetails ( currNode ); |
857 |
nodeVector.add(i, nl.item(i) ); |
856 |
nodeVector.add(i, nl.item(i)); |
858 |
} |
857 |
} |
859 |
} |
858 |
} |
860 |
|
859 |
|
861 |
public JSTLNodeList ( Node n ) { |
860 |
public JSTLNodeList(Node n) { |
862 |
nodeVector = new Vector(); |
861 |
nodeVector = new Vector(); |
863 |
nodeVector.addElement( n ); |
862 |
nodeVector.addElement(n); |
864 |
} |
863 |
} |
865 |
|
|
|
866 |
|
864 |
|
867 |
public Node item ( int index ) { |
865 |
|
868 |
return (Node)nodeVector.elementAt( index ); |
866 |
public Node item(int index) { |
|
|
867 |
return (Node) nodeVector.elementAt(index); |
869 |
} |
868 |
} |
870 |
|
869 |
|
871 |
@Override |
870 |
@Override |
872 |
public Object elementAt ( int index ) { |
871 |
public Object elementAt(int index) { |
873 |
return nodeVector.elementAt( index ); |
872 |
return nodeVector.elementAt(index); |
874 |
} |
873 |
} |
875 |
|
874 |
|
876 |
@Override |
875 |
@Override |
877 |
public Object get ( int index ) { |
876 |
public Object get(int index) { |
878 |
return nodeVector.get( index ); |
877 |
return nodeVector.get(index); |
879 |
} |
878 |
} |
880 |
|
879 |
|
881 |
public int getLength ( ) { |
880 |
public int getLength() { |
882 |
return nodeVector.size( ); |
881 |
return nodeVector.size(); |
883 |
} |
882 |
} |
884 |
|
883 |
|
885 |
@Override |
884 |
@Override |
886 |
public int size ( ) { |
885 |
public int size() { |
887 |
//System.out.println("JSTL node list size => " + nodeVector.size() ); |
886 |
//System.out.println("JSTL node list size => " + nodeVector.size() ); |
888 |
return nodeVector.size( ); |
887 |
return nodeVector.size(); |
889 |
} |
888 |
} |
890 |
|
889 |
|
891 |
// Can implement other Vector methods to redirect those methods to |
890 |
// Can implement other Vector methods to redirect those methods to |