OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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