下面列出了怎么用javax.swing.text.rtf.RTFEditorKit的API类实例代码及写法,或者点击链接到github查看源代码。
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);
}
}
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");
}
/**
* {@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();
}
}
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;
}
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;
}
/**
* 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;
}
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;
}
}
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;
}
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);
}
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);
}
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 );
}
public RtfRichTextConverter() {
editorKit = new RTFEditorKit();
}