KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jasper > tagplugins > jstl > core > Param


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.jasper.tagplugins.jstl.core;
20
21 import org.apache.jasper.compiler.tagplugin.TagPlugin;
22 import org.apache.jasper.compiler.tagplugin.TagPluginContext;
23
24 public class Param implements TagPlugin {
25     
26     public void doTag(TagPluginContext ctxt) {
27         
28         //don't support the body content
29

30         //define names of all the temp variables
31
String JavaDoc nameName = ctxt.getTemporaryVariableName();
32         String JavaDoc valueName = ctxt.getTemporaryVariableName();
33         String JavaDoc urlName = ctxt.getTemporaryVariableName();
34         String JavaDoc encName = ctxt.getTemporaryVariableName();
35         String JavaDoc index = ctxt.getTemporaryVariableName();
36         
37         //if the param tag has no parents, throw a exception
38
TagPluginContext parent = ctxt.getParentContext();
39         if(parent == null){
40             ctxt.generateJavaSource(" throw new JspTagExcption" +
41             "(\"<param> outside <import> or <urlEncode>\");");
42             return;
43         }
44         
45         //get the url string before adding this param
46
ctxt.generateJavaSource("String " + urlName + " = " +
47         "(String)pageContext.getAttribute(\"url_without_param\");");
48         
49         //get the value of "name"
50
ctxt.generateJavaSource("String " + nameName + " = ");
51         ctxt.generateAttribute("name");
52         ctxt.generateJavaSource(";");
53         
54         //if the "name" is null then do nothing.
55
//else add such string "name=value" to the url.
56
//and the url should be encoded
57
ctxt.generateJavaSource("if(" + nameName + " != null && !" + nameName + ".equals(\"\")){");
58         ctxt.generateJavaSource(" String " + valueName + " = ");
59         ctxt.generateAttribute("value");
60         ctxt.generateJavaSource(";");
61         ctxt.generateJavaSource(" if(" + valueName + " == null) " + valueName + " = \"\";");
62         ctxt.generateJavaSource(" String " + encName + " = pageContext.getResponse().getCharacterEncoding();");
63         ctxt.generateJavaSource(" " + nameName + " = java.net.URLEncoder.encode(" + nameName + ", " + encName + ");");
64         ctxt.generateJavaSource(" " + valueName + " = java.net.URLEncoder.encode(" + valueName + ", " + encName + ");");
65         ctxt.generateJavaSource(" int " + index + ";");
66         ctxt.generateJavaSource(" " + index + " = " + urlName + ".indexOf(\'?\');");
67         //if the current param is the first one, add a "?" ahead of it
68
//else add a "&" ahead of it
69
ctxt.generateJavaSource(" if(" + index + " == -1){");
70         ctxt.generateJavaSource(" " + urlName + " = " + urlName + " + \"?\" + " + nameName + " + \"=\" + " + valueName + ";");
71         ctxt.generateJavaSource(" }else{");
72         ctxt.generateJavaSource(" " + urlName + " = " + urlName + " + \"&\" + " + nameName + " + \"=\" + " + valueName + ";");
73         ctxt.generateJavaSource(" }");
74         ctxt.generateJavaSource(" pageContext.setAttribute(\"url_without_param\"," + urlName + ");");
75         ctxt.generateJavaSource("}");
76     }
77 }
78
Popular Tags