mirror of
https://github.com/SuspiciousActivity/ByteEdit.git
synced 2026-08-13 17:56:17 +00:00
Added code folding and formatted all files
This commit is contained in:
1 parent
11570d6b95
commit
66cd1dbd3e
233 files changed
+82245
-118134
No files matched your search
@@ -1,9 +1,7 @@
|
||||
/*
|
||||
* 07/28/2008
|
||||
*
|
||||
* RtfToText.java - Returns the plain text version of RTF documents.
|
||||
*
|
||||
* This library is distributed under a modified BSD license. See the included
|
||||
* This library is distributed under a modified BSD license. See the included
|
||||
* RSyntaxTextArea.License.txt file for details.
|
||||
*/
|
||||
package org.fife.ui.rsyntaxtextarea;
|
||||
@@ -18,9 +16,9 @@ import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
|
||||
|
||||
/**
|
||||
* Gets the plain text version of RTF documents.<p>
|
||||
* 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.
|
||||
@@ -29,18 +27,18 @@ import java.io.StringReader;
|
||||
* @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;
|
||||
@@ -49,79 +47,71 @@ 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
|
||||
* 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 '}'
|
||||
int i = r.read();
|
||||
if (i!='{') {
|
||||
if (i != '{') {
|
||||
throw new IOException("Invalid RTF file");
|
||||
}
|
||||
|
||||
while ((i=r.read())!=-1) {
|
||||
|
||||
char ch = (char)i;
|
||||
while ((i = r.read()) != -1) {
|
||||
char ch = (char) i;
|
||||
switch (ch) {
|
||||
case '{':
|
||||
if (inControlWord && controlWord.length()==0) { // "\{"
|
||||
if (inControlWord && controlWord.length() == 0) { // "\{"
|
||||
sb.append('{');
|
||||
controlWord.setLength(0);
|
||||
inControlWord = false;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
blockCount++;
|
||||
}
|
||||
break;
|
||||
case '}':
|
||||
if (inControlWord && controlWord.length()==0) { // "\}"
|
||||
if (inControlWord && controlWord.length() == 0) { // "\}"
|
||||
sb.append('}');
|
||||
controlWord.setLength(0);
|
||||
inControlWord = false;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
blockCount--;
|
||||
}
|
||||
break;
|
||||
case '\\':
|
||||
if (blockCount==0) {
|
||||
if (blockCount == 0) {
|
||||
if (inControlWord) {
|
||||
if (controlWord.length()==0) { // "\\"
|
||||
if (controlWord.length() == 0) { // "\\"
|
||||
sb.append('\\');
|
||||
controlWord.setLength(0);
|
||||
inControlWord = false;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
endControlWord();
|
||||
inControlWord = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
inControlWord = true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ' ':
|
||||
if (blockCount==0) {
|
||||
if (blockCount == 0) {
|
||||
if (inControlWord) {
|
||||
endControlWord();
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
sb.append(' ');
|
||||
}
|
||||
}
|
||||
break;
|
||||
case '\r':
|
||||
case '\n':
|
||||
if (blockCount==0) {
|
||||
if (blockCount == 0) {
|
||||
if (inControlWord) {
|
||||
endControlWord();
|
||||
}
|
||||
@@ -129,26 +119,21 @@ final class RtfToText {
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (blockCount==0) {
|
||||
if (blockCount == 0) {
|
||||
if (inControlWord) {
|
||||
controlWord.append(ch);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
sb.append(ch);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Ends a control word. Checks whether it is a common one that affects
|
||||
* 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.
|
||||
*/
|
||||
@@ -156,82 +141,84 @@ final class RtfToText {
|
||||
String word = controlWord.toString();
|
||||
if ("par".equals(word)) {
|
||||
sb.append('\n');
|
||||
}
|
||||
else if ("tab".equals(word)) {
|
||||
} else if ("tab".equals(word)) {
|
||||
sb.append('\t');
|
||||
}
|
||||
controlWord.setLength(0);
|
||||
inControlWord = false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @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 {
|
||||
RtfToText converter = new RtfToText(r);
|
||||
RtfToText converter = new RtfToText(r);
|
||||
return converter.convert();
|
||||
} finally {
|
||||
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