1   /**
2    * Copyright (c) 2000-2008 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.PortalException;
34  import com.liferay.portal.SystemException;
35  import com.liferay.portal.kernel.util.ArrayUtil;
36  import com.liferay.portal.kernel.util.FileUtil;
37  import com.liferay.portal.kernel.util.StringPool;
38  import com.liferay.portal.util.PrefsPropsUtil;
39  import com.liferay.portal.util.PropsKeys;
40  import com.liferay.portal.util.PropsValues;
41  import com.liferay.util.SystemProperties;
42  
43  import java.io.ByteArrayOutputStream;
44  import java.io.File;
45  import java.io.FileInputStream;
46  import java.io.IOException;
47  import java.io.InputStream;
48  
49  import java.util.ArrayList;
50  import java.util.HashMap;
51  import java.util.List;
52  import java.util.Map;
53  
54  /**
55   * <a href="DocumentConversionUtil.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Bruno Farache
58   *
59   */
60  public class DocumentConversionUtil {
61  
62      public static InputStream convert(
63              String id, InputStream is, String sourceExtension,
64              String targetExtension)
65          throws IOException, PortalException, SystemException {
66  
67          return _instance._convert(id, is, sourceExtension, targetExtension);
68      }
69  
70      public static void disconnect() {
71          _instance._disconnect();
72      }
73  
74      public static String[] getConversions(String extension) {
75          return _instance._getConversions(extension);
76      }
77  
78      public static String getTempFileId(long fileEntryId, double version) {
79          StringBuilder sb = new StringBuilder();
80  
81          sb.append(fileEntryId);
82          sb.append(StringPool.PERIOD);
83          sb.append(version);
84  
85          return sb.toString();
86      }
87  
88      private DocumentConversionUtil() {
89          _conversionsMap.put("svg", _DRAWING_CONVERSIONS);
90          _conversionsMap.put("swf", _DRAWING_CONVERSIONS);
91  
92          _conversionsMap.put("odp", _PRESENTATION_CONVERSIONS);
93          _conversionsMap.put("ppt", _PRESENTATION_CONVERSIONS);
94          _conversionsMap.put("sxi", _PRESENTATION_CONVERSIONS);
95  
96          _conversionsMap.put("csv", _SPREADSHEET_CONVERSIONS);
97          _conversionsMap.put("ods", _SPREADSHEET_CONVERSIONS);
98          _conversionsMap.put("sxc", _SPREADSHEET_CONVERSIONS);
99          _conversionsMap.put("tsv", _SPREADSHEET_CONVERSIONS);
100         _conversionsMap.put("xls", _SPREADSHEET_CONVERSIONS);
101 
102         _conversionsMap.put("doc", _TEXT_CONVERSIONS);
103         _conversionsMap.put("htm", _TEXT_CONVERSIONS);
104         _conversionsMap.put("html", _TEXT_CONVERSIONS);
105         _conversionsMap.put("odt", _TEXT_CONVERSIONS);
106         _conversionsMap.put("rtf", _TEXT_CONVERSIONS);
107         _conversionsMap.put("sxw", _TEXT_CONVERSIONS);
108         _conversionsMap.put("txt", _TEXT_CONVERSIONS);
109         _conversionsMap.put("wpd", _TEXT_CONVERSIONS);
110     }
111 
112     private InputStream _convert(
113             String id, InputStream is, String sourceExtension,
114             String targetExtension)
115         throws IOException, PortalException, SystemException {
116 
117         if (!PrefsPropsUtil.getBoolean(
118                 PropsKeys.OPENOFFICE_SERVER_ENABLED,
119                 PropsValues.OPENOFFICE_SERVER_ENABLED)) {
120 
121             return null;
122         }
123 
124         StringBuilder sb = new StringBuilder();
125 
126         sb.append(SystemProperties.get(SystemProperties.TMP_DIR));
127         sb.append("/liferay/document_conversion/");
128         sb.append(id);
129         sb.append(StringPool.PERIOD);
130         sb.append(targetExtension);
131 
132         String fileName = sb.toString();
133 
134         File file = new File(fileName);
135 
136         if (!file.exists()) {
137             DocumentFormatRegistry registry =
138                 new DefaultDocumentFormatRegistry();
139 
140             DocumentConverter converter = _getConverter(registry);
141 
142             if (sourceExtension.equals("htm")) {
143                 sourceExtension = "html";
144             }
145 
146             DocumentFormat inputFormat = registry.getFormatByFileExtension(
147                 sourceExtension);
148 
149             ByteArrayOutputStream baos = new ByteArrayOutputStream();
150 
151             DocumentFormat outputFormat = registry.getFormatByFileExtension(
152                 targetExtension);
153 
154             converter.convert(is, inputFormat, baos, outputFormat);
155 
156             FileUtil.write(file, baos.toByteArray());
157         }
158 
159         return new FileInputStream(file);
160     }
161 
162     private void _disconnect() {
163         if (_connection != null) {
164             _connection.disconnect();
165         }
166     }
167 
168     private String[] _getConversions(String extension) {
169         String[] conversions = _conversionsMap.get(extension);
170 
171         if (conversions == null) {
172             conversions = _DEFAULT_CONVERSIONS;
173         }
174         else {
175             if (ArrayUtil.contains(conversions, extension)) {
176                 List<String> list = new ArrayList<String>();
177 
178                 for (int i = 0; i < conversions.length; i++) {
179                     String conversion = conversions[i];
180 
181                     if (!conversion.equals(extension)) {
182                         list.add(conversion);
183                     }
184                 }
185 
186                 conversions = list.toArray(new String[list.size()]);
187             }
188         }
189 
190         return conversions;
191     }
192 
193     private DocumentConverter _getConverter(DocumentFormatRegistry registry)
194         throws PortalException, SystemException {
195 
196         if ((_connection == null) || (_converter == null)) {
197             int port = PrefsPropsUtil.getInteger(
198                 PropsKeys.OPENOFFICE_SERVER_PORT,
199                 PropsValues.OPENOFFICE_SERVER_PORT);
200 
201             _connection = new SocketOpenOfficeConnection(port);
202             _converter = new OpenOfficeDocumentConverter(_connection);
203         }
204 
205         return _converter;
206     }
207 
208     private static final String[] _DEFAULT_CONVERSIONS = new String[0];
209 
210     private static final String[] _DRAWING_CONVERSIONS = new String[] {"odg"};
211 
212     private static final String[] _PRESENTATION_CONVERSIONS = new String[] {
213         "odp", "pdf", "ppt", "swf", "sxi"
214     };
215 
216     private static final String[] _SPREADSHEET_CONVERSIONS = new String[] {
217         "csv", "ods", "pdf", "sxc", "tsv", "xls"
218     };
219 
220     private static final String[] _TEXT_CONVERSIONS = new String[] {
221         "doc", "odt", "pdf", "rtf", "sxw", "txt"
222     };
223 
224     private static DocumentConversionUtil _instance =
225         new DocumentConversionUtil();
226 
227     private Map<String, String[]> _conversionsMap =
228         new HashMap<String, String[]>();
229     private OpenOfficeConnection _connection;
230     private DocumentConverter _converter;
231 
232 }