mirror of
https://github.com/SuspiciousActivity/ByteEdit.git
synced 2026-08-13 17:56:17 +00:00
Fixes and updates
This commit is contained in:
1 parent
d1710ea6a5
commit
fb4da77359
211 files changed
+45363
-45571
No files matched your search
@@ -20,25 +20,24 @@ import java.io.StringReader;
|
||||
* Gets the plain text version of RTF documents.
|
||||
* <p>
|
||||
*
|
||||
* This is used by <code>RtfTransferable</code> to return the plain text
|
||||
* version of the transferable when the receiver does not support RTF.
|
||||
* This is used by <code>RtfTransferable</code> to return the plain text version
|
||||
* of the transferable when the receiver does not support RTF.
|
||||
*
|
||||
* @author Robert Futrell
|
||||
* @version 1.0
|
||||
*/
|
||||
final class RtfToText {
|
||||
|
||||
|
||||
private Reader r;
|
||||
private StringBuilder sb;
|
||||
private StringBuilder controlWord;
|
||||
private int blockCount;
|
||||
private boolean inControlWord;
|
||||
|
||||
|
||||
/**
|
||||
* Private constructor.
|
||||
*
|
||||
* @param r
|
||||
* The reader to read RTF text from.
|
||||
* @param r The reader to read RTF text from.
|
||||
*/
|
||||
private RtfToText(Reader r) {
|
||||
this.r = r;
|
||||
@@ -47,15 +46,14 @@ final class RtfToText {
|
||||
blockCount = 0;
|
||||
inControlWord = false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Converts the RTF text read from this converter's <code>Reader</code>
|
||||
* into plain text. It is the caller's responsibility to close the
|
||||
* reader after this method is called.
|
||||
* Converts the RTF text read from this converter's <code>Reader</code> into
|
||||
* plain text. It is the caller's responsibility to close the reader after this
|
||||
* method is called.
|
||||
*
|
||||
* @return The plain text.
|
||||
* @throws IOException
|
||||
* If an IO error occurs.
|
||||
* @throws IOException If an IO error occurs.
|
||||
*/
|
||||
private String convert() throws IOException {
|
||||
// Skip over first curly brace as the whole file is in '{' and '}'
|
||||
@@ -66,76 +64,76 @@ final class RtfToText {
|
||||
while ((i = r.read()) != -1) {
|
||||
char ch = (char) i;
|
||||
switch (ch) {
|
||||
case '{':
|
||||
if (inControlWord && controlWord.length() == 0) { // "\{"
|
||||
sb.append('{');
|
||||
controlWord.setLength(0);
|
||||
inControlWord = false;
|
||||
} else {
|
||||
blockCount++;
|
||||
}
|
||||
break;
|
||||
case '}':
|
||||
if (inControlWord && controlWord.length() == 0) { // "\}"
|
||||
sb.append('}');
|
||||
controlWord.setLength(0);
|
||||
inControlWord = false;
|
||||
} else {
|
||||
blockCount--;
|
||||
}
|
||||
break;
|
||||
case '\\':
|
||||
if (blockCount == 0) {
|
||||
if (inControlWord) {
|
||||
if (controlWord.length() == 0) { // "\\"
|
||||
sb.append('\\');
|
||||
controlWord.setLength(0);
|
||||
inControlWord = false;
|
||||
} else {
|
||||
endControlWord();
|
||||
inControlWord = true;
|
||||
}
|
||||
case '{':
|
||||
if (inControlWord && controlWord.length() == 0) { // "\{"
|
||||
sb.append('{');
|
||||
controlWord.setLength(0);
|
||||
inControlWord = false;
|
||||
} else {
|
||||
blockCount++;
|
||||
}
|
||||
break;
|
||||
case '}':
|
||||
if (inControlWord && controlWord.length() == 0) { // "\}"
|
||||
sb.append('}');
|
||||
controlWord.setLength(0);
|
||||
inControlWord = false;
|
||||
} else {
|
||||
blockCount--;
|
||||
}
|
||||
break;
|
||||
case '\\':
|
||||
if (blockCount == 0) {
|
||||
if (inControlWord) {
|
||||
if (controlWord.length() == 0) { // "\\"
|
||||
sb.append('\\');
|
||||
controlWord.setLength(0);
|
||||
inControlWord = false;
|
||||
} else {
|
||||
endControlWord();
|
||||
inControlWord = true;
|
||||
}
|
||||
} else {
|
||||
inControlWord = true;
|
||||
}
|
||||
break;
|
||||
case ' ':
|
||||
if (blockCount == 0) {
|
||||
if (inControlWord) {
|
||||
endControlWord();
|
||||
} else {
|
||||
sb.append(' ');
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ' ':
|
||||
if (blockCount == 0) {
|
||||
if (inControlWord) {
|
||||
endControlWord();
|
||||
} else {
|
||||
sb.append(' ');
|
||||
}
|
||||
break;
|
||||
case '\r':
|
||||
case '\n':
|
||||
if (blockCount == 0) {
|
||||
if (inControlWord) {
|
||||
endControlWord();
|
||||
}
|
||||
// Otherwise, ignore
|
||||
}
|
||||
break;
|
||||
case '\r':
|
||||
case '\n':
|
||||
if (blockCount == 0) {
|
||||
if (inControlWord) {
|
||||
endControlWord();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (blockCount == 0) {
|
||||
if (inControlWord) {
|
||||
controlWord.append(ch);
|
||||
} else {
|
||||
sb.append(ch);
|
||||
}
|
||||
// Otherwise, ignore
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (blockCount == 0) {
|
||||
if (inControlWord) {
|
||||
controlWord.append(ch);
|
||||
} else {
|
||||
sb.append(ch);
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Ends a control word. Checks whether it is a common one that affects
|
||||
* the plain text output (such as "<code>par</code>" or "<code>tab</code>")
|
||||
* and updates the text buffer accordingly.
|
||||
* Ends a control word. Checks whether it is a common one that affects the plain
|
||||
* text output (such as "<code>par</code>" or "<code>tab</code>") and updates
|
||||
* the text buffer accordingly.
|
||||
*/
|
||||
private void endControlWord() {
|
||||
String word = controlWord.toString();
|
||||
@@ -147,58 +145,50 @@ final class RtfToText {
|
||||
controlWord.setLength(0);
|
||||
inControlWord = false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Converts the contents of the specified byte array representing
|
||||
* an RTF document into plain text.
|
||||
* Converts the contents of the specified byte array representing an RTF
|
||||
* document into plain text.
|
||||
*
|
||||
* @param rtf
|
||||
* The byte array representing an RTF document.
|
||||
* @param rtf The byte array representing an RTF document.
|
||||
* @return The contents of the RTF document, in plain text.
|
||||
* @throws IOException
|
||||
* If an IO error occurs.
|
||||
* @throws IOException If an IO error occurs.
|
||||
*/
|
||||
public static String getPlainText(byte[] rtf) throws IOException {
|
||||
return getPlainText(new ByteArrayInputStream(rtf));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Converts the contents of the specified RTF file to plain text.
|
||||
*
|
||||
* @param file
|
||||
* The RTF file to convert.
|
||||
* @param file The RTF file to convert.
|
||||
* @return The contents of the file, in plain text.
|
||||
* @throws IOException
|
||||
* If an IO error occurs.
|
||||
* @throws IOException If an IO error occurs.
|
||||
*/
|
||||
public static String getPlainText(File file) throws IOException {
|
||||
return getPlainText(new BufferedReader(new FileReader(file)));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Converts the contents of the specified input stream to plain text.
|
||||
* The input stream will be closed when this method returns.
|
||||
* Converts the contents of the specified input stream to plain text. The input
|
||||
* stream will be closed when this method returns.
|
||||
*
|
||||
* @param in
|
||||
* The input stream to convert. This will be closed when this
|
||||
* method returns.
|
||||
* @param in The input stream to convert. This will be closed when this method
|
||||
* returns.
|
||||
* @return The contents of the stream, in plain text.
|
||||
* @throws IOException
|
||||
* If an IO error occurs.
|
||||
* @throws IOException If an IO error occurs.
|
||||
*/
|
||||
public static String getPlainText(InputStream in) throws IOException {
|
||||
return getPlainText(new InputStreamReader(in, "US-ASCII"));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Converts the contents of the specified <code>Reader</code> to plain text.
|
||||
*
|
||||
* @param r
|
||||
* The <code>Reader</code>. This will be closed when this method
|
||||
* returns.
|
||||
* @param r The <code>Reader</code>. This will be closed when this method
|
||||
* returns.
|
||||
* @return The contents of the <code>Reader</code>, in plain text.
|
||||
* @throws IOException
|
||||
* If an IO error occurs.
|
||||
* @throws IOException If an IO error occurs.
|
||||
*/
|
||||
private static String getPlainText(Reader r) throws IOException {
|
||||
try {
|
||||
@@ -208,15 +198,13 @@ final class RtfToText {
|
||||
r.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Converts the contents of the specified String to plain text.
|
||||
*
|
||||
* @param rtf
|
||||
* A string whose contents represent an RTF document.
|
||||
* @param rtf A string whose contents represent an RTF document.
|
||||
* @return The contents of the String, in plain text.
|
||||
* @throws IOException
|
||||
* If an IO error occurs.
|
||||
* @throws IOException If an IO error occurs.
|
||||
*/
|
||||
public static String getPlainText(String rtf) throws IOException {
|
||||
return getPlainText(new StringReader(rtf));
|
||||
|
||||
Reference in new issue
Block a user