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