html text原样式输出,如何将JTextPane样式的内容输出到HTML,包括自定义样式?

我目前使用JTextPane来允许用户添加/编辑文本.它允许粗体/斜体/下划线(我计划将来允许链接).它还允许用户删除按钮,这些按钮作为自定义样式插入.该小组看起来像:

< <图像已删除> >

我希望能够将内容保存/加载为HTML – 内容将合并到Flash swf中.我可以像HTML那样获取内容:

public String getHTMLText(){

ByteArrayOutputStream baos = new ByteArrayOutputStream();

try{

HTMLEditorKit hk = new HTMLEditorKit();

hk.write(baos,this.getStyledDocument(),this.getDocument().getLength());

} catch (IOException e) {

e.printStackTrace();

} catch (BadLocationException e) {

e.printStackTrace();

}

return baos.toString();

}

如果JTextPane仅包含粗体/斜体/带下划线的文本,则此方法可以正常工作.但输出过于复杂.我希望能够输出我的自定义样式,但是当我尝试时我收到此错误:

Exception occurred during event dispatching:

java.lang.NullPointerException

at javax.swing.text.html.MinimalHTMLWriter.writeAttributes(MinimalHTMLWriter.java:151)

at javax.swing.text.html.MinimalHTMLWriter.writeStyles(MinimalHTMLWriter.java:256)

at javax.swing.text.html.MinimalHTMLWriter.writeHeader(MinimalHTMLWriter.java:220)

at javax.swing.text.html.MinimalHTMLWriter.write(MinimalHTMLWriter.java:122)

at javax.swing.text.html.HTMLEditorKit.write(HTMLEditorKit.java:293)

at javax.swing.text.DefaultEditorKit.write(DefaultEditorKit.java:152)

at numeracy.referencetextpanel.NRefButtonTextArea.getHTMLText(NRefButtonTextArea.java:328)

at numeracy.referencetextpanel.NInputPanelRefTextButton.getReferencedText(NInputPanelRefTextButton.java:59)

at numeracy.referencetextpanel.NInputRefText.actionPerformed(NInputRefText.java:106)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)

我的自定义样式是这样插入的(cID是类似“{0-0}”的字符串):

StyledDocument doc = this.getStyledDocument();

NRefButton b = this.createRefButton(cID);

Style style = doc.addStyle(cID,null); //prepare a style

StyleConstants.setComponent(style,b);

doc.insertString(doc.getLength(),b.toString(),style); //insert button at index

函数createRefButton(String cID):

private NRefButton createRefButton(String cID) {

NRefButton b = new NRefButton(_equationButtons.get(cID).getText(),cID,_equationButtons.get(cID).isStruck()); //prepare a button

return b;

}

NRefButton重写toString,返回“{”cID“}”.

我想知道的是:我应该修改插入“Style”以获取此错误的方式吗?

我是否有不同的/更好的方法从这个JTextPane获取HTML?我想要的只是围绕粗体/斜体/带下划线的文本的HTML标签,而不是过于复杂,好像它是我必须去除不必要的HTML,并且“样式”以button.toString()形式出现.

或者我应该实现自己的toHTML()方法,用所需的标签包装粗体/斜体/带下划线的文本?我不介意这样做(在某些方面我更喜欢它),但我不知道如何获取给定JTextPane文档的样式.我想如果我能够获得这些样式,我可以迭代它们,将样式文本包装在适当的标签中?

理想情况下,图示的JTextPane内容将输出为:

required part of this project.

我希望能够将输出HTML读入JTextPane – 再次我不介意为此编写自己的fromHTML()方法,但我需要能够首先获取HTML.

感谢您抽出宝贵时间阅读我的问题.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值