-
Enhancement
-
Resolution: Unresolved
-
P5
-
None
-
6u10
-
x86
-
windows_vista
A DESCRIPTION OF THE REQUEST :
Using RTFEditorKit() and HTMLEditorKit() to convert the RTF text to HTML results in loss of strikethrough text. Bold, underline, and italics are intact and carry over just fine.
JUSTIFICATION :
Strikethrough text is part of the HTML just as much bold, italics and underline are. There are situations where documents contain strikethrough texts which need to be displayed as they are. The fact that the text comes over but it loses the strikethrough formatting can cause some serious confusions/problems.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Strikethrough texts should get displayed in HTML text as they are.
ACTUAL -
Strikethough texts get displayed as plain texts losing the strikethrough formatting.
---------- BEGIN SOURCE ----------
public String convertRTFDocumentToHTMLDocument(String docBody){
if ((docBody == null) || (docBody.length() == 0)) {
return null;
}
String convertedText = "";
RTFEditorKit rtfKit = new RTFEditorKit();
StyledDocument doc = (StyledDocument)rtfKit.createDefaultDocument();
try{
SimpleAttributeSet attribute = new SimpleAttributeSet();
StyleConstants.setFontFamily(attribute, "Arial");
StyleConstants.setFontSize(attribute, 10);
StyleConstants.setLineSpacing(attribute, -.9f);
BufferedReader input = new BufferedReader(new StringReader(docBody));
rtfKit.read(input, doc, 0);
input.close();
HTMLEditorKit htmlKit = new HTMLEditorKit();
StringWriter output = new StringWriter();
htmlKit.write(output, doc, 0, doc.getLength());
convertedText = output.toString();
}catch(Exception e){
ERROR_REPORTER.report("", e);
}
return convertedText;
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
1. Edit StyledEditorKit.java
a. Add StrikeThroughAction() as a defaultAction:
private static final Action[] defaultActions = {
new StrikeThroughAction()
};
b. Add the following class:
public static class StrikeThroughAction extends StyledTextAction {
/**
* Constructs a new StrikeThroughAction.
*/
public StrikeThroughAction() {
super("font-strikethrough");
}
/**
* Toggles the strikethrough attribute.
*
* @param e the action event
*/
public void actionPerformed(ActionEvent e) {
JEditorPane editor = getEditor(e);
if (editor != null) {
StyledEditorKit kit = getStyledEditorKit(editor);
MutableAttributeSet attr = kit.getInputAttributes();
boolean strikethrough = (StyleConstants.isStrikeThrough(attr)) ? false : true;
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setStrikeThrough(sas, strikethrough);
setCharacterAttributes(editor, sas, false);
}
}
}
2. Make changes to MinimalHTMLWriter.java
a) Add the following to protected writeHTMLTags(AttributeSet attr)
if ((oldMask & STRIKETHROUGH) != 0) {
if ((fontMask & STRIKETHROUGH) == 0) {
endMask |= STRIKETHROUGH;
}
} else if ((fontMask & STRIKETHROUGH) != 0) {
startMask |= STRIKETHROUGH;
}
b) Add the following to private void setFontMask(AttributeSet attr)
if (StyleConstants.isStrikeThrough(attr)) {
fontMask |= STRIKETHROUGH;
}
c) Add the following to the private void writeEndMask(int mask)
if ((mask & STRIKETHROUGH) != 0) {
write("</strike>");
3. Edit RTFattributes.java
a) add the following
a.addElement(new BooleanAttribute(CHR, StyleConstants.StrikeThrough, "strike"));
b) remove/comment out the following
//a.addElement(new BooleanAttribute(CHR, Constants.Strikethrough, "strike");
4. Edit RTFReader.java
a) add to protected MutableAttributeSet rootCharacterAttributes()
//additional attribute
StyleConstants.setStrikeThrough(set, false);
Using RTFEditorKit() and HTMLEditorKit() to convert the RTF text to HTML results in loss of strikethrough text. Bold, underline, and italics are intact and carry over just fine.
JUSTIFICATION :
Strikethrough text is part of the HTML just as much bold, italics and underline are. There are situations where documents contain strikethrough texts which need to be displayed as they are. The fact that the text comes over but it loses the strikethrough formatting can cause some serious confusions/problems.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Strikethrough texts should get displayed in HTML text as they are.
ACTUAL -
Strikethough texts get displayed as plain texts losing the strikethrough formatting.
---------- BEGIN SOURCE ----------
public String convertRTFDocumentToHTMLDocument(String docBody){
if ((docBody == null) || (docBody.length() == 0)) {
return null;
}
String convertedText = "";
RTFEditorKit rtfKit = new RTFEditorKit();
StyledDocument doc = (StyledDocument)rtfKit.createDefaultDocument();
try{
SimpleAttributeSet attribute = new SimpleAttributeSet();
StyleConstants.setFontFamily(attribute, "Arial");
StyleConstants.setFontSize(attribute, 10);
StyleConstants.setLineSpacing(attribute, -.9f);
BufferedReader input = new BufferedReader(new StringReader(docBody));
rtfKit.read(input, doc, 0);
input.close();
HTMLEditorKit htmlKit = new HTMLEditorKit();
StringWriter output = new StringWriter();
htmlKit.write(output, doc, 0, doc.getLength());
convertedText = output.toString();
}catch(Exception e){
ERROR_REPORTER.report("", e);
}
return convertedText;
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
1. Edit StyledEditorKit.java
a. Add StrikeThroughAction() as a defaultAction:
private static final Action[] defaultActions = {
new StrikeThroughAction()
};
b. Add the following class:
public static class StrikeThroughAction extends StyledTextAction {
/**
* Constructs a new StrikeThroughAction.
*/
public StrikeThroughAction() {
super("font-strikethrough");
}
/**
* Toggles the strikethrough attribute.
*
* @param e the action event
*/
public void actionPerformed(ActionEvent e) {
JEditorPane editor = getEditor(e);
if (editor != null) {
StyledEditorKit kit = getStyledEditorKit(editor);
MutableAttributeSet attr = kit.getInputAttributes();
boolean strikethrough = (StyleConstants.isStrikeThrough(attr)) ? false : true;
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setStrikeThrough(sas, strikethrough);
setCharacterAttributes(editor, sas, false);
}
}
}
2. Make changes to MinimalHTMLWriter.java
a) Add the following to protected writeHTMLTags(AttributeSet attr)
if ((oldMask & STRIKETHROUGH) != 0) {
if ((fontMask & STRIKETHROUGH) == 0) {
endMask |= STRIKETHROUGH;
}
} else if ((fontMask & STRIKETHROUGH) != 0) {
startMask |= STRIKETHROUGH;
}
b) Add the following to private void setFontMask(AttributeSet attr)
if (StyleConstants.isStrikeThrough(attr)) {
fontMask |= STRIKETHROUGH;
}
c) Add the following to the private void writeEndMask(int mask)
if ((mask & STRIKETHROUGH) != 0) {
write("</strike>");
3. Edit RTFattributes.java
a) add the following
a.addElement(new BooleanAttribute(CHR, StyleConstants.StrikeThrough, "strike"));
b) remove/comment out the following
//a.addElement(new BooleanAttribute(CHR, Constants.Strikethrough, "strike");
4. Edit RTFReader.java
a) add to protected MutableAttributeSet rootCharacterAttributes()
//additional attribute
StyleConstants.setStrikeThrough(set, false);