17 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
|
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
|
|
5 |
*
|
|
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
|
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
|
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
|
|
9 |
* language governing permissions and limitations under the License.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
package org.openconcerto.odtemplate;
|
|
|
15 |
|
|
|
16 |
import org.openconcerto.odtemplate.engine.DataModel;
|
|
|
17 |
import org.openconcerto.odtemplate.engine.Material;
|
|
|
18 |
import org.openconcerto.odtemplate.engine.Parsed;
|
|
|
19 |
import org.openconcerto.openoffice.ODPackage;
|
|
|
20 |
import org.openconcerto.openoffice.ODSingleXMLDocument;
|
|
|
21 |
|
|
|
22 |
import java.io.File;
|
|
|
23 |
import java.io.IOException;
|
|
|
24 |
import java.io.InputStream;
|
|
|
25 |
import java.io.OutputStream;
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Generates documents merging template and data. <br>
|
|
|
29 |
* <br>
|
|
|
30 |
* Sample usage:
|
|
|
31 |
*
|
|
|
32 |
* <pre>
|
|
|
33 |
* Template template = new Template(new FileInputStream("template.sxw"));
|
|
|
34 |
* Map vars = new HashMap();
|
|
|
35 |
* vars.put("title", "The Title");
|
|
|
36 |
* //...
|
|
|
37 |
* template.createDocument(vars, new FileOutputStream("document.sxw"));
|
|
|
38 |
* </pre>
|
|
|
39 |
*/
|
|
|
40 |
public class Template {
|
|
|
41 |
|
|
|
42 |
protected final Parsed<ODPackage> contentTemplate;
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Loads a template from the specified input stream.
|
|
|
46 |
*
|
|
|
47 |
* @param in a stream on a ODF package.
|
|
|
48 |
* @throws IOException if the stream can't be read.
|
|
|
49 |
* @throws TemplateException if the template statements are invalid.
|
|
|
50 |
*/
|
|
|
51 |
public Template(InputStream in) throws IOException, TemplateException {
|
|
|
52 |
this(new ODPackage(in));
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
public Template(File f) throws IOException, TemplateException {
|
|
|
56 |
this(new ODPackage(f));
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
public Template(final ODPackage contents) throws IOException, TemplateException {
|
|
|
60 |
// createDocument needs ODSingleXMLDocument
|
|
|
61 |
contents.toSingle();
|
|
|
62 |
this.contentTemplate = new Parsed<ODPackage>(Material.from(contents));
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Generates a document merging template and data.
|
|
|
67 |
*
|
|
|
68 |
* @param engine the data model.
|
|
|
69 |
* @param out stream the document is written to.
|
|
|
70 |
* @throws IOException si erreur de zippage.
|
|
|
71 |
* @throws TemplateException
|
|
|
72 |
*/
|
|
|
73 |
public void createDocument(DataModel engine, OutputStream out) throws IOException, TemplateException {
|
|
|
74 |
this.execute(engine).save(out);
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
public ODSingleXMLDocument createDocument(DataModel engine) throws TemplateException {
|
|
|
78 |
return (ODSingleXMLDocument) execute(engine).getContent();
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
private ODPackage execute(DataModel engine) throws TemplateException {
|
|
|
82 |
return this.contentTemplate.execute(engine);
|
|
|
83 |
}
|
|
|
84 |
}
|