org.apache.jasper.compiler.tagplugin.TagPluginContext Java Examples

The following examples show how to use org.apache.jasper.compiler.tagplugin.TagPluginContext. 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: 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 #2
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 #3
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 #4
Source File: When.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
public void doTag(TagPluginContext ctxt) {
    // Get the parent context to determine if this is the first <c:when>
    TagPluginContext parentContext = ctxt.getParentContext();
    if (parentContext == null) {
        ctxt.dontUseTagPlugin();
        return;
    }
    
    if ("true".equals(parentContext.getPluginAttribute("hasBeenHere"))) {
        ctxt.generateJavaSource("} else if(");
        // See comment below for the reason we generate the extra "}" here.
    }
    else {
        ctxt.generateJavaSource("if(");
        parentContext.setPluginAttribute("hasBeenHere", "true");
    }
    ctxt.generateAttribute("test");
    ctxt.generateJavaSource("){");
    ctxt.generateBody();
    
    // We don't generate the closing "}" for the "if" here because there
    // may be whitespaces in between <c:when>'s.  Instead we delay
    // generating it until the next <c:when> or <c:otherwise> or
    // <c:choose>
}
 
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: When.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
public void doTag(TagPluginContext ctxt) {
    // Get the parent context to determine if this is the first <c:when>
    TagPluginContext parentContext = ctxt.getParentContext();
    if (parentContext == null) {
        ctxt.dontUseTagPlugin();
        return;
    }

    if ("true".equals(parentContext.getPluginAttribute("hasBeenHere"))) {
        ctxt.generateJavaSource("} else if(");
        // See comment below for the reason we generate the extra "}" here.
    }
    else {
        ctxt.generateJavaSource("if(");
        parentContext.setPluginAttribute("hasBeenHere", "true");
    }
    ctxt.generateAttribute("test");
    ctxt.generateJavaSource("){");
    ctxt.generateBody();

    // We don't generate the closing "}" for the "if" here because there
    // may be whitespaces in between <c:when>'s.  Instead we delay
    // generating it until the next <c:when> or <c:otherwise> or
    // <c:choose>
}
 
Example #7
Source File: When.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Override
public void doTag(TagPluginContext ctxt) {
    // Get the parent context to determine if this is the first <c:when>
    TagPluginContext parentContext = ctxt.getParentContext();
    if (parentContext == null) {
        ctxt.dontUseTagPlugin();
        return;
    }
    
    if ("true".equals(parentContext.getPluginAttribute("hasBeenHere"))) {
        ctxt.generateJavaSource("} else if(");
        // See comment below for the reason we generate the extra "}" here.
    }
    else {
        ctxt.generateJavaSource("if(");
        parentContext.setPluginAttribute("hasBeenHere", "true");
    }
    ctxt.generateAttribute("test");
    ctxt.generateJavaSource("){");
    ctxt.generateBody();
    
    // We don't generate the closing "}" for the "if" here because there
    // may be whitespaces in between <c:when>'s.  Instead we delay
    // generating it until the next <c:when> or <c:otherwise> or
    // <c:choose>
}
 
Example #8
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 #9
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 #10
Source File: TagPluginManager.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Invoke tag plugin for the given custom tag, if a plugin exists for
 * the custom tag's tag handler.
 *
 * The given custom tag node will be manipulated by the plugin.
 */
private void invokePlugin(Node.CustomTag n, PageInfo pageInfo) {
    TagPlugin tagPlugin = tagPlugins.get(n.getTagHandlerClass().getName());
    if (tagPlugin == null) {
        return;
    }

    TagPluginContext tagPluginContext = new TagPluginContextImpl(n, pageInfo);
    n.setTagPluginContext(tagPluginContext);
    tagPlugin.doTag(tagPluginContext);
}
 
Example #11
Source File: Choose.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
public void doTag(TagPluginContext ctxt) {
    
    // Not much to do here, much of the work will be done in the
    // containing tags, <c:when> and <c:otherwise>.
    
    ctxt.generateBody();
    // See comments in When.java for the reason "}" is generated here.
    ctxt.generateJavaSource("}");
}
 
Example #12
Source File: Otherwise.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public void doTag(TagPluginContext ctxt) {

    // See When.java for the reason whey "}" is need at the beginning and
    // not at the end.
    ctxt.generateJavaSource("} else {");
    ctxt.generateBody();
}
 
Example #13
Source File: TagPluginManager.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
public TagPluginContext getParentContext() {
    Node parent = node.getParent();
    if (! (parent instanceof Node.CustomTag)) {
        return null;
    }
    return ((Node.CustomTag) parent).getTagPluginContext();
}
 
Example #14
Source File: TagPluginManager.java    From packagedrone with Eclipse Public License 1.0 5 votes vote down vote up
/**
    * Invoke tag plugin for the given custom tag, if a plugin exists for 
    * the custom tag's tag handler.
    *
    * The given custom tag node will be manipulated by the plugin.
    */
   private void invokePlugin(Node.CustomTag n) {
TagPlugin tagPlugin = tagPlugins.get(n.getTagHandlerClass().getName());
if (tagPlugin == null) {
    return;
}

TagPluginContext tagPluginContext = new TagPluginContextImpl(n, pageInfo);
n.setTagPluginContext(tagPluginContext);
tagPlugin.doTag(tagPluginContext);
   }
 
Example #15
Source File: Otherwise.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
public void doTag(TagPluginContext ctxt) {
    
    // See When.java for the reason whey "}" is need at the beginng and
    // not at the end.
    ctxt.generateJavaSource("} else {");
    ctxt.generateBody();
}
 
Example #16
Source File: TagPluginManager.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public TagPluginContext getParentContext() {
    Node parent = node.getParent();
    if (! (parent instanceof Node.CustomTag)) {
        return null;
    }
    return ((Node.CustomTag) parent).getTagPluginContext();
}
 
Example #17
Source File: TagPluginManager.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Invoke tag plugin for the given custom tag, if a plugin exists for
 * the custom tag's tag handler.
 *
 * The given custom tag node will be manipulated by the plugin.
 */
private void invokePlugin(Node.CustomTag n, PageInfo pageInfo) {
    TagPlugin tagPlugin = tagPlugins.get(n.getTagHandlerClass().getName());
    if (tagPlugin == null) {
        return;
    }

    TagPluginContext tagPluginContext = new TagPluginContextImpl(n, pageInfo);
    n.setTagPluginContext(tagPluginContext);
    tagPlugin.doTag(tagPluginContext);
}
 
Example #18
Source File: Choose.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public void doTag(TagPluginContext ctxt) {
    
    // Not much to do here, much of the work will be done in the
    // containing tags, <c:when> and <c:otherwise>.
    
    ctxt.generateBody();
    // See comments in When.java for the reason "}" is generated here.
    ctxt.generateJavaSource("}");
}
 
Example #19
Source File: TagPluginManager.java    From packagedrone with Eclipse Public License 1.0 5 votes vote down vote up
public TagPluginContext getParentContext() {
    Node parent = node.getParent();
    if (! (parent instanceof Node.CustomTag)) {
	return null;
    }
    return ((Node.CustomTag) parent).getTagPluginContext();
}
 
Example #20
Source File: Otherwise.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public void doTag(TagPluginContext ctxt) {
    
    // See When.java for the reason whey "}" is need at the beginng and
    // not at the end.
    ctxt.generateJavaSource("} else {");
    ctxt.generateBody();
}
 
Example #21
Source File: TagPluginManager.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Invoke tag plugin for the given custom tag, if a plugin exists for
 * the custom tag's tag handler.
 * <p/>
 * The given custom tag node will be manipulated by the plugin.
 */
private void invokePlugin(Node.CustomTag n, PageInfo pageInfo) {
    TagPlugin tagPlugin = tagPlugins.get(n.getTagHandlerClass().getName());
    if (tagPlugin == null) {
        return;
    }

    TagPluginContext tagPluginContext = new TagPluginContextImpl(n, pageInfo);
    n.setTagPluginContext(tagPluginContext);
    tagPlugin.doTag(tagPluginContext);
}
 
Example #22
Source File: TagPluginManager.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public TagPluginContext getParentContext() {
    Node parent = node.getParent();
    if (!(parent instanceof Node.CustomTag)) {
        return null;
    }
    return ((Node.CustomTag) parent).getTagPluginContext();
}
 
Example #23
Source File: Choose.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public void doTag(TagPluginContext ctxt) {

    // Not much to do here, much of the work will be done in the
    // containing tags, <c:when> and <c:otherwise>.

    ctxt.generateBody();
    // See comments in When.java for the reason "}" is generated here.
    ctxt.generateJavaSource("}");
}
 
