OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 19 | 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.openoffice;
15
 
25 ilm 16
import java.io.File;
17
import java.io.FileNotFoundException;
18
import java.io.IOException;
19
import java.text.ParseException;
20
 
21
import org.jdom.Document;
22
import org.jdom.Element;
23
import org.jdom.Namespace;
24
 
17 ilm 25
/**
26
 * An ODF document, like a spreadsheet or a text file.
27
 *
28
 * @author Sylvain
29
 */
25 ilm 30
public abstract class ODDocument {
17 ilm 31
 
25 ilm 32
    private final ODPackage pkg;
33
    private ODEpoch epoch;
19 ilm 34
 
25 ilm 35
    protected ODDocument(final ODPackage orig) {
36
        // don't want multiple document per package.
37
        if (orig.hasODDocument())
38
            throw new IllegalStateException("ODPackage already has an ODDocument");
39
        this.pkg = orig;
40
    }
41
 
42
    public final XMLVersion getVersion() {
43
        return this.getFormatVersion().getXMLVersion();
44
    }
45
 
46
    public final XMLFormatVersion getFormatVersion() {
47
        return this.getPackage().getFormatVersion();
48
    }
49
 
50
    public final ODPackage getPackage() {
51
        return this.pkg;
52
    }
53
 
54
    public final Document getContentDocument() {
55
        return this.getPackage().getContent().getDocument();
56
    }
57
 
58
    protected final Element getBody() {
59
        return getPackage().getContentType().getBody(this.getContentDocument());
60
    }
61
 
62
    private final String findEpoch() throws ParseException {
63
        final Namespace tableNS = getVersion().getTABLE();
64
        final Element settings = this.getBody().getChild("calculation-settings", tableNS);
65
        if (settings != null) {
66
            final Element nullDateElem = settings.getChild("null-date", tableNS);
67
            if (nullDateElem != null)
68
                return nullDateElem.getAttributeValue("date-value", tableNS);
69
        }
70
        return null;
71
    }
72
 
73
    public final ODEpoch getEpoch() {
74
        return this.getEpoch(false);
75
    }
76
 
77
    public final ODEpoch getEpoch(final boolean updateFromXML) {
78
        if (this.epoch == null || updateFromXML) {
79
            try {
80
                this.epoch = ODEpoch.getInstance(this.findEpoch());
81
            } catch (ParseException e) {
82
                // quite rare
83
                throw new IllegalStateException("Unable to parse the epoch of " + this, e);
84
            }
85
        }
86
        assert this.epoch != null;
87
        return this.epoch;
88
    }
89
 
90
    // *** Files
91
 
92
    public File saveAs(File file) throws FileNotFoundException, IOException {
93
        this.getPackage().setFile(file);
94
        return this.getPackage().save();
95
    }
17 ilm 96
}