Mic*_*son 5 java coding-style jtextpane
我目前使用JTextPane来允许用户添加/编辑文本.它允许粗体/斜体/下划线(我计划将来允许链接).它还允许用户删除按钮,这些按钮作为自定义样式插入.该小组看起来像:
<<< image deleted >>
我希望能够将内容保存/加载为HTML - 内容将合并到Flash swf中.我可以像HTML那样获取内容:
public String getHTMLText(){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try{
HTMLEditorKit hk = new HTMLEditorKit();
hk.write(baos, this.getStyledDocument(), 0, this.getDocument().getLength());
} catch (IOException e) {
e.printStackTrace();
} catch (BadLocationException e) {
e.printStackTrace();
}
return baos.toString();
}
Run Code Online (Sandbox Code Playgroud)
如果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)
Run Code Online (Sandbox Code Playgroud)
我的自定义样式是这样插入的(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
Run Code Online (Sandbox Code Playgroud)
函数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;
}
Run Code Online (Sandbox Code Playgroud)
NRefButton覆盖toString,返回"{"+ cID +"}".
我想知道的是:我应该修改插入"Style"以获取此错误的方式吗?
我是否有不同的/更好的方法从这个JTextPane获取HTML?我想要的只是围绕粗体/斜体/带下划线的文本的HTML标签,而不是过于复杂,好像它是我必须去除不必要的HTML,并且"样式"以button.toString()形式出现.
或者我应该实现自己的toHTML()方法,用所需的标签包装粗体/斜体/带下划线的文本?我不介意这样做(在某些方面我更喜欢它),但我不知道如何获取给定JTextPane文档的样式.我想如果我能够获得这些样式,我可以迭代它们,将样式文本包装在适当的标签中?
理想情况下,图示的JTextPane内容将输出为:
<html><p>This is some <b>styled</b> text. It is <u>incredible</u>.
<br/>
<br/>
Here we have a button that has been dropped in: {0-0}. These buttons are a <b><i>required part of this project.</i></b>
Run Code Online (Sandbox Code Playgroud)
我希望能够将输出HTML 读入 JTextPane - 再次我不介意为此编写自己的fromHTML()方法,但我需要能够首先获取HTML.
感谢您抽出宝贵时间阅读我的问题.
看完之后:
我编写了自己的 HTML 导出器:
package com.HTMLExport;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Element;
import javax.swing.text.ElementIterator;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
public class NHTMLWriter {
private StyledDocument _sd;
private ElementIterator _it;
protected static final char NEWLINE = '\n';
public NHTMLWriter(StyledDocument doc) {
_sd = doc;
_it = new ElementIterator(doc.getDefaultRootElement());
}
public String getHTML(){
return "<html>" + this.getBody() + "</html>";
}
protected String getBody() {
/*
This will be a section element for a styled document.
We represent this element in HTML as the body tags.
Therefore we ignore it.
*/
_it.current();
Element next = null;
String body = "<body>";
while((next = _it.next()) != null) {
if (this.isText(next)) {
body += writeContent(next);
}
else if(next.getName().equals("component")){
body += getText(next); //this is where the custom component is output.
}
}
body += "</body>";
return body;
}
/**
* Returns true if the element is a text element.
*/
protected boolean isText(Element elem) {
return (elem.getName() == AbstractDocument.ContentElementName);
}
protected String writeContent(Element elem){
AttributeSet attr = elem.getAttributes();
String startTags = this.getStartTag(attr);
String content = startTags + this.getText(elem) + this.getEndTag(startTags);
return content;
}
/**
* Writes out text
*/
protected String text(Element elem){
String contentStr = getText(elem);
if ((contentStr.length() > 0) && (contentStr.charAt(contentStr.length()-1) == NEWLINE)) {
contentStr = contentStr.substring(0, contentStr.length()-1) + "<br/>";
}
if (contentStr.length() > 0) {
return contentStr;
}
return contentStr;
}
protected String getText(Element elem){
try {
return _sd.getText(elem.getStartOffset(), elem.getEndOffset() - elem.getStartOffset()).replaceAll(NEWLINE+"", "<br/>");
} catch (BadLocationException e) {
e.printStackTrace();
}
return "";
}
private String getEndTag(String startTags) {
String[] startOrder = startTags.split("<");
String tags = "";
for(String s : startOrder){
tags = "</" + s + tags;
}
return tags;
}
private String getStartTag(AttributeSet attr) {
String tag = "";
if(StyleConstants.isBold(attr)){
tag += "<b>";
}
if(StyleConstants.isItalic(attr)){
tag += "<i>";
}
if(StyleConstants.isUnderline(attr)){
tag += "<u>";
}
return tag;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我需要编写代码,以便它可以执行相反的操作:将输出 HTML 转换为 StyledDocument。
首先,咖啡。
| 归档时间: |
|
| 查看次数: |
3711 次 |
| 最近记录: |