Skip to main content

easyofd_core/doc/
doc_dir.rs

1//! 文档目录(DocDir)。
2//!
3//! 对应 Java: org.ofdrw.pkg.container.DocDir
4//!
5//! 文档容器,对应 OFD 包中的 Doc_N/ 目录。
6//! 包含 Document.xml、资源、页面、签名等子目录和文件。
7
8/// 文档目录。
9///
10/// 描述单个 OFD 文档的目录结构,管理文档内的
11/// 各种子容器(页面、资源、签名、注释等)。
12#[derive(Debug, Clone)]
13pub struct DocDir {
14    /// 文档索引(从 0 开始)。
15    pub index: u32,
16    /// 文档根节点描述文件名称。
17    pub document_file_name: String,
18    /// 公共资源索引描述文件名称。
19    pub public_res_file_name: String,
20    /// 文档自身资源索引描述文件名称。
21    pub document_res_file_name: String,
22    /// 数字签名容器名称。
23    pub signs_dir_name: String,
24    /// 页面容器名称。
25    pub pages_dir_name: String,
26    /// 资源容器名称。
27    pub res_dir_name: String,
28    /// 注释容器名称。
29    pub annots_dir_name: String,
30    /// 注释入口文件名称。
31    pub annotations_file_name: String,
32    /// 附件入口文件名称。
33    pub attachments_file_name: String,
34}
35
36impl DocDir {
37    /// 文档容器名称前缀。
38    pub const DOC_CONTAINER_PREFIX: &'static str = "Doc_";
39    /// 文档根节点描述文件名。
40    pub const DOCUMENT_FILE_NAME: &'static str = "Document.xml";
41    /// 公共资源索引文件名。
42    pub const PUBLIC_RES_FILE_NAME: &'static str = "PublicRes.xml";
43    /// 文档自身资源索引文件名。
44    pub const DOCUMENT_RES_FILE_NAME: &'static str = "DocumentRes.xml";
45    /// 数字签名容器名。
46    pub const SIGNS_DIR_NAME: &'static str = "Signs";
47    /// 数字签名容器名称前缀。
48    pub const SIGN_CONTAINER_PREFIX: &'static str = "Sign_";
49    /// 自定义标签容器名。
50    pub const TAGS_DIR_NAME: &'static str = "Tags";
51    /// 临时文件容器名。
52    pub const TEMPS_DIR_NAME: &'static str = "Temps";
53    /// 页面容器名。
54    pub const PAGES_DIR_NAME: &'static str = "Pages";
55    /// 页面容器名称前缀。
56    pub const PAGE_CONTAINER_PREFIX: &'static str = "Page_";
57    /// 资源容器名。
58    pub const RES_DIR_NAME: &'static str = "Res";
59    /// 注释容器名。
60    pub const ANNOTS_DIR_NAME: &'static str = "Annots";
61    /// 注释入口文件名。
62    pub const ANNOTATIONS_FILE_NAME: &'static str = "Annotations.xml";
63    /// 附件入口文件名。
64    pub const ATTACHMENTS_FILE_NAME: &'static str = "Attachments.xml";
65
66    /// 创建新的文档目录。
67    #[must_use]
68    pub fn new(index: u32) -> Self {
69        Self {
70            index,
71            document_file_name: Self::DOCUMENT_FILE_NAME.to_string(),
72            public_res_file_name: Self::PUBLIC_RES_FILE_NAME.to_string(),
73            document_res_file_name: Self::DOCUMENT_RES_FILE_NAME.to_string(),
74            signs_dir_name: Self::SIGNS_DIR_NAME.to_string(),
75            pages_dir_name: Self::PAGES_DIR_NAME.to_string(),
76            res_dir_name: Self::RES_DIR_NAME.to_string(),
77            annots_dir_name: Self::ANNOTS_DIR_NAME.to_string(),
78            annotations_file_name: Self::ANNOTATIONS_FILE_NAME.to_string(),
79            attachments_file_name: Self::ATTACHMENTS_FILE_NAME.to_string(),
80        }
81    }
82
83    /// 获取文档容器目录名。
84    #[must_use]
85    pub fn container_name(&self) -> String {
86        format!("{}{}", Self::DOC_CONTAINER_PREFIX, self.index)
87    }
88
89    /// 获取文档索引。
90    pub fn index(&self) -> u32 {
91        self.index
92    }
93}
94
95#[cfg(test)]
96mod tests {
97    use super::*;
98
99    #[test]
100    fn test_doc_dir_new() {
101        let dd = DocDir::new(0);
102        assert_eq!(dd.index, 0);
103        assert_eq!(dd.document_file_name, "Document.xml");
104        assert_eq!(dd.signs_dir_name, "Signs");
105        assert_eq!(dd.pages_dir_name, "Pages");
106    }
107
108    #[test]
109    fn test_doc_dir_container_name() {
110        assert_eq!(DocDir::new(0).container_name(), "Doc_0");
111        assert_eq!(DocDir::new(3).container_name(), "Doc_3");
112    }
113
114    #[test]
115    fn test_doc_dir_constants() {
116        assert_eq!(DocDir::DOC_CONTAINER_PREFIX, "Doc_");
117        assert_eq!(DocDir::DOCUMENT_FILE_NAME, "Document.xml");
118        assert_eq!(DocDir::SIGNS_DIR_NAME, "Signs");
119        assert_eq!(DocDir::PAGES_DIR_NAME, "Pages");
120        assert_eq!(DocDir::RES_DIR_NAME, "Res");
121        assert_eq!(DocDir::SIGN_CONTAINER_PREFIX, "Sign_");
122    }
123
124    #[test]
125    fn test_doc_dir_index() {
126        let dd = DocDir::new(5);
127        assert_eq!(dd.index(), 5);
128    }
129}