Apache Commons Validator包含commons-validator-1.3.1.jar中的UrlValidator类。以下方法用于验证VLCJ中的给定媒体资源定位符(MRL),其基本上是URL。但是,UrlValidator不适用于基于文件的URL(使用file://方案)
/**
* Get a media resource locator (MRL) with the following valid conditions:
*
(1) a non-URI file link
*
(2) a URI link with "file:" scheme
*
(3) a URI link with "file:" scheme
*
* @param mrl media resource locator (MRL)
* @return valid MRL; otherwise return null
*/
private String validateMRL(final String mrl){
String media = null;
String newURI = mrl;
URI uri = new File(mrl.trim()).toURI();
try {
String f = new File(mrl).getAbsolutePath();
Outils.debugMessage("URI:" + f);
if (FilenameUtils.equalsNormalized(newURI, f)){
//this is a actual file link
newURI = uri.toURL().toString();
newURI = Outils.replaceAllString(newURI, "file:", "file://");
Outils.debugMessage("NEW URI: " + newURI);
media = newURI;
}
else if (!mrl.contains("file:")) {
//this is not a file link or "file:" link
if (mrl != null && mrl.length() > 0){
String[] schemes = {"http", "https", "rtsp", "rtp", "udp", "mms", "mmsh"};
UrlValidator urlValidator = new UrlValidator(schemes);
if (urlValidator.isValid(newURI))
media = mrl.trim();
}
}
else {
media = mrl;
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Outils.debugMessage("CURRENT: " + media);
return media;
}对不起,这个函数没有做任何参数剥离(或者我会说“方案”)。但是,可以使用replace()或substring()轻松完成