FileDocCategorySizeDatePackage
Url.javaAPI DocApache Tomcat 6.0.144414Fri Jul 20 04:20:30 BST 2007org.apache.jasper.tagplugins.jstl.core

Url

public class Url extends Object implements org.apache.jasper.compiler.tagplugin.TagPlugin

Fields Summary
Constructors Summary
Methods Summary
public voiddoTag(org.apache.jasper.compiler.tagplugin.TagPluginContext ctxt)

        
        //flags
        boolean hasVar, hasContext, hasScope;
        
        //init flags
        hasVar = ctxt.isAttributeSpecified("var");
        hasContext = ctxt.isAttributeSpecified("context");
        hasScope = ctxt.isAttributeSpecified("scope");
        
        //define name of the temp variables
        String valueName = ctxt.getTemporaryVariableName();
        String contextName = ctxt.getTemporaryVariableName();
        String baseUrlName = ctxt.getTemporaryVariableName();
        String resultName = ctxt.getTemporaryVariableName();
        String responseName = ctxt.getTemporaryVariableName();
        
        //get the scope
        String strScope = "page";
        if(hasScope){
            strScope = ctxt.getConstantAttribute("scope");
        }
        int iScope = Util.getScope(strScope);
        
        //get the value
        ctxt.generateJavaSource("String " + valueName + " = ");
        ctxt.generateAttribute("value");
        ctxt.generateJavaSource(";");
        
        //get the context
        ctxt.generateJavaSource("String " + contextName + " = null;");
        if(hasContext){
            ctxt.generateJavaSource(contextName + " = ");
            ctxt.generateAttribute("context");
            ctxt.generateJavaSource(";");
        }
        
        //get the raw url
        ctxt.generateJavaSource("String " + baseUrlName + " = " +
                "org.apache.jasper.tagplugins.jstl.Util.resolveUrl(" + valueName + ", " + contextName + ", pageContext);");
        ctxt.generateJavaSource("pageContext.setAttribute" +
                "(\"url_without_param\", " + baseUrlName + ");");
        
        //add params
        ctxt.generateBody();
        
        ctxt.generateJavaSource("String " + resultName + " = " +
        "(String)pageContext.getAttribute(\"url_without_param\");");
        ctxt.generateJavaSource("pageContext.removeAttribute(\"url_without_param\");");
        
        //if the url is relative, encode it
        ctxt.generateJavaSource("if(!org.apache.jasper.tagplugins.jstl.Util.isAbsoluteUrl(" + resultName + ")){");
        ctxt.generateJavaSource("    HttpServletResponse " + responseName + " = " +
        "((HttpServletResponse) pageContext.getResponse());");
        ctxt.generateJavaSource("    " + resultName + " = "
                + responseName + ".encodeURL(" + resultName + ");");
        ctxt.generateJavaSource("}");
        
        //if "var" is specified, the url string store in the attribute var defines
        if(hasVar){
            String strVar = ctxt.getConstantAttribute("var");
            ctxt.generateJavaSource("pageContext.setAttribute" +
                    "(\"" + strVar + "\", " + resultName + ", " + iScope + ");");
            
            //if var is not specified, just print out the url string
        }else{
            ctxt.generateJavaSource("try{");
            ctxt.generateJavaSource("    pageContext.getOut().print(" + resultName + ");");
            ctxt.generateJavaSource("}catch(java.io.IOException ex){");
            ctxt.generateJavaSource("    throw new JspTagException(ex.toString(), ex);");
            ctxt.generateJavaSource("}");
        }