Java Code Examples for org.apache.jasper.compiler.tagplugin.TagPluginContext#getConstantAttribute()

The following examples show how to use org.apache.jasper.compiler.tagplugin.TagPluginContext#getConstantAttribute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: If.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
public void doTag(TagPluginContext ctxt) {
    String condV = ctxt.getTemporaryVariableName();
    ctxt.generateJavaSource("boolean " + condV + "=");
    ctxt.generateAttribute("test");
    ctxt.generateJavaSource(";");
    if (ctxt.isAttributeSpecified("var")) {
        String scope = "PageContext.PAGE_SCOPE";
        if (ctxt.isAttributeSpecified("scope")) {
            String scopeStr = ctxt.getConstantAttribute("scope");
            if ("request".equals(scopeStr)) {
                scope = "PageContext.REQUEST_SCOPE";
            } else if ("session".equals(scopeStr)) {
                scope = "PageContext.SESSION_SCOPE";
            } else if ("application".equals(scopeStr)) {
                scope = "PageContext.APPLICATION_SCOPE";
            }
        }
        ctxt.generateJavaSource("_jspx_page_context.setAttribute(");
        ctxt.generateAttribute("var");
        ctxt.generateJavaSource(", new Boolean(" + condV + ")," + scope + ");");
    }
    ctxt.generateJavaSource("if (" + condV + "){");
    ctxt.generateBody();
    ctxt.generateJavaSource("}");
}
 
Example 2
Source File: Remove.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
public void doTag(TagPluginContext ctxt) {

    //scope flag
    boolean hasScope = ctxt.isAttributeSpecified("scope");

    //the value of the "var"
    String strVar = ctxt.getConstantAttribute("var");

    //remove attribute from certain scope.
    //default scope is "page".
    if(hasScope){
        int iScope = Util.getScope(ctxt.getConstantAttribute("scope"));
        ctxt.generateJavaSource("pageContext.removeAttribute(\"" + strVar + "\"," + iScope + ");");
    }else{
        ctxt.generateJavaSource("pageContext.removeAttribute(\"" + strVar + "\");");
    }
}
 
Example 3
Source File: If.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
public void doTag(TagPluginContext ctxt) {
    String condV = ctxt.getTemporaryVariableName();
    ctxt.generateJavaSource("boolean " + condV + "=");
    ctxt.generateAttribute("test");
    ctxt.generateJavaSource(";");
    if (ctxt.isAttributeSpecified("var")) {
        String scope = "PageContext.PAGE_SCOPE";
        if (ctxt.isAttributeSpecified("scope")) {
            String scopeStr = ctxt.getConstantAttribute("scope");
            if ("request".equals(scopeStr)) {
                scope = "PageContext.REQUEST_SCOPE";
            } else if ("session".equals(scopeStr)) {
                scope = "PageContext.SESSION_SCOPE";
            } else if ("application".equals(scopeStr)) {
                scope = "PageContext.APPLICATION_SCOPE";
            }
        }
        ctxt.generateJavaSource("_jspx_page_context.setAttribute(");
        ctxt.generateAttribute("var");
        ctxt.generateJavaSource(", new Boolean(" + condV + ")," + scope + ");");
    }
    ctxt.generateJavaSource("if (" + condV + "){");
    ctxt.generateBody();
    ctxt.generateJavaSource("}");
}
 
Example 4
Source File: Remove.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
public void doTag(TagPluginContext ctxt) {
    
    //scope flag
    boolean hasScope = ctxt.isAttributeSpecified("scope");
    
    //the value of the "var"
    String strVar = ctxt.getConstantAttribute("var");
    
    //remove attribute from certain scope.
    //default scope is "page".
    if(hasScope){
        int iScope = Util.getScope(ctxt.getConstantAttribute("scope"));
        ctxt.generateJavaSource("pageContext.removeAttribute(\"" + strVar + "\"," + iScope + ");");
    }else{
        ctxt.generateJavaSource("pageContext.removeAttribute(\"" + strVar + "\");");
    }
}
 
Example 5
Source File: If.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Override
public void doTag(TagPluginContext ctxt) {
    String condV = ctxt.getTemporaryVariableName();
    ctxt.generateJavaSource("boolean " + condV + "=");
    ctxt.generateAttribute("test");
    ctxt.generateJavaSource(";");
    if (ctxt.isAttributeSpecified("var")) {
        String scope = "PageContext.PAGE_SCOPE";
        if (ctxt.isAttributeSpecified("scope")) {
            String scopeStr = ctxt.getConstantAttribute("scope");
            if ("request".equals(scopeStr)) {
                scope = "PageContext.REQUEST_SCOPE";
            } else if ("session".equals(scopeStr)) {
                scope = "PageContext.SESSION_SCOPE";
            } else if ("application".equals(scopeStr)) {
                scope = "PageContext.APPLICATION_SCOPE";
            }
        }
        ctxt.generateJavaSource("_jspx_page_context.setAttribute(");
        ctxt.generateAttribute("var");
        ctxt.generateJavaSource(", new Boolean(" + condV + ")," + scope + ");");
    }
    ctxt.generateJavaSource("if (" + condV + "){");
    ctxt.generateBody();
    ctxt.generateJavaSource("}");
}
 
Example 6
Source File: Remove.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Override
public void doTag(TagPluginContext ctxt) {
    
    //scope flag
    boolean hasScope = ctxt.isAttributeSpecified("scope");
    
    //the value of the "var"
    String strVar = ctxt.getConstantAttribute("var");
    
    //remove attribute from certain scope.
    //default scope is "page".
    if(hasScope){
        int iScope = Util.getScope(ctxt.getConstantAttribute("scope"));
        ctxt.generateJavaSource("pageContext.removeAttribute(\"" + strVar + "\"," + iScope + ");");
    }else{
        ctxt.generateJavaSource("pageContext.removeAttribute(\"" + strVar + "\");");
    }
}
 
Example 7
Source File: ForTokens.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
public void doTag(TagPluginContext ctxt) {
    boolean hasVar, hasVarStatus, hasBegin, hasEnd, hasStep;

    //init the flags
    hasVar = ctxt.isAttributeSpecified("var");
    hasVarStatus = ctxt.isAttributeSpecified("varStatus");
    hasBegin = ctxt.isAttributeSpecified("begin");
    hasEnd = ctxt.isAttributeSpecified("end");
    hasStep = ctxt.isAttributeSpecified("step");

    if(hasVarStatus){
        ctxt.dontUseTagPlugin();
        return;
    }

    //define all the temp variables' names
    String itemsName = ctxt.getTemporaryVariableName();
    String delimsName = ctxt.getTemporaryVariableName();
    String stName = ctxt.getTemporaryVariableName();
    String beginName = ctxt.getTemporaryVariableName();
    String endName  = ctxt.getTemporaryVariableName();
    String stepName = ctxt.getTemporaryVariableName();
    String index = ctxt.getTemporaryVariableName();
    String temp  = ctxt.getTemporaryVariableName();
    String tokensCountName = ctxt.getTemporaryVariableName();

    //get the value of the "items" attribute
    ctxt.generateJavaSource("String " + itemsName + " = (String)");
    ctxt.generateAttribute("items");
    ctxt.generateJavaSource(";");

    //get the value of the "delim" attribute
    ctxt.generateJavaSource("String " + delimsName + " = (String)");
    ctxt.generateAttribute("delims");
    ctxt.generateJavaSource(";");

    //new a StringTokenizer Object according to the "items" and the "delim"
    ctxt.generateJavaSource("java.util.StringTokenizer " + stName + " = " +
            "new java.util.StringTokenizer(" + itemsName + ", " + delimsName + ");");

    //if "begin" specified, move the token to the "begin" place
    //and record the begin index. default begin place is 0.
    ctxt.generateJavaSource("int " + tokensCountName + " = " + stName + ".countTokens();");
    if(hasBegin){
        ctxt.generateJavaSource("int " + beginName + " = "  );
        ctxt.generateAttribute("begin");
        ctxt.generateJavaSource(";");
        ctxt.generateJavaSource("for(int " + index + " = 0; " + index + " < " + beginName + " && " + stName + ".hasMoreTokens(); " + index + "++, " + stName + ".nextToken()){}");
    }else{
        ctxt.generateJavaSource("int " + beginName + " = 0;");
    }

    //when "end" is specified, if the "end" is more than the last index,
    //record the end place as the last index, otherwise, record it as "end";
    //default end place is the last index
    if(hasEnd){
        ctxt.generateJavaSource("int " + endName + " = 0;"  );
        ctxt.generateJavaSource("if((" + tokensCountName + " - 1) < ");
        ctxt.generateAttribute("end");
        ctxt.generateJavaSource("){");
        ctxt.generateJavaSource("    " + endName + " = " + tokensCountName + " - 1;");
        ctxt.generateJavaSource("}else{");
        ctxt.generateJavaSource("    " + endName + " = ");
        ctxt.generateAttribute("end");
        ctxt.generateJavaSource(";}");
    }else{
        ctxt.generateJavaSource("int " + endName + " = " + tokensCountName + " - 1;");
    }

    //get the step value from "step" if specified.
    //default step value is 1.
    if(hasStep){
        ctxt.generateJavaSource("int " + stepName + " = "  );
        ctxt.generateAttribute("step");
        ctxt.generateJavaSource(";");
    }else{
        ctxt.generateJavaSource("int " + stepName + " = 1;");
    }

    //the loop
    ctxt.generateJavaSource("for(int " + index + " = " + beginName + "; " + index + " <= " + endName + "; " + index + "++){");
    ctxt.generateJavaSource("    String " + temp + " = " + stName + ".nextToken();");
    ctxt.generateJavaSource("    if(((" + index + " - " + beginName + ") % " + stepName + ") == 0){");
    //if var specified, put the current token into the attribute "var" defines.
    if(hasVar){
        String strVar = ctxt.getConstantAttribute("var");
        ctxt.generateJavaSource("        pageContext.setAttribute(\"" + strVar + "\", " + temp + ");");
    }
    ctxt.generateBody();
    ctxt.generateJavaSource("    }");
    ctxt.generateJavaSource("}");
}
 
Example 8
Source File: Url.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
public void doTag(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("}");
    }
}
 
