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