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