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