1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.documentlibrary.util;
24  
25  import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
26  import com.artofsolving.jodconverter.DocumentConverter;
27  import com.artofsolving.jodconverter.DocumentFormat;
28  import com.artofsolving.jodconverter.DocumentFormatRegistry;
29  import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
30  import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
31  import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
32  
33  import com.liferay.portal.SystemException;
34  import com.liferay.portal.kernel.util.ArrayUtil;
35  import com.liferay.portal.kernel.util.FileUtil;
36  import com.liferay.portal.kernel.util.StringPool;
37  import com.liferay.portal.util.PrefsPropsUtil;
38  import com.liferay.portal.util.PropsKeys;
39  import com.liferay.portal.util.PropsValues;
40  import com.liferay.util.SystemProperties;
41  
42  import java.io.ByteArrayOutputStream;
43  import java.io.File;
44  import java.io.FileInputStream;
45  import java.io.IOException;
46  import java.io.InputStream;
47  
48  import java.util.ArrayList;
49  import java.util.HashMap;
50  import java.util.List;
51  import java.util.Map;
52  
53  /**
54   * <a href="DocumentConversionUtil.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Bruno Farache
57   *
58   */
59  public class DocumentConversionUtil {
60  
61      public static InputStream convert(
62              String id, InputStream is, String sourceExtension,
63              String targetExtension)
64          throws IOException, SystemException {
65  
66          return _instance._convert(id, is, sourceExtension, targetExtension);
67      }
68  
69      public static void disconnect() {
70          _instance._disconnect();
71      }
72  
73      public static String[] getConversions(String extension) {
74          return _instance._getConversions(extension);
75      }
76  
77      public static String getTempFileId(long id, double version) {
78          StringBuilder sb = new StringBuilder();
79  
80          sb.append(id);
81          sb.append(StringPool.PERIOD);
82          sb.append(version);
83  
84          return sb.toString();
85      }
86  
87      private DocumentConversionUtil() {
88          _conversionsMap.put("svg", _DRAWING_CONVERSIONS);
89          _conversionsMap.put("swf", _DRAWING_CONVERSIONS);
90  
91          _conversionsMap.put("odp", _PRESENTATION_CONVERSIONS);
92          _conversionsMap.put("ppt", _PRESENTATION_CONVERSIONS);
93          _conversionsMap.put("sxi", _PRESENTATION_CONVERSIONS);
94  
95          _conversionsMap.put("csv", _SPREADSHEET_CONVERSIONS);
96          _conversionsMap.put("ods", _SPREADSHEET_CONVERSIONS);
97          _conversionsMap.put("sxc", _SPREADSHEET_CONVERSIONS);
98          _conversionsMap.put("tsv", _SPREADSHEET_CONVERSIONS);
99          _conversionsMap.put("xls", _SPREADSHEET_CONVERSIONS);
100 
101         _conversionsMap.put("doc", _TEXT_CONVERSIONS);
102         _conversionsMap.put("htm", _TEXT_CONVERSIONS);
103         _conversionsMap.put("html", _TEXT_CONVERSIONS);
104         _conversionsMap.put("odt", _TEXT_CONVERSIONS);
105         _conversionsMap.put("rtf", _TEXT_CONVERSIONS);
106         _conversionsMap.put("sxw", _TEXT_CONVERSIONS);
107         _conversionsMap.put("txt", _TEXT_CONVERSIONS);
108         _conversionsMap.put("wpd", _TEXT_CONVERSIONS);
109     }
110 
111     private InputStream _convert(
112             String id, InputStream is, String sourceExtension,
113             String targetExtension)
114         throws IOException, SystemException {
115 
116         if (!PrefsPropsUtil.getBoolean(
117                 PropsKeys.OPENOFFICE_SERVER_ENABLED,
118                 PropsValues.OPENOFFICE_SERVER_ENABLED)) {
119 
120             return null;
121         }
122 
123         StringBuilder sb = new StringBuilder();
124 
125         sb.append(SystemProperties.get(SystemProperties.TMP_DIR));
126         sb.append("/liferay/document_conversion/");
127         sb.append(id);
128         sb.append(StringPool.PERIOD);
129         sb.append(targetExtension);
130 
131         String fileName = sb.toString();
132 
133         File file = new File(fileName);
134 
135         if (!file.exists()) {
136             DocumentFormatRegistry registry =
137                 new DefaultDocumentFormatRegistry();
138 
139             DocumentConverter converter = _getConverter(registry);
140 
141             if (sourceExtension.equals("htm")) {
142                 sourceExtension = "html";
143             }
144 
145             DocumentFormat inputFormat = registry.getFormatByFileExtension(
146                 sourceExtension);
147 
148             ByteArrayOutputStream baos = new ByteArrayOutputStream();
149 
150             DocumentFormat outputFormat = registry.getFormatByFileExtension(
151                 targetExtension);
152 
153             converter.convert(is, inputFormat, baos, outputFormat);
154 
155             FileUtil.write(file, baos.toByteArray());
156         }
157 
158         return new FileInputStream(file);
159     }
160 
161     private void _disconnect() {
162         if (_connection != null) {
163             _connection.disconnect();
164         }
165     }
166 
167     private String[] _getConversions(String extension) {
168         String[] conversions = _conversionsMap.get(extension);
169 
170         if (conversions == null) {
171             conversions = _DEFAULT_CONVERSIONS;
172         }
173         else {
174             if (ArrayUtil.contains(conversions, extension)) {
175                 List<String> list = new ArrayList<String>();
176 
177                 for (int i = 0; i < conversions.length; i++) {
178                     String conversion = conversions[i];
179 
180                     if (!conversion.equals(extension)) {
181                         list.add(conversion);
182                     }
183                 }
184 
185                 conversions = list.toArray(new String[list.size()]);
186             }
187         }
188 
189         return conversions;
190     }
191 
192     private DocumentConverter _getConverter(DocumentFormatRegistry registry)
193         throws SystemException {
194 
195         if ((_connection == null) || (_converter == null)) {
196             int port = PrefsPropsUtil.getInteger(
197                 PropsKeys.OPENOFFICE_SERVER_PORT,
198                 PropsValues.OPENOFFICE_SERVER_PORT);
199 
200             _connection = new SocketOpenOfficeConnection(port);
201             _converter = new OpenOfficeDocumentConverter(_connection);
202         }
203 
204         return _converter;
205     }
206 
207     private static final String[] _DEFAULT_CONVERSIONS = new String[0];
208 
209     private static final String[] _DRAWING_CONVERSIONS = new String[] {"odg"};
210 
211     private static final String[] _PRESENTATION_CONVERSIONS = new String[] {
212         "odp", "pdf", "ppt", "swf", "sxi"
213     };
214 
215     private static final String[] _SPREADSHEET_CONVERSIONS = new String[] {
216         "csv", "ods", "pdf", "sxc", "tsv", "xls"
217     };
218 
219     private static final String[] _TEXT_CONVERSIONS = new String[] {
220         "doc", "odt", "pdf", "rtf", "sxw", "txt"
221     };
222 
223     private static DocumentConversionUtil _instance =
224         new DocumentConversionUtil();
225 
226     private Map<String, String[]> _conversionsMap =
227         new HashMap<String, String[]>();
228     private OpenOfficeConnection _connection;
229     private DocumentConverter _converter;
230 
231 }