Example #24
Source File: Param.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
public void doTag(TagPluginContext ctxt) {

    //don't support the body content

    //define names of all the temp variables
    String nameName = ctxt.getTemporaryVariableName();
    String valueName = ctxt.getTemporaryVariableName();
    String urlName = ctxt.getTemporaryVariableName();
    String encName = ctxt.getTemporaryVariableName();
    String index = ctxt.getTemporaryVariableName();

    //if the param tag has no parents, throw a exception
    TagPluginContext parent = ctxt.getParentContext();
    if(parent == null){
        ctxt.generateJavaSource(" throw new JspTagException" +
        "(\"&lt;param&gt; outside &lt;import&gt; or &lt;urlEncode&gt;\");");
        return;
    }

    //get the url string before adding this param
    ctxt.generateJavaSource("String " + urlName + " = " +
    "(String)pageContext.getAttribute(\"url_without_param\");");

    //get the value of "name"
    ctxt.generateJavaSource("String " + nameName + " = ");
    ctxt.generateAttribute("name");
    ctxt.generateJavaSource(";");

    //if the "name" is null then do nothing.
    //else add such string "name=value" to the url.
    //and the url should be encoded
    ctxt.generateJavaSource("if(" + nameName + " != null && !" + nameName + ".equals(\"\")){");
    ctxt.generateJavaSource("    String " + valueName + " = ");
    ctxt.generateAttribute("value");
    ctxt.generateJavaSource(";");
    ctxt.generateJavaSource("    if(" + valueName + " == null) " + valueName + " = \"\";");
    ctxt.generateJavaSource("    String " + encName + " = pageContext.getResponse().getCharacterEncoding();");
    ctxt.generateJavaSource("    " + nameName + " = java.net.URLEncoder.encode(" + nameName + ", " + encName + ");");
    ctxt.generateJavaSource("    " + valueName + " = java.net.URLEncoder.encode(" + valueName + ", " + encName + ");");
    ctxt.generateJavaSource("    int " + index + ";");
    ctxt.generateJavaSource("    " + index + " = " + urlName + ".indexOf(\'?\');");
    //if the current param is the first one, add a "?" ahead of it
    //else add a "&" ahead of it
    ctxt.generateJavaSource("    if(" + index + " == -1){");
    ctxt.generateJavaSource("        " + urlName + " = " + urlName + " + \"?\" + " + nameName + " + \"=\" + " + valueName + ";");
    ctxt.generateJavaSource("    }else{");
    ctxt.generateJavaSource("        " + urlName + " = " + urlName + " + \"&\" + " + nameName + " + \"=\" + " + valueName + ";");
    ctxt.generateJavaSource("    }");
    ctxt.generateJavaSource("    pageContext.setAttribute(\"url_without_param\"," + urlName + ");");
    ctxt.generateJavaSource("}");
}
 
Example #25
Source File: Param.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Override
public void doTag(TagPluginContext ctxt) {
    
    //don't support the body content
    
    //define names of all the temp variables
    String nameName = ctxt.getTemporaryVariableName();
    String valueName = ctxt.getTemporaryVariableName();
    String urlName = ctxt.getTemporaryVariableName();
    String encName = ctxt.getTemporaryVariableName();
    String index = ctxt.getTemporaryVariableName();
    
    //if the param tag has no parents, throw a exception
    TagPluginContext parent = ctxt.getParentContext();
    if(parent == null){
        ctxt.generateJavaSource(" throw new JspTagExcption" +
        "(\"&lt;param&gt; outside &lt;import&gt; or &lt;urlEncode&gt;\");");
        return;
    }
    
    //get the url string before adding this param
    ctxt.generateJavaSource("String " + urlName + " = " +
    "(String)pageContext.getAttribute(\"url_without_param\");");
    
    //get the value of "name"
    ctxt.generateJavaSource("String " + nameName + " = ");
    ctxt.generateAttribute("name");
    ctxt.generateJavaSource(";");
    
    //if the "name" is null then do nothing.
    //else add such string "name=value" to the url.
    //and the url should be encoded
    ctxt.generateJavaSource("if(" + nameName + " != null && !" + nameName + ".equals(\"\")){");
    ctxt.generateJavaSource("    String " + valueName + " = ");
    ctxt.generateAttribute("value");
    ctxt.generateJavaSource(";");
    ctxt.generateJavaSource("    if(" + valueName + " == null) " + valueName + " = \"\";");
    ctxt.generateJavaSource("    String " + encName + " = pageContext.getResponse().getCharacterEncoding();");
    ctxt.generateJavaSource("    " + nameName + " = java.net.URLEncoder.encode(" + nameName + ", " + encName + ");");
    ctxt.generateJavaSource("    " + valueName + " = java.net.URLEncoder.encode(" + valueName + ", " + encName + ");");
    ctxt.generateJavaSource("    int " + index + ";");
    ctxt.generateJavaSource("    " + index + " = " + urlName + ".indexOf(\'?\');");
    //if the current param is the first one, add a "?" ahead of it
    //else add a "&" ahead of it
    ctxt.generateJavaSource("    if(" + index + " == -1){");
    ctxt.generateJavaSource("        " + urlName + " = " + urlName + " + \"?\" + " + nameName + " + \"=\" + " + valueName + ";");
    ctxt.generateJavaSource("    }else{");
    ctxt.generateJavaSource("        " + urlName + " = " + urlName + " + \"&\" + " + nameName + " + \"=\" + " + valueName + ";");
    ctxt.generateJavaSource("    }");
    ctxt.generateJavaSource("    pageContext.setAttribute(\"url_without_param\"," + urlName + ");");
    ctxt.generateJavaSource("}");
}
 
