1 17 package org.alfresco.repo.content.transform; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.net.ConnectException ; 22 import java.util.HashMap ; 23 import java.util.Map ; 24 25 import net.sf.joott.uno.DocumentConverter; 26 import net.sf.joott.uno.DocumentFormat; 27 import net.sf.joott.uno.UnoConnection; 28 29 import org.alfresco.repo.content.MimetypeMap; 30 import org.alfresco.service.cmr.repository.ContentIOException; 31 import org.alfresco.service.cmr.repository.ContentReader; 32 import org.alfresco.service.cmr.repository.ContentWriter; 33 import org.alfresco.util.TempFileProvider; 34 35 46 public class UnoContentTransformer extends AbstractContentTransformer 47 { 48 49 private static Map <ContentTransformerRegistry.TransformationKey, DocumentFormatWrapper> formatsByConversion; 50 51 static 52 { 53 formatsByConversion = new HashMap <ContentTransformerRegistry.TransformationKey, DocumentFormatWrapper>(17); 55 56 formatsByConversion.put( 58 new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_OPENDOCUMENT_TEXT, MimetypeMap.MIMETYPE_TEXT_PLAIN), 59 new DocumentFormatWrapper(DocumentFormat.TEXT, 1.0)); 60 formatsByConversion.put( 61 new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_OPENDOCUMENT_TEXT, MimetypeMap.MIMETYPE_PDF), 62 new DocumentFormatWrapper(DocumentFormat.PDF_WRITER, 1.0)); 63 formatsByConversion.put( 64 new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_OPENDOCUMENT_SPREADSHEET, MimetypeMap.MIMETYPE_PDF), 65 new DocumentFormatWrapper(DocumentFormat.PDF_CALC, 1.0)); 66 formatsByConversion.put( 67 new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_OPENDOCUMENT_PRESENTATION, MimetypeMap.MIMETYPE_PDF), 68 new DocumentFormatWrapper(DocumentFormat.PDF_WRITER, 1.0)); 69 formatsByConversion.put( 71 new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_OPENOFFICE1_WRITER, MimetypeMap.MIMETYPE_TEXT_PLAIN), 72 new DocumentFormatWrapper(DocumentFormat.TEXT, 1.0)); 73 formatsByConversion.put( 74 new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_OPENOFFICE1_WRITER, MimetypeMap.MIMETYPE_PDF), 75 new DocumentFormatWrapper(DocumentFormat.PDF_WRITER, 1.0)); 76 formatsByConversion.put( 77 new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_OPENOFFICE1_CALC, MimetypeMap.MIMETYPE_PDF), 78 new DocumentFormatWrapper(DocumentFormat.PDF_WRITER, 1.0)); 79 formatsByConversion.put( 80 new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_OPENOFFICE1_DRAW, MimetypeMap.MIMETYPE_PDF), 81 new DocumentFormatWrapper(DocumentFormat.PDF_IMPRESS, 1.0)); 82 formatsByConversion.put( 83 new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_OPENOFFICE1_IMPRESS, MimetypeMap.MIMETYPE_PDF), 84 new DocumentFormatWrapper(DocumentFormat.PDF_IMPRESS, 1.0)); 85 formatsByConversion.put( 87 new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_STAROFFICE5_DRAW, MimetypeMap.MIMETYPE_PDF), 88 new DocumentFormatWrapper(DocumentFormat.PDF_IMPRESS, 1.0)); 89 formatsByConversion.put( 90 new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_STAROFFICE5_CALC, MimetypeMap.MIMETYPE_PDF), 91 new DocumentFormatWrapper(DocumentFormat.PDF_CALC, 1.0)); 92 formatsByConversion.put( 93 new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_STAROFFICE5_CHART, MimetypeMap.MIMETYPE_PDF), 94 new DocumentFormatWrapper(DocumentFormat.PDF_WRITER, 1.0)); 95 formatsByConversion.put( 96 new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_STAROFFICE5_IMPRESS, MimetypeMap.MIMETYPE_PDF), 97 new DocumentFormatWrapper(DocumentFormat.PDF_WRITER, 1.0)); 98 formatsByConversion.put( 99 new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_STAROFFICE5_IMPRESS_PACKED, MimetypeMap.MIMETYPE_PDF), 100 new DocumentFormatWrapper(DocumentFormat.PDF_IMPRESS, 1.0)); 101 formatsByConversion.put( 102 new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_STAROFFICE5_WRITER, MimetypeMap.MIMETYPE_PDF), 103 new DocumentFormatWrapper(DocumentFormat.PDF_WRITER, 1.0)); 104 formatsByConversion.put( 105 new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_STAROFFICE5_WRITER_GLOBAL, MimetypeMap.MIMETYPE_PDF), 106 new DocumentFormatWrapper(DocumentFormat.PDF_WRITER, 1.0)); 107 formatsByConversion.put( 109 new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_WORD, MimetypeMap.MIMETYPE_TEXT_PLAIN), 110 new DocumentFormatWrapper(DocumentFormat.TEXT, 1.0)); 111 formatsByConversion.put( 112 new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_WORD, MimetypeMap.MIMETYPE_PDF), 113 new DocumentFormatWrapper(DocumentFormat.PDF_WRITER, 1.0)); 114 formatsByConversion.put( 115 new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_EXCEL, MimetypeMap.MIMETYPE_PDF), 116 new DocumentFormatWrapper(DocumentFormat.PDF_CALC, 1.0)); 117 formatsByConversion.put( 118 new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_WORD, MimetypeMap.MIMETYPE_HTML), 119 new DocumentFormatWrapper(DocumentFormat.HTML_WRITER, 1.0)); 120 formatsByConversion.put( 121 new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_PPT, MimetypeMap.MIMETYPE_FLASH), 122 new DocumentFormatWrapper(DocumentFormat.FLASH_IMPRESS, 1.0)); 123 formatsByConversion.put( 124 new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_PPT, MimetypeMap.MIMETYPE_PDF), 125 new DocumentFormatWrapper(DocumentFormat.PDF_IMPRESS, 1.0)); 126 formatsByConversion.put( 128 new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_TEXT_PLAIN, MimetypeMap.MIMETYPE_HTML), 129 new DocumentFormatWrapper(DocumentFormat.HTML_WRITER, 1.0)); 130 formatsByConversion.put( 131 new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_TEXT_PLAIN, MimetypeMap.MIMETYPE_PDF), 132 new DocumentFormatWrapper(DocumentFormat.PDF_WRITER, 1.0)); 133 formatsByConversion.put( 134 new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_TEXT_PLAIN, MimetypeMap.MIMETYPE_WORD), 135 new DocumentFormatWrapper(DocumentFormat.TEXT, 1.0)); 136 formatsByConversion.put( 137 new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_HTML, MimetypeMap.MIMETYPE_PDF), 138 new DocumentFormatWrapper(DocumentFormat.PDF_WRITER_WEB, 1.0)); 139 140 } 159 160 private String connectionUrl = UnoConnection.DEFAULT_CONNECTION_STRING; 161 private UnoConnection connection; 162 private boolean isConnected; 163 164 170 public UnoContentTransformer() 171 { 172 isConnected = false; 173 } 174 175 182 public void setConnectionUrl(String connectionUrl) 183 { 184 this.connectionUrl = connectionUrl; 185 } 186 187 191 public synchronized void init() 192 { 193 connection = new UnoConnection(connectionUrl); 194 try 196 { 197 connection.connect(); 198 isConnected = true; 199 super.register(); 201 } 202 catch (ConnectException e) 203 { 204 isConnected = false; 205 } 206 } 207 208 211 public boolean isConnected() 212 { 213 return isConnected; 214 } 215 216 221 private static DocumentFormatWrapper getDocumentFormatWrapper(String sourceMimetype, String targetMimetype) 222 { 223 ContentTransformerRegistry.TransformationKey key = 225 new ContentTransformerRegistry.TransformationKey(sourceMimetype, targetMimetype); 226 DocumentFormatWrapper wrapper = UnoContentTransformer.formatsByConversion.get(key); 227 return wrapper; 228 } 229 230 239 public double getReliability(String sourceMimetype, String targetMimetype) 240 { 241 if (!isConnected()) 243 { 244 return 0.0; 246 } 247 DocumentFormatWrapper docFormatWrapper = getDocumentFormatWrapper(sourceMimetype, targetMimetype); 249 if (docFormatWrapper == null) 250 { 251 return 0.0; 252 } 253 else 254 { 255 return docFormatWrapper.getReliability(); 256 } 257 } 258 259 public void transformInternal(ContentReader reader, ContentWriter writer, Map <String , Object > options) 260 throws Exception 261 { 262 String sourceMimetype = getMimetype(reader); 263 String targetMimetype = getMimetype(writer); 264 265 File tempFromFile = TempFileProvider.createTempFile( 267 "UnoContentTransformer", 268 "." + getMimetypeService().getExtension(sourceMimetype)); 269 File tempToFile = TempFileProvider.createTempFile( 270 "UnoContentTransformer", 271 "." + getMimetypeService().getExtension(targetMimetype)); 272 reader.getContent(tempFromFile); 274 275 DocumentFormatWrapper docFormatWrapper = getDocumentFormatWrapper(sourceMimetype, targetMimetype); 277 try 278 { 279 docFormatWrapper.execute(tempFromFile, tempToFile, connection); 280 } 282 catch (ConnectException e) 283 { 284 throw new ContentIOException("Connection to Uno server failed: \n" + 285 " reader: " + reader + "\n" + 286 " writer: " + writer, 287 e); 288 } 289 catch (IOException e) 290 { 291 throw new ContentIOException("Uno server conversion failed: \n" + 292 " reader: " + reader + "\n" + 293 " writer: " + writer + "\n" + 294 " from file: " + tempFromFile + "\n" + 295 " to file: " + tempToFile, 296 e); 297 } 298 299 writer.putContent(tempToFile); 301 } 302 303 307 private static class DocumentFormatWrapper 308 { 309 313 314 private DocumentFormat documentFormat; 315 private double reliability; 316 317 public DocumentFormatWrapper(DocumentFormat documentFormat, double reliability) 318 { 319 this.documentFormat = documentFormat; 320 this.reliability = reliability; 321 } 322 323 public double getReliability() 324 { 325 return reliability; 326 } 327 328 331 public void execute(File fromFile, File toFile, UnoConnection connection) throws ConnectException , IOException 332 { 333 DocumentConverter converter = new DocumentConverter(connection); 334 converter.convert(fromFile, toFile, documentFormat); 335 } 336 } 337 } 338 | Popular Tags |