Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将JTextPane样式的内容输出到HTML,包括自定义样式?_Java_Coding Style_Jtextpane - Fatal编程技术网

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

Java 如何将JTextPane样式的内容输出到HTML,包括自定义样式?,java,coding-style,jtextpane,Java,Coding Style,Jtextpane,我目前使用JTextPane允许用户添加/编辑文本。它允许粗体/斜体/下划线(我计划将来允许链接)。它还允许用户插入按钮,这些按钮作为自定义样式插入。该小组的情况如下: > 我想能够保存/加载为HTML的内容-内容将纳入一个Flash swf。我能够以HTML格式获取内容,如下所示: public String getHTMLText(){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); try{

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

>

我想能够保存/加载为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();
     }
如果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}”的字符串):

函数createRefButton(字符串cID):

NRefButton重写toString,后者返回“{”+cID+“}”

我想知道的是:我是否应该修改插入“样式”的方式以获得此错误

是否有其他/更好的方法从这个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>
这是一些样式化的文本。真是不可思议。


在这里,我们有一个按钮,它已被删除:{0-0}。这些按钮是本项目的必需部分。
我还希望能够将输出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;
     }
    }
package com.HTMLExport;
导入javax.swing.text.AbstractDocument;
导入javax.swing.text.AttributeSet;
导入javax.swing.text.BadLocationException;
导入javax.swing.text.Element;
导入javax.swing.text.ElementIterator;
导入javax.swing.text.StyleConstants;
导入javax.swing.text.StyledDocument;
公共级NHTMLWriter{
私人风格文件;
私有元素迭代器;
受保护的静态最终字符换行符='\n';
公共NHTMLWriter(样式文件文档){
_sd=doc;
_它=新的ElementIterator(doc.getDefaultRootElement());
}
公共字符串getHTML(){
返回“+this.getBody()+”;
}
受保护的字符串getBody(){
/*
这将是样式化文档的节元素。
我们在HTML中将该元素表示为body标记。
因此我们忽略了它。
*/
_it.current();
元素next=null;
字符串体=”;
while((next=_it.next())!=null){
if(this.isText(next)){
正文+=写内容(下一个);
}
else if(next.getName().equals(“组件”)){
body+=getText(next);//这是自定义组件的输出位置。
}
}
正文+=”;
返回体;
}
/**
*如果元素是文本元素,则返回true。
*/
受保护的布尔isText(元素元素元素){
返回(elem.getName()==AbstractDocument.ContentElementName);
}
受保护的字符串writeContent(元素elem){
AttributeSet attr=elem.getAttributes();
字符串starttag=this.getStartTag(attr);
String content=startTags+this.getText(elem)+this.getEndTag(startTags);
返回内容;
}
/**
*写出文本
*/
受保护的字符串文本(元素elem){
字符串contentStr=getText(elem);
if((contentStr.length()>0)和&(contentStr.charAt(contentStr.length()-1)=换行符)){
contentStr=contentStr.substring(0,contentStr.length()-1)+“
”; } 如果(contentStr.length()>0){ 返回contentStr; } 返回contentStr; } 受保护的字符串getText(元素elem){ 试一试{ 返回_sd.getText(elem.getStartOffset(),elem.getEndOffset()-elem.getStartOffset()).replaceAll(换行符+“”,“
”); }捕获(错误位置异常e){ e、 printStackTrace(); } 返回“”; } 私有字符串getEndTag(字符串开始标记){
String[]startOrder=startTags.split(“我不知道你的问题的答案,但请用引用问题的投票率最高的答案替换该自定义输出流,即使用ByteArrayOutputStream。不错。你问了,你评论了,你回答了,你接受了。你有没有时间写下另一半?
<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>
  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;
     }
    }