Java Code Examples for javax.swing.text.rtf.RTFEditorKit

The following examples show how to use javax.swing.text.rtf.RTFEditorKit. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Quelea   Source File: SundayPlusParser.java    License: GNU General Public License v3.0 6 votes vote down vote up
private String getLyrics(String raw) {
    try {
        RTFEditorKit rtfParser = new RTFEditorKit();
        Document document = rtfParser.createDefaultDocument();
        rtfParser.read(new ByteArrayInputStream(raw.getBytes()), document, 0);
        String text = document.getText(0, document.getLength());
        StringBuilder textBuilder = new StringBuilder();
        for (String line : text.split("\n")) {
            textBuilder.append(line.trim()).append("\n");
        }
        return textBuilder.toString().replaceAll("[\n]{2,}", "\n\n").replace("^", "'").trim();
    } catch (Exception ex) {
        LOGGER.log(Level.WARNING, "Invalid RTF string, trying old method");
        return getLyricsOld(raw);
    }
}
 
Example 2
public static void main(String[] args) throws Exception{
    rtfEditorKit = new RTFEditorKit();
    robot = new Robot();

    SwingUtilities.invokeAndWait(() -> {
        frame = new JFrame();
        frame.setUndecorated(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(600, 200);
        jTextPane = new JTextPane();
        frame.getContentPane().add(jTextPane);
        frame.setVisible(true);
    });

    test(StyleConstants.ALIGN_LEFT);
    test(StyleConstants.ALIGN_CENTER);
    test(StyleConstants.ALIGN_RIGHT);
    test(StyleConstants.ALIGN_JUSTIFIED);

    SwingUtilities.invokeAndWait(()->frame.dispose());

    System.out.println("ok");
}
 
Example 3
/**
 * {@inheritDoc}
 */
public String extractText(InputStream stream, String type, String encoding) throws IOException {

	try {
		RTFEditorKit rek = new RTFEditorKit();
		DefaultStyledDocument doc = new DefaultStyledDocument();
		rek.read(stream, doc, 0);
		String text = doc.getText(0, doc.getLength());
		return text;
	} catch (Throwable e) {
		logger.warn("Failed to extract RTF text content", e);
		throw new IOException(e.getMessage(), e);
	} finally {
		stream.close();
	}
}
 
Example 4
private static String convertToRTF(final String htmlStr) {

        OutputStream os = new ByteArrayOutputStream();
        HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
        RTFEditorKit rtfEditorKit = new RTFEditorKit();
        String rtfStr = null;

        String tempStr = htmlStr.replace("</font>", "#END_FONT#").replace("<br>", "#NEW_LINE#");
        InputStream is = new ByteArrayInputStream(tempStr.getBytes());
        try {
            Document doc = htmlEditorKit.createDefaultDocument();
            htmlEditorKit.read(is, doc, 0);
            rtfEditorKit.write(os, doc, 0, doc.getLength());
            rtfStr = os.toString();
            rtfStr = rtfStr.replace("#NEW_LINE#", "\\line ");
            rtfStr = rtfStr.replace("#END_FONT#", "\\cf0 ");
        } catch (IOException | BadLocationException e) {
            e.printStackTrace();
        }
        return rtfStr;
    }
 
Example 5
private String convert(InputStream rtfDocumentInputStream) throws IOException {
  RTFEditorKit aRtfEditorkit = new RTFEditorKit();

  StyledDocument styledDoc = new DefaultStyledDocument();

  String textDocument;

  try {
    aRtfEditorkit.read(rtfDocumentInputStream, styledDoc, 0);

    textDocument = styledDoc.getText(0, styledDoc.getLength());
  } catch (BadLocationException e) {
    throw new IOException("Error during parsing");
  }

  return textDocument;
}
 
Example 6
Source Project: Quelea   Source File: MissionPraiseParser.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * Parse the file to get the songs.
 * <p>
 * @param file the RTF file.
 * @param statusPanel the status panel to update.
 * @return a list of the songs found in the Songs.MB file.
 * @throws IOException if something went wrong with the import.
 */
@Override
public List<SongDisplayable> getSongs(File file, StatusPanel statusPanel) throws IOException {
    List<SongDisplayable> ret = new ArrayList<>();
    String text = Utils.getTextFromFile(file.getAbsolutePath(), "", "CP1250");
    text = text.replace("’", "'");
    try {
        if (!text.isEmpty()) {
            RTFEditorKit rtfParser = new RTFEditorKit();
            Document document = rtfParser.createDefaultDocument();
            rtfParser.read(new ByteArrayInputStream(text.getBytes("CP1250")), document, 0);
            String plainText = document.getText(0, document.getLength());
            String title = getTitle(plainText);
            String lyrics = getLyrics(plainText);
            String author = getAuthor(plainText);
            String copyright = getCopyright(plainText);
            
            SongDisplayable song = new SongDisplayable(title, author);
            song.setLyrics(lyrics);
            song.setCopyright(copyright);
            ret.add(song);
        }
    } catch (IOException | BadLocationException ex) {
        LOGGER.log(Level.WARNING, "Error importing mission praise", ex);
    }
    return ret;
}
 
Example 7
Source Project: Quelea   Source File: ProPresenterParser.java    License: GNU General Public License v3.0 5 votes vote down vote up
private String stripRtfTags(String text) {
	RTFEditorKit rtfParser = new RTFEditorKit();
	javax.swing.text.Document document = rtfParser.createDefaultDocument();
	try {
		rtfParser.read(new ByteArrayInputStream(text.getBytes("UTF-8")), document, 0);
		return document.getText(0, document.getLength());
	} catch (IOException | BadLocationException ex) {
		LOGGER.log(Level.SEVERE, "Error stripping RTF tags", ex);
		return text;
	}
}
 
Example 8
protected String extractText(InputStream input) throws IOException, BadLocationException {
	RTFEditorKit rek = new RTFEditorKit();
	DefaultStyledDocument doc = new DefaultStyledDocument();
	rek.read(input, doc, 0);
	String text = doc.getText(0, doc.getLength());
	return text;
}
 
Example 9
Source Project: netbeans   Source File: EditorSanityTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testTextRtfEditorKits() {
    JEditorPane pane = new JEditorPane();
    setContentTypeInAwt(pane, "text/rtf");
    
    // Test JDK kit
    EditorKit kitFromJdk = pane.getEditorKit();
    assertNotNull("Can't find JDK kit for text/rtf", kitFromJdk);
    assertTrue("Wrong JDK kit for application/rtf", kitFromJdk instanceof RTFEditorKit);
}
 
Example 10
Source Project: netbeans   Source File: EditorSanityTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testApplicationRtfEditorKits() {
    JEditorPane pane = new JEditorPane();
    setContentTypeInAwt(pane, "application/rtf");
    
    // Test JDK kit
    EditorKit kitFromJdk = pane.getEditorKit();
    assertNotNull("Can't find JDK kit for application/rtf", kitFromJdk);
    assertTrue("Wrong JDK kit for application/rtf", kitFromJdk instanceof RTFEditorKit);
}
 
Example 11
Source Project: birt   Source File: RTFParser.java    License: Eclipse Public License 1.0 5 votes vote down vote up
public static void parse( String rtfString, RTFDocumentHandler handler )
		throws IOException, BadLocationException
{
	RTFEditorKit rtfeditorkit = new RTFEditorKit( );
	DefaultStyledDocument document = new DefaultStyledDocument( );
	ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream( rtfString.getBytes( ) );
	rtfeditorkit.read( bytearrayinputstream, document, 0 );
	Element element = document.getDefaultRootElement( );
	parseElement( document, element, handler, true );
}
 
Example 12
public RtfRichTextConverter() {
  editorKit = new RTFEditorKit();
}