Example usage for javax.swing.text.rtf RTFEditorKit RTFEditorKit

List of usage examples for javax.swing.text.rtf RTFEditorKit RTFEditorKit

Introduction

In this page you can find the example usage for javax.swing.text.rtf RTFEditorKit RTFEditorKit.

Prototype

public RTFEditorKit() 

Source Link

Document

Constructs an RTFEditorKit.

Usage

From source file:Main.java

public Main() throws Exception {
    setSize(400, 240);/* w  w w.  j  a  v a  2 s.c  o  m*/
    JPanel topPanel = new JPanel();
    topPanel.setLayout(new BorderLayout());
    getContentPane().add(topPanel, BorderLayout.CENTER);

    RTFEditorKit rtf = new RTFEditorKit();
    JEditorPane editor = new JEditorPane();
    editor.setEditorKit(rtf);

    JScrollPane scroller = new JScrollPane();
    scroller.getViewport().add(editor);
    topPanel.add(scroller, BorderLayout.CENTER);

    FileInputStream fi = new FileInputStream("test.rtf");
    rtf.read(fi, editor.getDocument(), 0);
}

From source file:RTFView.java

public RTFView() {
    setTitle("RTF Text Application");
    setSize(400, 240);//from   ww w  .ja v  a2  s. c o  m
    setBackground(Color.gray);
    getContentPane().setLayout(new BorderLayout());

    JPanel topPanel = new JPanel();
    topPanel.setLayout(new BorderLayout());
    getContentPane().add(topPanel, BorderLayout.CENTER);

    // Create an RTF editor window
    RTFEditorKit rtf = new RTFEditorKit();
    JEditorPane editor = new JEditorPane();
    editor.setEditorKit(rtf);
    editor.setBackground(Color.white);

    // This text could be big so add a scroll pane
    JScrollPane scroller = new JScrollPane();
    scroller.getViewport().add(editor);
    topPanel.add(scroller, BorderLayout.CENTER);

    // Load an RTF file into the editor
    try {
        FileInputStream fi = new FileInputStream("test.rtf");
        rtf.read(fi, editor.getDocument(), 0);
    } catch (FileNotFoundException e) {
        System.out.println("File not found");
    } catch (IOException e) {
        System.out.println("I/O error");
    } catch (BadLocationException e) {
    }
}

From source file:framework.retrieval.engine.index.create.impl.file.parse.RTFFileContentParser.java

public String getContent(RFileDocument document, String charsetName) {
    String content = "";
    InputStream fileInputStream = null;
    try {//from ww w.  ja v  a2  s  . co m
        fileInputStream = new FileInputStream(document.getFile());
        DefaultStyledDocument styledDoc = new DefaultStyledDocument();
        RTFEditorKit rtfEditorKit = new RTFEditorKit();
        rtfEditorKit.read(fileInputStream, styledDoc, 0);
        content = styledDoc.getText(0, styledDoc.getLength());
    } catch (Exception e) {
        RetrievalUtil.errorLog(log, document.getFile().getAbsolutePath(), e);
    } finally {
        try {
            if (fileInputStream != null) {
                fileInputStream.close();
            }
        } catch (Exception e) {
            RetrievalUtil.errorLog(log, e);
        }
    }
    return content;
}

From source file:com.stimulus.archiva.extraction.RTFExtractor.java

public Reader getText(InputStream is, Charset charset, IndexInfo indexInfo) throws ExtractionException {

    Reader reader = null;//from  w  w w .  j av  a2  s .  c o m
    FileWriter writer = null;
    File file = null;
    try {
        reader = new InputStreamReader(is);
        file = File.createTempFile("extract_rtf", ".tmp");
        indexInfo.addDeleteFile(file);
        writer = new FileWriter(file);
        DefaultStyledDocument doc = new DefaultStyledDocument();
        new RTFEditorKit().read(reader, doc, 0);
        writer.write(doc.getText(0, doc.getLength()));
    } catch (Throwable ioe) {
        throw new ExtractionException("failed to parse rtf document", ioe, logger);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ioe) {
            }
        }

        if (writer != null) {
            try {
                writer.close();
            } catch (IOException ioe) {
            }
        }
    }
    try {
        Reader outReader = new FileReader(file);
        indexInfo.addReader(outReader);
        return outReader;
    } catch (Exception ex) {
        throw new ExtractionException("failed to extract text from powerpoint document", ex, logger,
                ChainedException.Level.DEBUG);
    }

}

From source file:com.aurel.track.lucene.index.associatedFields.textExctractor.RTFExtractor.java

/**
 * Gets the text from file content /*w  ww.  ja va  2  s  . co m*/
 * @param file
 * @param fileExtension
 * @return
 */
@Override
public String getText(File file, String fileExtension) {
    FileInputStream fis = null;
    Reader reader = null;
    try {
        DefaultStyledDocument dsd = new DefaultStyledDocument();
        RTFEditorKit rtfEditorKit = new RTFEditorKit();

        try {
            fis = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            LOGGER.info("File " + file.getName() + " not found. " + e.getMessage());
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
            return null;
        }
        reader = new BufferedReader(new InputStreamReader(fis));
        rtfEditorKit.read(reader, dsd, 0);
        return dsd.getText(0, dsd.getLength());
    } catch (Exception e) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(
                    "Extracting text from the .rtf  file " + file.getName() + " failed with " + e.getMessage());
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        }
        return null;
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                LOGGER.debug(
                        "Closing the reader for file " + file.getName() + "  failed with " + e.getMessage());
                LOGGER.debug(ExceptionUtils.getStackTrace(e));
            }
        }
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                LOGGER.debug("Closing the FileInputStream for file " + file.getName() + " failed with "
                        + e.getMessage());
                LOGGER.debug(ExceptionUtils.getStackTrace(e));
            }
        }
    }
}

From source file:edu.ur.ir.index.DefaultRtfTextExtractor.java

/**
 * Extract text from the Rich text file document 
 * @throws Exception /*  w  w  w .j  av  a2 s  . co  m*/
 * 
 * @see edu.ur.ir.index.FileTextExtractor#getText(java.io.File)
 */
public String getText(File f) throws Exception {
    String text = null;
    // don't even try if the file is too large
    if (isFileTooLarge(f) || f.length() <= 0l) {
        return text;
    }
    DefaultStyledDocument styledDoc = new DefaultStyledDocument();
    RTFEditorKit editorKit = new RTFEditorKit();
    FileInputStream inputStream = null;

    try {
        inputStream = new FileInputStream(f);
        editorKit.read(inputStream, styledDoc, 0);
        String myText = styledDoc.getText(0, styledDoc.getLength());
        if (myText != null && !myText.trim().equals("")) {
            text = myText;
        }
    } catch (OutOfMemoryError oome) {
        text = null;
        log.error("could not extract text", oome);
        throw (oome);
    } catch (Exception e) {
        text = null;
        log.error("could not get text for rich text document " + f.getAbsolutePath(), e);
        throw (e);
    }

    finally {
        closeInputStream(inputStream);
        editorKit = null;
    }

    return text;
}

From source file:com.mirth.connect.server.util.FileUtil.java

public static String rtfToPlainText(String message, String replaceLinebreaksWith)
        throws IOException, BadLocationException {

    String convertedPlainText;/* ww w  .jav  a  2  s .co m*/

    // Reading the RTF content string
    Reader in = new StringReader(message);

    // creating a default blank styled document
    DefaultStyledDocument styledDoc = new DefaultStyledDocument();

    // Creating a RTF Editor kit
    RTFEditorKit rtfKit = new RTFEditorKit();

    // Populating the contents in the blank styled document
    rtfKit.read(in, styledDoc, 0);

    // Getting the root document
    Document doc = styledDoc.getDefaultRootElement().getDocument();

    convertedPlainText = doc.getText(0, doc.getLength());
    if (replaceLinebreaksWith != null) {
        convertedPlainText = convertedPlainText.replaceAll("\\n", replaceLinebreaksWith);
    }

    return convertedPlainText;
}

