1 changed files with 40 additions and 0 deletions
@ -0,0 +1,40 @@ |
|||||
|
import java.util.HashMap; |
||||
|
import java.util.Map; |
||||
|
import java.util.regex.Pattern; |
||||
|
|
||||
|
public class CommandUtil { |
||||
|
// 命令别名映射 |
||||
|
private static Map<String,String> aliasMap; |
||||
|
// URL正则校验 |
||||
|
private static final Pattern URL_REG = Pattern.compile("^https?://.+"); |
||||
|
|
||||
|
static { |
||||
|
aliasMap = new HashMap<>(); |
||||
|
// 作业要求:别名 c 代表 crawl |
||||
|
aliasMap.put("c", "crawl"); |
||||
|
// 可扩展其他别名 |
||||
|
aliasMap.put("h", "history"); |
||||
|
aliasMap.put("e", "exit"); |
||||
|
} |
||||
|
|
||||
|
// 解析别名,返回真实命令 |
||||
|
public static String parseAlias(String input) { |
||||
|
String[] arr = input.split(" ", 2); |
||||
|
String cmd = arr[0]; |
||||
|
// 如果有别名就替换 |
||||
|
if(aliasMap.containsKey(cmd)){ |
||||
|
cmd = aliasMap.get(cmd); |
||||
|
} |
||||
|
// 拼接参数 |
||||
|
if(arr.length > 1){ |
||||
|
return cmd + " " + arr[1]; |
||||
|
} |
||||
|
return cmd; |
||||
|
} |
||||
|
|
||||
|
// 校验URL是否合法 |
||||
|
public static boolean checkUrl(String url) { |
||||
|
if(url == null || url.isBlank()) return false; |
||||
|
return URL_REG.matcher(url).matches(); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue