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 org.openconcerto.utils.FileUtils;
|
|
|
17 |
import org.openconcerto.xml.JDOMUtils;
|
|
|
18 |
|
|
|
19 |
import java.awt.Color;
|
|
|
20 |
import java.io.File;
|
|
|
21 |
import java.io.IOException;
|
|
|
22 |
import java.io.StringReader;
|
180 |
ilm |
23 |
import java.util.Locale;
|
17 |
ilm |
24 |
|
|
|
25 |
import org.jdom.Document;
|
180 |
ilm |
26 |
import org.jdom.Element;
|
17 |
ilm |
27 |
import org.jdom.JDOMException;
|
|
|
28 |
import org.jdom.Namespace;
|
|
|
29 |
import org.jdom.input.SAXBuilder;
|
|
|
30 |
import org.jdom.xpath.XPath;
|
|
|
31 |
import org.xml.sax.EntityResolver;
|
|
|
32 |
import org.xml.sax.InputSource;
|
|
|
33 |
|
|
|
34 |
public class OOUtils {
|
|
|
35 |
|
|
|
36 |
// MAYBE configurable
|
|
|
37 |
static private final String[] executables = { "ooffice2", "ooffice", "soffice" };
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Open the passed file in OpenOffice.
|
|
|
41 |
*
|
|
|
42 |
* @param f the file to open.
|
|
|
43 |
* @throws IOException if openoffice could not be opened
|
|
|
44 |
*/
|
|
|
45 |
static public void open(File f) throws IOException {
|
|
|
46 |
FileUtils.open(f, executables);
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Return a builder who doesn't load DTD.
|
|
|
51 |
*
|
|
|
52 |
* @return a builder who doesn't load DTD.
|
|
|
53 |
*/
|
|
|
54 |
static public SAXBuilder getBuilder() {
|
|
|
55 |
SAXBuilder builder = new SAXBuilder();
|
|
|
56 |
builder.setEntityResolver(new EntityResolver() {
|
|
|
57 |
public InputSource resolveEntity(String publicId, String systemId) {
|
|
|
58 |
// les dtd sont "vidées"
|
|
|
59 |
if (systemId.endsWith(".dtd")) {
|
|
|
60 |
InputSource in = new InputSource();
|
|
|
61 |
in.setCharacterStream(new StringReader(""));
|
|
|
62 |
return in;
|
|
|
63 |
} else
|
|
|
64 |
return null;
|
|
|
65 |
}
|
|
|
66 |
});
|
|
|
67 |
return builder;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* Return a builder who loads DTD.
|
|
|
72 |
*
|
|
|
73 |
* @return a builder who loads DTD.
|
|
|
74 |
*/
|
|
|
75 |
static public SAXBuilder getBuilderLoadDTD() {
|
|
|
76 |
SAXBuilder builder = new SAXBuilder() {
|
|
|
77 |
public Document build(InputSource in) throws JDOMException, IOException {
|
|
|
78 |
in.setSystemId(OOUtils.class.getResource("oofficeDTDs/").toExternalForm());
|
|
|
79 |
return super.build(in);
|
|
|
80 |
}
|
|
|
81 |
};
|
|
|
82 |
return builder;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
static public Document parseDocument(String doc) throws JDOMException {
|
|
|
86 |
return JDOMUtils.parseStringDocument(doc, getBuilder());
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* Create an XPath with OO namespaces.
|
|
|
91 |
*
|
|
|
92 |
* @param path the xpath to create.
|
|
|
93 |
* @param version XML version.
|
|
|
94 |
* @return the specified XPath.
|
|
|
95 |
* @throws JDOMException if the path is malformed.
|
|
|
96 |
*/
|
|
|
97 |
public static final XPath getXPath(String path, XMLVersion version) throws JDOMException {
|
|
|
98 |
final XPath xp = XPath.newInstance(path);
|
|
|
99 |
for (final Namespace ns : version.getALL()) {
|
|
|
100 |
xp.addNamespace(ns);
|
|
|
101 |
}
|
|
|
102 |
return xp;
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
/**
|
|
|
106 |
* Encode to the color data type, see section 18.3.8 of OpenDocument-v1.2-part1.
|
|
|
107 |
*
|
|
|
108 |
* @param color a color
|
|
|
109 |
* @return the string encoded version.
|
|
|
110 |
*/
|
|
|
111 |
public static String encodeRGB(Color color) {
|
|
|
112 |
return "#" + Integer.toHexString(color.getRGB()).substring(2).toUpperCase();
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
/**
|
|
|
116 |
* Decode a color.
|
|
|
117 |
*
|
|
|
118 |
* @param color a RGB color in notation "#rrggbb", can be <code>null</code>.
|
|
|
119 |
* @return the corresponding color.
|
|
|
120 |
* @see #encodeRGB(Color)
|
|
|
121 |
*/
|
|
|
122 |
public static Color decodeRGB(String color) {
|
|
|
123 |
return color == null ? null : Color.decode(color.trim());
|
|
|
124 |
}
|
180 |
ilm |
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 |
}
|
17 |
ilm |
154 |
}
|