From source file:com.mirth.connect.server.userutil.FileUtil.java

/**
 * Converts an RTF into plain text using the Swing RTFEditorKit.
 * //ww w .  j a  v  a  2 s.co m
 * @param message
 *            The RTF message to convert.
 * @param replaceLinebreaksWith
 *            If not null, any line breaks in the converted message will be replaced with this
 *            string.
 * @return The converted plain text message.
 * @throws IOException
 * @throws BadLocationException
 */
public static String rtfToPlainText(String message, String replaceLinebreaksWith)
        throws IOException, BadLocationException {

    String convertedPlainText;

    // Reading the RTF content string
    Reader in = new StringReader(message);

    // creating a default blank styled document
    DefaultStyledDocument styledDoc = new DefaultStyledDocument();

    // Creating a RTF Editor kit
    RTFEditorKit rtfKit = new RTFEditorKit();

    // Populating the contents in the blank styled document
    rtfKit.read(in, styledDoc, 0);

    // Getting the root document
    Document doc = styledDoc.getDefaultRootElement().getDocument();

    convertedPlainText = doc.getText(0, doc.getLength());
    if (replaceLinebreaksWith != null) {
        convertedPlainText = convertedPlainText.replaceAll("\\n", replaceLinebreaksWith);
    }

    return convertedPlainText;
}

From source file:com.liferay.portal.util.LuceneFields.java

public static Field getFile(String field, File file, String fileExt) throws IOException {

    fileExt = fileExt.toLowerCase();//from  ww w  .  ja  v a 2  s.  c o m

    FileInputStream fis = new FileInputStream(file);
    Reader reader = new BufferedReader(new InputStreamReader(fis));

    String text = null;

    if (fileExt.equals(".doc")) {
        try {
            WordDocument wordDocument = new WordDocument(fis);

            StringWriter stringWriter = new StringWriter();

            wordDocument.writeAllText(stringWriter);

            text = stringWriter.toString();

            stringWriter.close();
        } catch (Exception e) {
            _log.error(e.getMessage());
        }
    } else if (fileExt.equals(".htm") || fileExt.equals(".html")) {
        try {
            DefaultStyledDocument dsd = new DefaultStyledDocument();

            HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
            htmlEditorKit.read(reader, dsd, 0);

            text = dsd.getText(0, dsd.getLength());
        } catch (Exception e) {
            _log.error(e.getMessage());
        }
    } else if (fileExt.equals(".pdf")) {
        try {
            PDFParser parser = new PDFParser(fis);
            parser.parse();

            PDDocument pdDoc = parser.getPDDocument();

            StringWriter stringWriter = new StringWriter();

            PDFTextStripper stripper = new PDFTextStripper();
            stripper.setLineSeparator("\n");
            stripper.writeText(pdDoc, stringWriter);

            text = stringWriter.toString();

            stringWriter.close();
            pdDoc.close();
        } catch (Exception e) {
            _log.error(e.getMessage());
        }
    } else if (fileExt.equals(".rtf")) {
        try {
            DefaultStyledDocument dsd = new DefaultStyledDocument();

            RTFEditorKit rtfEditorKit = new RTFEditorKit();
            rtfEditorKit.read(reader, dsd, 0);

            text = dsd.getText(0, dsd.getLength());
        } catch (Exception e) {
            _log.error(e.getMessage());
        }
    } else if (fileExt.equals(".xls")) {
        try {
            XLSTextStripper stripper = new XLSTextStripper(fis);

            text = stripper.getText();
        } catch (Exception e) {
            _log.error(e.getMessage());
        }
    }

    if (text != null) {
        return new Field(field, text, Field.Store.YES, Field.Index.NOT_ANALYZED);
    } else {
        return new Field(field, reader);
    }
}

From source file:com.egangotri.transliteratorAsSwing.TransliteratorJFrame.java

public TransliteratorJFrame() {
    super("eGangotri Indic Transliterator");

    PrintWriter pw = new PrintWriter(System.out, true);
    setSize(650, 650);//  w  ww  . j  a v a  2  s  .c o  m

    // menubar
    menubar = new JMenuBar();

    // menus
    file = new JMenu("File");
    help = new JMenu("Help");

    // JMenuItem
    save_1 = new JMenuItem("Save Input");
    save_1.setActionCommand("save_1");
    save_1.addActionListener(this);

    save_2 = new JMenuItem("Save Output-1");
    save_2.setActionCommand("save_2");
    save_2.addActionListener(this);

    save_3 = new JMenuItem("Save Output-2");
    save_3.setActionCommand("save_3");
    save_3.addActionListener(this);

    open_1 = new JMenuItem("Open File for Input");
    open_1.setActionCommand("open_1");
    open_1.addActionListener(this);

    exitItem = new JMenuItem("Exit");
    exitItem.setActionCommand("Exit");
    exitItem.addActionListener(this);

    aboutItem = new JMenuItem("About");
    aboutItem.setActionCommand("about_item");
    aboutItem.addActionListener(this);

    itransItem = new JMenuItem("ITRANS " + Constants.ENCODING_SCHEME);
    itransItem.setActionCommand("itrans_encoding");
    itransItem.addActionListener(this);

    slpItem = new JMenuItem("SLP " + Constants.ENCODING_SCHEME);
    slpItem.setActionCommand("slp_encoding");
    slpItem.addActionListener(this);

    hkItem = new JMenuItem("Harvard Kyoto " + Constants.ENCODING_SCHEME);
    hkItem.setActionCommand("hk_encoding");
    hkItem.addActionListener(this);

    velthuisItem = new JMenuItem("Velthuis " + Constants.ENCODING_SCHEME);
    velthuisItem.setActionCommand("velthuis_encoding");
    velthuisItem.addActionListener(this);

    dvnItem = new JMenuItem("Devanagari " + Constants.ENCODING_SCHEME);
    dvnItem.setActionCommand("devanagari_encoding");
    dvnItem.addActionListener(this);

    iastItem = new JMenuItem("IAST " + Constants.ENCODING_SCHEME);
    iastItem.setActionCommand("iast_encoding");
    iastItem.addActionListener(this);

    // add menuitems to menu
    file.add(open_1);
    file.add(save_1);
    file.add(save_2);
    file.add(save_3);
    file.add(exitItem);

    help.add(aboutItem);
    help.add(itransItem);
    help.add(slpItem);
    help.add(hkItem);
    help.add(velthuisItem);
    help.add(dvnItem);
    help.add(iastItem);

    // add menus to menubar
    menubar.add(file);
    menubar.add(help);
    // menus end

    // JPanel Initilization
    p1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
    p1a = new JPanel(new BorderLayout());
    p2 = new JPanel();
    p3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
    p3a = new JPanel(new BorderLayout());

    p4 = new JPanel();
    p5 = new JPanel(new FlowLayout(FlowLayout.LEFT));
    p5a = new JPanel(new BorderLayout());

    p6 = new JPanel();
    p6a = new JPanel();
    p7 = new JPanel();

    // JLabel Initialization
    label1 = new JLabel("Input:");
    label2 = new JLabel("Output-1");
    label3 = new JLabel("Output-2");

    capitalize = new JCheckBox("Capitalize Extended Latin");
    capitalize.setSelected(capitalizeIAST);
    capitalize.setActionCommand("capitalize");
    capitalize.addActionListener(this);

    // Buttons
    clearButton = new JButton("Clear");
    clearButton.setActionCommand("clear");
    clearButton.setToolTipText("Clear all Fields");

    refreshButton = new JButton("Refresh");
    refreshButton.setActionCommand("refresh");
    refreshButton.setToolTipText("Refesh the View");

    exitButton = new JButton("Exit");
    exitButton.setActionCommand("Exit");
    exitButton.setToolTipText("Quit the Application.");

    clipboardButton1 = new JButton("Clipboard");
    clipboardButton1.setActionCommand("clipboard-1");
    clipboardButton1.setToolTipText("Clipboard Input");

    clipboardButton2 = new JButton("Clipboard");
    clipboardButton2.setActionCommand("clipboard-2");
    clipboardButton2.setToolTipText("Clipboard Output-1");

    clipboardButton3 = new JButton("Clipboard");
    clipboardButton3.setActionCommand("clipboard-3");
    clipboardButton3.setToolTipText("Clipboard Output-2");

    clearButton.addActionListener(this);
    refreshButton.addActionListener(this);
    exitButton.addActionListener(this);

    clipboardButton1.addActionListener(this);
    clipboardButton2.addActionListener(this);
    clipboardButton3.addActionListener(this);

    Container contentPane = getContentPane();

    // JTextBox
    tb1 = new JTextArea(new PlainDocument(), null, 6, 45);
    tb1.setLineWrap(true);
    tb1.setWrapStyleWord(true);
    tb1.addKeyListener(this);

    tb2 = new JTextArea(new PlainDocument(), null, 6, 45);
    tb2.setLineWrap(true);
    tb2.setWrapStyleWord(true);
    tb2.addKeyListener(this);

    tb3 = new JTextArea(new PlainDocument(), null, 6, 45);
    tb3.setLineWrap(true);
    tb3.setWrapStyleWord(true);
    tb3.addKeyListener(this);

    // Setting Fonts
    Font unicodeFont = new Font(Constants.ARIAL_UNICODE_MS, Font.PLAIN, Constants.FONT_SIZE);
    tb1.setFont(unicodeFont);
    tb2.setFont(unicodeFont);
    tb3.setFont(unicodeFont);

    comboBox1 = new JComboBox(Constants.ENCODINGS.toArray());
    comboBox1.setActionCommand("comboBox1");
    comboBox1.setSelectedItem(Constants.ITRANS);
    comboBox1.addActionListener(this);

    comboBox2 = new JComboBox(Constants.ENCODINGS.toArray());
    comboBox2.setActionCommand("comboBox2");
    comboBox2.setSelectedItem(Constants.UNICODE_DVN);
    comboBox2.addActionListener(this);

    comboBox3 = new JComboBox(Constants.ENCODINGS.toArray());
    comboBox3.setActionCommand("comboBox3");
    comboBox3.setSelectedItem(Constants.IAST);
    comboBox3.addActionListener(this);

    /** *EXPERIMENT*** */
    textPane = new JTextPane();
    RTFEditorKit rtfkit = new RTFEditorKit();
    // HTMLEditorKit htmlkit = new HTMLEditorKit();
    textPane.setEditorKit(rtfkit); // set Kit which will read RTF Doc
    // textPane.setEditorKit(htmlkit);
    textPane.setEditable(false); // make uneditable
    textPane.setPreferredSize(new Dimension(200, 200));
    textPane.setText(""); // set

    p1.add(label1);
    p1a.add(comboBox1, BorderLayout.LINE_END);
    p1a.add(clipboardButton1, BorderLayout.LINE_START);

    p2.add(new JScrollPane(tb1));

    p3.add(label2);
    p3a.add(comboBox2, BorderLayout.LINE_END);
    p3a.add(clipboardButton2, BorderLayout.LINE_START);

    p4.add(new JScrollPane(tb2));

    p5.add(label3);
    p5a.add(comboBox3, BorderLayout.LINE_END);
    p5a.add(clipboardButton3, BorderLayout.LINE_START);

    p6.add(new JScrollPane(tb3));

    p6a.add(capitalize);
    p7.add(clearButton);
    p7.add(refreshButton);
    p7.add(exitButton);
    this.setJMenuBar(menubar);

    contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));

    contentPane.add(p1);
    contentPane.add(p1a);
    contentPane.add(p2);
    contentPane.add(p3);
    contentPane.add(p3a);
    contentPane.add(p4);
    contentPane.add(p5);
    contentPane.add(p5a);
    contentPane.add(p6);
    contentPane.add(p6a);
    contentPane.add(p7);

}