0 Replies Latest reply: Oct 11, 2007 8:57 AM by 807605 RSS

    Create a new HTML file by changing attributes

    807605
      Hello, I am going to add some attributes to create a new html file.
      Such as
      <div id="globalWrapper">
      becomes
      <div id="globalWrapper" style=...>
      I have completed partial task, but I have trouble on how to save it to a new file.
      Please look at my code.
      import java.awt.Point;
      import java.io.*;
      import java.util.*;
      import javax.swing.text.EditorKit;
      import javax.swing.text.MutableAttributeSet;
      import javax.swing.text.html.HTML;
      import javax.swing.text.html.HTMLDocument;
      import javax.swing.text.html.HTMLEditorKit;
      import javax.swing.text.html.HTMLEditorKit.InsertHTMLTextAction;
      import javax.swing.text.html.parser.ParserDelegator;
      
      public class ReadTest extends HTMLEditorKit.ParserCallback 
      {
           public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos){
      
               if     (
                           (t == HTML.Tag.BR)   ||
                           (t == HTML.Tag.DIV)  ||
                           (t == HTML.Tag.P)    ||
                           (t == HTML.Tag.UL)   ||
                           (t == HTML.Tag.UL)   ||
                           (t == HTML.Tag.DD)   ||
                           (t == HTML.Tag.BR)   ||
                           (t == HTML.Tag.H1)   ||
                           (t == HTML.Tag.H2)   ||
                           (t == HTML.Tag.H3)   ||
                           (t == HTML.Tag.H4)   ||
                           (t == HTML.Tag.H5)   ||
                           (t == HTML.Tag.H6)   
                      )
               a.addAttribute(HTMLEditorKit.ParserCallback.IMPLIED,"style=");// right or wrong? Then what?
               System.out.println(a);
        }
           
          public static void main(String argv[]) {
          try {
              Reader r = new FileReader("C:/test.html");
              ParserDelegator parser = new ParserDelegator();
              HTMLEditorKit.ParserCallback callback = new ReadTest();
              parser.parse(r, callback, true);
              EditorKit kit=new HTMLEditorKit();                        
              HTMLDocument doc=(HTMLDocument)kit.createDefaultDocument();   
          
        } catch (IOException e) {
              e.printStackTrace();
        }
       }
      
         
      }
      Thanks