📄 url.java
字号:
package lib.commons.net;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import lib.commons.Utils;
public class Url {
private Protocol _protocol;
private AuthDomain _domain;
private UrlPath _urlPath;
public Url(Protocol protocol, Domain domain, UrlPath urlPath) {
this(protocol, null, domain, urlPath);
}
public Url(Protocol protocol, UserAuthInfo userAuthInfo, Domain domain,
UrlPath urlPath) {
if (null == protocol)
throw new IllegalArgumentException("protocol can't be null");
_protocol = protocol;
if (protocol != Protocol.FILE) {
if (null != domain) {
_domain = new AuthDomain(userAuthInfo, domain);
} else {
throw new IllegalArgumentException("domain can't be null");
}
}
_urlPath = urlPath;
}
public Protocol getProtocol() {
return _protocol;
}
public UserAuthInfo getUserAuthInfo() {
return null == _domain ? null : _domain.getUserAuthInfo();
}
protected AuthDomain getAuthDomain() {
return _domain;
}
public Domain getDomain() {
return null == _domain ? null : _domain.getDomain();
}
public UrlPath getUrlPath() {
return null == _urlPath ? new UrlPath(Utils.EMPTY_STRING) : _urlPath;
}
public Url getRelativeUrlPath(String relativeUrlPathString) {
Url url = null;
if (!Utils.StringIsNullOrEmpty(relativeUrlPathString)
&& Url_Pattern.matcher(relativeUrlPathString).matches()) {
url = Url.parseUrl(relativeUrlPathString);
} else {
UrlPath urlPath = _urlPath
.getRelativeUrlPath(relativeUrlPathString);
url = new Url(_protocol, this.getUserAuthInfo(), this.getDomain(),
urlPath);
}
return url;
}
public String toString() {
StringBuffer buf = new StringBuffer();
if (null != this.getProtocol()) {
buf.append(this.getProtocol().toString().toLowerCase());
}
if (null != this.getAuthDomain())
buf.append(this.getAuthDomain().toString(this.getProtocol()));
buf.append(this.getUrlPath().toString());
return buf.toString();
}
public final static char SplitPathChar = '/';
public final static String convertValidPath(String filePath) {
String path = null;
if (!Utils.StringIsNullOrEmpty(filePath)) {
if ('/' != Url.SplitPathChar)
path = filePath.replace('/', Url.SplitPathChar);
if ('\\' != Url.SplitPathChar)
path = filePath.replace('\\', Url.SplitPathChar);
}
return path;
}
public final static String Url_Pattern_String = "([a-zA-Z0-9]+:\\/\\/){1}([^\\/]+)(.*)";
public final static Pattern Url_Pattern = Pattern
.compile(Url_Pattern_String);
public final static Url parseUrl(String urlString) {
Url url = null;
if (!Utils.StringIsNullOrEmpty(urlString)) {
Matcher match = Url_Pattern.matcher(urlString);
if (match.matches()) {
String protocolStr = match.group(1);
String domainStr = match.group(2);
String pathStr = match.group(3);
Protocol protocol = Protocol.parseProtocol(protocolStr);
AuthDomain domain = null;
UrlPath urlPath = null;
if (Protocol.FILE == protocol) {
pathStr = ((null == domainStr) ? Utils.EMPTY_STRING
: domainStr)
+ (null == pathStr ? Utils.EMPTY_STRING : pathStr);
} else {
protocol = (null == protocol ? Protocol.HTTP : protocol);
domain = AuthDomain.parseAuthDomain(domainStr, protocol);
}
urlPath = new UrlPath(pathStr);
url = new Url(protocol, null == domain ? null : domain
.getUserAuthInfo(), null == domain ? null : domain
.getDomain(), urlPath);
} else {
throw new IllegalArgumentException("can't parse url expression");
}
} else {
throw new IllegalArgumentException(
"urlString can't be null or empty");
}
return url;
}
public static void main(String[] args) {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -