1
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 import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
33
34 import com.liferay.portal.SystemException;
35 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
36 import com.liferay.portal.kernel.util.ArrayUtil;
37 import com.liferay.portal.kernel.util.FileUtil;
38 import com.liferay.portal.kernel.util.PropsKeys;
39 import com.liferay.portal.kernel.util.StringPool;
40 import com.liferay.portal.kernel.util.Validator;
41 import com.liferay.portal.util.PrefsPropsUtil;
42 import com.liferay.portal.util.PropsValues;
43 import com.liferay.util.SystemProperties;
44
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
60 public class DocumentConversionUtil {
61
62 public static InputStream convert(
63 String id, InputStream is, String sourceExtension,
64 String targetExtension)
65 throws IOException, 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 id, double version) {
79 StringBuilder sb = new StringBuilder();
80
81 sb.append(id);
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, 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 (!PropsValues.OPENOFFICE_CACHE_ENABLED || !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 UnsyncByteArrayOutputStream ubaos =
150 new UnsyncByteArrayOutputStream();
151
152 DocumentFormat outputFormat = registry.getFormatByFileExtension(
153 targetExtension);
154
155 converter.convert(is, inputFormat, ubaos, outputFormat);
156
157 FileUtil.write(file, ubaos.toByteArray());
158 }
159
160 return new FileInputStream(file);
161 }
162
163 private void _disconnect() {
164 if (_connection != null) {
165 _connection.disconnect();
166 }
167 }
168
169 private String[] _getConversions(String extension) {
170 String[] conversions = _conversionsMap.get(extension);
171
172 if (conversions == null) {
173 conversions = _DEFAULT_CONVERSIONS;
174 }
175 else {
176 if (ArrayUtil.contains(conversions, extension)) {
177 List<String> list = new ArrayList<String>();
178
179 for (int i = 0; i < conversions.length; i++) {
180 String conversion = conversions[i];
181
182 if (!conversion.equals(extension)) {
183 list.add(conversion);
184 }
185 }
186
187 conversions = list.toArray(new String[list.size()]);
188 }
189 }
190
191 return conversions;
192 }
193
194 private DocumentConverter _getConverter(DocumentFormatRegistry registry)
195 throws SystemException {
196
197 if ((_connection == null) || (_converter == null)) {
198 String host = PrefsPropsUtil.getString(
199 PropsKeys.OPENOFFICE_SERVER_HOST);
200 int port = PrefsPropsUtil.getInteger(
201 PropsKeys.OPENOFFICE_SERVER_PORT,
202 PropsValues.OPENOFFICE_SERVER_PORT);
203
204 if (_isRemoteOpenOfficeHost(host)) {
205 _connection = new SocketOpenOfficeConnection(host, port);
206 _converter = new StreamOpenOfficeDocumentConverter(_connection);
207 }
208 else {
209 _connection = new SocketOpenOfficeConnection(port);
210 _converter = new OpenOfficeDocumentConverter(_connection);
211 }
212 }
213
214 return _converter;
215 }
216
217 private boolean _isRemoteOpenOfficeHost(String host) {
218 if (Validator.isNotNull(host) && !host.equals(_LOCALHOST_IP) &&
219 !host.startsWith(_LOCALHOST)) {
220
221 return true;
222 }
223 else {
224 return false;
225 }
226 }
227
228 private static final String[] _DEFAULT_CONVERSIONS = new String[0];
229
230 private static final String[] _DRAWING_CONVERSIONS = new String[] {"odg"};
231
232 private static final String _LOCALHOST = "localhost";
233
234 private static final String _LOCALHOST_IP = "127.0.0.1";
235
236 private static final String[] _PRESENTATION_CONVERSIONS = new String[] {
237 "odp", "pdf", "ppt", "swf", "sxi"
238 };
239
240 private static final String[] _SPREADSHEET_CONVERSIONS = new String[] {
241 "csv", "ods", "pdf", "sxc", "tsv", "xls"
242 };
243
244 private static final String[] _TEXT_CONVERSIONS = new String[] {
245 "doc", "odt", "pdf", "rtf", "sxw", "txt"
246 };
247
248 private static DocumentConversionUtil _instance =
249 new DocumentConversionUtil();
250
251 private Map<String, String[]> _conversionsMap =
252 new HashMap<String, String[]>();
253 private OpenOfficeConnection _connection;
254 private DocumentConverter _converter;
255
256 }