一、前言
关于jasper的jasper-compiler源码中org.apache.jasper.tagplugins.jstl.Util工具类,通过其进行jsp对象及dom元素的解析处理,具体实现部分详情源码说明。
二、源码说明
package org.apache.jasper.tagplugins.jstl;@b@@b@import java.io.ByteArrayOutputStream;@b@import java.io.PrintWriter;@b@import java.io.StringWriter;@b@import java.io.UnsupportedEncodingException;@b@import java.util.Locale;@b@import javax.servlet.ServletOutputStream;@b@import javax.servlet.http.HttpServletRequest;@b@import javax.servlet.http.HttpServletResponse;@b@import javax.servlet.http.HttpServletResponseWrapper;@b@import javax.servlet.jsp.JspException;@b@import javax.servlet.jsp.JspTagException;@b@import javax.servlet.jsp.PageContext;@b@@b@public class Util@b@{@b@ public static final String VALID_SCHEME_CHAR = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+.-";@b@ public static final String DEFAULT_ENCODING = "ISO-8859-1";@b@ public static final int HIGHEST_SPECIAL = 62;@b@ public static char[][] specialCharactersRepresentation = new char[63][];@b@@b@ public static int getScope(String scope)@b@ {@b@ int ret = 1;@b@@b@ if ("request".equalsIgnoreCase(scope))@b@ ret = 2;@b@ else if ("session".equalsIgnoreCase(scope))@b@ ret = 3;@b@ else if ("application".equalsIgnoreCase(scope)) {@b@ ret = 4;@b@ }@b@@b@ return ret;@b@ }@b@@b@ public static boolean isAbsoluteUrl(String url)@b@ {@b@ if (url == null) {@b@ return false;@b@ }@b@@b@ int colonPos = url.indexOf(":");@b@ if (colonPos == -1) {@b@ return false;@b@ }@b@@b@ for (int i = 0; i < colonPos; ++i) {@b@ if ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+.-".indexOf(url.charAt(i)) == -1)@b@ return false;@b@@b@ }@b@@b@ return true;@b@ }@b@@b@ public static String getContentTypeAttribute(String input, String name)@b@ {@b@ int index = input.toUpperCase().indexOf(name.toUpperCase());@b@ if (index == -1) return null;@b@ index += name.length();@b@ index = input.indexOf(61, index);@b@ if (index == -1) return null;@b@ ++index;@b@ input = input.substring(index).trim();@b@@b@ if (input.charAt(0) == '"')@b@ {@b@ begin = 1;@b@ end = input.indexOf(34, begin);@b@ if (end != -1) break label119; return null;@b@ }@b@ int begin = 0;@b@ int end = input.indexOf(59);@b@ if (end == -1) end = input.indexOf(32);@b@ if (end == -1) end = input.length();@b@@b@ label119: return input.substring(begin, end).trim();@b@ }@b@@b@ public static String stripSession(String url)@b@ {@b@ StringBuffer u = new StringBuffer(url);@b@@b@ while ((sessionStart = u.toString().indexOf(";jsessionid=")) != -1) {@b@ int sessionStart;@b@ int sessionEnd = u.toString().indexOf(";", sessionStart + 1);@b@ if (sessionEnd == -1)@b@ sessionEnd = u.toString().indexOf("?", sessionStart + 1);@b@ if (sessionEnd == -1)@b@ sessionEnd = u.length();@b@ u.delete(sessionStart, sessionEnd);@b@ }@b@ return u.toString();@b@ }@b@@b@ public static String escapeXml(String buffer)@b@ {@b@ int start = 0;@b@ int length = buffer.length();@b@ char[] arrayBuffer = buffer.toCharArray();@b@ StringBuffer escapedBuffer = null;@b@@b@ for (int i = 0; i < length; ++i) {@b@ char c = arrayBuffer[i];@b@ if (c <= '>') {@b@ char[] escaped = specialCharactersRepresentation[c];@b@ if (escaped != null)@b@ {@b@ if (start == 0) {@b@ escapedBuffer = new StringBuffer(length + 5);@b@ }@b@@b@ if (start < i)@b@ escapedBuffer.append(arrayBuffer, start, i - start);@b@@b@ start = i + 1;@b@@b@ escapedBuffer.append(escaped);@b@ }@b@ }@b@ }@b@@b@ if (start == 0) {@b@ return buffer;@b@ }@b@@b@ if (start < length)@b@ escapedBuffer.append(arrayBuffer, start, length - start);@b@@b@ return escapedBuffer.toString();@b@ }@b@@b@ public static String resolveUrl(String url, String context, PageContext pageContext)@b@ throws JspException@b@ {@b@ if (isAbsoluteUrl(url)) {@b@ return url;@b@ }@b@@b@ HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();@b@@b@ if (context == null) {@b@ if (url.startsWith("/"))@b@ return request.getContextPath() + url;@b@@b@ return url;@b@ }@b@ if ((!(context.startsWith("/"))) || (!(url.startsWith("/")))) {@b@ throw new JspTagException("In URL tags, when the \"context\" attribute is specified, values of both \"context\" and \"url\" must start with \"/\".");@b@ }@b@@b@ if (context.equals("/"))@b@ {@b@ return url;@b@ }@b@ return context + url;@b@ }@b@@b@ static@b@ {@b@ specialCharactersRepresentation[38] = "&".toCharArray();@b@ specialCharactersRepresentation[60] = "<".toCharArray();@b@ specialCharactersRepresentation[62] = ">".toCharArray();@b@ specialCharactersRepresentation[34] = """.toCharArray();@b@ specialCharactersRepresentation[39] = "'".toCharArray();@b@ }@b@@b@ public static class ImportResponseWrapper extends HttpServletResponseWrapper@b@ {@b@ private StringWriter sw = new StringWriter();@b@ private ByteArrayOutputStream bos = new ByteArrayOutputStream();@b@ private ServletOutputStream sos = new Util.1(this);@b@ private boolean isWriterUsed;@b@ private boolean isStreamUsed;@b@ private int status = 200;@b@ private String charEncoding;@b@@b@ public ImportResponseWrapper(HttpServletResponse arg0)@b@ {@b@ super(arg0);@b@ }@b@@b@ public PrintWriter getWriter()@b@ {@b@ if (this.isStreamUsed)@b@ throw new IllegalStateException("Unexpected internal error during <import>: Target servlet called getWriter(), then getOutputStream()");@b@@b@ this.isWriterUsed = true;@b@ return new PrintWriter(this.sw);@b@ }@b@@b@ public ServletOutputStream getOutputStream() {@b@ if (this.isWriterUsed)@b@ throw new IllegalStateException("Unexpected internal error during <import>: Target servlet called getOutputStream(), then getWriter()");@b@@b@ this.isStreamUsed = true;@b@ return this.sos;@b@ }@b@@b@ public void setContentType(String x)@b@ {@b@ }@b@@b@ public void setLocale(Locale x)@b@ {@b@ }@b@@b@ public void setStatus(int status)@b@ {@b@ this.status = status;@b@ }@b@@b@ public int getStatus() {@b@ return this.status;@b@ }@b@@b@ public String getCharEncoding() {@b@ return this.charEncoding;@b@ }@b@@b@ public void setCharEncoding(String ce) {@b@ this.charEncoding = ce;@b@ }@b@@b@ public String getString() throws UnsupportedEncodingException {@b@ if (this.isWriterUsed)@b@ return this.sw.toString();@b@ if (this.isStreamUsed) {@b@ if ((this.charEncoding != null) && (!(this.charEncoding.equals(""))))@b@ return this.bos.toString(this.charEncoding);@b@@b@ return this.bos.toString("ISO-8859-1");@b@ }@b@ return "";@b@ }@b@@b@ static ByteArrayOutputStream access$000(ImportResponseWrapper x0)@b@ {@b@ return x0.bos;@b@ }@b@ }@b@}