001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.documentlibrary.util;
022:
023: import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
024: import com.artofsolving.jodconverter.DocumentConverter;
025: import com.artofsolving.jodconverter.DocumentFormat;
026: import com.artofsolving.jodconverter.DocumentFormatRegistry;
027: import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
028: import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
029: import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
030:
031: import com.liferay.portal.PortalException;
032: import com.liferay.portal.SystemException;
033: import com.liferay.portal.kernel.util.ArrayUtil;
034: import com.liferay.portal.kernel.util.StringMaker;
035: import com.liferay.portal.kernel.util.StringPool;
036: import com.liferay.portal.util.PrefsPropsUtil;
037: import com.liferay.portal.util.PropsUtil;
038: import com.liferay.portal.util.PropsValues;
039: import com.liferay.util.CollectionFactory;
040: import com.liferay.util.FileUtil;
041: import com.liferay.util.SystemProperties;
042:
043: import java.io.ByteArrayOutputStream;
044: import java.io.File;
045: import java.io.FileInputStream;
046: import java.io.IOException;
047: import java.io.InputStream;
048:
049: import java.util.ArrayList;
050: import java.util.List;
051: import java.util.Map;
052:
053: /**
054: * <a href="DocumentConversionUtil.java.html"><b><i>View Source</i></b></a>
055: *
056: * @author Bruno Farache
057: *
058: */
059: public class DocumentConversionUtil {
060:
061: public static InputStream convert(String id, InputStream is,
062: String sourceExtension, String targetExtension)
063: throws IOException, PortalException, SystemException {
064:
065: return _instance._convert(id, is, sourceExtension,
066: targetExtension);
067: }
068:
069: public static String[] getConversions(String extension) {
070: return _instance._getConversions(extension);
071: }
072:
073: private DocumentConversionUtil() {
074: _conversionsMap.put("svg", _DRAWING_CONVERSIONS);
075: _conversionsMap.put("swf", _DRAWING_CONVERSIONS);
076:
077: _conversionsMap.put("odp", _PRESENTATION_CONVERSIONS);
078: _conversionsMap.put("ppt", _PRESENTATION_CONVERSIONS);
079: _conversionsMap.put("sxi", _PRESENTATION_CONVERSIONS);
080:
081: _conversionsMap.put("csv", _SPREADSHEET_CONVERSIONS);
082: _conversionsMap.put("ods", _SPREADSHEET_CONVERSIONS);
083: _conversionsMap.put("sxc", _SPREADSHEET_CONVERSIONS);
084: _conversionsMap.put("tsv", _SPREADSHEET_CONVERSIONS);
085: _conversionsMap.put("xls", _SPREADSHEET_CONVERSIONS);
086:
087: _conversionsMap.put("doc", _TEXT_CONVERSIONS);
088: _conversionsMap.put("odt", _TEXT_CONVERSIONS);
089: _conversionsMap.put("rtf", _TEXT_CONVERSIONS);
090: _conversionsMap.put("sxw", _TEXT_CONVERSIONS);
091: _conversionsMap.put("txt", _TEXT_CONVERSIONS);
092: _conversionsMap.put("wpd", _TEXT_CONVERSIONS);
093: }
094:
095: private InputStream _convert(String id, InputStream is,
096: String sourceExtension, String targetExtension)
097: throws IOException, PortalException, SystemException {
098:
099: if (!PrefsPropsUtil.getBoolean(
100: PropsUtil.OPENOFFICE_SERVER_ENABLED,
101: PropsValues.OPENOFFICE_SERVER_ENABLED)) {
102:
103: return null;
104: }
105:
106: StringMaker sm = new StringMaker();
107:
108: sm.append(SystemProperties.get(SystemProperties.TMP_DIR));
109: sm.append("/liferay/document_conversion/");
110: sm.append(id);
111: sm.append(StringPool.PERIOD);
112: sm.append(targetExtension);
113:
114: String fileName = sm.toString();
115:
116: File file = new File(fileName);
117:
118: if (!file.exists()) {
119: DocumentFormatRegistry registry = new DefaultDocumentFormatRegistry();
120:
121: DocumentConverter converter = _getConverter(registry);
122:
123: DocumentFormat inputFormat = registry
124: .getFormatByFileExtension(sourceExtension);
125:
126: ByteArrayOutputStream baos = new ByteArrayOutputStream();
127:
128: DocumentFormat outputFormat = registry
129: .getFormatByFileExtension(targetExtension);
130:
131: converter.convert(is, inputFormat, baos, outputFormat);
132:
133: FileUtil.write(file, baos.toByteArray());
134: }
135:
136: return new FileInputStream(file);
137: }
138:
139: private String[] _getConversions(String extension) {
140: String[] conversions = (String[]) _conversionsMap
141: .get(extension);
142:
143: if (conversions == null) {
144: conversions = _DEFAULT_CONVERSIONS;
145: } else {
146: if (ArrayUtil.contains(conversions, extension)) {
147: List list = new ArrayList();
148:
149: for (int i = 0; i < conversions.length; i++) {
150: String conversion = conversions[i];
151:
152: if (!conversion.equals(extension)) {
153: list.add(conversion);
154: }
155: }
156:
157: conversions = (String[]) list.toArray(new String[0]);
158: }
159: }
160:
161: return conversions;
162: }
163:
164: private DocumentConverter _getConverter(
165: DocumentFormatRegistry registry) throws PortalException,
166: SystemException {
167:
168: if ((_connection == null) || (_converter == null)) {
169: String host = PrefsPropsUtil.getString(
170: PropsUtil.OPENOFFICE_SERVER_HOST,
171: PropsValues.OPENOFFICE_SERVER_HOST);
172: int port = PrefsPropsUtil.getInteger(
173: PropsUtil.OPENOFFICE_SERVER_PORT,
174: PropsValues.OPENOFFICE_SERVER_PORT);
175:
176: _connection = new SocketOpenOfficeConnection(host, port);
177: _converter = new StreamOpenOfficeDocumentConverter(
178: _connection);
179: }
180:
181: return _converter;
182: }
183:
184: private static final String[] _DEFAULT_CONVERSIONS = new String[0];
185:
186: private static final String[] _DRAWING_CONVERSIONS = new String[] { "odg" };
187:
188: private static final String[] _PRESENTATION_CONVERSIONS = new String[] {
189: "odp", "pdf", "ppt", "swf", "sxi" };
190:
191: private static final String[] _SPREADSHEET_CONVERSIONS = new String[] {
192: "csv", "ods", "pdf", "sxc", "tsv", "xls" };
193:
194: private static final String[] _TEXT_CONVERSIONS = new String[] {
195: "doc", "odt", "pdf", "rtf", "sxw", "txt" };
196:
197: private static DocumentConversionUtil _instance = new DocumentConversionUtil();
198:
199: private Map _conversionsMap = CollectionFactory.getHashMap();
200: private OpenOfficeConnection _connection;
201: private DocumentConverter _converter;
202:
203: }
|