This implements some more HTML rendering. Most notably it adds support for the CSS color and background-color properties, as well as super- and subscript. It also adds a compatibility layer for the font tag, so that this is mapped to the corresponding CSS attributes.

Example: http://kennke.org/~roman/Swing-HTML-CSS-styles.png

2006-08-24  Roman Kennke  <[EMAIL PROTECTED]>

        * javax/swing/text/html/CSS.java
        (getValue): Added color value conversion.
        * javax/swing/text/html/HTMLDocument.java
        (HTMLReader.ConvertAction): New class, converts HTML style tags
        to CSS attributes.
        (HTMLReader.initTags): Register ConvertAction for <font> tag.
        * javax/swing/text/html/InlineView.java
        (setPropertiesFromAttributes): Implemented to fetch
        CSS character attributes.
        * javax/swing/text/html/StyleSheet.java
        (addCSSAttribute): Convert value.
        (getBackground): Implemented to fetch CSS background color
        attribute.
        (getForeground): Implemented to fetch CSS color
        attribute.
        (getFont): Adjust font size for superscript and subscript.
        (translateHTMLToCSS): Rudimentary implementation that
        copies the original attributes, so that any CSS attributes in
        there are preserved.
        (stringToColor): Use CSSColor for conversion.
        * gnu/javax/swing/text/html/css/CSSColor.java:
        New class. Converts CSS color values to RGB color values.
        * gnu/javax/swing/text/html/CharacterAttributeTranslator.java:
        Removed. This is more or less replaced by CSSColor and the
        ConvertAction in HTMLReader.

/Roman
Index: gnu/javax/swing/text/html/CharacterAttributeTranslator.java
===================================================================
RCS file: gnu/javax/swing/text/html/CharacterAttributeTranslator.java
diff -N gnu/javax/swing/text/html/CharacterAttributeTranslator.java
--- gnu/javax/swing/text/html/CharacterAttributeTranslator.java	6 Jul 2006 11:31:41 -0000	1.4
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,192 +0,0 @@
-/* CharacterAttributeTranslator.java -- 
-   Copyright (C) 2006  Free Software Foundation, Inc.
-
-This file is part of GNU Classpath.
-
-GNU Classpath is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2, or (at your option)
-any later version.
-
-GNU Classpath is distributed in the hope that it will be useful, but
-WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with GNU Classpath; see the file COPYING.  If not, write to the
-Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-02110-1301 USA.
-
-Linking this library statically or dynamically with other modules is
-making a combined work based on this library.  Thus, the terms and
-conditions of the GNU General Public License cover the whole
-combination.
-
-As a special exception, the copyright holders of this library give you
-permission to link this library with independent modules to produce an
-executable, regardless of the license terms of these independent
-modules, and to copy and distribute the resulting executable under
-terms of your choice, provided that you also meet, for each linked
-independent module, the terms and conditions of the license of that
-module.  An independent module is a module which is not derived from
-or based on this library.  If you modify this library, you may extend
-this exception to your version of the library, but you are not
-obligated to do so.  If you do not wish to do so, delete this
-exception statement from your version. */
-
-package gnu.javax.swing.text.html;
-
-import java.awt.Color;
-import java.util.HashMap;
-import java.util.StringTokenizer;
-
-import javax.swing.text.MutableAttributeSet;
-import javax.swing.text.StyleConstants;
-import javax.swing.text.html.HTML.Attribute;
-import javax.swing.text.html.HTML.Tag;
-
-/**
- * This is a small utility class to translate HTML character attributes to
- * Swing StyleConstants
- */
-public class CharacterAttributeTranslator
-{
-  /**
-   * Maps color name to its hex encoding.
-   */
-  private static final HashMap colorMap = new HashMap();
-  static 
-  {
-    colorMap.put("aqua" , "#00FFFF");
-    colorMap.put("blue" , "#0000FF");
-    colorMap.put("black", "#000000");
-    colorMap.put("fuchsia" , "#FF00FF");
-    colorMap.put("gray" , "#808080");
-    colorMap.put("green" , "#008000");
-    colorMap.put("lime" , "#00FF00");
-    colorMap.put("maroon" , "#800000");
-    colorMap.put("navy" , "#000080");
-    colorMap.put("olive" , "#808000");
-    colorMap.put("purple" , "#800080");
-    colorMap.put("red" , "#FF0000");
-    colorMap.put("silver" , "#C0C0C0");
-    colorMap.put("teal" , "#008080");
-    colorMap.put("white" , "#FFFFFF");
-    colorMap.put("yellow" , "#FFFF00");
-  };
-  
-  /**
-   * Convert the color string represenation into java.awt.Color. The valid
-   * values are like "aqua" , "#00FFFF" or "rgb(1,6,44)".
-   * 
-   * @param colorName the color to convert.
-   * @return the matching java.awt.color
-   */
-  public static Color getColor(String colorName)
-  {
-    colorName = colorName.toLowerCase();
-    try
-      {
-        if (colorName.startsWith("rgb"))
-          {
-            // rgb(red, green, blue) notation.
-            StringTokenizer st = new StringTokenizer(colorName, " ,()");
-            String representation = st.nextToken();
-
-            // Return null if the representation is not supported.
-            if (! representation.equals("rgb"))
-              return null;
-            int red = Integer.parseInt(st.nextToken());
-            int green = Integer.parseInt(st.nextToken());
-            int blue = Integer.parseInt(st.nextToken());
-
-            return new Color(red, green, blue);
-          }
-        else
-          {
-            String s2 = (String) colorMap.get(colorName);
-            if (s2 == null)
-              s2 = colorName;
-            return Color.decode(s2);
-          }
-      }
-    catch (Exception nex)
-      {
-        // Can be either number format exception or illegal argument
-        // exception.
-        return null;
-      }
-  }
-  
-  /**
-   * Translate the HTML character attribute to the Swing style constant.
-   * 
-   * @param charAttr the character attributes of the html tag
-   * @param t the html tag itself
-   * @param a the attribute set where the translated attributes will be stored
-   * 
-   * @return true if some attributes were translated, false otherwise.
-   */
-  public static boolean translateTag(MutableAttributeSet charAttr, 
-				     Tag t, MutableAttributeSet a)
-  {
-    if(t == Tag.FONT)
-      {
-        Object color = a.getAttribute(Attribute.COLOR); 
-	if(color != null)
-	  {
-	    Color c = getColor(color.toString());
-	    if( c == null )
-	      return false;
-	    charAttr.addAttribute(StyleConstants.Foreground, c);
-	    return true;
-	  }
-
-	if(a.getAttribute(Attribute.SIZE) != null)
-	  {
-	    // FIXME
-	    //	    charAttr.addAttribute(StyleConstants.FontSize, 
-	    //				  new java.lang.Integer(72));
-	    return true;
-	  }
-      }
-
-    if( t == Tag.B )
-      {
-	charAttr.addAttribute(StyleConstants.Bold, Boolean.TRUE);
-	return true;
-      }
-
-    if( t == Tag.I )
-      {
-	charAttr.addAttribute(StyleConstants.Italic, Boolean.TRUE);
-	return true;
-      }
-
-    if( t == Tag.U )
-      {
-	charAttr.addAttribute(StyleConstants.Underline, Boolean.TRUE);
-	return true;
-      }
-
-    if( t == Tag.STRIKE )
-      {
-	charAttr.addAttribute(StyleConstants.StrikeThrough, Boolean.TRUE);
-	return true;
-      }
-
-    if( t == Tag.SUP )
-      {
-	charAttr.addAttribute(StyleConstants.Superscript, Boolean.TRUE);
-	return true;
-      }
-
-    if( t == Tag.SUB )
-      {
-	charAttr.addAttribute(StyleConstants.Subscript, Boolean.TRUE);
-	return true;
-      }
-    return false;
-  }
-}
Index: gnu/javax/swing/text/html/css/CSSColor.java
===================================================================
RCS file: gnu/javax/swing/text/html/css/CSSColor.java
diff -N gnu/javax/swing/text/html/css/CSSColor.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ gnu/javax/swing/text/html/css/CSSColor.java	25 Aug 2006 11:37:29 -0000
@@ -0,0 +1,134 @@
+/* CSSColor.java -- Converts CSS color values
+   Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package gnu.javax.swing.text.html.css;
+
+import java.awt.Color;
+import java.util.HashMap;
+
+/**
+ * Converts CSS color values into AWT Color values.
+ *
+ * @author Roman Kennke ([EMAIL PROTECTED])
+ */
+public class CSSColor
+{
+
+  private static final HashMap COLOR_MAP;
+  static
+  {
+    COLOR_MAP = new HashMap();
+    COLOR_MAP.put("maroon", "#800000");
+    COLOR_MAP.put("red", "#ff0000");
+    COLOR_MAP.put("orange", "#ffa500");
+    COLOR_MAP.put("yellow", "#ffff00");
+    COLOR_MAP.put("olive", "#808000");
+    COLOR_MAP.put("purple", "#800080");
+    COLOR_MAP.put("fuchsia", "#ff00ff");
+    COLOR_MAP.put("white", "#ffffff");
+    COLOR_MAP.put("lime", "#00ff00");
+    COLOR_MAP.put("green", "#008000");
+    COLOR_MAP.put("navy", "#000080");
+    COLOR_MAP.put("blue", "#0000ff");
+    COLOR_MAP.put("aqua", "#00ffff");
+    COLOR_MAP.put("teal", "#008080");
+    COLOR_MAP.put("black", "#000000");
+    COLOR_MAP.put("silver", "#c0c0c0");
+    COLOR_MAP.put("gray", "#808080");
+  }
+
+  /**
+   * The CSS value.
+   */
+  private String value;
+
+  /**
+   * The converted color.
+   */
+  private Color color;
+
+  /**
+   * Creates a new instance.
+   *
+   * @param val the CSS value
+   */
+  public CSSColor(String val)
+  {
+    value = val;
+    color = convertValue(value);
+  }
+
+  /**
+   * Converts a CSS color value to an AWT color.
+   *
+   * @param value the CSS color value
+   *
+   * @return the converted color value
+   */
+  public static Color convertValue(String value)
+  {
+    Color color;
+    String val1 = value.toLowerCase();
+    if (val1.charAt(0) != '#')
+      val1 = (String) COLOR_MAP.get(val1);
+    if (val1 != null)
+      {
+        String hexVal = val1.substring(1);
+        int rgb = Integer.parseInt(hexVal, 16);
+        color = new Color(rgb);
+      }
+    else
+      color = null;
+    return color;
+  }
+
+  /**
+   * Returns the converted color.
+   *
+   * @return the converted color
+   */
+  public Color getValue()
+  {
+    return color;
+  }
+
+  public String toString()
+  {
+    return value;
+  }
+}
Index: javax/swing/text/html/CSS.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/html/CSS.java,v
retrieving revision 1.4
diff -u -1 -2 -r1.4 CSS.java
--- javax/swing/text/html/CSS.java	24 Aug 2006 16:58:11 -0000	1.4
+++ javax/swing/text/html/CSS.java	25 Aug 2006 11:37:29 -0000
@@ -28,24 +28,25 @@
 executable, regardless of the license terms of these independent
 modules, and to copy and distribute the resulting executable under
 terms of your choice, provided that you also meet, for each linked
 independent module, the terms and conditions of the license of that
 module.  An independent module is a module which is not derived from
 or based on this library.  If you modify this library, you may extend
 this exception to your version of the library, but you are not
 obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version. */
 
 package javax.swing.text.html;
 
+import gnu.javax.swing.text.html.css.CSSColor;
 import gnu.javax.swing.text.html.css.FontSize;
 import gnu.javax.swing.text.html.css.FontStyle;
 import gnu.javax.swing.text.html.css.FontWeight;
 
 import java.io.Serializable;
 import java.util.HashMap;
 
 /**
  * Provides CSS attributes to be used by the HTML view classes. The constants
  * defined here are used as keys for text attributes for use in
  * [EMAIL PROTECTED] javax.swing.text.AttributeSet}s of [EMAIL PROTECTED] javax.swing.text.Element}s.
  *
@@ -473,17 +474,19 @@
    *
    * @return the wrapped value
    */
   static Object getValue(Attribute att, String v)
   {
     Object o;
     if (att == Attribute.FONT_SIZE)
       o = new FontSize(v);
     else if (att == Attribute.FONT_WEIGHT)
       o = new FontWeight(v);
     else if (att == Attribute.FONT_STYLE)
       o = new FontStyle(v);
+    else if (att == Attribute.COLOR || att == Attribute.BACKGROUND_COLOR)
+      o = new CSSColor(v);
     else
       o = v;
     return o;
   }
 }
Index: javax/swing/text/html/CSSParser.java
===================================================================
RCS file: javax/swing/text/html/CSSParser.java
diff -N javax/swing/text/html/CSSParser.java
--- javax/swing/text/html/CSSParser.java	20 Dec 2005 18:57:16 -0000	1.2
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,568 +0,0 @@
-/* CSSParser.java --
-   Copyright (C) 2005 Free Software Foundation, Inc.
-
-This file is part of GNU Classpath.
-
-GNU Classpath is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2, or (at your option)
-any later version.
-
-GNU Classpath is distributed in the hope that it will be useful, but
-WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with GNU Classpath; see the file COPYING.  If not, write to the
-Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-02110-1301 USA.
-
-Linking this library statically or dynamically with other modules is
-making a combined work based on this library.  Thus, the terms and
-conditions of the GNU General Public License cover the whole
-combination.
-
-As a special exception, the copyright holders of this library give you
-permission to link this library with independent modules to produce an
-executable, regardless of the license terms of these independent
-modules, and to copy and distribute the resulting executable under
-terms of your choice, provided that you also meet, for each linked
-independent module, the terms and conditions of the license of that
-module.  An independent module is a module which is not derived from
-or based on this library.  If you modify this library, you may extend
-this exception to your version of the library, but you are not
-obligated to do so.  If you do not wish to do so, delete this
-exception statement from your version. */
-
-
-package javax.swing.text.html;
-
-import java.io.*;
-
-/**
- * Parses a CSS document. This works by way of a delegate that implements the
- * CSSParserCallback interface. The delegate is notified of the following
- * events: 
- * - Import statement: handleImport 
- * - Selectors handleSelector. This is invoked for each string. For example if 
- * the Reader contained p, bar , a {}, the delegate would be notified 4 times, 
- * for 'p,' 'bar' ',' and 'a'. 
- * - When a rule starts, startRule 
- * - Properties in the rule via the handleProperty. This
- * is invoked one per property/value key, eg font size: foo;, would cause the
- * delegate to be notified once with a value of 'font size'. 
- * - Values in the rule via the handleValue, this is notified for the total value. 
- * - When a rule ends, endRule
- * 
- * @author Lillian Angel ([EMAIL PROTECTED])
- */
-class CSSParser
-{
-
-  /**
-   * Receives all information about the CSS document structure while parsing it.
-   * The methods are invoked by parser.
-   */
-  static interface CSSParserCallback
-  {
-    /**
-     * Handles the import statment in the document.
-     * 
-     * @param imp - the import string
-     */
-    public abstract void handleImport(String imp);
-
-    /**
-     * Called when the start of a rule is encountered.
-     */
-    public abstract void startRule();
-
-    /**
-     * Called when the end of a rule is encountered.
-     */
-    public abstract void endRule();
-
-    /**
-     * Handles the selector of a rule.
-     * 
-     * @param selector - the selector in the rule
-     */
-    public abstract void handleSelector(String selector);
-
-    /**
-     * Handles the properties in the document.
-     * 
-     * @param property - the property in the document.
-     */
-    public abstract void handleProperty(String property);
-
-    /**
-     * Handles the values in the document.
-     * 
-     * @param value - the value to handle.
-     */
-    public abstract void handleValue(String value);
-
-  }
-
-  /**
-   * The identifier of the rule.
-   */
-  private static final int IDENTIFIER = 1;
-
-  /**
-   * The open bracket.
-   */
-  private static final int BRACKET_OPEN = 2;
-
-  /**
-   * The close bracket.
-   */
-  private static final int BRACKET_CLOSE = 3;
-
-  /**
-   * The open brace.
-   */
-  private static final int BRACE_OPEN = 4;
-
-  /**
-   * The close brace.
-   */
-  private static final int BRACE_CLOSE = 5;
-
-  /**
-   * The open parentheses.
-   */
-  private static final int PAREN_OPEN = 6;
-
-  /**
-   * The close parentheses.
-   */
-  private static final int PAREN_CLOSE = 7;
-
-  /**
-   * The end of the document.
-   */
-  private static final int END = -1;
-
-  /**
-   * The character mapping in the document.
-   */
-  // FIXME: What is this used for?
-  private static final char[] charMapping = null;
-
-  /**
-   * Set to true if one character has been read ahead.
-   */
-  private boolean didPushChar;
-
-  /**
-   * The read ahead character.
-   */
-  private int pushedChar;
-
-  /**
-   * Temporary place to hold identifiers.
-   */
-  private StringBuffer unitBuffer;
-
-  /**
-   * Used to indicate blocks.
-   */
-  private int[] unitStack;
-
-  /**
-   * Number of valid blocks.
-   */
-  private int stackCount;
-
-  /**
-   * Holds the incoming CSS rules.
-   */
-  private Reader reader;
-
-  /**
-   * Set to true when the first non @ rule is encountered.
-   */
-  private boolean encounteredRuleSet;
-
-  /**
-   * The call back used to parse.
-   */
-  private CSSParser.CSSParserCallback callback;
-
-  /**
-   * nextToken() inserts the string here.
-   */
-  private char[] tokenBuffer;
-
-  /**
-   * Current number of chars in tokenBufferLength.
-   */
-  private int tokenBufferLength;
-
-  /**
-   * Set to true if any whitespace is read.
-   */
-  private boolean readWS;
-
-  /**
-   * Constructor
-   */
-  CSSParser()
-  {
-    unitBuffer = new StringBuffer();
-    tokenBuffer = new char[10];
-  }
-
-  /**
-   * Appends a character to the token buffer.
-   * 
-   * @param c - the character to append
-   */
-  private void append(char c)
-  {
-    if (tokenBuffer.length >= tokenBufferLength)
-      {
-        char[] temp = new char[tokenBufferLength * 2];
-        if (tokenBuffer != null)
-          System.arraycopy(tokenBuffer, 0, temp, 0, tokenBufferLength);
-
-        temp[tokenBufferLength] = c;
-        tokenBuffer = temp;
-      }
-    else
-      tokenBuffer[tokenBufferLength] = c;
-    tokenBufferLength++;
-  }
-
-  /**
-   * Fetches the next token.
-   * 
-   * @param c - the character to fetch.
-   * @return the location
-   * @throws IOException - any i/o error encountered while reading
-   */
-  private int nextToken(char c) throws IOException
-  {
-    readWS = false;
-    int next = readWS();
-
-    switch (next)
-      {
-      case '\"':
-        if (tokenBufferLength > 0)
-          tokenBufferLength--;
-        return IDENTIFIER;
-      case '\'':
-        if (tokenBufferLength > 0)
-          tokenBufferLength--;
-        return IDENTIFIER;
-      case '(':
-        return PAREN_OPEN;
-      case ')':
-        return PAREN_CLOSE;
-      case '{':
-        return BRACE_OPEN;
-      case '}':
-        return BRACE_CLOSE;
-      case '[':
-        return BRACKET_OPEN;
-      case ']':
-        return BRACKET_CLOSE;
-      case -1:
-        return END;
-      default:
-        pushChar(next);
-        getIdentifier(c);
-        return IDENTIFIER;
-      }
-  }
-
-  /**
-   * Reads a character from the stream.
-   * 
-   * @return the number of characters read or -1 if end of stream is reached.
-   * @throws IOException - any i/o encountered while reading
-   */
-  private int readChar() throws IOException
-  {
-    if (didPushChar)
-      {
-        didPushChar = false;
-        return pushedChar;
-      }
-    return reader.read();
-  }
-
-  /**
-   * Parses the the contents of the reader using the
-   * callback.
-   * 
-   * @param reader - the reader to read from
-   * @param callback - the callback instance
-   * @param parsingDeclaration - true if parsing a declaration
-   * @throws IOException - any i/o error from the reader
-   */
-  void parse(Reader reader, CSSParser.CSSParserCallback callback, 
-             boolean parsingDeclaration)
-      throws IOException
-  {
-    this.reader = reader;
-    this.callback = callback;
-    
-    try
-    {
-      if (!parsingDeclaration)
-        while(getNextStatement());
-      else
-        parseDeclarationBlock();
-    }
-    catch (IOException ioe)
-    {
-      // Nothing to do here.
-    }
-  }
-
-  /**
-   * Skips any white space, returning the character after the white space.
-   * 
-   * @return the character after the whitespace
-   * @throws IOException - any i/o error from the reader
-   */
-  private int readWS() throws IOException
-  {
-    int next = readChar();
-    while (Character.isWhitespace((char) next))
-      {
-        readWS = true;
-        int tempNext = readChar();
-        if (tempNext == END)
-          return next;
-        next = tempNext;
-      }
-    
-    // Its all whitespace
-    return END;
-  }
-
-  /**
-   * Gets the next statement, returning false if the end is reached.
-   * A statement is either an At-rule, or a ruleset.
-   * 
-   * @return false if the end is reached
-   * @throws IOException - any i/o error from the reader
-   */
-  private boolean getNextStatement() throws IOException
-  {
-    int c = nextToken((char) 0);
-    switch (c)
-      {
-        case PAREN_OPEN:
-        case BRACE_OPEN:
-        case BRACKET_OPEN:
-          parseTillClosed(c);
-          break;
-        case BRACKET_CLOSE:
-        case BRACE_CLOSE:
-        case PAREN_CLOSE:
-          throw new IOException("Not a proper statement.");
-        case IDENTIFIER:
-          if (tokenBuffer[0] == ('@'))
-            parseAtRule();
-          else
-            parseRuleSet();
-          break;  
-        case END:
-          return false;
-      }
-    return true;
-  }
-
-  /**
-   * Parses an @ rule, stopping at a matching brace pair, or ;.
-   * 
-   * @throws IOException - any i/o error from the reader
-   */
-  private void parseAtRule() throws IOException
-  {    
-    // An At-Rule begins with the "@" character followed immediately by a keyword. 
-    // Following the keyword separated by a space is an At-rule statement appropriate 
-    // to the At-keyword used. If the At-Rule is a simple declarative statement 
-    // (charset, import, fontdef), it is terminated by a semi-colon (";".) 
-    // If the At-Rule is a conditional or informative statement (media, page, font-face), 
-    // it is followed by optional arguments and then a style declaration block inside matching 
-    // curly braces ("{", "}".) At-Rules are sometimes nestable, depending on the context. 
-    // If any part of an At-Rule is not understood, it should be ignored.
-    
-    // FIXME: Not Implemented
-    // call handleimport 
-  }
-
-  /**
-   * Parses the next rule set, which is a selector followed by a declaration 
-   * block.
-   * 
-   * @throws IOException - any i/o error from the reader
-   */
-  private void parseRuleSet() throws IOException
-  {
-    // call parseDeclarationBlock
-    // call parse selectors
-    // call parse identifiers
-    // call startrule/endrule
-    // FIXME: Not Implemented
-  }
-
-  /**
-   * Parses a set of selectors, returning false if the end of the stream is 
-   * reached.
-   * 
-   * @return false if the end of stream is reached
-   * @throws IOException - any i/o error from the reader
-   */
-  private boolean parseSelectors() throws IOException
-  {
-    // FIXME: Not Implemented
-    // call handleselector
-    return false; 
-  }
-
-  /**
-   * Parses a declaration block. Which a number of declarations followed by a
-   * })].
-   * 
-   * @throws IOException - any i/o error from the reader
-   */
-  private void parseDeclarationBlock() throws IOException
-  {
-    // call parseDeclaration
-    // FIXME: Not Implemented
-  }
-
-  /**
-   * Parses a single declaration, which is an identifier a : and another identifier.
-   * This returns the last token seen.
-   * 
-   * @returns the last token
-   * @throws IOException - any i/o error from the reader
-   */
-  private int parseDeclaration() throws IOException
-  {
-    // call handleValue
-    // FIXME: Not Implemented
-    return 0; 
-  }
-
-  /**
-   * Parses identifiers until c is encountered, returning the ending token,
-   * which will be IDENTIFIER if c is found.
-   * 
-   * @param c - the stop character
-   * @param wantsBlocks - true if blocks are wanted
-   * @return the ending token
-   * @throws IOException - any i/o error from the reader
-   */
-  private int parseIdentifiers(char c, boolean wantsBlocks) throws IOException
-  {
-    // FIXME: Not implemented
-    // call handleproperty?
-    return 0;
-  }
-
-  /**
-   * Parses till a matching block close is encountered. This is only appropriate
-   * to be called at the top level (no nesting).
-   * 
-   * @param i - FIXME
-   * @throws IOException - any i/o error from the reader
-   */
-  private void parseTillClosed(int i) throws IOException
-  {
-    // FIXME: Not Implemented
-  }
-
-  /**
-   * Gets an identifier, returning true if the length of the string is greater
-   * than 0, stopping when c, whitespace, or one of {}()[] is hit.
-   * 
-   * @param c - the stop character
-   * @return returns true if the length of the string > 0
-   * @throws IOException - any i/o error from the reader
-   */
-  private boolean getIdentifier(char c) throws IOException
-  {
-    // FIXME: Not Implemented
-    return false;
-  }
-
-  /**
-   * Reads till c is encountered, escaping characters as necessary.
-   * 
-   * @param c - the stop character
-   * @throws IOException - any i/o error from the reader
-   */
-  private void readTill(char c) throws IOException
-  {
-    // FIXME: Not Implemented
-  }
-
-  /**
-   * Parses a comment block.
-   * 
-   * @throws IOException - any i/o error from the reader
-   */
-  private void readComment() throws IOException
-  {
-    // Should ignore comments. Read until end of comment.
-    // FIXME: Not implemented
-  }
-
-  /**
-   * Called when a block start is encountered ({[.
-   * 
-   * @param start of block
-   */
-  private void startBlock(int start)
-  {
-    // FIXME: Not Implemented
-  }
-
-  /**
-   * Called when an end block is encountered )]}
-   * 
-   * @param end of block
-   */
-  private void endBlock(int end)
-  {
-    // FIXME: Not Implemented
-  }
-
-  /**
-   * Checks if currently in a block.
-   * 
-   * @return true if currently in a block.
-   */
-  private boolean inBlock()
-  {
-    // FIXME: Not Implemented
-    return false; 
-  }
-
-  /**
-   * Supports one character look ahead, this will throw if called twice in a row.
-   * 
-   * @param c - the character to push.
-   * @throws IOException - if called twice in a row
-   */
-  private void pushChar(int c) throws IOException
-  {
-    if (didPushChar)
-      throw new IOException("pushChar called twice.");
-    didPushChar = true;
-    pushedChar = c;
-  }
-}
-
- 
\ No newline at end of file
Index: javax/swing/text/html/HTMLDocument.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/html/HTMLDocument.java,v
retrieving revision 1.39
diff -u -1 -2 -r1.39 HTMLDocument.java
--- javax/swing/text/html/HTMLDocument.java	24 Aug 2006 16:58:11 -0000	1.39
+++ javax/swing/text/html/HTMLDocument.java	25 Aug 2006 11:37:30 -0000
@@ -786,25 +786,60 @@
       
       /**
        * Called when an end tag is seen for one of the types of tags associated
        * with this Action.
        */
       public void end(HTML.Tag t)
         throws NotImplementedException
       {
         // FIXME: Implement.
         print ("AreaAction.end not implemented");
       } 
     }
-    
+
+    /**
+     * Converts HTML tags to CSS attributes.
+     */
+    class ConvertAction
+      extends TagAction
+    {
+
+      public void start(HTML.Tag tag, MutableAttributeSet atts)
+      {
+        pushCharacterStyle();
+        charAttr.addAttribute(tag, atts.copyAttributes());
+        StyleSheet styleSheet = getStyleSheet();
+        // TODO: Add other tags here.
+        if (tag == HTML.Tag.FONT)
+          {
+            String color = (String) atts.getAttribute(HTML.Attribute.COLOR);
+            if (color != null)
+              styleSheet.addCSSAttribute(charAttr, CSS.Attribute.COLOR, color);
+            String face = (String) atts.getAttribute(HTML.Attribute.FACE);
+            if (face != null)
+              styleSheet.addCSSAttribute(charAttr, CSS.Attribute.FONT_FAMILY,
+                                         face);
+            String size = (String) atts.getAttribute(HTML.Attribute.SIZE);
+            if (size != null)
+              styleSheet.addCSSAttribute(charAttr, CSS.Attribute.FONT_SIZE,
+                                         size);
+          }
+      }
+
+      public void end(HTML.Tag tag)
+      {
+        popCharacterStyle();
+      }
+    }
+
     class BaseAction extends TagAction
     {
       /**
        * This method is called when a start tag is seen for one of the types
        * of tags associated with this Action.
        */
       public void start(HTML.Tag t, MutableAttributeSet a)
         throws NotImplementedException
       {
         // FIXME: Implement.
         print ("BaseAction.start not implemented");
       }
@@ -1002,48 +1037,48 @@
       SpecialAction specialAction = new SpecialAction();
       ParagraphAction paragraphAction = new ParagraphAction();
       HeadAction headAction = new HeadAction();
       FormAction formAction = new FormAction();
       IsindexAction isindexAction = new IsindexAction();
       LinkAction linkAction = new LinkAction();
       MapAction mapAction = new MapAction();
       PreAction preAction = new PreAction();
       MetaAction metaAction = new MetaAction();
       StyleAction styleAction = new StyleAction();
       TitleAction titleAction = new TitleAction();
       
-      
+      ConvertAction convertAction = new ConvertAction();
       tagToAction.put(HTML.Tag.A, characterAction);
       tagToAction.put(HTML.Tag.ADDRESS, characterAction);
       tagToAction.put(HTML.Tag.APPLET, hiddenAction);
       tagToAction.put(HTML.Tag.AREA, areaAction);
       tagToAction.put(HTML.Tag.B, characterAction);
       tagToAction.put(HTML.Tag.BASE, baseAction);
       tagToAction.put(HTML.Tag.BASEFONT, characterAction);
       tagToAction.put(HTML.Tag.BIG, characterAction);
       tagToAction.put(HTML.Tag.BLOCKQUOTE, blockAction);
       tagToAction.put(HTML.Tag.BODY, blockAction);
       tagToAction.put(HTML.Tag.BR, specialAction);
       tagToAction.put(HTML.Tag.CAPTION, blockAction);
       tagToAction.put(HTML.Tag.CENTER, blockAction);
       tagToAction.put(HTML.Tag.CITE, characterAction);
       tagToAction.put(HTML.Tag.CODE, characterAction);
       tagToAction.put(HTML.Tag.DD, blockAction);
       tagToAction.put(HTML.Tag.DFN, characterAction);
       tagToAction.put(HTML.Tag.DIR, blockAction);
       tagToAction.put(HTML.Tag.DIV, blockAction);
       tagToAction.put(HTML.Tag.DL, blockAction);
       tagToAction.put(HTML.Tag.DT, paragraphAction);
       tagToAction.put(HTML.Tag.EM, characterAction);
-      tagToAction.put(HTML.Tag.FONT, characterAction);
+      tagToAction.put(HTML.Tag.FONT, convertAction);
       tagToAction.put(HTML.Tag.FORM, blockAction);
       tagToAction.put(HTML.Tag.FRAME, specialAction);
       tagToAction.put(HTML.Tag.FRAMESET, blockAction);
       tagToAction.put(HTML.Tag.H1, paragraphAction);
       tagToAction.put(HTML.Tag.H2, paragraphAction);
       tagToAction.put(HTML.Tag.H3, paragraphAction);
       tagToAction.put(HTML.Tag.H4, paragraphAction);
       tagToAction.put(HTML.Tag.H5, paragraphAction);
       tagToAction.put(HTML.Tag.H6, paragraphAction);
       tagToAction.put(HTML.Tag.HEAD, headAction);
       tagToAction.put(HTML.Tag.HR, specialAction);
       tagToAction.put(HTML.Tag.HTML, blockAction);
Index: javax/swing/text/html/InlineView.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/html/InlineView.java,v
retrieving revision 1.3
diff -u -1 -2 -r1.3 InlineView.java
--- javax/swing/text/html/InlineView.java	24 Aug 2006 16:58:11 -0000	1.3
+++ javax/swing/text/html/InlineView.java	25 Aug 2006 11:37:30 -0000
@@ -145,28 +145,61 @@
   public int getBreakWeight(int axis, float pos, float len)
   {
     // FIXME: Implement this.
     return super.getBreakWeight(axis, pos, len);
   }
 
   public View breakView(int axis, int offset, float pos, float len)
   {
     // FIXME: Implement this.
     return super.breakView(axis, offset, pos, len);
   }
 
+  /**
+   * Loads the character style properties from the stylesheet.
+   */
   protected void setPropertiesFromAttributes()
   {
-    // FIXME: Implement this.
     super.setPropertiesFromAttributes();
+    AttributeSet atts = getAttributes();
+    Object o = atts.getAttribute(CSS.Attribute.TEXT_DECORATION);
+
+    // Check for underline.
+    boolean b = false;
+    if (o != null && o.toString().contains("underline"))
+      b = true;
+    setUnderline(b);
+
+    // Check for line-through.
+    b = false;
+    if (o != null && o.toString().contains("line-through"))
+      b = true;
+    setStrikeThrough(b);
+
+    // Check for vertical alignment (subscript/superscript).
+    o = atts.getAttribute(CSS.Attribute.VERTICAL_ALIGN);
+
+    // Subscript.
+    b = false;
+    if (o != null && o.toString().contains("sub"))
+      b = true;
+    setSubscript(b);
+
+    // Superscript.
+    b = false;
+    if (o != null && o.toString().contains("sup"))
+      b = true;
+    setSuperscript(b);
+
+    // TODO: Handle white-space: nowrap property.
   }
 
   /**
    * Returns the stylesheet used by this view. This returns the stylesheet
    * of the <code>HTMLDocument</code> that is rendered by this view.
    *
    * @return the stylesheet used by this view
    */
   protected StyleSheet getStyleSheet()
   {
     Document doc = getDocument();
     StyleSheet styleSheet = null;
Index: javax/swing/text/html/StyleSheet.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/html/StyleSheet.java,v
retrieving revision 1.7
diff -u -1 -2 -r1.7 StyleSheet.java
--- javax/swing/text/html/StyleSheet.java	24 Aug 2006 16:58:11 -0000	1.7
+++ javax/swing/text/html/StyleSheet.java	25 Aug 2006 11:37:31 -0000
@@ -30,25 +30,25 @@
 terms of your choice, provided that you also meet, for each linked
 independent module, the terms and conditions of the license of that
 module.  An independent module is a module which is not derived from
 or based on this library.  If you modify this library, you may extend
 this exception to your version of the library, but you are not
 obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version. */
 
 
 package javax.swing.text.html;
 
 import gnu.classpath.NotImplementedException;
-import gnu.javax.swing.text.html.CharacterAttributeTranslator;
+import gnu.javax.swing.text.html.css.CSSColor;
 import gnu.javax.swing.text.html.css.CSSParser;
 import gnu.javax.swing.text.html.css.CSSParserCallback;
 import gnu.javax.swing.text.html.css.FontSize;
 import gnu.javax.swing.text.html.css.FontStyle;
 import gnu.javax.swing.text.html.css.FontWeight;
 
 import java.awt.Color;
 import java.awt.Font;
 import java.awt.Graphics;
 import java.io.IOException;
 import java.io.Reader;
 import java.io.Serializable;
@@ -561,25 +561,26 @@
   }
   
   /**
    * Adds a CSS attribute to the given set.
    * 
    * @param attr - the attribute set
    * @param key - the attribute to add
    * @param value - the value of the key
    */
   public void addCSSAttribute(MutableAttributeSet attr, CSS.Attribute key,
                               String value)
   {
-    attr.addAttribute(key, value);
+    Object val = CSS.getValue(key, value);
+    attr.addAttribute(key, val);
   }
   
   /**
    * Adds a CSS attribute to the given set.
    * This method parses the value argument from HTML based on key. 
    * Returns true if it finds a valid value for the given key, 
    * and false otherwise.
    * 
    * @param attr - the attribute set
    * @param key - the attribute to add
    * @param value - the value of the key
    * @return true if a valid value was found.
@@ -590,28 +591,30 @@
     // FIXME: Need to parse value from HTML based on key.
     attr.addAttribute(key, value);
     return attr.containsAttribute(key, value);
   }
   
   /**
    * Converts a set of HTML attributes to an equivalent set of CSS attributes.
    * 
    * @param htmlAttrSet - the set containing the HTML attributes.
    * @return the set of CSS attributes
    */
   public AttributeSet translateHTMLToCSS(AttributeSet htmlAttrSet)
-    throws NotImplementedException
   {
-    // FIXME: Implement me.
-    return SimpleAttributeSet.EMPTY;
+    // FIXME: Really convert HTML to CSS here.
+    AttributeSet cssAttr = htmlAttrSet.copyAttributes();
+    MutableAttributeSet cssStyle = addStyle(null, null);
+    cssStyle.addAttributes(cssAttr);
+    return cssStyle;
   }
 
   /**
    * Adds an attribute to the given set and returns a new set. This is implemented
    * to convert StyleConstants attributes to CSS before forwarding them to the superclass.
    * The StyleConstants attribute do not have corresponding CSS entry, the attribute
    * is stored (but will likely not be used).
    * 
    * @param old - the old set
    * @param key - the non-null attribute key
    * @param value - the attribute value
    * @return the updated set 
@@ -712,61 +715,78 @@
    * Gets the font to use for the given set.
    * 
    * @param a - the set to get the font for.
    * @return the font for the set
    */
   public Font getFont(AttributeSet a)
   {
     FontSize size = (FontSize) a.getAttribute(CSS.Attribute.FONT_SIZE);
     int realSize = 12;
     if (size != null)
       realSize = size.getValue();
 
+    // Decrement size for subscript and superscript.
+    Object valign = a.getAttribute(CSS.Attribute.VERTICAL_ALIGN);
+    if (valign != null)
+      {
+        String v = valign.toString();
+        if (v.contains("sup") || v.contains("sub"))
+          realSize -= 2;
+      }
+
     // TODO: Convert font family.
     String family = "SansSerif";
 
     int style = Font.PLAIN;
     FontWeight weight = (FontWeight) a.getAttribute(CSS.Attribute.FONT_WEIGHT);
     if (weight != null)
       style |= weight.getValue();
     FontStyle fStyle = (FontStyle) a.getAttribute(CSS.Attribute.FONT_STYLE);
     if (fStyle != null)
       style |= fStyle.getValue();
     return new Font(family, style, realSize);
   }
   
   /**
    * Takes a set of attributes and turns it into a foreground
    * color specification. This is used to specify things like, brigher, more hue
    * etc.
    * 
    * @param a - the set to get the foreground color for
    * @return the foreground color for the set
    */
   public Color getForeground(AttributeSet a)
   {
-    return super.getForeground(a);     
+    CSSColor c = (CSSColor) a.getAttribute(CSS.Attribute.COLOR);
+    Color color = null;
+    if (c != null)
+      color = c.getValue();
+    return color;     
   }
   
   /**
    * Takes a set of attributes and turns it into a background
    * color specification. This is used to specify things like, brigher, more hue
    * etc.
    * 
    * @param a - the set to get the background color for
    * @return the background color for the set
    */
   public Color getBackground(AttributeSet a)
   {
-    return super.getBackground(a);     
+    CSSColor c = (CSSColor) a.getAttribute(CSS.Attribute.BACKGROUND_COLOR);
+    Color color = null;
+    if (c != null)
+      color = c.getValue();
+    return color;     
   }
   
   /**
    * Gets the box formatter to use for the given set of CSS attributes.
    * 
    * @param a - the given set
    * @return the box formatter
    */
   public BoxPainter getBoxPainter(AttributeSet a)
   {
     return new BoxPainter(a);     
   }
@@ -862,25 +882,25 @@
     return 0;    
   }
   
   /**
    * Convert the color string represenation into java.awt.Color. The valid
    * values are like "aqua" , "#00FFFF" or "rgb(1,6,44)".
    * 
    * @param colorName the color to convert.
    * @return the matching java.awt.color
    */
   public Color stringToColor(String colorName)
   {
-    return CharacterAttributeTranslator.getColor(colorName);
+    return CSSColor.convertValue(colorName);
   }
   
   /**
    * This class carries out some of the duties of CSS formatting. This enables views
    * to present the CSS formatting while not knowing how the CSS values are cached.
    * 
    * This object is reponsible for the insets of a View and making sure
    * the background is maintained according to the CSS attributes.
    * 
    * @author Lillian Angel ([EMAIL PROTECTED])
    */
   public static class BoxPainter extends Object implements Serializable

Reply via email to