📄 encodes.java
字号:
} /** * Sets a map of parameters (name=value) to a query string. * If the name already exists in the query string, it will be removed first. */ public static final String setToQueryString(String str, Map params) throws UnsupportedEncodingException { return setToQueryString(new StringBuffer(str), params).toString(); } /** * Sets a map of parameters (name=value) to a query string. * If the name already exists in the query string, it will be removed first. */ public static final StringBuffer setToQueryString(StringBuffer sb, Map params) throws UnsupportedEncodingException { if (params != null) { for (Iterator it = params.entrySet().iterator(); it.hasNext();) { final Map.Entry me = (Map.Entry)it.next(); setToQueryString(sb, (String)me.getKey(), me.getValue()); } } return sb; } /** Tests whether a parameter exists in the query string. */ public static final boolean containsQuery(String str, String name) { int j = str.indexOf(name); if (j <= 0) return false; char cc = str.charAt(j - 1); if (cc != '?' && cc != '&') return false; j += name.length(); if (j >= str.length()) return true; cc = str.charAt(j); return cc == '=' || cc == '&'; } /** Remove all name/value pairs of the specified name from a string. * * <p>The query string might contain servlet path and other parts. * This method starts the searching from the last '?'. * If the query string doesn't contain '?', it is assumed to be a string * without query's name/value pairs. */ public static final String removeFromQueryString(String str, String name) throws UnsupportedEncodingException { if (str == null) return null; int j = str.indexOf('?'); if (j < 0) return str; final StringBuffer sb = new StringBuffer(str); removeFromQueryString(sb, name); return sb.length() == str.length() ? str: sb.toString(); } /** Remove all name/value pairs of the specified name from a string. */ public static final StringBuffer removeFromQueryString(StringBuffer sb, String name) throws UnsupportedEncodingException { name = encodeURIComponent(name); int j = sb.indexOf("?"); if (j < 0) return sb; //no '?' j = sb.indexOf(name, j + 1); if (j < 0) return sb; //no name final int len = name.length(); do { //1. make sure left is & or ? int k = j + len; char cc = sb.charAt(j - 1); if (cc != '&' && cc != '?') { j = k; continue; } //2. make sure right is = or & or end-of-string if (k < sb.length()) { cc = sb.charAt(k); if (cc != '=' && cc != '&') { j = k; continue; } } //3. remove it until next & or end-of-string k = next(sb, '&', k); if (k < sb.length()) sb.delete(j, k + 1); //also remove '&' else sb.delete(j - 1, k); //also preceding '?' or '&' } while ((j = sb.indexOf(name, j)) > 0); return sb; } /** Encodes a URL. * It resolves "*" contained in URI, if any, to the proper Locale, * and the browser code. * Refer to {@link Servlets#locate(ServletContext, ServletRequest, String, Locator)} * for details. * * <p>In additions, if uri starts with "/", the context path, e.g., * /zkdemo, is prefixed. * In other words, "/ab/cd" means it is relevant to the servlet * context path (say, "/zkdemo"). * * <p>If uri starts with "~abc/", it means it is relevant to a foreign * Web context called /abc. And, it will be converted to "/abc/" first * (without prefix request.getContextPath()). * * <p>Finally, the uri is encoded by HttpServletResponse.encodeURL. * * <p>This method invokes {@link #encodeURI} for any characters * before '?'. However, it does NOT encode any character after '?'. Thus, * you might hvae to invoke * {@link #encodeURIComponent} or {@link #addToQueryString(StringBuffer,Map)} * to encode the query parameters. * * @param request the request; never null * @param response the response; never null * @param uri it must be empty or starts with "/". It might contain * "*" for current browser code and Locale. * @param ctx the servlet context; used only if "*" is contained in uri * @exception IndexOutOfBoundException if uri is empty * @see #encodeURL(PageContext, String) * @see org.zkoss.web.servlet.Servlets#locate * @see org.zkoss.web.servlet.Servlets#generateURI */ public static final String encodeURL(ServletContext ctx, ServletRequest request, ServletResponse response, String uri) throws ServletException { try { return encodeURL0(ctx, request, response, uri); } catch (Exception ex) { log.realCause(ex); throw new ServletException("Unable to encode "+uri, ex); } } /** Encodes a URL. * * <p>It resolves "*" contained in URI, if any, to the proper Locale, * and the browser code. * Refer to {@link Servlets#locate(ServletContext, ServletRequest, String, Locator)} * for details. * * @see #encodeURL(ServletContext, ServletRequest, ServletResponse, String) */ public static final String encodeURL(PageContext ctx, String uri) throws JspException { try { return encodeURL0(ctx.getServletContext(), ctx.getRequest(), ctx.getResponse(), uri); } catch (Exception ex) { throw new JspException("Unable to encode "+uri, ex); } } private static final String encodeURL0(ServletContext ctx, ServletRequest request, ServletResponse response, String uri) throws Exception { if (uri == null || uri.length() == 0) return uri; //keep as it is boolean ctxpathSpecified = false; if (uri.charAt(0) != '/') { //NOT relative to context path if (Servlets.isUniversalURL(uri)) return uri; //nothing to do if (uri.charAt(0) == '~') { //foreign context final String ctxroot; if (uri.length() == 1) { ctxroot = uri = "/"; } else if (uri.charAt(1) == '/') { ctxroot = "/"; uri = uri.substring(1); } else { uri = '/' + uri.substring(1); final int j = uri.indexOf('/', 1); ctxroot = j >= 0 ? uri.substring(0, j): uri; } final ExtendedWebContext extctx = Servlets.getExtendedWebContext(ctx, ctxroot.substring(1)); if (extctx != null) { final int j = uri.indexOf('/', 1); return extctx.encodeURL(request, response, j >= 0 ? uri.substring(j): "/"); } final ServletContext newctx = ctx.getContext(ctxroot); if (newctx != null) { ctx = newctx; } else if (D.ON && log.debugable()) { log.debug("Context not found: "+ctxroot); } ctxpathSpecified = true; } else if (Https.isIncluded(request) || Https.isForwarded(request)) { //if reletive URI and being included/forwarded, //converts to absolute String pgpath = Https.getThisServletPath(request); if (pgpath != null) { int j = pgpath.lastIndexOf('/'); if (j >= 0) { uri = pgpath.substring(0, j + 1) + uri; } else { log.warning("The current page doesn't contain '/':" + pgpath); } } } } //locate by locale and browser if necessary uri = Servlets.locate(ctx, request, uri, null); //prefix context path if (!ctxpathSpecified && uri.charAt(0) == '/' && (request instanceof HttpServletRequest)) { //Work around with a bug when we wrap Pluto's RenderRequest (1.0.1) String ctxpath = ((HttpServletRequest)request).getContextPath(); if (ctxpath.length() > 0 && ctxpath.charAt(0) != '/') ctxpath = '/' + ctxpath; uri = ctxpath + uri; } int j = uri.indexOf('?'); if (j < 0) { uri = encodeURI(uri); } else { uri = encodeURI(uri.substring(0, j)) + uri.substring(j); } //encode if (response instanceof HttpServletResponse) uri = ((HttpServletResponse)response).encodeURL(uri); return uri; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -