Package org.sf.mustru.filters

Source Code of org.sf.mustru.filters.RtfHandler

package org.sf.mustru.filters;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.rtf.RTFEditorKit;
import javax.swing.text.BadLocationException;

import org.apache.log4j.Logger;
import org.sf.mustru.docs.IndexableDoc;
import org.sf.mustru.utils.*;

/**
* Extract text from a RTF file using a Swing class.
*/
public class RtfHandler implements HandlerInterface
{
  static Logger logger = Logger.getLogger(RtfHandler.class.getName());
 
  /**
   * Empty constructor
   */
  public RtfHandler() { super(); }
 
  /**
   *  Extract the text from a rtf file
   */
  public void getDocument(String ifile, IndexableDoc doc)
  {
    String bodyText = "";
    DefaultStyledDocument styledDoc = new DefaultStyledDocument();
    logger.info("Extracting text from RTF :" + ifile);
    try
     { new RTFEditorKit().read(new FileInputStream(new File(ifile)), styledDoc, 0);
       if (styledDoc != null)
        { bodyText = styledDoc.getText(0, styledDoc.getLength()); }
     }
    //*-- RTF Editor Kit throws array index out of bounds exception for some I18N files
    catch (ArrayIndexOutOfBoundsException e)
     { logger.error("Array out of Bounds: Could not extract text from RTF document " + ifile); }
    catch (OutOfMemoryError exc)
    { logger.error("Ran out of memory for " + ifile + " or could be corrupt file " + exc.getMessage()); }
    catch (IOException e)
     { logger.error("IO Error: Cannot extract text from a RTF document " + ifile + " " + e.getMessage()); }
    catch (BadLocationException e)
     { logger.error("Bad Location: Cannot extract text from a RTF document " + ifile + " " + e.getMessage()); }
    catch (Exception e)
    { logger.error("Could not extract text from RTF document " + ifile + e.getMessage() )}

    //TextDoc doc = new TextDoc(ifile);
    if (bodyText != null)
     { bodyText = StringTools.filterChars(bodyText); doc.setContents(new StringBuffer(bodyText) ); }
    doc.setFileType("text"); doc.setFileName(ifile);
    return;
  }

}
TOP

Related Classes of org.sf.mustru.filters.RtfHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.