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