FileDocCategorySizeDatePackage
JSTLXPathVariableResolver.javaAPI DocGlassfish v2 API8137Sat May 05 19:17:54 BST 2007org.apache.taglibs.standard.tag.common.xml

JSTLXPathVariableResolver

public class JSTLXPathVariableResolver extends Object implements XPathVariableResolver
The XPathVariableResolver implementation provides access to user XPath variables.

Fields Summary
private javax.servlet.jsp.PageContext
pageContext
private static final String
PAGE_NS_URL
private static final String
REQUEST_NS_URL
private static final String
SESSION_NS_URL
private static final String
APP_NS_URL
private static final String
PARAM_NS_URL
private static final String
INITPARAM_NS_URL
private static final String
COOKIE_NS_URL
private static final String
HEADER_NS_URL
Constructors Summary
public JSTLXPathVariableResolver(javax.servlet.jsp.PageContext pc)

    
    //*********************************************************************
    // Constructor

       
        pageContext = pc;
    
Methods Summary
protected java.lang.ObjectgetVariableValue(java.lang.String namespace, java.lang.String prefix, java.lang.String localName)
Retrieve an XPath's variable value using JSTL's custom variable-mapping rules

       
        // p("resolving: ns=" + namespace + " prefix=" + prefix + " localName=" + localName);  
        // We can match on namespace with Xalan but leaving as is
        // [ I 'd prefer to match on namespace, but this doesn't appear
        // to work in Jaxen] 
        if (namespace == null || namespace.equals("")) {
            return notNull(
            pageContext.findAttribute(localName),
            namespace,
            localName);
        } else if (namespace.equals(PAGE_NS_URL)) {
            return notNull(
            pageContext.getAttribute(localName,PageContext.PAGE_SCOPE),
            namespace,
            localName);
        } else if (namespace.equals(REQUEST_NS_URL)) {
            return notNull(
            pageContext.getAttribute(localName,
            PageContext.REQUEST_SCOPE),
            namespace,
            localName);
        } else if (namespace.equals(SESSION_NS_URL)) {
            return notNull(
            pageContext.getAttribute(localName,
            PageContext.SESSION_SCOPE),
            namespace,
            localName);
        } else if (namespace.equals(APP_NS_URL)) {
            return notNull(
            pageContext.getAttribute(localName,
            PageContext.APPLICATION_SCOPE),
            namespace,
            localName);
        } else if (namespace.equals(PARAM_NS_URL)) {
            return notNull(
            pageContext.getRequest().getParameter(localName),
            namespace,
            localName);            
        } else if (namespace.equals(INITPARAM_NS_URL)) {
            return notNull(
            pageContext.getServletContext().
            getInitParameter(localName),
            namespace,
            localName);
        } else if (namespace.equals(HEADER_NS_URL)) {
            HttpServletRequest hsr =
            (HttpServletRequest) pageContext.getRequest();
            return notNull(
            hsr.getHeader(localName),
            namespace,
            localName);
        } else if (namespace.equals(COOKIE_NS_URL)) {
            HttpServletRequest hsr =
            (HttpServletRequest) pageContext.getRequest();
            Cookie[] c = hsr.getCookies();
            for (int i = 0; i < c.length; i++)
                if (c[i].getName().equals(localName))
                    return c[i].getValue();
            throw new UnresolvableException("$" + namespace + ":" + localName);
        } else {
            throw new UnresolvableException("$" + namespace + ":" + localName);
        }
    
private java.lang.ObjectnotNull(java.lang.Object o, java.lang.String namespace, java.lang.String localName)
Validate that the Object returned is not null. If it is null, throw an exception.

        if (o == null) {
            throw new UnresolvableException("$" + (namespace==null?"":namespace+":") + localName);
        }
        // p("resolved to: " + o);
        return o;
    
private static voidp(java.lang.String s)

        System.out.println("[JSTLXPathVariableResolver] " + s);
    
public java.lang.ObjectresolveVariable(javax.xml.namespace.QName qname)
Find variable in set of variables

param
QName variable name
return
QName variables value
throws
NullPointerException if variable name is null


        Object varObject = null;

        if (qname == null) {
            throw new NullPointerException("Cannot resolve null variable");
        }

        String namespace = qname.getNamespaceURI();
        String prefix = qname.getPrefix();
        String localName = qname.getLocalPart();
        // p("[resolveVariable] namespace: " + namespace + " prefix: " + prefix + " localName: " + localName);

        try {
            varObject = getVariableValue(namespace, prefix, localName);
        } catch (UnresolvableException ue) {
            System.out.println("JSTLXpathVariableResolver.resolveVariable threw UnresolvableException: " + ue);
        }

        // p("[resolveVariable] varObject: " + varObject);
        return varObject;