2009-10-11 12 views
5

私は現在、ユーザーがテキストを追加/編集できるようにJTextPaneを使用しています。それは太字/斜体/下線を許します(そして私は将来リンクを許可する予定です)。また、ユーザーはカスタムスタイルとして挿入されるボタンをドロップすることもできます。パネルは次のようになります。>>カスタムスタイルを含め、JTextPaneスタイルのコンテンツをHTMLに出力する方法は?

削除

< <画像Iは、/ HTMLなどのロードコンテンツを保存できるようにしたい - コンテンツは、フラッシュの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}のような文字列であるように私のカスタムスタイルが挿入されています"):

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(文字列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 +"}」。

私が知りたいことは:このエラーを得るために「スタイル」を挿入する方法を変更する必要がありますか?

この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> 

同様 JTextPaneのに出力HTML 読む - 再び私は書く気にしない私このためにfromHTML()メソッドを所有していますが、最初にHTMLを取得する必要があります。

私の質問を読んでいただきありがとうございます。

+1

あなたの質問に対する答えはわかりませんが、そのカスタムOutputStreamを、参照されている質問に対する最高の投票回答、つまりByteArrayOutputStreamに置き換えてください。 –

+0

Dave、ありがとうDave。 –

答えて

3

読んだ後:

を私は自分の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; 
    } 
    } 

ありませんw逆にすることができるようにコードを書く必要があります。出力HTMLをStyledDocumentに変換します。

まず、コーヒー。

+3

いいです。あなたはあなたが答えた、あなたが受け入れたとコメントしました。 – Petah

+0

もう半分を書くことに慣れましたか? – nyxaria

関連する問題