Example 9
Source File: ForTokens.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Override
public void doTag(TagPluginContext ctxt) {
    boolean hasVar, hasVarStatus, hasBegin, hasEnd, hasStep;
    
    //init the flags
    hasVar = ctxt.isAttributeSpecified("var");
    hasVarStatus = ctxt.isAttributeSpecified("varStatus");
    hasBegin = ctxt.isAttributeSpecified("begin");
    hasEnd = ctxt.isAttributeSpecified("end");
    hasStep = ctxt.isAttributeSpecified("step");
    
    if(hasVarStatus){
        ctxt.dontUseTagPlugin();
        return;
    }
    
    //define all the temp variables' names
    String itemsName = ctxt.getTemporaryVariableName();
    String delimsName = ctxt.getTemporaryVariableName();
    String stName = ctxt.getTemporaryVariableName();
    String beginName = ctxt.getTemporaryVariableName();
    String endName  = ctxt.getTemporaryVariableName();
    String stepName = ctxt.getTemporaryVariableName();
    String index = ctxt.getTemporaryVariableName();
    String temp  = ctxt.getTemporaryVariableName();
    String tokensCountName = ctxt.getTemporaryVariableName();
    
    //get the value of the "items" attribute 
    ctxt.generateJavaSource("String " + itemsName + " = (String)");
    ctxt.generateAttribute("items");
    ctxt.generateJavaSource(";");
    
    //get the value of the "delim" attribute
    ctxt.generateJavaSource("String " + delimsName + " = (String)");
    ctxt.generateAttribute("delims");
    ctxt.generateJavaSource(";");
    
    //new a StringTokenizer Object according to the "items" and the "delim"
    ctxt.generateJavaSource("java.util.StringTokenizer " + stName + " = " +
            "new java.util.StringTokenizer(" + itemsName + ", " + delimsName + ");");
    
    //if "begin" specified, move the token to the "begin" place
    //and record the begin index. default begin place is 0.
    ctxt.generateJavaSource("int " + tokensCountName + " = " + stName + ".countTokens();");
    if(hasBegin){
        ctxt.generateJavaSource("int " + beginName + " = "  );
        ctxt.generateAttribute("begin");
        ctxt.generateJavaSource(";");
        ctxt.generateJavaSource("for(int " + index + " = 0; " + index + " < " + beginName + " && " + stName + ".hasMoreTokens(); " + index + "++, " + stName + ".nextToken()){}");
    }else{
        ctxt.generateJavaSource("int " + beginName + " = 0;");
    }
    
    //when "end" is specified, if the "end" is more than the last index,
    //record the end place as the last index, otherwise, record it as "end";
    //default end place is the last index 
    if(hasEnd){
        ctxt.generateJavaSource("int " + endName + " = 0;"  );
        ctxt.generateJavaSource("if((" + tokensCountName + " - 1) < ");
        ctxt.generateAttribute("end");
        ctxt.generateJavaSource("){");
        ctxt.generateJavaSource("    " + endName + " = " + tokensCountName + " - 1;");
        ctxt.generateJavaSource("}else{");
        ctxt.generateJavaSource("    " + endName + " = ");
        ctxt.generateAttribute("end");
        ctxt.generateJavaSource(";}");
    }else{
        ctxt.generateJavaSource("int " + endName + " = " + tokensCountName + " - 1;");
    }
    
    //get the step value from "step" if specified.
    //default step value is 1.
    if(hasStep){
        ctxt.generateJavaSource("int " + stepName + " = "  );
        ctxt.generateAttribute("step");
        ctxt.generateJavaSource(";");
    }else{
        ctxt.generateJavaSource("int " + stepName + " = 1;");
    }
    
    //the loop
    ctxt.generateJavaSource("for(int " + index + " = " + beginName + "; " + index + " <= " + endName + "; " + index + "++){");
    ctxt.generateJavaSource("    String " + temp + " = " + stName + ".nextToken();");
    ctxt.generateJavaSource("    if(((" + index + " - " + beginName + ") % " + stepName + ") == 0){");
    //if var specified, put the current token into the attribute "var" defines.
    if(hasVar){
        String strVar = ctxt.getConstantAttribute("var");
        ctxt.generateJavaSource("        pageContext.setAttribute(\"" + strVar + "\", " + temp + ");");
    }
    ctxt.generateBody();
    ctxt.generateJavaSource("    }");
    ctxt.generateJavaSource("}");
}
 
Example 10
Source File: Url.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Override
public void doTag(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("}");
    }
}
 
Example 11
Source File: ForTokens.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Override
public void doTag(TagPluginContext ctxt) {
    boolean hasVar, hasVarStatus, hasBegin, hasEnd, hasStep;
    
    //init the flags
    hasVar = ctxt.isAttributeSpecified("var");
    hasVarStatus = ctxt.isAttributeSpecified("varStatus");
    hasBegin = ctxt.isAttributeSpecified("begin");
    hasEnd = ctxt.isAttributeSpecified("end");
    hasStep = ctxt.isAttributeSpecified("step");
    
    if(hasVarStatus){
        ctxt.dontUseTagPlugin();
        return;
    }
    
    //define all the temp variables' names
    String itemsName = ctxt.getTemporaryVariableName();
    String delimsName = ctxt.getTemporaryVariableName();
    String stName = ctxt.getTemporaryVariableName();
    String beginName = ctxt.getTemporaryVariableName();
    String endName  = ctxt.getTemporaryVariableName();
    String stepName = ctxt.getTemporaryVariableName();
    String index = ctxt.getTemporaryVariableName();
    String temp  = ctxt.getTemporaryVariableName();
    String tokensCountName = ctxt.getTemporaryVariableName();
    
    //get the value of the "items" attribute 
    ctxt.generateJavaSource("String " + itemsName + " = (String)");
    ctxt.generateAttribute("items");
    ctxt.generateJavaSource(";");
    
    //get the value of the "delim" attribute
    ctxt.generateJavaSource("String " + delimsName + " = (String)");
    ctxt.generateAttribute("delims");
    ctxt.generateJavaSource(";");
    
    //new a StringTokenizer Object according to the "items" and the "delim"
    ctxt.generateJavaSource("java.util.StringTokenizer " + stName + " = " +
            "new java.util.StringTokenizer(" + itemsName + ", " + delimsName + ");");
    
    //if "begin" specified, move the token to the "begin" place
    //and record the begin index. default begin place is 0.
    ctxt.generateJavaSource("int " + tokensCountName + " = " + stName + ".countTokens();");
    if(hasBegin){
        ctxt.generateJavaSource("int " + beginName + " = "  );
        ctxt.generateAttribute("begin");
        ctxt.generateJavaSource(";");
        ctxt.generateJavaSource("for(int " + index + " = 0; " + index + " < " + beginName + " && " + stName + ".hasMoreTokens(); " + index + "++, " + stName + ".nextToken()){}");
    }else{
        ctxt.generateJavaSource("int " + beginName + " = 0;");
    }
    
    //when "end" is specified, if the "end" is more than the last index,
    //record the end place as the last index, otherwise, record it as "end";
    //default end place is the last index 
    if(hasEnd){
        ctxt.generateJavaSource("int " + endName + " = 0;"  );
        ctxt.generateJavaSource("if((" + tokensCountName + " - 1) < ");
        ctxt.generateAttribute("end");
        ctxt.generateJavaSource("){");
        ctxt.generateJavaSource("    " + endName + " = " + tokensCountName + " - 1;");
        ctxt.generateJavaSource("}else{");
        ctxt.generateJavaSource("    " + endName + " = ");
        ctxt.generateAttribute("end");
        ctxt.generateJavaSource(";}");
    }else{
        ctxt.generateJavaSource("int " + endName + " = " + tokensCountName + " - 1;");
    }
    
    //get the step value from "step" if specified.
    //default step value is 1.
    if(hasStep){
        ctxt.generateJavaSource("int " + stepName + " = "  );
        ctxt.generateAttribute("step");
        ctxt.generateJavaSource(";");
    }else{
        ctxt.generateJavaSource("int " + stepName + " = 1;");
    }
    
    //the loop
    ctxt.generateJavaSource("for(int " + index + " = " + beginName + "; " + index + " <= " + endName + "; " + index + "++){");
    ctxt.generateJavaSource("    String " + temp + " = " + stName + ".nextToken();");
    ctxt.generateJavaSource("    if(((" + index + " - " + beginName + ") % " + stepName + ") == 0){");
    //if var specified, put the current token into the attribute "var" defines.
    if(hasVar){
        String strVar = ctxt.getConstantAttribute("var");
        ctxt.generateJavaSource("        pageContext.setAttribute(\"" + strVar + "\", " + temp + ");");
    }
    ctxt.generateBody();
    ctxt.generateJavaSource("    }");
    ctxt.generateJavaSource("}");
}
 
Example 12
Source File: Url.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Override
public void doTag(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("}");
    }
}