001/* 002 * (C) Copyright 2006-2016 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 */ 019package org.nuxeo.ecm.core.convert.plugins.text.extractors; 020 021import java.io.File; 022import java.io.FileInputStream; 023import java.io.IOException; 024import java.io.InputStream; 025import java.io.Serializable; 026import java.util.Map; 027 028import javax.swing.text.BadLocationException; 029import javax.swing.text.Document; 030import javax.swing.text.rtf.RTFEditorKit; 031 032import org.apache.commons.io.FileUtils; 033import org.nuxeo.ecm.core.api.Blob; 034import org.nuxeo.ecm.core.api.Blobs; 035import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 036import org.nuxeo.ecm.core.convert.api.ConversionException; 037import org.nuxeo.ecm.core.convert.cache.SimpleCachableBlobHolder; 038import org.nuxeo.ecm.core.convert.extension.Converter; 039import org.nuxeo.ecm.core.convert.extension.ConverterDescriptor; 040import org.nuxeo.runtime.api.Framework; 041 042public class RTF2TextConverter implements Converter { 043 044 @Override 045 public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters) throws ConversionException { 046 File f = null; 047 try { 048 RTFEditorKit rtfParser = new RTFEditorKit(); 049 Document document = rtfParser.createDefaultDocument(); 050 rtfParser.read(blobHolder.getBlob().getStream(), document, 0); 051 String text = document.getText(0, document.getLength()); 052 f = Framework.createTempFile("swing-rtf2text", ".txt"); 053 FileUtils.writeStringToFile(f, text); 054 Blob blob; 055 try (InputStream in = new FileInputStream(f)) { 056 blob = Blobs.createBlob(in, "text/plain"); 057 } 058 return new SimpleCachableBlobHolder(blob); 059 } catch (IOException | BadLocationException e) { 060 throw new ConversionException("Error during Word2Text conversion", e); 061 } finally { 062 if (f != null) { 063 f.delete(); 064 } 065 } 066 } 067 068 @Override 069 public void init(ConverterDescriptor descriptor) { 070 } 071 072}