Example #26
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("}");
    }
}
 
Example #27
Source File: Redirect.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Override
public void doTag(TagPluginContext ctxt) {
    
    //flag for the existence of the "context"
    boolean hasContext = ctxt.isAttributeSpecified("context");
    
    //names of the temp variables
    String urlName = ctxt.getTemporaryVariableName();
    String contextName = ctxt.getTemporaryVariableName();
    String baseUrlName = ctxt.getTemporaryVariableName();
    String resultName = ctxt.getTemporaryVariableName();
    String responseName = ctxt.getTemporaryVariableName();
    
    //get context
    ctxt.generateJavaSource("String " + contextName + " = null;");
    if(hasContext){
        ctxt.generateJavaSource(contextName + " = ");
        ctxt.generateAttribute("context");
        ctxt.generateJavaSource(";");
    }
    
    //get the url
    ctxt.generateJavaSource("String " + urlName + " = ");
    ctxt.generateAttribute("url");
    ctxt.generateJavaSource(";");
    
    //get the raw url according to "url" and "context"
    ctxt.generateJavaSource("String " + baseUrlName + " = " +
            "org.apache.jasper.tagplugins.jstl.Util.resolveUrl(" + urlName + ", " + 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\");");
    
    //get the response object
    ctxt.generateJavaSource("HttpServletResponse " + responseName + " = " +
    "((HttpServletResponse) pageContext.getResponse());");
    
    //if the url is relative, encode it
    ctxt.generateJavaSource("if(!org.apache.jasper.tagplugins.jstl.Util.isAbsoluteUrl(" + resultName + ")){");
    ctxt.generateJavaSource("    " + resultName + " = "
            + responseName + ".encodeRedirectURL(" + resultName + ");");
    ctxt.generateJavaSource("}");
    
    //do redirect
    ctxt.generateJavaSource("try{");
    ctxt.generateJavaSource("    " + responseName + ".sendRedirect(" + resultName + ");");
    ctxt.generateJavaSource("}catch(java.io.IOException ex){");
    ctxt.generateJavaSource("    throw new JspTagException(ex.toString(), ex);");
    ctxt.generateJavaSource("}");
}
 
