⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 urlpath.java

📁 用java开发的一些实用的短信通信模块其中包含MD5加密、http发送等信息
💻 JAVA
字号:
package lib.commons.net;

import java.util.List;
import java.util.StringTokenizer;

import lib.commons.Utils;

public class UrlPath {
	private String _originPath, _path, _queryString;

	public UrlPath(String urlPath) {
		_originPath = urlPath;
		if (Utils.StringIsNullOrEmpty(urlPath)) {
			_path = Utils.EMPTY_STRING;
		} else {
			_path = getRelativePath(
					new String(new char[] { Url.SplitPathChar }),
					Url.convertValidPath(getPathWithoutQueryString(urlPath)));
			_queryString = getQueryString(urlPath);

			_path = null == _path ? Utils.EMPTY_STRING : _path;
		}

		if (_path.length() < 1 || _path.charAt(0) != Url.SplitPathChar)
			_path = Url.SplitPathChar + _path;
	}

	public String getOriginPath() {
		return _originPath;
	}

	public String getPath() {
		return _path;
	}

	public String getFile() {
		return getFileFromPath(_path);
	}

	public String getQueryString() {
		return _queryString;
	}

	public UrlPath getRelativeUrlPath(String relativeUrlPathString) {
		UrlPath urlPath = new UrlPath(null);
		urlPath._originPath = relativeUrlPathString;
		urlPath._path = getRelativePath(_path, urlPath._originPath);
		return urlPath;
	}

	public String toString() {
		StringBuffer buf = new StringBuffer();
		buf.append(_path);
		if (!Utils.StringIsNullOrEmpty(_queryString)) {
			buf.append('?').append(_queryString);
		}
		return buf.toString();
	}

	public static String getQueryString(String filePath) {
		String queryString = null;
		if (!Utils.StringIsNullOrEmpty(filePath)) {
			int pos = filePath.indexOf('?');
			if (pos > -1 && pos + 1 < filePath.length())
				queryString = filePath.substring(pos + 1);
		}
		return queryString;
	}

	public static String getPathWithoutQueryString(String filePath) {
		String pureFilePath = null;
		if (!Utils.StringIsNullOrEmpty(filePath)) {
			int pos = filePath.indexOf('?');
			if (pos > -1)
				pureFilePath = filePath.substring(0, pos);
			else
				pureFilePath = filePath;
		}
		return pureFilePath;
	}

	public static String getFileFromPath(String filePath) {
		String file = null;
		if (null != filePath && filePath.length() > 0) {
			String pureFilePath = Url
					.convertValidPath(getPathWithoutQueryString(filePath));
			pureFilePath = null == pureFilePath ? Utils.EMPTY_STRING
					: pureFilePath;
			int pos = pureFilePath.lastIndexOf(Url.SplitPathChar);
			if (pos > -1) {
				if (pos + 1 < filePath.length())
					file = pureFilePath.substring(pos + 1);
			} else
				file = pureFilePath;
		}
		return file;
	}

	public static String getRelativePath(String path, String relativePath) {
		StringBuffer relativePathBuf = new StringBuffer();

		String purePath = Url.convertValidPath(getPathWithoutQueryString(path));
		purePath = (null == purePath ? Utils.EMPTY_STRING : purePath);
		String pureRelativePath = Url
				.convertValidPath(getPathWithoutQueryString(relativePath));
		pureRelativePath = (null == pureRelativePath ? Utils.EMPTY_STRING
				: pureRelativePath);

		List pathQueue = new java.util.ArrayList();

		boolean isRoot = false;
		if (purePath.length() > 0 && purePath.charAt(0) == Url.SplitPathChar)
			isRoot = true;
		if (pureRelativePath.length() > 0
				&& pureRelativePath.charAt(0) == Url.SplitPathChar)
			isRoot = true;
		else {
			StringTokenizer st = new StringTokenizer(purePath, new String(
					new char[] { Url.SplitPathChar }));
			while (st.hasMoreTokens()) {
				String p = st.nextToken();
				if (!Utils.StringIsNullOrEmpty(p))
					pathQueue.add(p);
			}
			String file = getFileFromPath(path);
			if (!Utils.StringIsNullOrEmpty(file) && pathQueue.size() > 0)
				pathQueue.remove(pathQueue.size() - 1);
		}

		StringTokenizer st = new StringTokenizer(pureRelativePath, new String(
				new char[] { Url.SplitPathChar }));
		while (st.hasMoreElements()) {
			String p = st.nextToken();
			if (!Utils.StringIsNullOrEmpty(p)) {
				if ("..".equals(p)) {
					if (pathQueue.size() > 0)
						pathQueue.remove(pathQueue.size() - 1);
				} else if (!".".equals(p))
					pathQueue.add(p);
			}
		}

		int lastIdx = pathQueue.size() - 1;
		String relativeFile = getFileFromPath(pureRelativePath);
		if (isRoot)
			relativePathBuf.append(Url.SplitPathChar);
		for (int i = 0; i < pathQueue.size(); i++) {
			relativePathBuf.append(pathQueue.get(i));
			if (i < lastIdx)
				relativePathBuf.append(Url.SplitPathChar);
			else {
				if (Utils.StringIsNullOrEmpty(relativeFile))
					relativePathBuf.append(Url.SplitPathChar);
			}
		}
		String relativeQueryString = getQueryString(relativePath);
		if (!Utils.StringIsNullOrEmpty(relativeQueryString)) {
			relativePathBuf.append('?');
			relativePathBuf.append(relativeQueryString);
		}

		return relativePathBuf.toString();
	}

	public static void main(String[] args) {
		UrlPath urlPath = new UrlPath("aaa/bb/./ccc/");
		System.out.println(urlPath.toString() + ":" + urlPath.getFile());
		System.out.print(UrlPath.getRelativePath("/ax?nvn=1",
				"a/b/cc/../../../aa?bb=1"));
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -