feat: Add doc app props

main
bokuweb 2019-11-05 19:20:40 +09:00
parent 3e154d6904
commit 490efa47c2
11 changed files with 277 additions and 8 deletions

View File

@ -1,2 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"><Template></Template><TotalTime>0</TotalTime><Application>LibreOffice/6.0.7.3$Linux_X86_64 LibreOffice_project/00m0$Build-3</Application><Pages>1</Pages><Words>1</Words><Characters>5</Characters><CharactersWithSpaces>5</CharactersWithSpaces><Paragraphs>1</Paragraphs></Properties>
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties"
xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
<Template></Template>
<TotalTime>0</TotalTime>
<Application>LibreOffice/6.0.7.3$Linux_X86_64 LibreOffice_project/00m0$Build-3</Application>
<Pages>1</Pages>
<Words>1</Words>
<Characters>5</Characters>
<CharactersWithSpaces>5</CharactersWithSpaces>
<Paragraphs>1</Paragraphs>
</Properties>

View File

@ -1,2 +1,16 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><dcterms:created xsi:type="dcterms:W3CDTF">2019-09-13T19:32:17Z</dcterms:created><dc:creator></dc:creator><dc:description></dc:description><dc:language>ja-JP</dc:language><cp:lastModifiedBy></cp:lastModifiedBy><dcterms:modified xsi:type="dcterms:W3CDTF">2019-09-13T19:32:34Z</dcterms:modified><cp:revision>1</cp:revision><dc:subject></dc:subject><dc:title></dc:title></cp:coreProperties>
<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:dcmitype="http://purl.org/dc/dcmitype/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<dcterms:created xsi:type="dcterms:W3CDTF">2019-09-13T19:32:17Z</dcterms:created>
<dc:creator></dc:creator>
<dc:description></dc:description>
<dc:language>ja-JP</dc:language>
<cp:lastModifiedBy></cp:lastModifiedBy>
<dcterms:modified xsi:type="dcterms:W3CDTF">2019-09-13T19:32:34Z</dcterms:modified>
<cp:revision>1</cp:revision>
<dc:subject></dc:subject>
<dc:title></dc:title>
</cp:coreProperties>

View File

@ -9,7 +9,7 @@ impl ContentTypes {
pub fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new();
b.declaration()
b.declaration(None)
.open_types("http://schemas.openxmlformats.org/package/2006/content-types")
.add_override(
"/_rels/.rels",

View File

@ -0,0 +1,113 @@
use crate::xml_builder::*;
pub struct AppProps {
config: Option<AppPropsConfig>,
}
pub struct AppPropsConfig {
template: Option<&'static str>,
total_time: Option<usize>,
application: Option<&'static str>,
pages: Option<usize>,
words: Option<usize>,
characters: Option<usize>,
characters_with_spaces: Option<usize>,
paragraphs: Option<usize>,
}
impl AppProps {
pub fn new(config: Option<AppPropsConfig>) -> AppProps {
AppProps { config }
}
pub(crate) fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new();
let base = b.declaration(Some(true)).open_properties(
"http://schemas.openxmlformats.org/officeDocument/2006/extended-properties",
"http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes",
);
let convert = |v: usize| format!("{}", v);
let default = || "".to_owned();
if let Some(c) = &self.config {
base.template(c.template.map_or_else(|| "", |v| v))
.total_time(&c.total_time.map_or_else(default, convert))
.application(c.application.map_or_else(|| "", |v| v))
.pages(&c.pages.map_or_else(default, convert))
.words(&c.words.map_or_else(default, convert))
.characters(&c.characters.map_or_else(default, convert))
.characters_with_spaces(&c.characters_with_spaces.map_or_else(default, convert))
.paragraphs(&c.paragraphs.map_or_else(default, convert))
.close()
.build()
} else {
base.template("")
.total_time("")
.application("")
.pages("")
.words("")
.characters("")
.characters_with_spaces("")
.paragraphs("")
.close()
.build()
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::str;
#[test]
fn test_default_doc_props_app_build() {
let c = AppProps::new(None);
let b = c.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
<Template></Template>
<TotalTime></TotalTime>
<Application></Application>
<Pages></Pages>
<Words></Words>
<Characters></Characters>
<CharactersWithSpaces></CharactersWithSpaces>
<Paragraphs></Paragraphs>
</Properties>"#
);
}
#[test]
fn test_configured_doc_props_app_build() {
let c = AppProps::new(Some(AppPropsConfig {
template: Some("temp"),
total_time: Some(10),
application: Some("Lawgue beta1.0"),
pages: Some(1),
words: Some(20),
characters: Some(10),
characters_with_spaces: Some(22),
paragraphs: Some(30),
}));
let b = c.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
<Template>temp</Template>
<TotalTime>10</TotalTime>
<Application>Lawgue beta1.0</Application>
<Pages>1</Pages>
<Words>20</Words>
<Characters>10</Characters>
<CharactersWithSpaces>22</CharactersWithSpaces>
<Paragraphs>30</Paragraphs>
</Properties>"#
);
}
}

View File

@ -0,0 +1,14 @@
mod app;
pub use app::*;
pub(crate) struct DocProps {
app: AppProps,
}
impl DocProps {
pub(crate) fn new(appConfig: Option<AppPropsConfig>) -> DocProps {
let app = AppProps::new(appConfig);
DocProps { app }
}
}

View File

@ -1,19 +1,27 @@
mod content_types;
mod doc_props;
mod rels;
mod xml_document;
use content_types::*;
use doc_props::*;
use rels::*;
pub(crate) struct Document {
content_type: ContentTypes,
rels: Rels,
doc_props: DocProps,
}
impl Document {
pub fn new() -> Document {
let content_type = ContentTypes::new();
let rels = Rels::new();
Document { content_type, rels }
let doc_props = DocProps::new(None /* TODO: */);
Document {
content_type,
rels,
doc_props,
}
}
}

View File

@ -9,7 +9,7 @@ impl Rels {
pub fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new();
b.declaration()
b.declaration(None)
.open_relationships("http://schemas.openxmlformats.org/package/2006/relationships")
.relationship(
"rId1",

View File

@ -3,12 +3,12 @@ use super::XMLBuilder;
impl XMLBuilder {
// Build XML declaration
// i.e. <?xml version="1.0" encoding="UTF-8"?>
pub(crate) fn declaration(mut self) -> Self {
pub(crate) fn declaration(mut self, standalone: Option<bool>) -> Self {
self.writer
.write(super::XmlEvent::StartDocument {
version: super::XmlVersion::Version10,
encoding: Some("UTF-8"),
standalone: None,
standalone,
})
.expect("should write to buf");
self
@ -24,7 +24,7 @@ mod tests {
#[test]
fn test_declaration() {
let b = XMLBuilder::new();
let r = b.declaration().build();
let r = b.declaration(None).build();
assert_eq!(
str::from_utf8(&r).unwrap(),
r#"<?xml version="1.0" encoding="UTF-8"?>"#

View File

@ -7,6 +7,61 @@ macro_rules! opened_el {
self
}
};
($name: ident, $el_name: expr, $attr0: expr, $attr1: expr) => {
pub(crate) fn $name(mut self, arg0: &str, arg1: &str) -> Self {
self.writer
.write(super::XmlEvent::start_element($el_name).attr($attr0, arg0).attr($attr1, arg1))
.expect("should write to buf");
self
}
};
}
macro_rules! closed_el_with_child {
($name: ident, $el_name: expr) => {
pub(crate) fn $name(mut self, child: &str) -> Self {
self.writer
.write(super::XmlEvent::start_element($el_name))
.expect("should write to buf");
self.writer
.write(child)
.expect("should write to buf");
self.close()
}
};
($name: ident, $el_name: expr, $attr0: expr) => {
pub(crate) fn $name(mut self, arg0: &str, child: &str) -> Self {
self.writer
.write(super::XmlEvent::start_element($el_name).attr($attr0, arg0))
.expect("should write to buf");
self.writer
.write(child)
.expect("should write to buf");
self.close()
}
};
($name: ident, $el_name: expr, $attr0: expr, $attr1: expr) => {
pub(crate) fn $name(mut self, arg0: &str, arg1: &str, child: &str) -> Self {
self.writer
.write(super::XmlEvent::start_element($el_name).attr($attr0, arg0).attr($attr1, arg1))
.expect("should write to buf");
self.writer
.write(child)
.expect("should write to buf");
self.close()
}
};
($name: ident, $el_name: expr, $attr0: expr, $attr1: expr, $attr2: expr) => {
pub(crate) fn $name(mut self, arg0: &str, arg1: &str, arg2: &str, child: &str) -> Self {
self.writer
.write(super::XmlEvent::start_element($el_name).attr($attr0, arg0).attr($attr1, arg1).attr($attr2, arg2))
.expect("should write to buf");
self.writer
.write(child)
.expect("should write to buf");
self.close()
}
};
}
macro_rules! closed_el {

View File

@ -1,6 +1,7 @@
#[macro_use]
mod macros;
mod declaration;
mod properties;
mod relationship;
use xml::common::XmlVersion;

View File

@ -0,0 +1,54 @@
use super::XMLBuilder;
impl XMLBuilder {
// Build Properties element
// i.e. <Properties xmlns:vt="http://schemas.openxmlformats.org/package/2006/relationships">
opened_el!(open_properties, "Properties", "xmlns", "xmlns:vt");
closed_el_with_child!(template, "Template");
closed_el_with_child!(total_time, "TotalTime");
closed_el_with_child!(application, "Application");
closed_el_with_child!(pages, "Pages");
closed_el_with_child!(words, "Words");
closed_el_with_child!(characters, "Characters");
closed_el_with_child!(characters_with_spaces, "CharactersWithSpaces");
closed_el_with_child!(paragraphs, "Paragraphs");
}
#[cfg(test)]
mod tests {
use super::XMLBuilder;
use std::str;
#[test]
fn test_properties() {
let b = XMLBuilder::new();
let r = b
.open_properties("http://example", "http://example2")
.text("child")
.close()
.build();
assert_eq!(
str::from_utf8(&r).unwrap(),
r#"<Properties xmlns="http://example" xmlns:vt="http://example2">child</Properties>"#
);
}
#[test]
fn test_template() {
let b = XMLBuilder::new();
let r = b.template("0").build();
assert_eq!(str::from_utf8(&r).unwrap(), r#"<Template>0</Template>"#);
}
#[test]
fn test_application() {
let b = XMLBuilder::new();
let r = b.template("Lawgue").build();
assert_eq!(
str::from_utf8(&r).unwrap(),
r#"<Application>Lawgue</Application>"#
);
}
}