OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 21 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 21 Rev 180
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
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
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
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.
9
 * language governing permissions and limitations under the License.
10
 * 
10
 * 
11
 * When distributing the software, include this License Header Notice in each file.
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.openoffice;
14
 package org.openconcerto.openoffice;
15
 
15
 
16
import org.openconcerto.utils.FileUtils;
16
import org.openconcerto.utils.FileUtils;
17
import org.openconcerto.xml.JDOMUtils;
17
import org.openconcerto.xml.JDOMUtils;
18
 
18
 
19
import java.awt.Color;
19
import java.awt.Color;
20
import java.io.File;
20
import java.io.File;
21
import java.io.IOException;
21
import java.io.IOException;
22
import java.io.StringReader;
22
import java.io.StringReader;
-
 
23
import java.util.Locale;
23
 
24
 
24
import org.jdom.Document;
25
import org.jdom.Document;
-
 
26
import org.jdom.Element;
25
import org.jdom.JDOMException;
27
import org.jdom.JDOMException;
26
import org.jdom.Namespace;
28
import org.jdom.Namespace;
27
import org.jdom.input.SAXBuilder;
29
import org.jdom.input.SAXBuilder;
28
import org.jdom.xpath.XPath;
30
import org.jdom.xpath.XPath;
29
import org.xml.sax.EntityResolver;
31
import org.xml.sax.EntityResolver;
30
import org.xml.sax.InputSource;
32
import org.xml.sax.InputSource;
31
 
33
 
32
public class OOUtils {
34
public class OOUtils {
33
 
35
 
34
    // MAYBE configurable
36
    // MAYBE configurable
35
    static private final String[] executables = { "ooffice2", "ooffice", "soffice" };
37
    static private final String[] executables = { "ooffice2", "ooffice", "soffice" };
36
 
38
 
37
    /**
39
    /**
38
     * Open the passed file in OpenOffice.
40
     * Open the passed file in OpenOffice.
39
     * 
41
     * 
40
     * @param f the file to open.
42
     * @param f the file to open.
41
     * @throws IOException if openoffice could not be opened
43
     * @throws IOException if openoffice could not be opened
42
     */
44
     */
43
    static public void open(File f) throws IOException {
45
    static public void open(File f) throws IOException {
44
        FileUtils.open(f, executables);
46
        FileUtils.open(f, executables);
45
    }
47
    }
46
 
48
 
47
    /**
49
    /**
48
     * Return a builder who doesn't load DTD.
50
     * Return a builder who doesn't load DTD.
49
     * 
51
     * 
50
     * @return a builder who doesn't load DTD.
52
     * @return a builder who doesn't load DTD.
51
     */
53
     */
52
    static public SAXBuilder getBuilder() {
54
    static public SAXBuilder getBuilder() {
53
        SAXBuilder builder = new SAXBuilder();
55
        SAXBuilder builder = new SAXBuilder();
54
        builder.setEntityResolver(new EntityResolver() {
56
        builder.setEntityResolver(new EntityResolver() {
55
            public InputSource resolveEntity(String publicId, String systemId) {
57
            public InputSource resolveEntity(String publicId, String systemId) {
56
                // les dtd sont "vidées"
58
                // les dtd sont "vidées"
57
                if (systemId.endsWith(".dtd")) {
59
                if (systemId.endsWith(".dtd")) {
58
                    InputSource in = new InputSource();
60
                    InputSource in = new InputSource();
59
                    in.setCharacterStream(new StringReader(""));
61
                    in.setCharacterStream(new StringReader(""));
60
                    return in;
62
                    return in;
61
                } else
63
                } else
62
                    return null;
64
                    return null;
63
            }
65
            }
64
        });
66
        });
65
        return builder;
67
        return builder;
66
    }
68
    }
67
 
69
 
68
    /**
70
    /**
69
     * Return a builder who loads DTD.
71
     * Return a builder who loads DTD.
70
     * 
72
     * 
71
     * @return a builder who loads DTD.
73
     * @return a builder who loads DTD.
72
     */
74
     */
73
    static public SAXBuilder getBuilderLoadDTD() {
75
    static public SAXBuilder getBuilderLoadDTD() {
74
        SAXBuilder builder = new SAXBuilder() {
76
        SAXBuilder builder = new SAXBuilder() {
75
            public Document build(InputSource in) throws JDOMException, IOException {
77
            public Document build(InputSource in) throws JDOMException, IOException {
76
                in.setSystemId(OOUtils.class.getResource("oofficeDTDs/").toExternalForm());
78
                in.setSystemId(OOUtils.class.getResource("oofficeDTDs/").toExternalForm());
77
                return super.build(in);
79
                return super.build(in);
78
            }
80
            }
79
        };
81
        };
80
        return builder;
82
        return builder;
81
    }
83
    }
82
 
84
 
83
    static public Document parseDocument(String doc) throws JDOMException {
85
    static public Document parseDocument(String doc) throws JDOMException {
84
        return JDOMUtils.parseStringDocument(doc, getBuilder());
86
        return JDOMUtils.parseStringDocument(doc, getBuilder());
85
    }
87
    }
86
 
88
 
87
    /**
89
    /**
88
     * Create an XPath with OO namespaces.
90
     * Create an XPath with OO namespaces.
89
     * 
91
     * 
90
     * @param path the xpath to create.
92
     * @param path the xpath to create.
91
     * @param version XML version.
93
     * @param version XML version.
92
     * @return the specified XPath.
94
     * @return the specified XPath.
93
     * @throws JDOMException if the path is malformed.
95
     * @throws JDOMException if the path is malformed.
94
     */
96
     */
95
    public static final XPath getXPath(String path, XMLVersion version) throws JDOMException {
97
    public static final XPath getXPath(String path, XMLVersion version) throws JDOMException {
96
        final XPath xp = XPath.newInstance(path);
98
        final XPath xp = XPath.newInstance(path);
97
        for (final Namespace ns : version.getALL()) {
99
        for (final Namespace ns : version.getALL()) {
98
            xp.addNamespace(ns);
100
            xp.addNamespace(ns);
99
        }
101
        }
100
        return xp;
102
        return xp;
101
    }
103
    }
102
 
104
 
103
    /**
105
    /**
104
     * Encode to the color data type, see section 18.3.8 of OpenDocument-v1.2-part1.
106
     * Encode to the color data type, see section 18.3.8 of OpenDocument-v1.2-part1.
105
     * 
107
     * 
106
     * @param color a color
108
     * @param color a color
107
     * @return the string encoded version.
109
     * @return the string encoded version.
108
     */
110
     */
109
    public static String encodeRGB(Color color) {
111
    public static String encodeRGB(Color color) {
110
        return "#" + Integer.toHexString(color.getRGB()).substring(2).toUpperCase();
112
        return "#" + Integer.toHexString(color.getRGB()).substring(2).toUpperCase();
111
    }
113
    }
112
 
114
 
113
    /**
115
    /**
114
     * Decode a color.
116
     * Decode a color.
115
     * 
117
     * 
116
     * @param color a RGB color in notation "#rrggbb", can be <code>null</code>.
118
     * @param color a RGB color in notation "#rrggbb", can be <code>null</code>.
117
     * @return the corresponding color.
119
     * @return the corresponding color.
118
     * @see #encodeRGB(Color)
120
     * @see #encodeRGB(Color)
119
     */
121
     */
120
    public static Color decodeRGB(String color) {
122
    public static Color decodeRGB(String color) {
121
        return color == null ? null : Color.decode(color.trim());
123
        return color == null ? null : Color.decode(color.trim());
122
    }
124
    }
-
 
125
 
-
 
126
    public static final Locale getElementLocale(final Element elem) {
-
 
127
        return getElementLocale(elem, elem.getNamespace());
-
 
128
    }
-
 
129
 
-
 
130
    public static final Locale getElementLocale(final Element elem, final Namespace ns) {
-
 
131
        final Locale res;
-
 
132
        final String country = elem.getAttributeValue("country", ns);
-
 
133
        final String lang = elem.getAttributeValue("language", ns);
-
 
134
        if (lang != null) {
-
 
135
            res = new Locale.Builder().setLanguage(lang).setRegion(country).build();
-
 
136
        } else {
-
 
137
            res = null;
-
 
138
        }
-
 
139
        return res;
-
 
140
    }
-
 
141
 
-
 
142
    public static final void setElementLocale(final Element elem, final Namespace ns, final Locale l) {
-
 
143
        if (l == null)
-
 
144
            elem.removeAttribute("country", ns);
-
 
145
        else
-
 
146
            elem.setAttribute("country", l.getCountry(), ns);
-
 
147
 
-
 
148
        final String lang = l == null ? null : l.getLanguage();
-
 
149
        if (lang == null || lang.isEmpty())
-
 
150
            elem.removeAttribute("language", ns);
-
 
151
        else
-
 
152
            elem.setAttribute("language", lang, ns);
-
 
153
    }
123
}
154
}