/** * Copyright (c) 2015, biezhi 王爵 (biezhi.me@gmail.com) * <p> * Licensed 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 * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * 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 com.blade.mvc.http; import com.blade.kit.CollectionKit; import com.blade.kit.StringKit; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.util.List; /** * Path URL * * @author <a href="mailto:biezhi.me@gmail.com" target="_blank">biezhi</a> * @since 1.5 */ public final class Path { public static final String VAR_REGEXP = ":(\\w+)"; public static final String VAR_REPLACE = "([^#/?.]+)"; public static final String SLASH = "/"; public static String getRelativePath(String path, String contextPath) { contextPath = null == contextPath ? "" : contextPath; path = path.substring(contextPath.length()); if (path.length() > 0) { path = path.substring(1); } if (!path.startsWith(SLASH)) { path = SLASH + path; } try { path = URLDecoder.decode(path, "UTF-8"); } catch (UnsupportedEncodingException ex) { } return path; } public static List<String> convertRouteToList(String route) { String[] pathArray = StringKit.split(route, "/"); if (null != pathArray && pathArray.length > 0) { List<String> path = CollectionKit.newArrayList(); for (String p : pathArray) { if (p.length() > 0) { path.add(p); } } return path; } return CollectionKit.newArrayList(0); } public static boolean isParam(String routePart) { return routePart.startsWith(":"); } public static boolean isSplat(String routePart) { return routePart.equals("*"); } public static String fixPath(String path) { if (path == null) { return "/"; } if (!path.startsWith("/")) { path = "/" + path; } if (path.length() > 1 && path.endsWith("/")) { path = path.substring(0, path.length() - 1); } path = path.replaceAll("\\s", "%20"); return path; } public static String cleanPath(String path) { if (path == null) { return null; } return path.replaceAll("[/]+", "/"); } }