Source for gnu.javax.swing.text.html.CharacterAttributeTranslator

   1: /* CharacterAttributeTranslator.java --
   2:    Copyright (C) 2006  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package gnu.javax.swing.text.html;
  39: 
  40: import java.awt.Color;
  41: import java.util.HashMap;
  42: import java.util.StringTokenizer;
  43: 
  44: import javax.swing.text.MutableAttributeSet;
  45: import javax.swing.text.StyleConstants;
  46: import javax.swing.text.html.HTML.Attribute;
  47: import javax.swing.text.html.HTML.Tag;
  48: 
  49: /**
  50:  * This is a small utility class to translate HTML character attributes to
  51:  * Swing StyleConstants
  52:  */
  53: public class CharacterAttributeTranslator
  54: {
  55:   /**
  56:    * Maps color name to its hex encoding.
  57:    */
  58:   private static final HashMap colorMap = new HashMap();
  59:   static
  60:   {
  61:     colorMap.put("aqua" , "#00FFFF");
  62:     colorMap.put("blue" , "#0000FF");
  63:     colorMap.put("black", "#000000");
  64:     colorMap.put("fuchsia" , "#FF00FF");
  65:     colorMap.put("gray" , "#808080");
  66:     colorMap.put("green" , "#008000");
  67:     colorMap.put("lime" , "#00FF00");
  68:     colorMap.put("maroon" , "#800000");
  69:     colorMap.put("navy" , "#000080");
  70:     colorMap.put("olive" , "#808000");
  71:     colorMap.put("purple" , "#800080");
  72:     colorMap.put("red" , "#FF0000");
  73:     colorMap.put("silver" , "#C0C0C0");
  74:     colorMap.put("teal" , "#008080");
  75:     colorMap.put("white" , "#FFFFFF");
  76:     colorMap.put("yellow" , "#FFFF00");
  77:   }
  78: 
  79:   /**
  80:    * Convert the color string represenation into java.awt.Color. The valid
  81:    * values are like "aqua" , "#00FFFF" or "rgb(1,6,44)".
  82:    *
  83:    * @param colorName the color to convert.
  84:    * @return the matching java.awt.color
  85:    */
  86:   public static Color getColor(String colorName)
  87:   {
  88:     colorName = colorName.toLowerCase();
  89:     try
  90:       {
  91:         if (colorName.startsWith("rgb"))
  92:           {
  93:             // rgb(red, green, blue) notation.
  94:             StringTokenizer st = new StringTokenizer(colorName, " ,()");
  95:             String representation = st.nextToken();
  96: 
  97:             // Return null if the representation is not supported.
  98:             if (! representation.equals("rgb"))
  99:               return null;
 100:             int red = Integer.parseInt(st.nextToken());
 101:             int green = Integer.parseInt(st.nextToken());
 102:             int blue = Integer.parseInt(st.nextToken());
 103: 
 104:             return new Color(red, green, blue);
 105:           }
 106:         else
 107:           {
 108:             String s2 = (String) colorMap.get(colorName);
 109:             if (s2 == null)
 110:               s2 = colorName;
 111:             return Color.decode(s2);
 112:           }
 113:       }
 114:     catch (Exception nex)
 115:       {
 116:         // Can be either number format exception or illegal argument
 117:         // exception.
 118:         return null;
 119:       }
 120:   }
 121: 
 122:   /**
 123:    * Translate the HTML character attribute to the Swing style constant.
 124:    *
 125:    * @param charAttr the character attributes of the html tag
 126:    * @param t the html tag itself
 127:    * @param a the attribute set where the translated attributes will be stored
 128:    *
 129:    * @return true if some attributes were translated, false otherwise.
 130:    */
 131:   public static boolean translateTag(MutableAttributeSet charAttr,
 132:                                      Tag t, MutableAttributeSet a)
 133:   {
 134:     if(t == Tag.FONT)
 135:       {
 136:         Object color = a.getAttribute(Attribute.COLOR);
 137:         if(color != null)
 138:           {
 139:             Color c = getColor(color.toString());
 140:             if( c == null )
 141:               return false;
 142:             charAttr.addAttribute(StyleConstants.Foreground, c);
 143:             return true;
 144:           }
 145: 
 146:         if(a.getAttribute(Attribute.SIZE) != null)
 147:           {
 148:             // FIXME
 149:             //      charAttr.addAttribute(StyleConstants.FontSize,
 150:             //                            new java.lang.Integer(72));
 151:             return true;
 152:           }
 153:       }
 154: 
 155:     if( t == Tag.B )
 156:       {
 157:         charAttr.addAttribute(StyleConstants.Bold, Boolean.TRUE);
 158:         return true;
 159:       }
 160: 
 161:     if( t == Tag.I )
 162:       {
 163:         charAttr.addAttribute(StyleConstants.Italic, Boolean.TRUE);
 164:         return true;
 165:       }
 166: 
 167:     if( t == Tag.U )
 168:       {
 169:         charAttr.addAttribute(StyleConstants.Underline, Boolean.TRUE);
 170:         return true;
 171:       }
 172: 
 173:     if( t == Tag.STRIKE )
 174:       {
 175:         charAttr.addAttribute(StyleConstants.StrikeThrough, Boolean.TRUE);
 176:         return true;
 177:       }
 178: 
 179:     if( t == Tag.SUP )
 180:       {
 181:         charAttr.addAttribute(StyleConstants.Superscript, Boolean.TRUE);
 182:         return true;
 183:       }
 184: 
 185:     if( t == Tag.SUB )
 186:       {
 187:         charAttr.addAttribute(StyleConstants.Subscript, Boolean.TRUE);
 188:         return true;
 189:       }
 190:     return false;
 191:   }
 192: }