diff --git a/命令别名+URL工具类 b/命令别名+URL工具类 new file mode 100644 index 0000000..8fcb4fd --- /dev/null +++ b/命令别名+URL工具类 @@ -0,0 +1,40 @@ +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Pattern; + +public class CommandUtil { + // 命令别名映射 + private static Map 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(); + } +} \ No newline at end of file