1 /* CharacterAttributeTranslator.java --
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
38 package gnu.javax.swing.text.html;
40 import java.awt.Color;
41 import java.util.HashMap;
42 import java.util.StringTokenizer;
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;
50 * This is a small utility class to translate HTML character attributes to
51 * Swing StyleConstants
53 public class CharacterAttributeTranslator
56 * Maps color name to its hex encoding.
58 private static final HashMap colorMap = new HashMap();
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");
80 * Convert the color string represenation into java.awt.Color. The valid
81 * values are like "aqua" , "#00FFFF" or "rgb(1,6,44)".
83 * @param colorName the color to convert.
84 * @return the matching java.awt.color
86 public static Color getColor(String colorName)
88 colorName = colorName.toLowerCase();
91 if (colorName.startsWith("rgb"))
93 // rgb(red, green, blue) notation.
94 StringTokenizer st = new StringTokenizer(colorName, " ,()");
95 String representation = st.nextToken();
97 // Return null if the representation is not supported.
98 if (! representation.equals("rgb"))
100 int red = Integer.parseInt(st.nextToken());
101 int green = Integer.parseInt(st.nextToken());
102 int blue = Integer.parseInt(st.nextToken());
104 return new Color(red, green, blue);
108 String s2 = (String) colorMap.get(colorName);
111 return Color.decode(s2);
114 catch (Exception nex)
116 // Can be either number format exception or illegal argument
123 * Translate the HTML character attribute to the Swing style constant.
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
129 * @return true if some attributes were translated, false otherwise.
131 public static boolean translateTag(MutableAttributeSet charAttr,
132 Tag t, MutableAttributeSet a)
136 Object color = a.getAttribute(Attribute.COLOR);
139 Color c = getColor(color.toString());
142 charAttr.addAttribute(StyleConstants.Foreground, c);
146 if(a.getAttribute(Attribute.SIZE) != null)
149 // charAttr.addAttribute(StyleConstants.FontSize,
150 // new java.lang.Integer(72));
157 charAttr.addAttribute(StyleConstants.Bold, Boolean.TRUE);
163 charAttr.addAttribute(StyleConstants.Italic, Boolean.TRUE);
169 charAttr.addAttribute(StyleConstants.Underline, Boolean.TRUE);
173 if( t == Tag.STRIKE )
175 charAttr.addAttribute(StyleConstants.StrikeThrough, Boolean.TRUE);
181 charAttr.addAttribute(StyleConstants.Superscript, Boolean.TRUE);
187 charAttr.addAttribute(StyleConstants.Subscript, Boolean.TRUE);