1
14
15 package com.liferay.portlet.documentlibrary.util;
16
17 import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
18 import com.artofsolving.jodconverter.DocumentConverter;
19 import com.artofsolving.jodconverter.DocumentFormat;
20 import com.artofsolving.jodconverter.DocumentFormatRegistry;
21 import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
22 import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
23 import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
24 import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
25
26 import com.liferay.portal.SystemException;
27 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
28 import com.liferay.portal.kernel.util.ArrayUtil;
29 import com.liferay.portal.kernel.util.FileUtil;
30 import com.liferay.portal.kernel.util.PropsKeys;
31 import com.liferay.portal.kernel.util.StringBundler;
32 import com.liferay.portal.kernel.util.StringPool;
33 import com.liferay.portal.kernel.util.Validator;
34 import com.liferay.portal.util.PrefsPropsUtil;
35 import com.liferay.portal.util.PropsValues;
36 import com.liferay.util.SystemProperties;
37
38 import java.io.File;
39 import java.io.FileInputStream;
40 import java.io.IOException;
41 import java.io.InputStream;
42
43 import java.util.ArrayList;
44 import java.util.HashMap;
45 import java.util.List;
46 import java.util.Map;
47
48
53 public class DocumentConversionUtil {
54
55 public static InputStream convert(
56 String id, InputStream is, String sourceExtension,
57 String targetExtension)
58 throws IOException, SystemException {
59
60 return _instance._convert(id, is, sourceExtension, targetExtension);
61 }
62
63 public static void disconnect() {
64 _instance._disconnect();
65 }
66
67 public static String[] getConversions(String extension) {
68 return _instance._getConversions(extension);
69 }
70
71 public static String getTempFileId(long id, double version) {
72 return String.valueOf(id).concat(StringPool.PERIOD).concat(
73 String.valueOf(version));
74 }
75
76 private DocumentConversionUtil() {
77 _conversionsMap.put("svg", _DRAWING_CONVERSIONS);
78 _conversionsMap.put("swf", _DRAWING_CONVERSIONS);
79
80 _conversionsMap.put("odp", _PRESENTATION_CONVERSIONS);
81 _conversionsMap.put("ppt", _PRESENTATION_CONVERSIONS);
82 _conversionsMap.put("pptx", _PRESENTATION_CONVERSIONS);
83 _conversionsMap.put("sxi", _PRESENTATION_CONVERSIONS);
84
85 _conversionsMap.put("csv", _SPREADSHEET_CONVERSIONS);
86 _conversionsMap.put("ods", _SPREADSHEET_CONVERSIONS);
87 _conversionsMap.put("sxc", _SPREADSHEET_CONVERSIONS);
88 _conversionsMap.put("tsv", _SPREADSHEET_CONVERSIONS);
89 _conversionsMap.put("xls", _SPREADSHEET_CONVERSIONS);
90 _conversionsMap.put("xlsx", _SPREADSHEET_CONVERSIONS);
91
92 _conversionsMap.put("doc", _TEXT_CONVERSIONS);
93 _conversionsMap.put("docx", _TEXT_CONVERSIONS);
94 _conversionsMap.put("htm", _TEXT_CONVERSIONS);
95 _conversionsMap.put("html", _TEXT_CONVERSIONS);
96 _conversionsMap.put("odt", _TEXT_CONVERSIONS);
97 _conversionsMap.put("rtf", _TEXT_CONVERSIONS);
98 _conversionsMap.put("sxw", _TEXT_CONVERSIONS);
99 _conversionsMap.put("txt", _TEXT_CONVERSIONS);
100 _conversionsMap.put("wpd", _TEXT_CONVERSIONS);
101 }
102
103 private InputStream _convert(
104 String id, InputStream is, String sourceExtension,
105 String targetExtension)
106 throws IOException, SystemException {
107
108 if (!PrefsPropsUtil.getBoolean(
109 PropsKeys.OPENOFFICE_SERVER_ENABLED,
110 PropsValues.OPENOFFICE_SERVER_ENABLED)) {
111
112 return null;
113 }
114
115 StringBundler sb = new StringBundler(5);
116
117 sb.append(SystemProperties.get(SystemProperties.TMP_DIR));
118 sb.append("/liferay/document_conversion/");
119 sb.append(id);
120 sb.append(StringPool.PERIOD);
121 sb.append(targetExtension);
122
123 String fileName = sb.toString();
124
125 File file = new File(fileName);
126
127 if (!PropsValues.OPENOFFICE_CACHE_ENABLED || !file.exists()) {
128 DocumentFormatRegistry registry =
129 new DefaultDocumentFormatRegistry();
130
131 DocumentConverter converter = _getConverter(registry);
132
133 if (sourceExtension.equals("htm")) {
134 sourceExtension = "html";
135 }
136
137 DocumentFormat inputFormat = registry.getFormatByFileExtension(
138 sourceExtension);
139
140 UnsyncByteArrayOutputStream ubaos =
141 new UnsyncByteArrayOutputStream();
142
143 DocumentFormat outputFormat = registry.getFormatByFileExtension(
144 targetExtension);
145
146 converter.convert(is, inputFormat, ubaos, outputFormat);
147
148 FileUtil.write(file, ubaos.unsafeGetByteArray(), 0, ubaos.size());
149 }
150
151 return new FileInputStream(file);
152 }
153
154 private void _disconnect() {
155 if (_connection != null) {
156 _connection.disconnect();
157 }
158 }
159
160 private String[] _getConversions(String extension) {
161 String[] conversions = _conversionsMap.get(extension);
162
163 if (conversions == null) {
164 conversions = _DEFAULT_CONVERSIONS;
165 }
166 else {
167 if (ArrayUtil.contains(conversions, extension)) {
168 List<String> list = new ArrayList<String>();
169
170 for (int i = 0; i < conversions.length; i++) {
171 String conversion = conversions[i];
172
173 if (!conversion.equals(extension)) {
174 list.add(conversion);
175 }
176 }
177
178 conversions = list.toArray(new String[list.size()]);
179 }
180 }
181
182 return conversions;
183 }
184
185 private DocumentConverter _getConverter(DocumentFormatRegistry registry)
186 throws SystemException {
187
188 if ((_connection == null) || (_converter == null)) {
189 String host = PrefsPropsUtil.getString(
190 PropsKeys.OPENOFFICE_SERVER_HOST);
191 int port = PrefsPropsUtil.getInteger(
192 PropsKeys.OPENOFFICE_SERVER_PORT,
193 PropsValues.OPENOFFICE_SERVER_PORT);
194
195 if (_isRemoteOpenOfficeHost(host)) {
196 _connection = new SocketOpenOfficeConnection(host, port);
197 _converter = new StreamOpenOfficeDocumentConverter(_connection);
198 }
199 else {
200 _connection = new SocketOpenOfficeConnection(port);
201 _converter = new OpenOfficeDocumentConverter(_connection);
202 }
203 }
204
205 return _converter;
206 }
207
208 private boolean _isRemoteOpenOfficeHost(String host) {
209 if (Validator.isNotNull(host) && !host.equals(_LOCALHOST_IP) &&
210 !host.startsWith(_LOCALHOST)) {
211
212 return true;
213 }
214 else {
215 return false;
216 }
217 }
218
219 private static final String[] _DEFAULT_CONVERSIONS = new String[0];
220
221 private static final String[] _DRAWING_CONVERSIONS = new String[] {"odg"};
222
223 private static final String _LOCALHOST = "localhost";
224
225 private static final String _LOCALHOST_IP = "127.0.0.1";
226
227 private static final String[] _PRESENTATION_CONVERSIONS = new String[] {
228 "odp", "pdf", "ppt", "swf", "sxi"
229 };
230
231 private static final String[] _SPREADSHEET_CONVERSIONS = new String[] {
232 "csv", "ods", "pdf", "sxc", "tsv", "xls"
233 };
234
235 private static final String[] _TEXT_CONVERSIONS = new String[] {
236 "doc", "odt", "pdf", "rtf", "sxw", "txt"
237 };
238
239 private static DocumentConversionUtil _instance =
240 new DocumentConversionUtil();
241
242 private Map<String, String[]> _conversionsMap =
243 new HashMap<String, String[]>();
244 private OpenOfficeConnection _connection;
245 private DocumentConverter _converter;
246
247 }