Example #28
Source File: Out.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Override
public void doTag(TagPluginContext ctxt) {
    
    //these two data member are to indicate 
    //whether the corresponding attribute is specified
    boolean hasDefault=false, hasEscapeXml=false;
    hasDefault = ctxt.isAttributeSpecified("default");
    hasEscapeXml = ctxt.isAttributeSpecified("escapeXml");
    
    //strValName, strEscapeXmlName & strDefName are two variables' name 
    //standing for value, escapeXml and default attribute
    String strObjectName = ctxt.getTemporaryVariableName();
    String strValName = ctxt.getTemporaryVariableName();
    String strDefName = ctxt.getTemporaryVariableName();
    String strEscapeXmlName = ctxt.getTemporaryVariableName();
    String strSkipBodyName = ctxt.getTemporaryVariableName();

    //according to the tag file, the value attribute is mandatory.
    ctxt.generateImport("java.io.Reader");
    ctxt.generateJavaSource("Object " + strObjectName + "=");
    ctxt.generateAttribute("value");
    ctxt.generateJavaSource(";");
    ctxt.generateJavaSource("String " + strValName + "=null;");
    ctxt.generateJavaSource("if(!(" + strObjectName +
            " instanceof Reader) && "+ strObjectName + " != null){");
    ctxt.generateJavaSource(
            strValName + " = " + strObjectName + ".toString();");
    ctxt.generateJavaSource("}");
    
    //initiate the strDefName with null.
    //if the default has been specified, then assign the value to it;
    ctxt.generateJavaSource("String " + strDefName + " = null;");
    if(hasDefault){
        ctxt.generateJavaSource("if(");
        ctxt.generateAttribute("default");
        ctxt.generateJavaSource(" != null){");
        ctxt.generateJavaSource(strDefName + " = (");
        ctxt.generateAttribute("default");
        ctxt.generateJavaSource(").toString();");
        ctxt.generateJavaSource("}");
    }
    
    //initiate the strEscapeXmlName with true;
    //if the escapeXml is specified, assign the value to it;
    ctxt.generateJavaSource("boolean " + strEscapeXmlName + " = true;");
    if(hasEscapeXml){
        ctxt.generateJavaSource(strEscapeXmlName + " = ");
        ctxt.generateAttribute("escapeXml");
        ctxt.generateJavaSource(";");
    }
    
    //main part. 
    ctxt.generateJavaSource(
            "boolean " + strSkipBodyName + " = " +
            "org.apache.jasper.tagplugins.jstl.core.Out.output(out, " +
            strObjectName + ", " + strValName + ", " + strDefName + ", " +
            strEscapeXmlName + ");");
    ctxt.generateJavaSource("if(!" + strSkipBodyName + ") {");
    ctxt.generateBody();
    ctxt.generateJavaSource("}");
}
 
Example #29
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 #30
Source File: Out.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
public void doTag(TagPluginContext ctxt) {

    //these two data member are to indicate
    //whether the corresponding attribute is specified
    boolean hasDefault=false, hasEscapeXml=false;
    hasDefault = ctxt.isAttributeSpecified("default");
    hasEscapeXml = ctxt.isAttributeSpecified("escapeXml");

    //strValName, strEscapeXmlName & strDefName are two variables' name
    //standing for value, escapeXml and default attribute
    String strObjectName = ctxt.getTemporaryVariableName();
    String strValName = ctxt.getTemporaryVariableName();
    String strDefName = ctxt.getTemporaryVariableName();
    String strEscapeXmlName = ctxt.getTemporaryVariableName();
    String strSkipBodyName = ctxt.getTemporaryVariableName();

    //according to the tag file, the value attribute is mandatory.
    ctxt.generateImport("java.io.Reader");
    ctxt.generateJavaSource("Object " + strObjectName + "=");
    ctxt.generateAttribute("value");
    ctxt.generateJavaSource(";");
    ctxt.generateJavaSource("String " + strValName + "=null;");
    ctxt.generateJavaSource("if(!(" + strObjectName +
            " instanceof Reader) && "+ strObjectName + " != null){");
    ctxt.generateJavaSource(
            strValName + " = " + strObjectName + ".toString();");
    ctxt.generateJavaSource("}");

    //initiate the strDefName with null.
    //if the default has been specified, then assign the value to it;
    ctxt.generateJavaSource("String " + strDefName + " = null;");
    if(hasDefault){
        ctxt.generateJavaSource("if(");
        ctxt.generateAttribute("default");
        ctxt.generateJavaSource(" != null){");
        ctxt.generateJavaSource(strDefName + " = (");
        ctxt.generateAttribute("default");
        ctxt.generateJavaSource(").toString();");
        ctxt.generateJavaSource("}");
    }

    //initiate the strEscapeXmlName with true;
    //if the escapeXml is specified, assign the value to it;
    ctxt.generateJavaSource("boolean " + strEscapeXmlName + " = true;");
    if(hasEscapeXml){
        ctxt.generateJavaSource(strEscapeXmlName + " = ");
        ctxt.generateAttribute("escapeXml");
        ctxt.generateJavaSource(";");
    }

    //main part.
    ctxt.generateJavaSource(
            "boolean " + strSkipBodyName + " = " +
            "org.apache.jasper.tagplugins.jstl.core.Out.output(out, " +
            strObjectName + ", " + strValName + ", " + strDefName + ", " +
            strEscapeXmlName + ");");
    ctxt.generateJavaSource("if(!" + strSkipBodyName + ") {");
    ctxt.generateBody();
    ctxt.generateJavaSource("}");
}