View Javadoc

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  
24  
25  public final class Out implements TagPlugin {
26  
27      public void doTag(TagPluginContext ctxt) {
28  
29          //these two data member are to indicate 
30          //whether the corresponding attribute is specified
31          boolean hasDefault = false, hasEscapeXml = false;
32          hasDefault = ctxt.isAttributeSpecified("default");
33          hasEscapeXml = ctxt.isAttributeSpecified("escapeXml");
34  
35          //strValName, strEscapeXmlName & strDefName are two variables' name 
36          //standing for value, escapeXml and default attribute
37          String strValName = ctxt.getTemporaryVariableName();
38          String strDefName = ctxt.getTemporaryVariableName();
39          String strEscapeXmlName = ctxt.getTemporaryVariableName();
40  
41          //according to the tag file, the value attribute is mandatory.
42          ctxt.generateJavaSource("String " + strValName + " = null;");
43          ctxt.generateJavaSource("if(");
44          ctxt.generateAttribute("value");
45          ctxt.generateJavaSource("!=null){");
46          ctxt.generateJavaSource("    " + strValName + " = (");
47          ctxt.generateAttribute("value");
48          ctxt.generateJavaSource(").toString();");
49          ctxt.generateJavaSource("}");
50  
51          //initiate the strDefName with null.
52          //if the default has been specified, then assign the value to it;
53          ctxt.generateJavaSource("String " + strDefName + " = null;\n");
54          if (hasDefault) {
55              ctxt.generateJavaSource("if(");
56              ctxt.generateAttribute("default");
57              ctxt.generateJavaSource(" != null){");
58              ctxt.generateJavaSource(strDefName + " = (");
59              ctxt.generateAttribute("default");
60              ctxt.generateJavaSource(").toString();");
61              ctxt.generateJavaSource("}");
62          }
63  
64          //initiate the strEscapeXmlName with true;
65          //if the escapeXml is specified, assign the value to it;
66          ctxt.generateJavaSource("boolean " + strEscapeXmlName + " = true;");
67          if (hasEscapeXml) {
68              ctxt.generateJavaSource(strEscapeXmlName + " = Boolean.parseBoolean((");
69              ctxt.generateAttribute("default");
70              ctxt.generateJavaSource(").toString());");
71          }
72  
73          //main part. 
74          ctxt.generateJavaSource("if(null != " + strValName + "){");
75          ctxt.generateJavaSource("    if(" + strEscapeXmlName + "){");
76          ctxt.generateJavaSource("        " + strValName + " = org.apache.struts2.jasper.tagplugins.jstl.Util.escapeXml(" + strValName + ");");
77          ctxt.generateJavaSource("    }");
78          ctxt.generateJavaSource("    out.write(" + strValName + ");");
79          ctxt.generateJavaSource("}else{");
80          ctxt.generateJavaSource("    if(null != " + strDefName + "){");
81          ctxt.generateJavaSource("        if(" + strEscapeXmlName + "){");
82          ctxt.generateJavaSource("            " + strDefName + " = org.apache.struts2.jasper.tagplugins.jstl.Util.escapeXml(" + strDefName + ");");
83          ctxt.generateJavaSource("        }");
84          ctxt.generateJavaSource("        out.write(" + strDefName + ");");
85          ctxt.generateJavaSource("    }else{");
86          ctxt.generateBody();
87          ctxt.generateJavaSource("    }");
88          ctxt.generateJavaSource("}");
89      }
90  }