package pt.tumba.parser.rtf;
import java.awt.Color;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.StringTokenizer;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Document;
import javax.swing.text.Element;
import javax.swing.text.StyleConstants;
import javax.swing.text.rtf.RTFEditorKit;
/**
* Description of the Class
*
*@author bmartins
*@created 22 de Agosto de 2002
*/
public class RTF2HTML {
/**
* Description of the Class
*
*@author bmartins
*@created 22 de Agosto de 2002
*/
private class HTMLStateMachine {
private String alignNames[] = { "left", "center", "right" };
/**
* Description of the Field
*/
public boolean acceptFonts;
private String fontName;
private Color color;
private int size;
private int alignment;
private boolean bold;
private boolean italic;
private boolean underline;
private double firstLineIndent;
private double oldLeftIndent;
private double oldRightIndent;
private double leftIndent;
private double rightIndent;
private boolean firstLine;
/**
* Constructor for the HTMLStateMachine object
*/
HTMLStateMachine() {
acceptFonts = true;
fontName = "";
alignment = -1;
bold = false;
italic = false;
underline = false;
color = null;
size = -1;
firstLineIndent = 0.0D;
oldLeftIndent = 0.0D;
oldRightIndent = 0.0D;
leftIndent = 0.0D;
rightIndent = 0.0D;
firstLine = false;
}
/**
* Description of the Method
*
*@param attributeset Description of the Parameter
*@param stringbuffer Description of the Parameter
*@param element Description of the Parameter
*/
public void updateState(
AttributeSet attributeset,
StringBuffer stringbuffer,
Element element) {
String s = element.getName();
if (s.equalsIgnoreCase("paragraph")) {
firstLine = true;
}
leftIndent =
updateDouble(
attributeset,
leftIndent,
StyleConstants.LeftIndent);
rightIndent =
updateDouble(
attributeset,
rightIndent,
StyleConstants.RightIndent);
if (leftIndent != oldLeftIndent || rightIndent != oldRightIndent) {
closeIndentTable(stringbuffer, oldLeftIndent, oldRightIndent);
}
bold =
updateBoolean(
attributeset,
StyleConstants.Bold,
"b",
bold,
stringbuffer);
italic =
updateBoolean(
attributeset,
StyleConstants.Italic,
"i",
italic,
stringbuffer);
underline =
updateBoolean(
attributeset,
StyleConstants.Underline,
"u",
underline,
stringbuffer);
size = updateFontSize(attributeset, size, stringbuffer);
color = updateFontColor(attributeset, color, stringbuffer);
if (acceptFonts) {
fontName = updateFontName(attributeset, fontName, stringbuffer);
}
alignment = updateAlignment(attributeset, alignment, stringbuffer);
firstLineIndent =
updateDouble(
attributeset,
firstLineIndent,
StyleConstants.FirstLineIndent);
if (leftIndent != oldLeftIndent || rightIndent != oldRightIndent) {
openIndentTable(stringbuffer, leftIndent, rightIndent);
oldLeftIndent = leftIndent;
oldRightIndent = rightIndent;
}
}
/**
* Description of the Method
*
*@param stringbuffer Description of the Parameter
*@param d Description of the Parameter
*@param d1 Description of the Parameter
*/
private void openIndentTable(
StringBuffer stringbuffer,
double d,
double d1) {
if (d != 0.0D || d1 != 0.0D) {
closeSubsetTags(stringbuffer);
stringbuffer.append("");
String s = getSpaceTab((int) (d / 4D));
if (s.length() > 0) {
stringbuffer.append("" + s + " ");
}
stringbuffer.append("");
}
}
/**
* Description of the Method
*
*@param stringbuffer Description of the Parameter
*@param d Description of the Parameter
*@param d1 Description of the Parameter
*/
private void closeIndentTable(
StringBuffer stringbuffer,
double d,
double d1) {
if (d != 0.0D || d1 != 0.0D) {
closeSubsetTags(stringbuffer);
stringbuffer.append(" ");
String s = getSpaceTab((int) (d1 / 4D));
if (s.length() > 0) {
stringbuffer.append("" + s + " ");
}
stringbuffer.append("
");
}
}
/**
* Description of the Method
*
*@param stringbuffer Description of the Parameter
*/
public void closeTags(StringBuffer stringbuffer) {
closeSubsetTags(stringbuffer);
closeTag(alignment, -1, "div", stringbuffer);
alignment = -1;
closeIndentTable(stringbuffer, oldLeftIndent, oldRightIndent);
}
/**
* Description of the Method
*
*@param stringbuffer Description of the Parameter
*/
private void closeSubsetTags(StringBuffer stringbuffer) {
closeTag(bold, "b", stringbuffer);
closeTag(italic, "i", stringbuffer);
closeTag(underline, "u", stringbuffer);
closeTag(color, "font", stringbuffer);
closeTag(fontName, "font", stringbuffer);
closeTag(size, -1, "font", stringbuffer);
bold = false;
italic = false;
underline = false;
color = null;
fontName = "";
size = -1;
}
/**
* Description of the Method
*
*@param flag Description of the Parameter
*@param s Description of the Parameter
*@param stringbuffer Description of the Parameter
*/
private void closeTag(
boolean flag,
String s,
StringBuffer stringbuffer) {
if (flag) {
stringbuffer.append("" + s + ">");
}
}
/**
* Description of the Method
*
*@param color1 Description of the Parameter
*@param s Description of the Parameter
*@param stringbuffer Description of the Parameter
*/
private void closeTag(
Color color1,
String s,
StringBuffer stringbuffer) {
if (color1 != null) {
stringbuffer.append("" + s + ">");
}
}
/**
* Description of the Method
*
*@param s Description of the Parameter
*@param s1 Description of the Parameter
*@param stringbuffer Description of the Parameter
*/
private void closeTag(String s, String s1, StringBuffer stringbuffer) {
if (s.length() > 0) {
stringbuffer.append("" + s1 + ">");
}
}
/**
* Description of the Method
*
*@param i Description of the Parameter
*@param j Description of the Parameter
*@param s Description of the Parameter
*@param stringbuffer Description of the Parameter
*/
private void closeTag(
int i,
int j,
String s,
StringBuffer stringbuffer) {
if (i > j) {
stringbuffer.append("" + s + ">");
}
}
/**
* Description of the Method
*
*@param attributeset Description of the Parameter
*@param i Description of the Parameter
*@param stringbuffer Description of the Parameter
*@return Description of the Return Value
*/
private int updateAlignment(
AttributeSet attributeset,
int k,
StringBuffer stringbuffer) {
int i = k;
Object obj = attributeset.getAttribute(StyleConstants.Alignment);
if (obj == null)
return i;
int j = ((Integer) obj).intValue();
if (j == 3) {
j = 0;
}
if (j != i && j >= 0 && j <= 2) {
if (i > -1) {
stringbuffer.append("
代码来自 webcat,适用于英文.且不包含图片应用范例如下:
String rlt = new RTF2HTML().convertRTFToHTML(new File(filepath));
System.out.println(rlt);