Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4290248

HTMLEditorKit ignores HTML <FORM tag

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.3.0
    • client-libs



      Name: skT88420 Date: 11/10/99


      java version "1.3beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3beta-O)
      Java(TM) HotSpot Client VM (build 1.3beta-O, mixed mode)

      When HTML input is parsed by the HTMLEditorKit, <FORM amd </FORM> tags are
      ignored.
      I discovered the bug when I wanted to use an iterator
      HTMLDocument.Iterator formIterator=doc.getIterator(HTML.Tag.INPUT)
      That iterator never returned a tag.
      The test example demonstrates the bug by simply writing the document to a file.
      However, it does not directly demonstrate the iterator problem, for which
      I have not opened a separate bug.
      The behaviour is the same in JDK1.2.2


      Input HTML file input.htm:
      <html>
        <head>
            <title>Test</title>
        </head>
        <body>
          <form action="http://www.javasoft.com" method=get>
          <input type="hidden" name="test" value="1">
          </form>
        </body>
      </html>

      Output HTML file output.htm:
      <html>
        <head>
          <title>Test </title>
          
        </head>
        <body>
          <input value="1" type="hidden" name="test">
          

          <p>
            
          </p>
        </body>
      </html>
      Java source file:

      import java.io.*;
      import java.net.*;
      import javax.swing.text.*;
      import javax.swing.text.html.*;
       
      class HtmlDoc {

          static FileWriter writer;
          static FileReader reader;
          static String inputFile = "input.htm";
          static String resultFile = "output.htm";
          public static void main(String[] args) {


              HTMLEditorKit kit = new HTMLEditorKit();
              HTMLDocument doc = (HTMLDocument)kit.createDefaultDocument();
              doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
             
              try {
                  // Create a reader on the HTML content.
                  reader = new FileReader(inputFile);
                  writer = new FileWriter(new File(resultFile));
                  kit.read(reader, doc, 0);
                  kit.write(writer, doc, 0, 10000);
                  writer.close();
              } catch (Exception e) {
                  e.printStackTrace();
              }
              System.exit(1);

          }//End main()
       
      }//End class HtmlDoc
      (Review ID: 97558)
      ======================================================================

      Name: skT88420 Date: 11/17/99


      java version "1.3beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3beta-O)
      Java(TM) HotSpot Client VM (build 1.3beta-O, mixed mode)


      See below code:
      /*
      HTMLDocument.Iterator ignores tags e.g. <INPUT. I think <IMG is not working,
      either.
      It would be handy to have an easy way to iterate through the input tags
      of e.g. the 2nd form on a page only. Similar to JavaScript
      document.forms[1].elements[index] and document.forms[1].elements.length. I came
      across this bug when I was looking for such functionality.
      */
      import java.io.*;
      import java.net.*;
      import javax.swing.text.*;
      import javax.swing.text.html.*;
       
      class HtmlTest {

          public static void main(String[] args) {


              HTMLEditorKit kit = new HTMLEditorKit();
              HTMLDocument doc =
      (javax.swing.text.html.HTMLDocument)kit.createDefaultDocument();
              // The Document class does not yet handle charset's properly.
              doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);

              try {
                  // Create a reader on the HTML content.
                  Reader rd = getReader(args[0]);
                  // Parse the HTML.
                  kit.read(rd, doc, 0);
                  // Iterate through specific HTML tags of the HTML document.
                  HTMLDocument.Iterator tagIterator=doc.getIterator(HTML.Tag.INPUT);
                  int tagCount=0;
                  while (tagIterator.isValid()){
                      tagCount++;
                      tagIterator.next();
                  }
                  System.out.println("tagCount="+tagCount);
              } catch (Exception e) {
                  e.printStackTrace();
              }

              System.out.println("Please press any key to exit");
              try {System.in.read();}catch(IOException e){}
              System.exit(1);


          }//End main()
       
          // Returns a reader on the HTML data. If 'uri' begins
          // with "http:", it's treated as a URL; otherwise,
          // it's assumed to be a local filename.
          static Reader getReader(String uri) throws IOException {
              if (uri.startsWith("http:")) {
                  // Retrieve from Internet.
                  URLConnection conn = new URL(uri).openConnection();
                  return new InputStreamReader(conn.getInputStream());
              } else {
                  // Retrieve from file.
                  return new FileReader(uri);
              }
          }

      }//End class HtmlTest


      input file input.htm:
      <html>
        <head>
            <title>Test</title>
        </head>
        <body>
          <form action="http://www.javasoft.com" method=get>
          <input type="hidden" name="test" value="1">
          </form>
        </body>
      </html>

      execute: java HtmlTest input.htm
      (Review ID: 97821)
      ======================================================================

            svioletsunw Scott Violet (Inactive)
            skonchad Sandeep Konchady
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: