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

Iterator returned by HTMLDocument.getIterator() is incorrectly empty

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 6
    • 1.4.0
    • client-libs
    • beta
    • sparc
    • solaris_8

      Name: gm110360 Date: 09/20/2002


      FULL PRODUCT VERSION :
      java version "1.4.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
      Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)


      A DESCRIPTION OF THE PROBLEM :
      The HTMLDocument.Iterator instance returned with argument
      HTML.Tag.META and HTML.Tag.IMG (and perhaps others) is
      empty even though the parsed document contained those
      tags. The attributes of the tags can be accessed using an
      ElementIterator and searching for the tag.



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Create input file "demo3.html":
      <HTML>
      <HEAD>
      <META name="keywords" content="key word list">
      </HEAD>
      <BODY >
      <IMG SRC="./images/mumble.gif">
      </BODY>
      </HTML>

      2. Compile eand execute source (below)
      javac Demo3.java
      java Demo3 demo3.html

      3. Examine stdout to see HTMLDocument.Iterator failed to
      show tag attributes displayed by ElementIterator. Note
      also that file written by Demo3 program containes the tags
      and atributes as expected.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      ANTICIPATED OUTPUT:

      List for <img> using HTMLDocument.Iterator
         src=./images/mumble.gif
      List for <meta> using HTMLDocument.Iterator
         name=keywords
         content=key word list

      List for <img> using ElementIterator
         src: ./images/mumble.gif
         name: img
      List for <meta> using ElementIterator
         name: keywords
         content: key word list
         name: meta

      ACTUAL OUTPUT:

      List for <img> using HTMLDocument.Iterator
         empty
      List for <meta> using HTMLDocument.Iterator
         empty

      List for <img> using ElementIterator
         src: ./images/mumble.gif
         name: img
      List for <meta> using ElementIterator
         name: keywords
         content: key word list
         name: meta


      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      no errors as such...

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.io.*;
      import java.util.*;
      import javax.swing.text.*;
      import javax.swing.text.html.*;

      public class Demo3
      {
        private static void docIterator (HTMLDocument doc, HTML.Tag tag)
        {
          System.out.println("List for <" + tag + "> using HTMLDocument.Iterator");
          HTMLDocument.Iterator itr = doc.getIterator(tag);
          if ((itr == null) || !itr.isValid()) {
            System.out.println(" empty");
          }
          else {
            while (itr.isValid()) {
              System.out.println(" " + itr.getAttributes().toString());
              itr.next();
            }
          }
        }

        private static void elementIterator (HTMLDocument doc, HTML.Tag tag)
        {
          System.out.println("List for <" + tag + "> using ElementIterator");
          ElementIterator ei = new ElementIterator(doc);
          Element element = null;
          int cnt = 0;
          while ((element = ei.next()) != null) {
            if (element.getName().equals(tag.toString())) {
              AttributeSet attrs = element.getAttributes();
              Enumeration attrNames = attrs.getAttributeNames();
              while (attrNames.hasMoreElements()) {
                Object key = attrNames.nextElement();
                System.out.println(" " + key + ": " + attrs.getAttribute(key));
                ++cnt;
              }
            }
          }
          if (cnt == 0) {
            System.out.println(" empty");
          }
        }
        public static void main (String [] args)
        {
          String inFile = args[0];
          String outFile = "new." + inFile;
          FileWriter writer = null;
          FileReader reader = null;

          HTMLEditorKit kit = new HTMLEditorKit();
          HTMLDocument doc = (HTMLDocument)kit.createDefaultDocument();
          doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);

          try {
            reader = new FileReader(inFile);
            writer = new FileWriter(new File(outFile));
            kit.read(reader, doc, 0);

            docIterator(doc, HTML.Tag.IMG);
            docIterator(doc, HTML.Tag.META);

            System.out.println("");

            elementIterator(doc, HTML.Tag.IMG);
            elementIterator(doc, HTML.Tag.META);

            kit.write(writer, doc, 0, doc.getLength());
            writer.close();
          }
          catch (Exception ex) {
            ex.printStackTrace();
          }
        }

      }

      ---------- END SOURCE ----------

      CUSTOMER WORKAROUND :
      Possible to subclass HTMLDocument and override getIterator
      to synthisize a attribute list using ElementIterator
      (Review ID: 164770)
      ======================================================================
      ###@###.### 10/13/04 17:56 GMT

            idk Igor Kushnirskiy (Inactive)
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: