Save This Page
Home » struts-2.1.8.1-src » org.apache.struts2.jasper.tagplugins » jstl » core » [javadoc | source]
    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   
   18   
   19   package org.apache.struts2.jasper.tagplugins.jstl.core;
   20   
   21   import org.apache.struts2.jasper.compiler.tagplugin.TagPlugin;
   22   import org.apache.struts2.jasper.compiler.tagplugin.TagPluginContext;
   23   import org.apache.struts2.jasper.tagplugins.jstl.Util;
   24   
   25   public class Url implements TagPlugin {
   26   
   27       public void doTag(TagPluginContext ctxt) {
   28   
   29           //flags
   30           boolean hasVar, hasContext, hasScope;
   31   
   32           //init flags
   33           hasVar = ctxt.isAttributeSpecified("var");
   34           hasContext = ctxt.isAttributeSpecified("context");
   35           hasScope = ctxt.isAttributeSpecified("scope");
   36   
   37           //define name of the temp variables
   38           String valueName = ctxt.getTemporaryVariableName();
   39           String contextName = ctxt.getTemporaryVariableName();
   40           String baseUrlName = ctxt.getTemporaryVariableName();
   41           String resultName = ctxt.getTemporaryVariableName();
   42           String responseName = ctxt.getTemporaryVariableName();
   43   
   44           //get the scope
   45           String strScope = "page";
   46           if (hasScope) {
   47               strScope = ctxt.getConstantAttribute("scope");
   48           }
   49           int iScope = Util.getScope(strScope);
   50   
   51           //get the value
   52           ctxt.generateJavaSource("String " + valueName + " = ");
   53           ctxt.generateAttribute("value");
   54           ctxt.generateJavaSource(";");
   55   
   56           //get the context
   57           ctxt.generateJavaSource("String " + contextName + " = null;");
   58           if (hasContext) {
   59               ctxt.generateJavaSource(contextName + " = ");
   60               ctxt.generateAttribute("context");
   61               ctxt.generateJavaSource(";");
   62           }
   63   
   64           //get the raw url
   65           ctxt.generateJavaSource("String " + baseUrlName + " = " +
   66                   "org.apache.struts2.jasper.tagplugins.jstl.Util.resolveUrl(" + valueName + ", " + contextName + ", pageContext);");
   67           ctxt.generateJavaSource("pageContext.setAttribute" +
   68                   "(\"url_without_param\", " + baseUrlName + ");");
   69   
   70           //add params
   71           ctxt.generateBody();
   72   
   73           ctxt.generateJavaSource("String " + resultName + " = " +
   74                   "(String)pageContext.getAttribute(\"url_without_param\");");
   75           ctxt.generateJavaSource("pageContext.removeAttribute(\"url_without_param\");");
   76   
   77           //if the url is relative, encode it
   78           ctxt.generateJavaSource("if(!org.apache.struts2.jasper.tagplugins.jstl.Util.isAbsoluteUrl(" + resultName + ")){");
   79           ctxt.generateJavaSource("    HttpServletResponse " + responseName + " = " +
   80                   "((HttpServletResponse) pageContext.getResponse());");
   81           ctxt.generateJavaSource("    " + resultName + " = "
   82                   + responseName + ".encodeURL(" + resultName + ");");
   83           ctxt.generateJavaSource("}");
   84   
   85           //if "var" is specified, the url string store in the attribute var defines
   86           if (hasVar) {
   87               String strVar = ctxt.getConstantAttribute("var");
   88               ctxt.generateJavaSource("pageContext.setAttribute" +
   89                       "(\"" + strVar + "\", " + resultName + ", " + iScope + ");");
   90   
   91               //if var is not specified, just print out the url string
   92           } else {
   93               ctxt.generateJavaSource("try{");
   94               ctxt.generateJavaSource("    pageContext.getOut().print(" + resultName + ");");
   95               ctxt.generateJavaSource("}catch(java.io.IOException ex){");
   96               ctxt.generateJavaSource("    throw new JspTagException(ex.toString(), ex);");
   97               ctxt.generateJavaSource("}");
   98           }
   99       }
  100   
  101   }

Save This Page
Home » struts-2.1.8.1-src » org.apache.struts2.jasper.tagplugins » jstl » core » [javadoc | source]