OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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(&quot;template.sxw&quot;));
34
 * Map vars = new HashMap();
35
 * vars.put(&quot;title&quot;, &quot;The Title&quot;);
180 ilm 36
 * // ...
17 ilm 37
 * template.createDocument(vars, new FileOutputStream(&quot;document.sxw&quot;));
38
 * </pre>
39
 */
180 ilm 40
public class Template implements AutoCloseable {
17 ilm 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
 
180 ilm 65
    @Override
66
    public void close() throws Exception {
67
        this.contentTemplate.destroy();
68
    }
69
 
17 ilm 70
    /**
71
     * Generates a document merging template and data.
72
     *
73
     * @param engine the data model.
74
     * @param out stream the document is written to.
75
     * @throws IOException si erreur de zippage.
76
     * @throws TemplateException
77
     */
78
    public void createDocument(DataModel engine, OutputStream out) throws IOException, TemplateException {
79
        this.execute(engine).save(out);
80
    }
81
 
82
    public ODSingleXMLDocument createDocument(DataModel engine) throws TemplateException {
83
        return (ODSingleXMLDocument) execute(engine).getContent();
84
    }
85
 
86
    private ODPackage execute(DataModel engine) throws TemplateException {
87
        return this.contentTemplate.execute(engine);
88
    }
89
}