📄 import.java
字号:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jasper.tagplugins.jstl.core;
import org.apache.jasper.compiler.tagplugin.TagPlugin;
import org.apache.jasper.compiler.tagplugin.TagPluginContext;
import org.apache.jasper.tagplugins.jstl.Util;
public class Import implements TagPlugin {
public void doTag(TagPluginContext ctxt) {
boolean hasContext, hasVar, hasScope, hasVarReader, hasCharEncoding;
//flags
hasContext = ctxt.isAttributeSpecified("context");
hasVar = ctxt.isAttributeSpecified("var");
hasScope = ctxt.isAttributeSpecified("scope");
hasVarReader = ctxt.isAttributeSpecified("varReader");
hasCharEncoding = ctxt.isAttributeSpecified("charEncoding");
//variables' names
String urlName = ctxt.getTemporaryVariableName();
String contextName = ctxt.getTemporaryVariableName();
String iauName = ctxt.getTemporaryVariableName(); // is absolute url
String urlObjName = ctxt.getTemporaryVariableName(); //URL object
String ucName = ctxt.getTemporaryVariableName(); //URLConnection
String inputStreamName = ctxt.getTemporaryVariableName();
String tempReaderName = ctxt.getTemporaryVariableName();
String tempReaderName2 = ctxt.getTemporaryVariableName();
String charSetName = ctxt.getTemporaryVariableName();
String charEncodingName = ctxt.getTemporaryVariableName();
String contentTypeName = ctxt.getTemporaryVariableName();
String varReaderName = ctxt.getTemporaryVariableName();
String servletContextName = ctxt.getTemporaryVariableName();
String servletPathName = ctxt.getTemporaryVariableName();
String requestDispatcherName = ctxt.getTemporaryVariableName();
String irwName = ctxt.getTemporaryVariableName(); //ImportResponseWrapper name
String brName = ctxt.getTemporaryVariableName(); //BufferedReader name
String sbName = ctxt.getTemporaryVariableName(); //StringBuffer name
String tempStringName = ctxt.getTemporaryVariableName();
//is absolute url
ctxt.generateJavaSource("boolean " + iauName + ";");
//get the url value
ctxt.generateJavaSource("String " + urlName + " = ");
ctxt.generateAttribute("url");
ctxt.generateJavaSource(";");
//validate the url
ctxt.generateJavaSource("if(" + urlName + " == null || " + urlName + ".equals(\"\")){");
ctxt.generateJavaSource(" throw new JspTagException(\"The \\\"url\\\" attribute " +
"illegally evaluated to \\\"null\\\" or \\\"\\\" in <import>\");");
ctxt.generateJavaSource("}");
//initialize the is_absolute_url
ctxt.generateJavaSource(iauName + " = " +
"org.apache.jasper.tagplugins.jstl.Util.isAbsoluteUrl(" + urlName + ");");
//validate the context
if(hasContext){
ctxt.generateJavaSource("String " + contextName + " = ");
ctxt.generateAttribute("context");
ctxt.generateJavaSource(";");
ctxt.generateJavaSource("if((!" + contextName + ".startsWith(\"/\")) " +
"|| (!" + urlName + ".startsWith(\"/\"))){");
ctxt.generateJavaSource(" throw new JspTagException" +
"(\"In URL tags, when the \\\"context\\\" attribute is specified, " +
"values of both \\\"context\\\" and \\\"url\\\" must start with \\\"/\\\".\");");
ctxt.generateJavaSource("}");
}
//define charset
ctxt.generateJavaSource("String " + charSetName + " = null;");
//if the charEncoding attribute is specified
if(hasCharEncoding){
//initialize the charEncoding
ctxt.generateJavaSource("String " + charEncodingName + " = ");
ctxt.generateAttribute("charEncoding");
ctxt.generateJavaSource(";");
//assign appropriate value tp the charset
ctxt.generateJavaSource("if(null != " + charEncodingName + " " +
"&& !" + charEncodingName + ".equals(\"\")){");
ctxt.generateJavaSource(" " + charSetName + " = "
+ charEncodingName + ";");
ctxt.generateJavaSource("}");
}
//reshape the url string
ctxt.generateJavaSource("if(!"+iauName+"){");
ctxt.generateJavaSource(" if(!" + urlName + ".startsWith(\"/\")){");
ctxt.generateJavaSource(" String " + servletPathName + " = " +
"((HttpServletRequest)pageContext.getRequest()).getServletPath();");
ctxt.generateJavaSource(" " + urlName + " = "
+ servletPathName + ".substring(0," + servletPathName + ".lastIndexOf('/')) + '/' + " + urlName + ";");
ctxt.generateJavaSource(" }");
ctxt.generateJavaSource("}");
//if the varReader attribute specified
if(hasVarReader){
//get the String value of varReader
ctxt.generateJavaSource("String " + varReaderName + " = ");
ctxt.generateAttribute("varReader");
ctxt.generateJavaSource(";");
//if the url is absolute url
ctxt.generateJavaSource("if(" + iauName + "){");
//get the content of the target
ctxt.generateJavaSource(" java.net.URL " + urlObjName + " = new java.net.URL(" + urlName + ");");
ctxt.generateJavaSource(" java.net.URLConnection " + ucName + " = "
+ urlObjName + ".openConnection();");
ctxt.generateJavaSource(" java.io.InputStream " + inputStreamName + " = "
+ ucName + ".getInputStream();");
ctxt.generateJavaSource(" if(" + charSetName + " == null){");
ctxt.generateJavaSource(" String " + contentTypeName + " = "
+ ucName + ".getContentType();");
ctxt.generateJavaSource(" if(null != " + contentTypeName + "){");
ctxt.generateJavaSource(" " + charSetName + " = " +
"org.apache.jasper.tagplugins.jstl.Util.getContentTypeAttribute(" + contentTypeName + ", \"charset\");");
ctxt.generateJavaSource(" if(" + charSetName + " == null) "
+ charSetName + " = org.apache.jasper.tagplugins.jstl.Util.DEFAULT_ENCODING;");
ctxt.generateJavaSource(" }else{");
ctxt.generateJavaSource(" " + charSetName + " = org.apache.jasper.tagplugins.jstl.Util.DEFAULT_ENCODING;");
ctxt.generateJavaSource(" }");
ctxt.generateJavaSource(" }");
if(!hasCharEncoding){
ctxt.generateJavaSource(" String " + contentTypeName + " = " + ucName + ".getContentType();");
}
//define the Reader
ctxt.generateJavaSource(" java.io.Reader " + tempReaderName + " = null;");
//initialize the Reader object
ctxt.generateJavaSource(" try{");
ctxt.generateJavaSource(" " + tempReaderName + " = new java.io.InputStreamReader(" + inputStreamName + ", " + charSetName + ");");
ctxt.generateJavaSource(" }catch(Exception ex){");
ctxt.generateJavaSource(" " + tempReaderName + " = new java.io.InputStreamReader(" + inputStreamName + ", org.apache.jasper.tagplugins.jstl.Util.DEFAULT_ENCODING);");
ctxt.generateJavaSource(" }");
//validate the response
ctxt.generateJavaSource(" if(" + ucName + " instanceof java.net.HttpURLConnection){");
ctxt.generateJavaSource(" int status = ((java.net.HttpURLConnection) " + ucName + ").getResponseCode();");
ctxt.generateJavaSource(" if(status < 200 || status > 299){");
ctxt.generateJavaSource(" throw new JspTagException(status + \" \" + " + urlName + ");");
ctxt.generateJavaSource(" }");
ctxt.generateJavaSource(" }");
//set attribute in the page context scope
ctxt.generateJavaSource(" pageContext.setAttribute(" + varReaderName + ", " + tempReaderName + ");");
//if the url is relative
ctxt.generateJavaSource("}else{");
//if the url is relative, http request is needed
ctxt.generateJavaSource(" if (!(pageContext.getRequest() instanceof HttpServletRequest " +
"&& pageContext.getResponse() instanceof HttpServletResponse)){");
ctxt.generateJavaSource(" throw new JspTagException(\"Relative <import> from non-HTTP request not allowed\");");
ctxt.generateJavaSource(" }");
//get the servlet context of the context defined in the context attribute
ctxt.generateJavaSource(" ServletContext " + servletContextName + " = null;");
if(hasContext){
ctxt.generateJavaSource(" if(null != " + contextName + "){");
ctxt.generateJavaSource(" " + servletContextName + " = pageContext.getServletContext().getContext(" + contextName + ");" );
ctxt.generateJavaSource(" }else{");
ctxt.generateJavaSource(" " + servletContextName + " = pageContext.getServletContext();");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -