Dépôt officiel du code source de l'ERP OpenConcerto
Rev 21 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
*
* The contents of this file are subject to the terms of the GNU General Public License Version 3
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each file.
*/
package org.openconcerto.openoffice;
import org.openconcerto.utils.FileUtils;
import org.openconcerto.xml.JDOMUtils;
import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.Locale;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
public class OOUtils {
// MAYBE configurable
static private final String[] executables = { "ooffice2", "ooffice", "soffice" };
/**
* Open the passed file in OpenOffice.
*
* @param f the file to open.
* @throws IOException if openoffice could not be opened
*/
static public void open(File f) throws IOException {
FileUtils.open(f, executables);
}
/**
* Return a builder who doesn't load DTD.
*
* @return a builder who doesn't load DTD.
*/
static public SAXBuilder getBuilder() {
SAXBuilder builder = new SAXBuilder();
builder.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) {
// les dtd sont "vidées"
if (systemId.endsWith(".dtd")) {
InputSource in = new InputSource();
in.setCharacterStream(new StringReader(""));
return in;
} else
return null;
}
});
return builder;
}
/**
* Return a builder who loads DTD.
*
* @return a builder who loads DTD.
*/
static public SAXBuilder getBuilderLoadDTD() {
SAXBuilder builder = new SAXBuilder() {
public Document build(InputSource in) throws JDOMException, IOException {
in.setSystemId(OOUtils.class.getResource("oofficeDTDs/").toExternalForm());
return super.build(in);
}
};
return builder;
}
static public Document parseDocument(String doc) throws JDOMException {
return JDOMUtils.parseStringDocument(doc, getBuilder());
}
/**
* Create an XPath with OO namespaces.
*
* @param path the xpath to create.
* @param version XML version.
* @return the specified XPath.
* @throws JDOMException if the path is malformed.
*/
public static final XPath getXPath(String path, XMLVersion version) throws JDOMException {
final XPath xp = XPath.newInstance(path);
for (final Namespace ns : version.getALL()) {
xp.addNamespace(ns);
}
return xp;
}
/**
* Encode to the color data type, see section 18.3.8 of OpenDocument-v1.2-part1.
*
* @param color a color
* @return the string encoded version.
*/
public static String encodeRGB(Color color) {
return "#" + Integer.toHexString(color.getRGB()).substring(2).toUpperCase();
}
/**
* Decode a color.
*
* @param color a RGB color in notation "#rrggbb", can be <code>null</code>.
* @return the corresponding color.
* @see #encodeRGB(Color)
*/
public static Color decodeRGB(String color) {
return color == null ? null : Color.decode(color.trim());
}
public static final Locale getElementLocale(final Element elem) {
return getElementLocale(elem, elem.getNamespace());
}
public static final Locale getElementLocale(final Element elem, final Namespace ns) {
final Locale res;
final String country = elem.getAttributeValue("country", ns);
final String lang = elem.getAttributeValue("language", ns);
if (lang != null) {
res = new Locale.Builder().setLanguage(lang).setRegion(country).build();
} else {
res = null;
}
return res;
}
public static final void setElementLocale(final Element elem, final Namespace ns, final Locale l) {
if (l == null)
elem.removeAttribute("country", ns);
else
elem.setAttribute("country", l.getCountry(), ns);
final String lang = l == null ? null : l.getLanguage();
if (lang == null || lang.isEmpty())
elem.removeAttribute("language", ns);
else
elem.setAttribute("language", lang, ns);
}
}