Skip to content

Flechazoie/Text-Editor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Text Editor

项目简介

这是一个基于命令行的多功能编辑器,支持文本和 XML 文件编辑、undo/redo、日志记录、拼写检查、编辑时长统计和工作区状态持久化等功能。

实现的功能模块

1. 基础设施层 (Infrastructure Layer)

  • FileIO: UTF-8 文件读写
  • EventBus: 观察者模式实现
  • DateTimeUtil: 时间戳工具
  • WorkspaceMemento: 状态持久化(JSON)

2. 核心层 (Core Layer)

  • TextEditor: 完整的文本编辑功能
  • CommandHistory: undo/redo 管理
  • Workspace: 多文件管理和状态持久化
  • Logger: 日志记录(观察者模式)
  • 所有编辑命令: AppendCommand, InsertCommand, DeleteCommand, ReplaceCommand

3. 应用层 (Application Layer)

  • Main: REPL 循环主程序
  • CommandParser: 命令解析器

4. Lab2 新增模块

  • XML 编辑器: XmlEditor, XmlElement, XmlDocument (组合模式)
  • XML 命令: InsertBeforeCommand, AppendChildCommand, EditIdCommand, EditTextCommand, DeleteElementCommand
  • 统计模块: EditingTimeTracker, TimeFormatter (观察者模式)
  • 拼写检查: SpellChecker 接口, LanguageToolAdapter, MockSpellChecker (适配器模式)

5. 命令系统 (Lab1 + Lab2)

  • 工作区命令 (10个): load, save, init (扩展), close, edit, editor-list (扩展), dir-tree, undo, redo, exit
  • 文本编辑命令 (5个): append, insert, delete, replace, show
  • XML 编辑命令 (6个): insert-before, append-child, edit-id, edit-text, delete-element, xml-tree
  • 拼写检查命令 (1个): spell-check
  • 日志命令 (3个): log-on, log-off, log-show

6. 设计模式应用

  • Composite Pattern: XML 树结构 (Lab2)
  • Adapter Pattern: 拼写检查器适配 (Lab2)
  • Command Pattern: 所有编辑操作 undo/redo
  • Observer Pattern: 日志事件监听、编辑时长追踪 (Lab2)
  • Memento Pattern: 工作区状态持久化

7. 测试与文档

  • Lab1: 65个单元测试,全部通过
  • Lab2: 自动化集成测试 + 详细测试清单
  • 架构设计文档
  • 完整的测试文档
  • 运行说明

快速开始

编译项目

mvn clean compile

运行测试

Lab1 单元测试:

mvn test
# 输出: Tests run: 65, Failures: 0, Errors: 0, Skipped: 0

Lab2 集成测试:

./test_lab2.sh
# 自动测试所有 Lab2 功能

手动测试 (使用详细测试清单):

# 查看测试清单
cat LAB2_测试清单.md

# 运行程序
mvn exec:java -Dexec.mainClass="com.editor.app.Main"

打包项目

mvn package

运行程序

推荐方法:使用启动脚本

# 一键启动(自动配置 LanguageTool 所需的 JVM 参数)
./run.sh

其他方法

# 方法 1: 使用 Maven(需要手动设置 JVM 参数)
mvn exec:java -Dexec.mainClass="com.editor.app.Main"

# 方法 2: 使用 JAR(需要设置 JVM 参数以启用 LanguageTool)
java -Djdk.xml.totalEntitySizeLimit=1000000 \
     -Djdk.xml.entityExpansionLimit=200000 \
     -Djdk.xml.elementAttributeLimit=200000 \
     -jar target/text-editor-1.0-SNAPSHOT-jar-with-dependencies.jar

# 跳过工作区恢复
mvn exec:java -Dexec.mainClass="com.editor.app.Main" -Dexec.args="--no-restore"

关于 JVM 参数

  • LanguageTool 需要放宽 JDK 11+ 的 XML 解析限制(grammar.xml 实体大小为 500,005 字节)
  • 如果不设置这些参数,拼写检查会自动降级到 MockSpellChecker(基本功能仍然可用)
  • run.sh 脚本已自动包含所需参数,推荐使用

项目结构

lab/
├── src/
│   ├── main/java/com/editor/
│   │   ├── app/                    # 应用层
│   │   │   ├── Main.java           # 主程序 (Lab1 + Lab2)
│   │   │   └── CommandParser.java
│   │   ├── core/                   # 核心层
│   │   │   ├── workspace/          # 工作区
│   │   │   ├── editor/             # 编辑器 (TextEditor + XmlEditor)
│   │   │   ├── command/            # 命令 (文本 + XML)
│   │   │   ├── logging/            # 日志
│   │   │   ├── xml/                # XML 核心 (Lab2 新增)
│   │   │   ├── statistics/         # 统计模块 (Lab2 新增)
│   │   │   └── spellcheck/         # 拼写检查 (Lab2 新增)
│   │   └── infrastructure/         # 基础设施层
│   │       ├── io/                 # 文件I/O
│   │       ├── observer/           # 观察者模式
│   │       └── util/               # 工具类
│   └── test/java/com/editor/       # 测试代码
├── docs/                           # Lab1 文档
├── test_lab2.sh                    # Lab2 自动化测试脚本
├── pom.xml                         # Maven配置
└── README.md                       # 项目说明 (本文件)

架构设计

三层架构

  • Application Layer: 命令解析、用户交互
  • Core Layer: 业务逻辑、编辑器、工作区、日志
  • Infrastructure Layer: 文件I/O、事件总线、工具类

设计模式

  • Observer: EventBus 事件发布订阅
  • Command: TextCommand undo/redo
  • Memento: WorkspaceMemento 状态持久化
  • Decorator: LoggingDecorator 日志装饰器
  • Composite: XML 树结构 (Lab2)
  • Adapter: 拼写检查器 (Lab2)

Lab2 功能演示

XML 编辑示例

> init xml book.xml
新 XML 文件已创建: book.xml

> append-child title t1 root "My Book"
子元素已追加: t1

> append-child author a1 root "John Doe"
子元素已追加: a1

> append-child chapter c1 root
子元素已追加: c1

> append-child title t2 c1 "Chapter 1"
子元素已追加: t2

> xml-tree
└── root [id="root"]
    ├── title [id="t1"]
    │   └── "My Book"
    ├── author [id="a1"]
    │   └── "John Doe"
    └── chapter [id="c1"]
        └── title [id="t2"]
            └── "Chapter 1"

> edit-id t1 main-title
元素 ID 已修改: t1 -> main-title

> edit-text a1 "Jane Smith"
元素文本已修改: a1

> save
文件已保存

编辑时长统计示例

> init xml test.xml
> init text notes.txt
> editor-list
> test.xml* (3秒)
  notes.txt* (1秒)

拼写检查示例

> init text note.txt
> append "This is a test with recieve error."
> spell-check
拼写检查结果 (使用 Mock 检查器):
第1行,第21列: "recieve" -> 建议: receive

技术栈

  • Java 11
  • Maven 3.6+
  • JUnit 5 (单元测试)
  • Gson (JSON 序列化)
  • LanguageTool (拼写检查)

许可证

MIT License

About

2025 Fall Software Design

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors