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

javax.swing.text.html.HTMLWriter is badly written and needs rewriting

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P5 P5
    • None
    • 1.3.1
    • client-libs



      Name: bsC130419 Date: 07/31/2001


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


      I've been trying to extend HTMLWriter to be more useful. From looking at the
      source, I can see it's very badly written and needs rewriting. I appreciate that
      CSS support is only partly implemented, however the author could still have
      taken the time to allow the user to extend HTMLWriter and make it more useful.

      (i) CSS output.

      The HTMLWriter in the 'writeAttributes' method calls a method that converts the
      Attributes (that are CSS) into HTML3.2 attributes. Why! If I put HTML in that
      contains:

      <div style="x; y; z">

      I expect the same output. I don't expect it to be stripped of nearly all the CSS
      attributes. There is clearly a 'writeCSS' boolean in HTMLWriter that is private
      and can't be set. Why is no set method provided? There is a method called
      convertToHTML, and that uses writeToCSS. So why does writeAttributes call
      convertToHTML32 and not convertToHTML?

      I've overridden writeAttributes and made it do something like this:

         protected void writeAttributes(AttributeSet attr)
            throws IOException
         {
            MutableAttributeSet convAttr = new SimpleAttributeSet();
       
            // Work out if we have CSS attributes and build a string containing the
            // attributes
            StringBuffer value = new StringBuffer();
            Enumeration names = attr.getAttributeNames();
            Object name;
            while (names.hasMoreElements())
            {
               name = names.nextElement();
               if (name instanceof CSS.Attribute)
               {
                  if (value.length() > 0)
                     value.append("; ");
       
                  value.append(name);
                  value.append(": ");
                  value.append(attr.getAttribute(name));
               }
               else
                  convAttr.addAttribute(name, attr.getAttribute(name));
            }
            // If we got a CSS style then add it to the attributes
            if (value.length() > 0)
               convAttr.addAttribute(HTML.Attribute.STYLE, value.toString());
       
            // Now write out the attributes
            names = convAttr.getAttributeNames();
            while (names.hasMoreElements())
            {
              name = names.nextElement();
               if (name instanceof HTML.Tag ||
                   name instanceof StyleConstants ||
                   name == HTML.Attribute.ENDTAG)
               {}
               else
                  write(" " + name + "=\"" + convAttr.getAttribute(name) + "\"");
            }
         }

      which I believe provides a better solution than the current implementation.

      (ii) Styles.

      There is a method called writeStyles that is called with the HTMLDcouments
      default style sheet if the <head> tag hasn't been written. Using
      JTextPane.setPage() and providing an html page, it will create a <head> tag
      regardless of whether there is one in the HTML, so HTMLWriter will never write
      the styles within the HTMLDocument as there will always be a head tag to write.
      Regardless of whether there is or is not a head tag, it should always write the
      styles in the HTMLDocument (or at least provide a flag to write them!). But of
      course, it doesn't. So I did this:


         protected void startTag(Element elem)
            throws IOException, BadLocationException
         {
            // Make sure we put the Styles in
            if (matchNameAttribute(elem.getAttributes(), HTML.Tag.HEAD))
            {
               super.startTag(elem);
               incrIndent();
               writeStyles(((HTMLDocument)getDocument()).getStyleSheet());
               decrIndent();
               writeLineSeparator();
            }
            else
               super.startTag(elem);
         }

      And as the author seems to have a fettish for private methods, I also had to cut
      and paste all the writeStyles stuff. Very annoying. This also needs fixing.


      Come on Sun, this is kids stuff. It'll take ten minutes to sort these issues out
      (at least make the bloody methods protected!!). So let's see it in JDK1.4 final.
      (Review ID: 125836)
      ======================================================================

            gsm Sergey Groznyh (Inactive)
            bstrathesunw Bill Strathearn (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: