OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 83 | 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
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
17 ilm 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
 
16
import java.util.HashMap;
83 ilm 17
import java.util.HashSet;
17 ilm 18
import java.util.Map;
83 ilm 19
import java.util.Set;
17 ilm 20
 
21
import org.jdom.Document;
22
import org.jdom.Element;
23
import org.jdom.Namespace;
24
 
182 ilm 25
import net.jcip.annotations.Immutable;
26
 
17 ilm 27
/**
28
 * Encapsulate all namespaces for a particular version of xml.
29
 *
30
 * @author ILM Informatique 26 juil. 2004
31
 */
80 ilm 32
@Immutable
17 ilm 33
public enum XMLVersion {
34
 
35
    // OpenOffice.org 1.x.
73 ilm 36
    OOo("OpenOffice.org", Namespace.getNamespace("manifest", "http://openoffice.org/2001/manifest"), "script") {
17 ilm 37
        {
38
            this.putMandatory(OFFICE_1, STYLE_1, TEXT_1, TABLE_1);
39
            this.put("number", NUMBER_1);
40
            this.put("draw", DRAW_1);
41
            this.put("fo", FO_1);
42
            this.put("form", "http://openoffice.org/2000/form");
43
            this.put("xlink", "http://www.w3.org/1999/xlink");
44
            this.put("script", "http://openoffice.org/2000/script");
45
            this.put("svg", "http://www.w3.org/2000/svg");
46
            this.put("meta", "http://openoffice.org/2000/meta");
73 ilm 47
            this.put("config", "http://openoffice.org/2001/config");
17 ilm 48
            this.put("dc", "http://purl.org/dc/elements/1.1/");
49
        }
50
    },
51
    // OpenDocument 1.x/OpenOffice.org 2.x.
73 ilm 52
    OD("OpenDocument", Namespace.getNamespace("manifest", "urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"), "ooo") {
17 ilm 53
        {
54
            this.putMandatory(OFFICE_2, STYLE_2, TEXT_2, TABLE_2);
55
            this.put("number", NUMBER_2);
56
            this.put("draw", DRAW_2);
57
            this.put("fo", FO_2);
58
            this.put("form", "urn:oasis:names:tc:opendocument:xmlns:form:1.0");
59
            this.put("xlink", "http://www.w3.org/1999/xlink");
60
            this.put("script", "urn:oasis:names:tc:opendocument:xmlns:script:1.0");
61
            this.put("svg", "urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0");
62
            this.put("meta", "urn:oasis:names:tc:opendocument:xmlns:meta:1.0");
73 ilm 63
            this.put("config", "urn:oasis:names:tc:opendocument:xmlns:config:1.0");
17 ilm 64
            this.put("dc", "http://purl.org/dc/elements/1.1/");
73 ilm 65
            this.put("ooo", "http://openoffice.org/2004/office");
66
            this.put("of", "urn:oasis:names:tc:opendocument:xmlns:of:1.2");
182 ilm 67
            this.put("loext", "urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0");
17 ilm 68
        }
69
    };
70
 
73 ilm 71
    public static final Namespace LIBRARY_NS = Namespace.getNamespace("library", "http://openoffice.org/2000/library");
72
    public static final Namespace DIALOG_NS = Namespace.getNamespace("dlg", "http://openoffice.org/2000/dialog");
73
 
17 ilm 74
    private static final String OFFICE_1 = "http://openoffice.org/2000/office";
75
    private static final String STYLE_1 = "http://openoffice.org/2000/style";
76
    private static final String TEXT_1 = "http://openoffice.org/2000/text";
77
    private static final String NUMBER_1 = "http://openoffice.org/2000/datastyle";
78
    private static final String TABLE_1 = "http://openoffice.org/2000/table";
79
    private static final String DRAW_1 = "http://openoffice.org/2000/drawing";
80
    private static final String FO_1 = "http://www.w3.org/1999/XSL/Format";
81
 
82
    private static final String OFFICE_2 = "urn:oasis:names:tc:opendocument:xmlns:office:1.0";
83
    private static final String STYLE_2 = "urn:oasis:names:tc:opendocument:xmlns:style:1.0";
84
    private static final String TEXT_2 = "urn:oasis:names:tc:opendocument:xmlns:text:1.0";
85
    private static final String NUMBER_2 = "urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0";
86
    private static final String TABLE_2 = "urn:oasis:names:tc:opendocument:xmlns:table:1.0";
87
    private static final String DRAW_2 = "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0";
88
    private static final String FO_2 = "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0";
89
 
90
    private final String name;
91
    private final Map<String, Namespace> nss;
73 ilm 92
    private final String librariesPrefix;
17 ilm 93
    private final Namespace manifest;
94
 
73 ilm 95
    private XMLVersion(String name, Namespace manifest, final String librariesPrefix) {
17 ilm 96
        this.name = name;
97
        this.nss = new HashMap<String, Namespace>(16);
73 ilm 98
        this.librariesPrefix = librariesPrefix;
17 ilm 99
        this.manifest = manifest;
100
    }
101
 
102
    protected final void putMandatory(String office, String style, String text, String table) {
103
        this.put("office", office);
104
        this.put("style", style);
105
        this.put("text", text);
106
        this.put("table", table);
107
    }
108
 
109
    protected final void put(String prefix, String uri) {
110
        this.nss.put(prefix, Namespace.getNamespace(prefix, uri));
111
    }
112
 
113
    public final Namespace getNS(String prefix) {
114
        if (!this.nss.containsKey(prefix))
115
            throw new IllegalStateException("unknown " + prefix + " : " + this.nss.keySet());
116
        return this.nss.get(prefix);
117
    }
118
 
119
    public final String getName() {
120
        return this.name;
121
    }
122
 
123
    public final Namespace getManifest() {
124
        return this.manifest;
125
    }
126
 
127
    public Namespace getOFFICE() {
128
        return this.getNS("office");
129
    }
130
 
131
    public Namespace getSTYLE() {
132
        return this.getNS("style");
133
    }
134
 
135
    public Namespace getTEXT() {
136
        return this.getNS("text");
137
    }
138
 
139
    public Namespace getTABLE() {
140
        return this.getNS("table");
141
    }
142
 
143
    public Namespace getMETA() {
144
        return this.getNS("meta");
145
    }
146
 
147
    public Namespace[] getALL() {
148
        return this.nss.values().toArray(new Namespace[this.nss.size()]);
149
    }
150
 
83 ilm 151
    public Set<Namespace> getNamespaceSet() {
152
        return new HashSet<Namespace>(this.nss.values());
153
    }
154
 
73 ilm 155
    public Namespace getLibrariesNS() {
156
        return this.getNS(this.librariesPrefix);
157
    }
158
 
17 ilm 159
    // *** static public
160
 
161
    /**
162
     * Namespaces for OpenOffice.org 1.x.
163
     *
164
     * @return namespaces for OO.o 1.
165
     */
166
    public static final XMLVersion getOOo() {
167
        return OOo;
168
    }
169
 
170
    /**
171
     * Namespaces for OpenDocument/OpenOffice.org 2.x.
172
     *
173
     * @return namespaces for OpenDocument.
174
     */
175
    public static final XMLVersion getOD() {
176
        return OD;
177
    }
178
 
179
    /**
180
     * Find the NS to which belongs the passed namespace.
181
     *
182
     * @param ns the namespace, eg office=http://openoffice.org/2000/office.
183
     * @return the matching NS, eg NS.getOOo(), or <code>null</code> if none is found.
184
     */
185
    public static final XMLVersion getParent(Namespace ns) {
186
        for (XMLVersion v : values()) {
187
            if (v.getNS(ns.getPrefix()).equals(ns))
188
                return v;
189
        }
190
        return null;
191
    }
192
 
193
    public static final XMLVersion getVersion(Document doc) {
194
        return getVersion(doc.getRootElement());
195
    }
196
 
197
    /**
198
     * Infer the version of an XML element from its namespace.
199
     *
200
     * @param elem the element to be tested, eg &lt;text:line-break/&gt;.
201
     * @return the version.
202
     * @throws IllegalArgumentException if the namespace is unknown.
203
     */
204
    public static final XMLVersion getVersion(Element elem) {
205
        final XMLVersion parent = getParent(elem.getNamespace());
206
        if (parent == null)
207
            throw new IllegalArgumentException(elem + " is not an OpenOffice element.");
208
        return parent;
209
    }
210
 
211
    public static XMLVersion getDefault() {
19 ilm 212
        return OOXML.getDefault().getVersion();
17 ilm 213
    }
214
}