5 changed files with 153 additions and 0 deletions
@ -0,0 +1,57 @@ |
|||
package com.example.datacollect.controller; |
|||
|
|||
import com.example.datacollect.command.AnalyzeCommand; |
|||
import com.example.datacollect.command.Command; |
|||
import com.example.datacollect.command.CrawlCommand; |
|||
import com.example.datacollect.command.ExitCommand; |
|||
import com.example.datacollect.command.HelpCommand; |
|||
import com.example.datacollect.command.ListCommand; |
|||
import com.example.datacollect.exception.CrawlerException; |
|||
import com.example.datacollect.exception.ErrorCode; |
|||
import com.example.datacollect.repository.ArticleRepository; |
|||
import com.example.datacollect.strategy.StrategyFactory; |
|||
import com.example.datacollect.view.ConsoleView; |
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
|
|||
public class CrawlerController { |
|||
private final Map<String, Command> commands = new HashMap<>(); |
|||
private final ConsoleView view; |
|||
private final ArticleRepository repository; |
|||
|
|||
public CrawlerController(ConsoleView view, ArticleRepository repository, StrategyFactory strategyFactory) { |
|||
this.view = view; |
|||
this.repository = repository; |
|||
register(new HelpCommand(view)); |
|||
register(new ListCommand(view)); |
|||
register(new CrawlCommand(view, strategyFactory)); |
|||
register(new ExitCommand(view)); |
|||
register(new AnalyzeCommand(view, strategyFactory)); |
|||
} |
|||
|
|||
private void register(Command command) { |
|||
commands.put(command.getName(), command); |
|||
} |
|||
|
|||
public void handle(String input) { |
|||
String text = input == null ? "" : input.trim(); |
|||
if (text.isEmpty()) { |
|||
return; |
|||
} |
|||
|
|||
String[] args = text.split("\\s+"); |
|||
String cmdName = args[0].toLowerCase(); |
|||
Command command = commands.get(cmdName); |
|||
if (command == null) { |
|||
view.printError("[" + ErrorCode.UNKNOWN_COMMAND.getCode() + "] " + ErrorCode.UNKNOWN_COMMAND.getMessage() + ": " + cmdName); |
|||
return; |
|||
} |
|||
try { |
|||
command.execute(args, repository); |
|||
} catch (CrawlerException e) { |
|||
view.printError("[" + e.getErrorCode().getCode() + "] " + e.getMessage()); |
|||
} catch (Exception e) { |
|||
view.printError("[ERR] 执行命令时发生错误: " + e.getMessage()); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,19 @@ |
|||
package com.example.datacollect.exception; |
|||
|
|||
public class CrawlerException extends RuntimeException { |
|||
private final ErrorCode errorCode; |
|||
|
|||
public CrawlerException(ErrorCode errorCode, String message) { |
|||
super(message); |
|||
this.errorCode = errorCode; |
|||
} |
|||
|
|||
public CrawlerException(ErrorCode errorCode, String message, Throwable cause) { |
|||
super(message, cause); |
|||
this.errorCode = errorCode; |
|||
} |
|||
|
|||
public ErrorCode getErrorCode() { |
|||
return errorCode; |
|||
} |
|||
} |
|||
@ -0,0 +1,28 @@ |
|||
package com.example.datacollect.exception; |
|||
|
|||
public enum ErrorCode { |
|||
UNKNOWN_COMMAND("CMD001", "未知命令"), |
|||
INVALID_URL("URL001", "无效的URL"), |
|||
STRATEGY_NOT_FOUND("STR001", "未找到匹配的解析策略"), |
|||
NETWORK_ERROR("NET001", "网络请求失败"), |
|||
PARSE_ERROR("PRS001", "网页解析失败"), |
|||
FILE_IO_ERROR("FIO001", "文件读写错误"), |
|||
INVALID_ARGUMENT("ARG001", "参数无效"), |
|||
DATA_ACCESS_ERROR("DTA001", "数据访问错误"); |
|||
|
|||
private final String code; |
|||
private final String message; |
|||
|
|||
ErrorCode(String code, String message) { |
|||
this.code = code; |
|||
this.message = message; |
|||
} |
|||
|
|||
public String getCode() { |
|||
return code; |
|||
} |
|||
|
|||
public String getMessage() { |
|||
return message; |
|||
} |
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
package com.example.datacollect.command; |
|||
|
|||
import com.example.datacollect.repository.ArticleRepository; |
|||
import com.example.datacollect.view.ConsoleView; |
|||
|
|||
public class HelpCommand implements Command { |
|||
private final ConsoleView view; |
|||
|
|||
public HelpCommand(ConsoleView view) { |
|||
this.view = view; |
|||
} |
|||
|
|||
@Override |
|||
public String getName() { |
|||
return "help"; |
|||
} |
|||
|
|||
@Override |
|||
public void execute(String[] args, ArticleRepository repository) { |
|||
view.printInfo("可用命令:"); |
|||
view.printInfo(" crawl <url> - 爬取指定URL的文章并保存"); |
|||
view.printInfo(" analyze <url> - 分析指定URL(不保存数据)"); |
|||
view.printInfo(" list - 列出已保存的文章"); |
|||
view.printInfo(" help - 显示此帮助信息"); |
|||
view.printInfo(" exit - 退出程序"); |
|||
} |
|||
} |
|||
@ -0,0 +1,22 @@ |
|||
package com.example.datacollect.command; |
|||
|
|||
import com.example.datacollect.repository.ArticleRepository; |
|||
import com.example.datacollect.view.ConsoleView; |
|||
|
|||
public class ListCommand implements Command { |
|||
private final ConsoleView view; |
|||
|
|||
public ListCommand(ConsoleView view) { |
|||
this.view = view; |
|||
} |
|||
|
|||
@Override |
|||
public String getName() { |
|||
return "list"; |
|||
} |
|||
|
|||
@Override |
|||
public void execute(String[] args, ArticleRepository repository) { |
|||
view.display(repository.getAll()); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue