0N/A/*
2362N/A * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage javax.swing.text.html;
0N/A
0N/Aimport java.awt.Color;
0N/Aimport java.awt.Font;
0N/Aimport java.awt.GraphicsEnvironment;
0N/Aimport java.awt.Toolkit;
0N/Aimport java.awt.HeadlessException;
0N/Aimport java.awt.Image;
0N/Aimport java.io.*;
0N/Aimport java.lang.reflect.Method;
0N/Aimport java.net.URL;
0N/Aimport java.net.MalformedURLException;
0N/Aimport java.util.Enumeration;
0N/Aimport java.util.Hashtable;
0N/Aimport java.util.Vector;
0N/Aimport java.util.Locale;
0N/Aimport javax.swing.ImageIcon;
0N/Aimport javax.swing.SizeRequirements;
0N/Aimport javax.swing.text.*;
0N/A
0N/A/**
0N/A * Defines a set of
0N/A * <a href="http://www.w3.org/TR/REC-CSS1">CSS attributes</a>
0N/A * as a typesafe enumeration. The HTML View implementations use
0N/A * CSS attributes to determine how they will render. This also defines
0N/A * methods to map between CSS/HTML/StyleConstants. Any shorthand
0N/A * properties, such as font, are mapped to the intrinsic properties.
0N/A * <p>The following describes the CSS properties that are suppored by the
0N/A * rendering engine:
0N/A * <ul><li>font-family
0N/A * <li>font-style
0N/A * <li>font-size (supports relative units)
0N/A * <li>font-weight
0N/A * <li>font
0N/A * <li>color
0N/A * <li>background-color (with the exception of transparent)
0N/A * <li>background-image
0N/A * <li>background-repeat
0N/A * <li>background-position
0N/A * <li>background
0N/A * <li>text-decoration (with the exception of blink and overline)
0N/A * <li>vertical-align (only sup and super)
0N/A * <li>text-align (justify is treated as center)
0N/A * <li>margin-top
0N/A * <li>margin-right
0N/A * <li>margin-bottom
0N/A * <li>margin-left
0N/A * <li>margin
0N/A * <li>padding-top
0N/A * <li>padding-right
0N/A * <li>padding-bottom
0N/A * <li>padding-left
3961N/A * <li>padding
3961N/A * <li>border-top-style
3961N/A * <li>border-right-style
3961N/A * <li>border-bottom-style
3961N/A * <li>border-left-style
0N/A * <li>border-style (only supports inset, outset and none)
3961N/A * <li>border-top-color
3961N/A * <li>border-right-color
3961N/A * <li>border-bottom-color
3961N/A * <li>border-left-color
3961N/A * <li>border-color
3961N/A * <li>list-style-image
0N/A * <li>list-style-type
0N/A * <li>list-style-position
0N/A * </ul>
0N/A * The following are modeled, but currently not rendered.
0N/A * <ul><li>font-variant
0N/A * <li>background-attachment (background always treated as scroll)
0N/A * <li>word-spacing
0N/A * <li>letter-spacing
0N/A * <li>text-indent
0N/A * <li>text-transform
0N/A * <li>line-height
0N/A * <li>border-top-width (this is used to indicate if a border should be used)
0N/A * <li>border-right-width
0N/A * <li>border-bottom-width
0N/A * <li>border-left-width
0N/A * <li>border-width
0N/A * <li>border-top
0N/A * <li>border-right
0N/A * <li>border-bottom
0N/A * <li>border-left
0N/A * <li>border
0N/A * <li>width
0N/A * <li>height
0N/A * <li>float
0N/A * <li>clear
0N/A * <li>display
0N/A * <li>white-space
0N/A * <li>list-style
0N/A * </ul>
0N/A * <p><b>Note: for the time being we do not fully support relative units,
0N/A * unless noted, so that
0N/A * p { margin-top: 10% } will be treated as if no margin-top was specified.
0N/A *
0N/A * @author Timothy Prinzing
0N/A * @author Scott Violet
0N/A * @see StyleSheet
0N/A */
0N/Apublic class CSS implements Serializable {
0N/A
0N/A /**
0N/A * Definitions to be used as a key on AttributeSet's
0N/A * that might hold CSS attributes. Since this is a
0N/A * closed set (i.e. defined exactly by the specification),
0N/A * it is final and cannot be extended.
0N/A */
0N/A public static final class Attribute {
0N/A
0N/A private Attribute(String name, String defaultValue, boolean inherited) {
0N/A this.name = name;
0N/A this.defaultValue = defaultValue;
0N/A this.inherited = inherited;
0N/A }
0N/A
0N/A /**
0N/A * The string representation of the attribute. This
0N/A * should exactly match the string specified in the
0N/A * CSS specification.
0N/A */
0N/A public String toString() {
0N/A return name;
0N/A }
0N/A
0N/A /**
0N/A * Fetch the default value for the attribute.
0N/A * If there is no default value (such as for
0N/A * composite attributes), null will be returned.
0N/A */
0N/A public String getDefaultValue() {
0N/A return defaultValue;
0N/A }
0N/A
0N/A /**
0N/A * Indicates if the attribute should be inherited
0N/A * from the parent or not.
0N/A */
0N/A public boolean isInherited() {
0N/A return inherited;
0N/A }
0N/A
0N/A private String name;
0N/A private String defaultValue;
0N/A private boolean inherited;
0N/A
0N/A
0N/A public static final Attribute BACKGROUND =
0N/A new Attribute("background", null, false);
0N/A
0N/A public static final Attribute BACKGROUND_ATTACHMENT =
0N/A new Attribute("background-attachment", "scroll", false);
0N/A
0N/A public static final Attribute BACKGROUND_COLOR =
0N/A new Attribute("background-color", "transparent", false);
0N/A
0N/A public static final Attribute BACKGROUND_IMAGE =
0N/A new Attribute("background-image", "none", false);
0N/A
0N/A public static final Attribute BACKGROUND_POSITION =
0N/A new Attribute("background-position", null, false);
0N/A
0N/A public static final Attribute BACKGROUND_REPEAT =
0N/A new Attribute("background-repeat", "repeat", false);
0N/A
0N/A public static final Attribute BORDER =
0N/A new Attribute("border", null, false);
0N/A
0N/A public static final Attribute BORDER_BOTTOM =
0N/A new Attribute("border-bottom", null, false);
0N/A
0N/A public static final Attribute BORDER_BOTTOM_COLOR =
0N/A new Attribute("border-bottom-color", null, false);
0N/A
0N/A public static final Attribute BORDER_BOTTOM_STYLE =
0N/A new Attribute("border-bottom-style", "none", false);
0N/A
0N/A public static final Attribute BORDER_BOTTOM_WIDTH =
0N/A new Attribute("border-bottom-width", "medium", false);
0N/A
0N/A public static final Attribute BORDER_COLOR =
0N/A new Attribute("border-color", null, false);
0N/A
0N/A public static final Attribute BORDER_LEFT =
0N/A new Attribute("border-left", null, false);
0N/A
0N/A public static final Attribute BORDER_LEFT_COLOR =
0N/A new Attribute("border-left-color", null, false);
0N/A
0N/A public static final Attribute BORDER_LEFT_STYLE =
0N/A new Attribute("border-left-style", "none", false);
0N/A
0N/A public static final Attribute BORDER_LEFT_WIDTH =
0N/A new Attribute("border-left-width", "medium", false);
0N/A
0N/A public static final Attribute BORDER_RIGHT =
0N/A new Attribute("border-right", null, false);
0N/A
0N/A public static final Attribute BORDER_RIGHT_COLOR =
0N/A new Attribute("border-right-color", null, false);
0N/A
0N/A public static final Attribute BORDER_RIGHT_STYLE =
0N/A new Attribute("border-right-style", "none", false);
0N/A
0N/A public static final Attribute BORDER_RIGHT_WIDTH =
0N/A new Attribute("border-right-width", "medium", false);
0N/A
0N/A public static final Attribute BORDER_STYLE =
0N/A new Attribute("border-style", "none", false);
0N/A
0N/A public static final Attribute BORDER_TOP =
0N/A new Attribute("border-top", null, false);
0N/A
0N/A public static final Attribute BORDER_TOP_COLOR =
0N/A new Attribute("border-top-color", null, false);
0N/A
0N/A public static final Attribute BORDER_TOP_STYLE =
0N/A new Attribute("border-top-style", "none", false);
0N/A
0N/A public static final Attribute BORDER_TOP_WIDTH =
0N/A new Attribute("border-top-width", "medium", false);
0N/A
0N/A public static final Attribute BORDER_WIDTH =
0N/A new Attribute("border-width", "medium", false);
0N/A
0N/A public static final Attribute CLEAR =
0N/A new Attribute("clear", "none", false);
0N/A
0N/A public static final Attribute COLOR =
0N/A new Attribute("color", "black", true);
0N/A
0N/A public static final Attribute DISPLAY =
0N/A new Attribute("display", "block", false);
0N/A
0N/A public static final Attribute FLOAT =
0N/A new Attribute("float", "none", false);
0N/A
0N/A public static final Attribute FONT =
0N/A new Attribute("font", null, true);
0N/A
0N/A public static final Attribute FONT_FAMILY =
0N/A new Attribute("font-family", null, true);
0N/A
0N/A public static final Attribute FONT_SIZE =
0N/A new Attribute("font-size", "medium", true);
0N/A
0N/A public static final Attribute FONT_STYLE =
0N/A new Attribute("font-style", "normal", true);
0N/A
0N/A public static final Attribute FONT_VARIANT =
0N/A new Attribute("font-variant", "normal", true);
0N/A
0N/A public static final Attribute FONT_WEIGHT =
0N/A new Attribute("font-weight", "normal", true);
0N/A
0N/A public static final Attribute HEIGHT =
0N/A new Attribute("height", "auto", false);
0N/A
0N/A public static final Attribute LETTER_SPACING =
0N/A new Attribute("letter-spacing", "normal", true);
0N/A
0N/A public static final Attribute LINE_HEIGHT =
0N/A new Attribute("line-height", "normal", true);
0N/A
0N/A public static final Attribute LIST_STYLE =
0N/A new Attribute("list-style", null, true);
0N/A
0N/A public static final Attribute LIST_STYLE_IMAGE =
0N/A new Attribute("list-style-image", "none", true);
0N/A
0N/A public static final Attribute LIST_STYLE_POSITION =
0N/A new Attribute("list-style-position", "outside", true);
0N/A
0N/A public static final Attribute LIST_STYLE_TYPE =
0N/A new Attribute("list-style-type", "disc", true);
0N/A
0N/A public static final Attribute MARGIN =
0N/A new Attribute("margin", null, false);
0N/A
0N/A public static final Attribute MARGIN_BOTTOM =
0N/A new Attribute("margin-bottom", "0", false);
0N/A
0N/A public static final Attribute MARGIN_LEFT =
0N/A new Attribute("margin-left", "0", false);
0N/A
0N/A public static final Attribute MARGIN_RIGHT =
0N/A new Attribute("margin-right", "0", false);
0N/A
0N/A /*
0N/A * made up css attributes to describe orientation depended
0N/A * margins. used for <dir>, <menu>, <ul> etc. see
0N/A * 5088268 for more details
0N/A */
0N/A static final Attribute MARGIN_LEFT_LTR =
0N/A new Attribute("margin-left-ltr",
0N/A Integer.toString(Integer.MIN_VALUE), false);
0N/A
0N/A static final Attribute MARGIN_LEFT_RTL =
0N/A new Attribute("margin-left-rtl",
0N/A Integer.toString(Integer.MIN_VALUE), false);
0N/A
0N/A static final Attribute MARGIN_RIGHT_LTR =
0N/A new Attribute("margin-right-ltr",
0N/A Integer.toString(Integer.MIN_VALUE), false);
0N/A
0N/A static final Attribute MARGIN_RIGHT_RTL =
0N/A new Attribute("margin-right-rtl",
0N/A Integer.toString(Integer.MIN_VALUE), false);
0N/A
0N/A
0N/A public static final Attribute MARGIN_TOP =
0N/A new Attribute("margin-top", "0", false);
0N/A
0N/A public static final Attribute PADDING =
0N/A new Attribute("padding", null, false);
0N/A
0N/A public static final Attribute PADDING_BOTTOM =
0N/A new Attribute("padding-bottom", "0", false);
0N/A
0N/A public static final Attribute PADDING_LEFT =
0N/A new Attribute("padding-left", "0", false);
0N/A
0N/A public static final Attribute PADDING_RIGHT =
0N/A new Attribute("padding-right", "0", false);
0N/A
0N/A public static final Attribute PADDING_TOP =
0N/A new Attribute("padding-top", "0", false);
0N/A
0N/A public static final Attribute TEXT_ALIGN =
0N/A new Attribute("text-align", null, true);
0N/A
0N/A public static final Attribute TEXT_DECORATION =
0N/A new Attribute("text-decoration", "none", true);
0N/A
0N/A public static final Attribute TEXT_INDENT =
0N/A new Attribute("text-indent", "0", true);
0N/A
0N/A public static final Attribute TEXT_TRANSFORM =
0N/A new Attribute("text-transform", "none", true);
0N/A
0N/A public static final Attribute VERTICAL_ALIGN =
0N/A new Attribute("vertical-align", "baseline", false);
0N/A
0N/A public static final Attribute WORD_SPACING =
0N/A new Attribute("word-spacing", "normal", true);
0N/A
0N/A public static final Attribute WHITE_SPACE =
0N/A new Attribute("white-space", "normal", true);
0N/A
0N/A public static final Attribute WIDTH =
0N/A new Attribute("width", "auto", false);
0N/A
0N/A /*public*/ static final Attribute BORDER_SPACING =
0N/A new Attribute("border-spacing", "0", true);
0N/A
0N/A /*public*/ static final Attribute CAPTION_SIDE =
0N/A new Attribute("caption-side", "left", true);
0N/A
0N/A // All possible CSS attribute keys.
0N/A static final Attribute[] allAttributes = {
0N/A BACKGROUND, BACKGROUND_ATTACHMENT, BACKGROUND_COLOR,
0N/A BACKGROUND_IMAGE, BACKGROUND_POSITION, BACKGROUND_REPEAT,
0N/A BORDER, BORDER_BOTTOM, BORDER_BOTTOM_WIDTH, BORDER_COLOR,
0N/A BORDER_LEFT, BORDER_LEFT_WIDTH, BORDER_RIGHT, BORDER_RIGHT_WIDTH,
0N/A BORDER_STYLE, BORDER_TOP, BORDER_TOP_WIDTH, BORDER_WIDTH,
0N/A BORDER_TOP_STYLE, BORDER_RIGHT_STYLE, BORDER_BOTTOM_STYLE,
0N/A BORDER_LEFT_STYLE,
0N/A BORDER_TOP_COLOR, BORDER_RIGHT_COLOR, BORDER_BOTTOM_COLOR,
0N/A BORDER_LEFT_COLOR,
0N/A CLEAR, COLOR, DISPLAY, FLOAT, FONT, FONT_FAMILY, FONT_SIZE,
0N/A FONT_STYLE, FONT_VARIANT, FONT_WEIGHT, HEIGHT, LETTER_SPACING,
0N/A LINE_HEIGHT, LIST_STYLE, LIST_STYLE_IMAGE, LIST_STYLE_POSITION,
0N/A LIST_STYLE_TYPE, MARGIN, MARGIN_BOTTOM, MARGIN_LEFT, MARGIN_RIGHT,
0N/A MARGIN_TOP, PADDING, PADDING_BOTTOM, PADDING_LEFT, PADDING_RIGHT,
0N/A PADDING_TOP, TEXT_ALIGN, TEXT_DECORATION, TEXT_INDENT, TEXT_TRANSFORM,
0N/A VERTICAL_ALIGN, WORD_SPACING, WHITE_SPACE, WIDTH,
0N/A BORDER_SPACING, CAPTION_SIDE,
0N/A MARGIN_LEFT_LTR, MARGIN_LEFT_RTL, MARGIN_RIGHT_LTR, MARGIN_RIGHT_RTL
0N/A };
0N/A
0N/A private static final Attribute[] ALL_MARGINS =
0N/A { MARGIN_TOP, MARGIN_RIGHT, MARGIN_BOTTOM, MARGIN_LEFT };
0N/A private static final Attribute[] ALL_PADDING =
0N/A { PADDING_TOP, PADDING_RIGHT, PADDING_BOTTOM, PADDING_LEFT };
0N/A private static final Attribute[] ALL_BORDER_WIDTHS =
0N/A { BORDER_TOP_WIDTH, BORDER_RIGHT_WIDTH, BORDER_BOTTOM_WIDTH,
0N/A BORDER_LEFT_WIDTH };
0N/A private static final Attribute[] ALL_BORDER_STYLES =
0N/A { BORDER_TOP_STYLE, BORDER_RIGHT_STYLE, BORDER_BOTTOM_STYLE,
0N/A BORDER_LEFT_STYLE };
0N/A private static final Attribute[] ALL_BORDER_COLORS =
0N/A { BORDER_TOP_COLOR, BORDER_RIGHT_COLOR, BORDER_BOTTOM_COLOR,
0N/A BORDER_LEFT_COLOR };
0N/A
0N/A }
0N/A
0N/A static final class Value {
0N/A
0N/A private Value(String name) {
0N/A this.name = name;
0N/A }
0N/A
0N/A /**
0N/A * The string representation of the attribute. This
0N/A * should exactly match the string specified in the
0N/A * CSS specification.
0N/A */
0N/A public String toString() {
0N/A return name;
0N/A }
0N/A
0N/A static final Value INHERITED = new Value("inherited");
0N/A static final Value NONE = new Value("none");
0N/A static final Value HIDDEN = new Value("hidden");
0N/A static final Value DOTTED = new Value("dotted");
0N/A static final Value DASHED = new Value("dashed");
0N/A static final Value SOLID = new Value("solid");
0N/A static final Value DOUBLE = new Value("double");
0N/A static final Value GROOVE = new Value("groove");
0N/A static final Value RIDGE = new Value("ridge");
0N/A static final Value INSET = new Value("inset");
0N/A static final Value OUTSET = new Value("outset");
0N/A // Lists.
0N/A static final Value DISC = new Value("disc");
0N/A static final Value CIRCLE = new Value("circle");
0N/A static final Value SQUARE = new Value("square");
0N/A static final Value DECIMAL = new Value("decimal");
0N/A static final Value LOWER_ROMAN = new Value("lower-roman");
0N/A static final Value UPPER_ROMAN = new Value("upper-roman");
0N/A static final Value LOWER_ALPHA = new Value("lower-alpha");
0N/A static final Value UPPER_ALPHA = new Value("upper-alpha");
0N/A // background-repeat
0N/A static final Value BACKGROUND_NO_REPEAT = new Value("no-repeat");
0N/A static final Value BACKGROUND_REPEAT = new Value("repeat");
0N/A static final Value BACKGROUND_REPEAT_X = new Value("repeat-x");
0N/A static final Value BACKGROUND_REPEAT_Y = new Value("repeat-y");
0N/A // background-attachment
0N/A static final Value BACKGROUND_SCROLL = new Value("scroll");
0N/A static final Value BACKGROUND_FIXED = new Value("fixed");
0N/A
0N/A private String name;
0N/A
0N/A static final Value[] allValues = {
0N/A INHERITED, NONE, DOTTED, DASHED, SOLID, DOUBLE, GROOVE,
0N/A RIDGE, INSET, OUTSET, DISC, CIRCLE, SQUARE, DECIMAL,
0N/A LOWER_ROMAN, UPPER_ROMAN, LOWER_ALPHA, UPPER_ALPHA,
0N/A BACKGROUND_NO_REPEAT, BACKGROUND_REPEAT,
0N/A BACKGROUND_REPEAT_X, BACKGROUND_REPEAT_Y,
0N/A BACKGROUND_FIXED, BACKGROUND_FIXED
0N/A };
0N/A }
0N/A
0N/A public CSS() {
0N/A baseFontSize = baseFontSizeIndex + 1;
0N/A // setup the css conversion table
611N/A valueConvertor = new Hashtable<Object, Object>();
0N/A valueConvertor.put(CSS.Attribute.FONT_SIZE, new FontSize());
0N/A valueConvertor.put(CSS.Attribute.FONT_FAMILY, new FontFamily());
0N/A valueConvertor.put(CSS.Attribute.FONT_WEIGHT, new FontWeight());
0N/A Object bs = new BorderStyle();
0N/A valueConvertor.put(CSS.Attribute.BORDER_TOP_STYLE, bs);
0N/A valueConvertor.put(CSS.Attribute.BORDER_RIGHT_STYLE, bs);
0N/A valueConvertor.put(CSS.Attribute.BORDER_BOTTOM_STYLE, bs);
0N/A valueConvertor.put(CSS.Attribute.BORDER_LEFT_STYLE, bs);
0N/A Object cv = new ColorValue();
0N/A valueConvertor.put(CSS.Attribute.COLOR, cv);
0N/A valueConvertor.put(CSS.Attribute.BACKGROUND_COLOR, cv);
0N/A valueConvertor.put(CSS.Attribute.BORDER_TOP_COLOR, cv);
0N/A valueConvertor.put(CSS.Attribute.BORDER_RIGHT_COLOR, cv);
0N/A valueConvertor.put(CSS.Attribute.BORDER_BOTTOM_COLOR, cv);
0N/A valueConvertor.put(CSS.Attribute.BORDER_LEFT_COLOR, cv);
0N/A Object lv = new LengthValue();
0N/A valueConvertor.put(CSS.Attribute.MARGIN_TOP, lv);
0N/A valueConvertor.put(CSS.Attribute.MARGIN_BOTTOM, lv);
0N/A valueConvertor.put(CSS.Attribute.MARGIN_LEFT, lv);
0N/A valueConvertor.put(CSS.Attribute.MARGIN_LEFT_LTR, lv);
0N/A valueConvertor.put(CSS.Attribute.MARGIN_LEFT_RTL, lv);
0N/A valueConvertor.put(CSS.Attribute.MARGIN_RIGHT, lv);
0N/A valueConvertor.put(CSS.Attribute.MARGIN_RIGHT_LTR, lv);
0N/A valueConvertor.put(CSS.Attribute.MARGIN_RIGHT_RTL, lv);
0N/A valueConvertor.put(CSS.Attribute.PADDING_TOP, lv);
0N/A valueConvertor.put(CSS.Attribute.PADDING_BOTTOM, lv);
0N/A valueConvertor.put(CSS.Attribute.PADDING_LEFT, lv);
0N/A valueConvertor.put(CSS.Attribute.PADDING_RIGHT, lv);
0N/A Object bv = new BorderWidthValue(null, 0);
0N/A valueConvertor.put(CSS.Attribute.BORDER_TOP_WIDTH, bv);
0N/A valueConvertor.put(CSS.Attribute.BORDER_BOTTOM_WIDTH, bv);
0N/A valueConvertor.put(CSS.Attribute.BORDER_LEFT_WIDTH, bv);
0N/A valueConvertor.put(CSS.Attribute.BORDER_RIGHT_WIDTH, bv);
0N/A Object nlv = new LengthValue(true);
0N/A valueConvertor.put(CSS.Attribute.TEXT_INDENT, nlv);
0N/A valueConvertor.put(CSS.Attribute.WIDTH, lv);
0N/A valueConvertor.put(CSS.Attribute.HEIGHT, lv);
0N/A valueConvertor.put(CSS.Attribute.BORDER_SPACING, lv);
0N/A Object sv = new StringValue();
0N/A valueConvertor.put(CSS.Attribute.FONT_STYLE, sv);
0N/A valueConvertor.put(CSS.Attribute.TEXT_DECORATION, sv);
0N/A valueConvertor.put(CSS.Attribute.TEXT_ALIGN, sv);
0N/A valueConvertor.put(CSS.Attribute.VERTICAL_ALIGN, sv);
0N/A Object valueMapper = new CssValueMapper();
0N/A valueConvertor.put(CSS.Attribute.LIST_STYLE_TYPE,
0N/A valueMapper);
0N/A valueConvertor.put(CSS.Attribute.BACKGROUND_IMAGE,
0N/A new BackgroundImage());
0N/A valueConvertor.put(CSS.Attribute.BACKGROUND_POSITION,
0N/A new BackgroundPosition());
0N/A valueConvertor.put(CSS.Attribute.BACKGROUND_REPEAT,
0N/A valueMapper);
0N/A valueConvertor.put(CSS.Attribute.BACKGROUND_ATTACHMENT,
0N/A valueMapper);
0N/A Object generic = new CssValue();
0N/A int n = CSS.Attribute.allAttributes.length;
0N/A for (int i = 0; i < n; i++) {
0N/A CSS.Attribute key = CSS.Attribute.allAttributes[i];
0N/A if (valueConvertor.get(key) == null) {
0N/A valueConvertor.put(key, generic);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Sets the base font size. <code>sz</code> is a CSS value, and is
0N/A * not necessarily the point size. Use getPointSize to determine the
0N/A * point size corresponding to <code>sz</code>.
0N/A */
0N/A void setBaseFontSize(int sz) {
0N/A if (sz < 1)
0N/A baseFontSize = 0;
0N/A else if (sz > 7)
0N/A baseFontSize = 7;
0N/A else
0N/A baseFontSize = sz;
0N/A }
0N/A
0N/A /**
0N/A * Sets the base font size from the passed in string.
0N/A */
0N/A void setBaseFontSize(String size) {
0N/A int relSize, absSize, diff;
0N/A
0N/A if (size != null) {
0N/A if (size.startsWith("+")) {
0N/A relSize = Integer.valueOf(size.substring(1)).intValue();
0N/A setBaseFontSize(baseFontSize + relSize);
0N/A } else if (size.startsWith("-")) {
0N/A relSize = -Integer.valueOf(size.substring(1)).intValue();
0N/A setBaseFontSize(baseFontSize + relSize);
0N/A } else {
0N/A setBaseFontSize(Integer.valueOf(size).intValue());
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the base font size.
0N/A */
0N/A int getBaseFontSize() {
0N/A return baseFontSize;
0N/A }
0N/A
0N/A /**
0N/A * Parses the CSS property <code>key</code> with value
0N/A * <code>value</code> placing the result in <code>att</code>.
0N/A */
0N/A void addInternalCSSValue(MutableAttributeSet attr,
0N/A CSS.Attribute key, String value) {
0N/A if (key == CSS.Attribute.FONT) {
0N/A ShorthandFontParser.parseShorthandFont(this, value, attr);
0N/A }
0N/A else if (key == CSS.Attribute.BACKGROUND) {
0N/A ShorthandBackgroundParser.parseShorthandBackground
0N/A (this, value, attr);
0N/A }
0N/A else if (key == CSS.Attribute.MARGIN) {
0N/A ShorthandMarginParser.parseShorthandMargin(this, value, attr,
0N/A CSS.Attribute.ALL_MARGINS);
0N/A }
0N/A else if (key == CSS.Attribute.PADDING) {
0N/A ShorthandMarginParser.parseShorthandMargin(this, value, attr,
0N/A CSS.Attribute.ALL_PADDING);
0N/A }
0N/A else if (key == CSS.Attribute.BORDER_WIDTH) {
0N/A ShorthandMarginParser.parseShorthandMargin(this, value, attr,
0N/A CSS.Attribute.ALL_BORDER_WIDTHS);
0N/A }
0N/A else if (key == CSS.Attribute.BORDER_COLOR) {
0N/A ShorthandMarginParser.parseShorthandMargin(this, value, attr,
0N/A CSS.Attribute.ALL_BORDER_COLORS);
0N/A }
0N/A else if (key == CSS.Attribute.BORDER_STYLE) {
0N/A ShorthandMarginParser.parseShorthandMargin(this, value, attr,
0N/A CSS.Attribute.ALL_BORDER_STYLES);
0N/A }
0N/A else if ((key == CSS.Attribute.BORDER) ||
0N/A (key == CSS.Attribute.BORDER_TOP) ||
0N/A (key == CSS.Attribute.BORDER_RIGHT) ||
0N/A (key == CSS.Attribute.BORDER_BOTTOM) ||
0N/A (key == CSS.Attribute.BORDER_LEFT)) {
0N/A ShorthandBorderParser.parseShorthandBorder(attr, key, value);
0N/A }
0N/A else {
0N/A Object iValue = getInternalCSSValue(key, value);
0N/A if (iValue != null) {
0N/A attr.addAttribute(key, iValue);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Gets the internal CSS representation of <code>value</code> which is
0N/A * a CSS value of the CSS attribute named <code>key</code>. The receiver
0N/A * should not modify <code>value</code>, and the first <code>count</code>
0N/A * strings are valid.
0N/A */
0N/A Object getInternalCSSValue(CSS.Attribute key, String value) {
0N/A CssValue conv = (CssValue) valueConvertor.get(key);
0N/A Object r = conv.parseCssValue(value);
0N/A return r != null ? r : conv.parseCssValue(key.getDefaultValue());
0N/A }
0N/A
0N/A /**
0N/A * Maps from a StyleConstants to a CSS Attribute.
0N/A */
0N/A Attribute styleConstantsKeyToCSSKey(StyleConstants sc) {
611N/A return styleConstantToCssMap.get(sc);
0N/A }
0N/A
0N/A /**
0N/A * Maps from a StyleConstants value to a CSS value.
0N/A */
0N/A Object styleConstantsValueToCSSValue(StyleConstants sc,
0N/A Object styleValue) {
611N/A Attribute cssKey = styleConstantsKeyToCSSKey(sc);
0N/A if (cssKey != null) {
0N/A CssValue conv = (CssValue)valueConvertor.get(cssKey);
0N/A return conv.fromStyleConstants(sc, styleValue);
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Converts the passed in CSS value to a StyleConstants value.
0N/A * <code>key</code> identifies the CSS attribute being mapped.
0N/A */
0N/A Object cssValueToStyleConstantsValue(StyleConstants key, Object value) {
0N/A if (value instanceof CssValue) {
611N/A return ((CssValue)value).toStyleConstants(key, null);
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns the font for the values in the passed in AttributeSet.
0N/A * It is assumed the keys will be CSS.Attribute keys.
0N/A * <code>sc</code> is the StyleContext that will be messaged to get
0N/A * the font once the size, name and style have been determined.
0N/A */
0N/A Font getFont(StyleContext sc, AttributeSet a, int defaultSize, StyleSheet ss) {
0N/A ss = getStyleSheet(ss);
0N/A int size = getFontSize(a, defaultSize, ss);
0N/A
0N/A /*
0N/A * If the vertical alignment is set to either superscirpt or
0N/A * subscript we reduce the font size by 2 points.
0N/A */
0N/A StringValue vAlignV = (StringValue)a.getAttribute
0N/A (CSS.Attribute.VERTICAL_ALIGN);
0N/A if ((vAlignV != null)) {
0N/A String vAlign = vAlignV.toString();
0N/A if ((vAlign.indexOf("sup") >= 0) ||
0N/A (vAlign.indexOf("sub") >= 0)) {
0N/A size -= 2;
0N/A }
0N/A }
0N/A
0N/A FontFamily familyValue = (FontFamily)a.getAttribute
0N/A (CSS.Attribute.FONT_FAMILY);
0N/A String family = (familyValue != null) ? familyValue.getValue() :
0N/A Font.SANS_SERIF;
0N/A int style = Font.PLAIN;
0N/A FontWeight weightValue = (FontWeight) a.getAttribute
0N/A (CSS.Attribute.FONT_WEIGHT);
0N/A if ((weightValue != null) && (weightValue.getValue() > 400)) {
0N/A style |= Font.BOLD;
0N/A }
0N/A Object fs = a.getAttribute(CSS.Attribute.FONT_STYLE);
0N/A if ((fs != null) && (fs.toString().indexOf("italic") >= 0)) {
0N/A style |= Font.ITALIC;
0N/A }
0N/A if (family.equalsIgnoreCase("monospace")) {
0N/A family = Font.MONOSPACED;
0N/A }
0N/A Font f = sc.getFont(family, style, size);
0N/A if (f == null
0N/A || (f.getFamily().equals(Font.DIALOG)
0N/A && ! family.equalsIgnoreCase(Font.DIALOG))) {
0N/A family = Font.SANS_SERIF;
0N/A f = sc.getFont(family, style, size);
0N/A }
0N/A return f;
0N/A }
0N/A
0N/A static int getFontSize(AttributeSet attr, int defaultSize, StyleSheet ss) {
0N/A // PENDING(prinz) this is a 1.1 based implementation, need to also
0N/A // have a 1.2 version.
0N/A FontSize sizeValue = (FontSize)attr.getAttribute(CSS.Attribute.
0N/A FONT_SIZE);
0N/A
0N/A return (sizeValue != null) ? sizeValue.getValue(attr, ss)
0N/A : defaultSize;
0N/A }
0N/A
0N/A /**
0N/A * Takes a set of attributes and turn it into a color
0N/A * specification. This might be used to specify things
0N/A * like brighter, more hue, etc.
0N/A * This will return null if there is no value for <code>key</code>.
0N/A *
0N/A * @param key CSS.Attribute identifying where color is stored.
0N/A * @param a the set of attributes
0N/A * @return the color
0N/A */
0N/A Color getColor(AttributeSet a, CSS.Attribute key) {
0N/A ColorValue cv = (ColorValue) a.getAttribute(key);
0N/A if (cv != null) {
0N/A return cv.getValue();
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns the size of a font from the passed in string.
0N/A *
0N/A * @param size CSS string describing font size
0N/A * @param baseFontSize size to use for relative units.
0N/A */
0N/A float getPointSize(String size, StyleSheet ss) {
0N/A int relSize, absSize, diff, index;
0N/A ss = getStyleSheet(ss);
0N/A if (size != null) {
0N/A if (size.startsWith("+")) {
0N/A relSize = Integer.valueOf(size.substring(1)).intValue();
0N/A return getPointSize(baseFontSize + relSize, ss);
0N/A } else if (size.startsWith("-")) {
0N/A relSize = -Integer.valueOf(size.substring(1)).intValue();
0N/A return getPointSize(baseFontSize + relSize, ss);
0N/A } else {
0N/A absSize = Integer.valueOf(size).intValue();
0N/A return getPointSize(absSize, ss);
0N/A }
0N/A }
0N/A return 0;
0N/A }
0N/A
0N/A /**
0N/A * Returns the length of the attribute in <code>a</code> with
0N/A * key <code>key</code>.
0N/A */
0N/A float getLength(AttributeSet a, CSS.Attribute key, StyleSheet ss) {
0N/A ss = getStyleSheet(ss);
0N/A LengthValue lv = (LengthValue) a.getAttribute(key);
0N/A boolean isW3CLengthUnits = (ss == null) ? false : ss.isW3CLengthUnits();
0N/A float len = (lv != null) ? lv.getValue(isW3CLengthUnits) : 0;
0N/A return len;
0N/A }
0N/A
0N/A /**
0N/A * Convert a set of HTML attributes to an equivalent
0N/A * set of CSS attributes.
0N/A *
611N/A * @param htmlAttrSet AttributeSet containing the HTML attributes.
0N/A * @return AttributeSet containing the corresponding CSS attributes.
0N/A * The AttributeSet will be empty if there are no mapping
0N/A * CSS attributes.
0N/A */
0N/A AttributeSet translateHTMLToCSS(AttributeSet htmlAttrSet) {
0N/A MutableAttributeSet cssAttrSet = new SimpleAttributeSet();
0N/A Element elem = (Element)htmlAttrSet;
0N/A HTML.Tag tag = getHTMLTag(htmlAttrSet);
0N/A if ((tag == HTML.Tag.TD) || (tag == HTML.Tag.TH)) {
0N/A // translate border width into the cells, if it has non-zero value.
0N/A AttributeSet tableAttr = elem.getParentElement().
0N/A getParentElement().getAttributes();
4379N/A
4379N/A int borderWidth = getTableBorder(tableAttr);
0N/A if (borderWidth > 0) {
4379N/A // If table contains the BORDER attribute cells should have border width equals 1
4379N/A translateAttribute(HTML.Attribute.BORDER, "1", cssAttrSet);
0N/A }
0N/A String pad = (String)tableAttr.getAttribute(HTML.Attribute.CELLPADDING);
0N/A if (pad != null) {
0N/A LengthValue v =
0N/A (LengthValue)getInternalCSSValue(CSS.Attribute.PADDING_TOP, pad);
0N/A v.span = (v.span < 0) ? 0 : v.span;
0N/A cssAttrSet.addAttribute(CSS.Attribute.PADDING_TOP, v);
0N/A cssAttrSet.addAttribute(CSS.Attribute.PADDING_BOTTOM, v);
0N/A cssAttrSet.addAttribute(CSS.Attribute.PADDING_LEFT, v);
0N/A cssAttrSet.addAttribute(CSS.Attribute.PADDING_RIGHT, v);
0N/A }
0N/A }
0N/A if (elem.isLeaf()) {
0N/A translateEmbeddedAttributes(htmlAttrSet, cssAttrSet);
0N/A } else {
0N/A translateAttributes(tag, htmlAttrSet, cssAttrSet);
0N/A }
0N/A if (tag == HTML.Tag.CAPTION) {
0N/A /*
0N/A * Navigator uses ALIGN for caption placement and IE uses VALIGN.
0N/A */
0N/A Object v = htmlAttrSet.getAttribute(HTML.Attribute.ALIGN);
0N/A if ((v != null) && (v.equals("top") || v.equals("bottom"))) {
0N/A cssAttrSet.addAttribute(CSS.Attribute.CAPTION_SIDE, v);
0N/A cssAttrSet.removeAttribute(CSS.Attribute.TEXT_ALIGN);
0N/A } else {
0N/A v = htmlAttrSet.getAttribute(HTML.Attribute.VALIGN);
0N/A if (v != null) {
0N/A cssAttrSet.addAttribute(CSS.Attribute.CAPTION_SIDE, v);
0N/A }
0N/A }
0N/A }
0N/A return cssAttrSet;
0N/A }
0N/A
4379N/A private static int getTableBorder(AttributeSet tableAttr) {
4379N/A String borderValue = (String) tableAttr.getAttribute(HTML.Attribute.BORDER);
4379N/A
4379N/A if (borderValue == HTML.NULL_ATTRIBUTE_VALUE || "".equals(borderValue)) {
4379N/A // Some browsers accept <TABLE BORDER> and <TABLE BORDER=""> with the same semantics as BORDER=1
4379N/A return 1;
4379N/A }
4379N/A
4379N/A try {
4379N/A return Integer.parseInt(borderValue);
4379N/A } catch (NumberFormatException e) {
4379N/A return 0;
4379N/A }
4379N/A }
4379N/A
611N/A private static final Hashtable<String, Attribute> attributeMap = new Hashtable<String, Attribute>();
611N/A private static final Hashtable<String, Value> valueMap = new Hashtable<String, Value>();
0N/A
0N/A /**
0N/A * The hashtable and the static initalization block below,
0N/A * set up a mapping from well-known HTML attributes to
0N/A * CSS attributes. For the most part, there is a 1-1 mapping
0N/A * between the two. However in the case of certain HTML
0N/A * attributes for example HTML.Attribute.VSPACE or
0N/A * HTML.Attribute.HSPACE, end up mapping to two CSS.Attribute's.
0N/A * Therefore, the value associated with each HTML.Attribute.
0N/A * key ends up being an array of CSS.Attribute.* objects.
0N/A */
611N/A private static final Hashtable<HTML.Attribute, CSS.Attribute[]> htmlAttrToCssAttrMap = new Hashtable<HTML.Attribute, CSS.Attribute[]>(20);
0N/A
0N/A /**
0N/A * The hashtable and static initialization that follows sets
0N/A * up a translation from StyleConstants (i.e. the <em>well known</em>
0N/A * attributes) to the associated CSS attributes.
0N/A */
611N/A private static final Hashtable<Object, Attribute> styleConstantToCssMap = new Hashtable<Object, Attribute>(17);
0N/A /** Maps from HTML value to a CSS value. Used in internal mapping. */
611N/A private static final Hashtable<String, CSS.Value> htmlValueToCssValueMap = new Hashtable<String, CSS.Value>(8);
0N/A /** Maps from CSS value (string) to internal value. */
611N/A private static final Hashtable<String, CSS.Value> cssValueToInternalValueMap = new Hashtable<String, CSS.Value>(13);
0N/A
0N/A static {
0N/A // load the attribute map
0N/A for (int i = 0; i < Attribute.allAttributes.length; i++ ) {
0N/A attributeMap.put(Attribute.allAttributes[i].toString(),
0N/A Attribute.allAttributes[i]);
0N/A }
0N/A // load the value map
0N/A for (int i = 0; i < Value.allValues.length; i++ ) {
0N/A valueMap.put(Value.allValues[i].toString(),
0N/A Value.allValues[i]);
0N/A }
0N/A
0N/A htmlAttrToCssAttrMap.put(HTML.Attribute.COLOR,
0N/A new CSS.Attribute[]{CSS.Attribute.COLOR});
0N/A htmlAttrToCssAttrMap.put(HTML.Attribute.TEXT,
0N/A new CSS.Attribute[]{CSS.Attribute.COLOR});
0N/A htmlAttrToCssAttrMap.put(HTML.Attribute.CLEAR,
0N/A new CSS.Attribute[]{CSS.Attribute.CLEAR});
0N/A htmlAttrToCssAttrMap.put(HTML.Attribute.BACKGROUND,
0N/A new CSS.Attribute[]{CSS.Attribute.BACKGROUND_IMAGE});
0N/A htmlAttrToCssAttrMap.put(HTML.Attribute.BGCOLOR,
0N/A new CSS.Attribute[]{CSS.Attribute.BACKGROUND_COLOR});
0N/A htmlAttrToCssAttrMap.put(HTML.Attribute.WIDTH,
0N/A new CSS.Attribute[]{CSS.Attribute.WIDTH});
0N/A htmlAttrToCssAttrMap.put(HTML.Attribute.HEIGHT,
0N/A new CSS.Attribute[]{CSS.Attribute.HEIGHT});
0N/A htmlAttrToCssAttrMap.put(HTML.Attribute.BORDER,
0N/A new CSS.Attribute[]{CSS.Attribute.BORDER_TOP_WIDTH, CSS.Attribute.BORDER_RIGHT_WIDTH, CSS.Attribute.BORDER_BOTTOM_WIDTH, CSS.Attribute.BORDER_LEFT_WIDTH});
0N/A htmlAttrToCssAttrMap.put(HTML.Attribute.CELLPADDING,
0N/A new CSS.Attribute[]{CSS.Attribute.PADDING});
0N/A htmlAttrToCssAttrMap.put(HTML.Attribute.CELLSPACING,
0N/A new CSS.Attribute[]{CSS.Attribute.BORDER_SPACING});
0N/A htmlAttrToCssAttrMap.put(HTML.Attribute.MARGINWIDTH,
0N/A new CSS.Attribute[]{CSS.Attribute.MARGIN_LEFT,
0N/A CSS.Attribute.MARGIN_RIGHT});
0N/A htmlAttrToCssAttrMap.put(HTML.Attribute.MARGINHEIGHT,
0N/A new CSS.Attribute[]{CSS.Attribute.MARGIN_TOP,
0N/A CSS.Attribute.MARGIN_BOTTOM});
0N/A htmlAttrToCssAttrMap.put(HTML.Attribute.HSPACE,
0N/A new CSS.Attribute[]{CSS.Attribute.PADDING_LEFT,
0N/A CSS.Attribute.PADDING_RIGHT});
0N/A htmlAttrToCssAttrMap.put(HTML.Attribute.VSPACE,
0N/A new CSS.Attribute[]{CSS.Attribute.PADDING_BOTTOM,
0N/A CSS.Attribute.PADDING_TOP});
0N/A htmlAttrToCssAttrMap.put(HTML.Attribute.FACE,
0N/A new CSS.Attribute[]{CSS.Attribute.FONT_FAMILY});
0N/A htmlAttrToCssAttrMap.put(HTML.Attribute.SIZE,
0N/A new CSS.Attribute[]{CSS.Attribute.FONT_SIZE});
0N/A htmlAttrToCssAttrMap.put(HTML.Attribute.VALIGN,
0N/A new CSS.Attribute[]{CSS.Attribute.VERTICAL_ALIGN});
0N/A htmlAttrToCssAttrMap.put(HTML.Attribute.ALIGN,
0N/A new CSS.Attribute[]{CSS.Attribute.VERTICAL_ALIGN,
0N/A CSS.Attribute.TEXT_ALIGN,
0N/A CSS.Attribute.FLOAT});
0N/A htmlAttrToCssAttrMap.put(HTML.Attribute.TYPE,
0N/A new CSS.Attribute[]{CSS.Attribute.LIST_STYLE_TYPE});
0N/A htmlAttrToCssAttrMap.put(HTML.Attribute.NOWRAP,
0N/A new CSS.Attribute[]{CSS.Attribute.WHITE_SPACE});
0N/A
0N/A // initialize StyleConstants mapping
0N/A styleConstantToCssMap.put(StyleConstants.FontFamily,
0N/A CSS.Attribute.FONT_FAMILY);
0N/A styleConstantToCssMap.put(StyleConstants.FontSize,
0N/A CSS.Attribute.FONT_SIZE);
0N/A styleConstantToCssMap.put(StyleConstants.Bold,
0N/A CSS.Attribute.FONT_WEIGHT);
0N/A styleConstantToCssMap.put(StyleConstants.Italic,
0N/A CSS.Attribute.FONT_STYLE);
0N/A styleConstantToCssMap.put(StyleConstants.Underline,
0N/A CSS.Attribute.TEXT_DECORATION);
0N/A styleConstantToCssMap.put(StyleConstants.StrikeThrough,
0N/A CSS.Attribute.TEXT_DECORATION);
0N/A styleConstantToCssMap.put(StyleConstants.Superscript,
0N/A CSS.Attribute.VERTICAL_ALIGN);
0N/A styleConstantToCssMap.put(StyleConstants.Subscript,
0N/A CSS.Attribute.VERTICAL_ALIGN);
0N/A styleConstantToCssMap.put(StyleConstants.Foreground,
0N/A CSS.Attribute.COLOR);
0N/A styleConstantToCssMap.put(StyleConstants.Background,
0N/A CSS.Attribute.BACKGROUND_COLOR);
0N/A styleConstantToCssMap.put(StyleConstants.FirstLineIndent,
0N/A CSS.Attribute.TEXT_INDENT);
0N/A styleConstantToCssMap.put(StyleConstants.LeftIndent,
0N/A CSS.Attribute.MARGIN_LEFT);
0N/A styleConstantToCssMap.put(StyleConstants.RightIndent,
0N/A CSS.Attribute.MARGIN_RIGHT);
0N/A styleConstantToCssMap.put(StyleConstants.SpaceAbove,
0N/A CSS.Attribute.MARGIN_TOP);
0N/A styleConstantToCssMap.put(StyleConstants.SpaceBelow,
0N/A CSS.Attribute.MARGIN_BOTTOM);
0N/A styleConstantToCssMap.put(StyleConstants.Alignment,
0N/A CSS.Attribute.TEXT_ALIGN);
0N/A
0N/A // HTML->CSS
0N/A htmlValueToCssValueMap.put("disc", CSS.Value.DISC);
0N/A htmlValueToCssValueMap.put("square", CSS.Value.SQUARE);
0N/A htmlValueToCssValueMap.put("circle", CSS.Value.CIRCLE);
0N/A htmlValueToCssValueMap.put("1", CSS.Value.DECIMAL);
0N/A htmlValueToCssValueMap.put("a", CSS.Value.LOWER_ALPHA);
0N/A htmlValueToCssValueMap.put("A", CSS.Value.UPPER_ALPHA);
0N/A htmlValueToCssValueMap.put("i", CSS.Value.LOWER_ROMAN);
0N/A htmlValueToCssValueMap.put("I", CSS.Value.UPPER_ROMAN);
0N/A
0N/A // CSS-> internal CSS
0N/A cssValueToInternalValueMap.put("none", CSS.Value.NONE);
0N/A cssValueToInternalValueMap.put("disc", CSS.Value.DISC);
0N/A cssValueToInternalValueMap.put("square", CSS.Value.SQUARE);
0N/A cssValueToInternalValueMap.put("circle", CSS.Value.CIRCLE);
0N/A cssValueToInternalValueMap.put("decimal", CSS.Value.DECIMAL);
0N/A cssValueToInternalValueMap.put("lower-roman", CSS.Value.LOWER_ROMAN);
0N/A cssValueToInternalValueMap.put("upper-roman", CSS.Value.UPPER_ROMAN);
0N/A cssValueToInternalValueMap.put("lower-alpha", CSS.Value.LOWER_ALPHA);
0N/A cssValueToInternalValueMap.put("upper-alpha", CSS.Value.UPPER_ALPHA);
0N/A cssValueToInternalValueMap.put("repeat", CSS.Value.BACKGROUND_REPEAT);
0N/A cssValueToInternalValueMap.put("no-repeat",
0N/A CSS.Value.BACKGROUND_NO_REPEAT);
0N/A cssValueToInternalValueMap.put("repeat-x",
0N/A CSS.Value.BACKGROUND_REPEAT_X);
0N/A cssValueToInternalValueMap.put("repeat-y",
0N/A CSS.Value.BACKGROUND_REPEAT_Y);
0N/A cssValueToInternalValueMap.put("scroll",
0N/A CSS.Value.BACKGROUND_SCROLL);
0N/A cssValueToInternalValueMap.put("fixed",
0N/A CSS.Value.BACKGROUND_FIXED);
0N/A
0N/A // Register all the CSS attribute keys for archival/unarchival
0N/A Object[] keys = CSS.Attribute.allAttributes;
0N/A try {
611N/A for (Object key : keys) {
611N/A StyleContext.registerStaticAttributeKey(key);
0N/A }
0N/A } catch (Throwable e) {
0N/A e.printStackTrace();
0N/A }
0N/A
0N/A // Register all the CSS Values for archival/unarchival
0N/A keys = CSS.Value.allValues;
0N/A try {
611N/A for (Object key : keys) {
611N/A StyleContext.registerStaticAttributeKey(key);
0N/A }
0N/A } catch (Throwable e) {
0N/A e.printStackTrace();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Return the set of all possible CSS attribute keys.
0N/A */
0N/A public static Attribute[] getAllAttributeKeys() {
0N/A Attribute[] keys = new Attribute[Attribute.allAttributes.length];
0N/A System.arraycopy(Attribute.allAttributes, 0, keys, 0, Attribute.allAttributes.length);
0N/A return keys;
0N/A }
0N/A
0N/A /**
0N/A * Translates a string to a <code>CSS.Attribute</code> object.
0N/A * This will return <code>null</code> if there is no attribute
0N/A * by the given name.
0N/A *
0N/A * @param name the name of the CSS attribute to fetch the
0N/A * typesafe enumeration for
0N/A * @return the <code>CSS.Attribute</code> object,
0N/A * or <code>null</code> if the string
0N/A * doesn't represent a valid attribute key
0N/A */
0N/A public static final Attribute getAttribute(String name) {
611N/A return attributeMap.get(name);
0N/A }
0N/A
0N/A /**
0N/A * Translates a string to a <code>CSS.Value</code> object.
0N/A * This will return <code>null</code> if there is no value
0N/A * by the given name.
0N/A *
0N/A * @param name the name of the CSS value to fetch the
0N/A * typesafe enumeration for
0N/A * @return the <code>CSS.Value</code> object,
0N/A * or <code>null</code> if the string
0N/A * doesn't represent a valid CSS value name; this does
0N/A * not mean that it doesn't represent a valid CSS value
0N/A */
0N/A static final Value getValue(String name) {
611N/A return valueMap.get(name);
0N/A }
0N/A
0N/A
0N/A //
0N/A // Conversion related methods/classes
0N/A //
0N/A
0N/A /**
0N/A * Returns a URL for the given CSS url string. If relative,
0N/A * <code>base</code> is used as the parent. If a valid URL can not
0N/A * be found, this will not throw a MalformedURLException, instead
0N/A * null will be returned.
0N/A */
0N/A static URL getURL(URL base, String cssString) {
0N/A if (cssString == null) {
0N/A return null;
0N/A }
0N/A if (cssString.startsWith("url(") &&
0N/A cssString.endsWith(")")) {
0N/A cssString = cssString.substring(4, cssString.length() - 1);
0N/A }
0N/A // Absolute first
0N/A try {
0N/A URL url = new URL(cssString);
0N/A if (url != null) {
0N/A return url;
0N/A }
0N/A } catch (MalformedURLException mue) {
0N/A }
0N/A // Then relative
0N/A if (base != null) {
0N/A // Relative URL, try from base
0N/A try {
0N/A URL url = new URL(base, cssString);
0N/A return url;
0N/A }
0N/A catch (MalformedURLException muee) {
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Converts a type Color to a hex string
0N/A * in the format "#RRGGBB"
0N/A */
0N/A static String colorToHex(Color color) {
0N/A
215N/A String colorstr = "#";
0N/A
0N/A // Red
0N/A String str = Integer.toHexString(color.getRed());
0N/A if (str.length() > 2)
0N/A str = str.substring(0, 2);
0N/A else if (str.length() < 2)
0N/A colorstr += "0" + str;
0N/A else
0N/A colorstr += str;
0N/A
0N/A // Green
0N/A str = Integer.toHexString(color.getGreen());
0N/A if (str.length() > 2)
0N/A str = str.substring(0, 2);
0N/A else if (str.length() < 2)
0N/A colorstr += "0" + str;
0N/A else
0N/A colorstr += str;
0N/A
0N/A // Blue
0N/A str = Integer.toHexString(color.getBlue());
0N/A if (str.length() > 2)
0N/A str = str.substring(0, 2);
0N/A else if (str.length() < 2)
0N/A colorstr += "0" + str;
0N/A else
0N/A colorstr += str;
0N/A
0N/A return colorstr;
0N/A }
0N/A
0N/A /**
0N/A * Convert a "#FFFFFF" hex string to a Color.
0N/A * If the color specification is bad, an attempt
0N/A * will be made to fix it up.
0N/A */
0N/A static final Color hexToColor(String value) {
0N/A String digits;
0N/A int n = value.length();
0N/A if (value.startsWith("#")) {
0N/A digits = value.substring(1, Math.min(value.length(), 7));
0N/A } else {
0N/A digits = value;
0N/A }
0N/A String hstr = "0x" + digits;
0N/A Color c;
0N/A try {
0N/A c = Color.decode(hstr);
0N/A } catch (NumberFormatException nfe) {
0N/A c = null;
0N/A }
0N/A return c;
0N/A }
0N/A
0N/A /**
0N/A * Convert a color string such as "RED" or "#NNNNNN" or "rgb(r, g, b)"
0N/A * to a Color.
0N/A */
0N/A static Color stringToColor(String str) {
611N/A Color color;
0N/A
0N/A if (str == null) {
0N/A return null;
0N/A }
0N/A if (str.length() == 0)
0N/A color = Color.black;
0N/A else if (str.startsWith("rgb(")) {
0N/A color = parseRGB(str);
0N/A }
0N/A else if (str.charAt(0) == '#')
0N/A color = hexToColor(str);
0N/A else if (str.equalsIgnoreCase("Black"))
0N/A color = hexToColor("#000000");
0N/A else if(str.equalsIgnoreCase("Silver"))
0N/A color = hexToColor("#C0C0C0");
0N/A else if(str.equalsIgnoreCase("Gray"))
0N/A color = hexToColor("#808080");
0N/A else if(str.equalsIgnoreCase("White"))
0N/A color = hexToColor("#FFFFFF");
0N/A else if(str.equalsIgnoreCase("Maroon"))
0N/A color = hexToColor("#800000");
0N/A else if(str.equalsIgnoreCase("Red"))
0N/A color = hexToColor("#FF0000");
0N/A else if(str.equalsIgnoreCase("Purple"))
0N/A color = hexToColor("#800080");
0N/A else if(str.equalsIgnoreCase("Fuchsia"))
0N/A color = hexToColor("#FF00FF");
0N/A else if(str.equalsIgnoreCase("Green"))
0N/A color = hexToColor("#008000");
0N/A else if(str.equalsIgnoreCase("Lime"))
0N/A color = hexToColor("#00FF00");
0N/A else if(str.equalsIgnoreCase("Olive"))
0N/A color = hexToColor("#808000");
0N/A else if(str.equalsIgnoreCase("Yellow"))
0N/A color = hexToColor("#FFFF00");
0N/A else if(str.equalsIgnoreCase("Navy"))
0N/A color = hexToColor("#000080");
0N/A else if(str.equalsIgnoreCase("Blue"))
0N/A color = hexToColor("#0000FF");
0N/A else if(str.equalsIgnoreCase("Teal"))
0N/A color = hexToColor("#008080");
0N/A else if(str.equalsIgnoreCase("Aqua"))
0N/A color = hexToColor("#00FFFF");
0N/A else if(str.equalsIgnoreCase("Orange"))
0N/A color = hexToColor("#FF8000");
0N/A else
0N/A color = hexToColor(str); // sometimes get specified without leading #
0N/A return color;
0N/A }
0N/A
0N/A /**
0N/A * Parses a String in the format <code>rgb(r, g, b)</code> where
0N/A * each of the Color components is either an integer, or a floating number
0N/A * with a % after indicating a percentage value of 255. Values are
0N/A * constrained to fit with 0-255. The resulting Color is returned.
0N/A */
0N/A private static Color parseRGB(String string) {
0N/A // Find the next numeric char
0N/A int[] index = new int[1];
0N/A
0N/A index[0] = 4;
0N/A int red = getColorComponent(string, index);
0N/A int green = getColorComponent(string, index);
0N/A int blue = getColorComponent(string, index);
0N/A
0N/A return new Color(red, green, blue);
0N/A }
0N/A
0N/A /**
0N/A * Returns the next integer value from <code>string</code> starting
0N/A * at <code>index[0]</code>. The value can either can an integer, or
0N/A * a percentage (floating number ending with %), in which case it is
0N/A * multiplied by 255.
0N/A */
0N/A private static int getColorComponent(String string, int[] index) {
0N/A int length = string.length();
0N/A char aChar;
0N/A
0N/A // Skip non-decimal chars
0N/A while(index[0] < length && (aChar = string.charAt(index[0])) != '-' &&
0N/A !Character.isDigit(aChar) && aChar != '.') {
0N/A index[0]++;
0N/A }
0N/A
0N/A int start = index[0];
0N/A
0N/A if (start < length && string.charAt(index[0]) == '-') {
0N/A index[0]++;
0N/A }
0N/A while(index[0] < length &&
0N/A Character.isDigit(string.charAt(index[0]))) {
0N/A index[0]++;
0N/A }
0N/A if (index[0] < length && string.charAt(index[0]) == '.') {
0N/A // Decimal value
0N/A index[0]++;
0N/A while(index[0] < length &&
0N/A Character.isDigit(string.charAt(index[0]))) {
0N/A index[0]++;
0N/A }
0N/A }
0N/A if (start != index[0]) {
0N/A try {
0N/A float value = Float.parseFloat(string.substring
0N/A (start, index[0]));
0N/A
0N/A if (index[0] < length && string.charAt(index[0]) == '%') {
0N/A index[0]++;
0N/A value = value * 255f / 100f;
0N/A }
0N/A return Math.min(255, Math.max(0, (int)value));
0N/A } catch (NumberFormatException nfe) {
0N/A // Treat as 0
0N/A }
0N/A }
0N/A return 0;
0N/A }
0N/A
0N/A static int getIndexOfSize(float pt, int[] sizeMap) {
0N/A for (int i = 0; i < sizeMap.length; i ++ )
0N/A if (pt <= sizeMap[i])
0N/A return i + 1;
0N/A return sizeMap.length;
0N/A }
0N/A
0N/A static int getIndexOfSize(float pt, StyleSheet ss) {
0N/A int[] sizeMap = (ss != null) ? ss.getSizeMap() :
0N/A StyleSheet.sizeMapDefault;
0N/A return getIndexOfSize(pt, sizeMap);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * @return an array of all the strings in <code>value</code>
0N/A * that are separated by whitespace.
0N/A */
0N/A static String[] parseStrings(String value) {
0N/A int current, last;
0N/A int length = (value == null) ? 0 : value.length();
611N/A Vector<String> temp = new Vector<String>(4);
0N/A
0N/A current = 0;
0N/A while (current < length) {
0N/A // Skip ws
0N/A while (current < length && Character.isWhitespace
0N/A (value.charAt(current))) {
0N/A current++;
0N/A }
0N/A last = current;
0N/A while (current < length && !Character.isWhitespace
0N/A (value.charAt(current))) {
0N/A current++;
0N/A }
0N/A if (last != current) {
0N/A temp.addElement(value.substring(last, current));
0N/A }
0N/A current++;
0N/A }
0N/A String[] retValue = new String[temp.size()];
0N/A temp.copyInto(retValue);
0N/A return retValue;
0N/A }
0N/A
0N/A /**
0N/A * Return the point size, given a size index. Legal HTML index sizes
0N/A * are 1-7.
0N/A */
0N/A float getPointSize(int index, StyleSheet ss) {
0N/A ss = getStyleSheet(ss);
0N/A int[] sizeMap = (ss != null) ? ss.getSizeMap() :
0N/A StyleSheet.sizeMapDefault;
0N/A --index;
0N/A if (index < 0)
0N/A return sizeMap[0];
0N/A else if (index > sizeMap.length - 1)
0N/A return sizeMap[sizeMap.length - 1];
0N/A else
0N/A return sizeMap[index];
0N/A }
0N/A
0N/A
0N/A private void translateEmbeddedAttributes(AttributeSet htmlAttrSet,
0N/A MutableAttributeSet cssAttrSet) {
0N/A Enumeration keys = htmlAttrSet.getAttributeNames();
0N/A if (htmlAttrSet.getAttribute(StyleConstants.NameAttribute) ==
0N/A HTML.Tag.HR) {
0N/A // HR needs special handling due to us treating it as a leaf.
0N/A translateAttributes(HTML.Tag.HR, htmlAttrSet, cssAttrSet);
0N/A }
0N/A while (keys.hasMoreElements()) {
0N/A Object key = keys.nextElement();
0N/A if (key instanceof HTML.Tag) {
0N/A HTML.Tag tag = (HTML.Tag)key;
0N/A Object o = htmlAttrSet.getAttribute(tag);
0N/A if (o != null && o instanceof AttributeSet) {
0N/A translateAttributes(tag, (AttributeSet)o, cssAttrSet);
0N/A }
0N/A } else if (key instanceof CSS.Attribute) {
0N/A cssAttrSet.addAttribute(key, htmlAttrSet.getAttribute(key));
0N/A }
0N/A }
0N/A }
0N/A
0N/A private void translateAttributes(HTML.Tag tag,
0N/A AttributeSet htmlAttrSet,
0N/A MutableAttributeSet cssAttrSet) {
0N/A Enumeration names = htmlAttrSet.getAttributeNames();
0N/A while (names.hasMoreElements()) {
0N/A Object name = names.nextElement();
0N/A
0N/A if (name instanceof HTML.Attribute) {
0N/A HTML.Attribute key = (HTML.Attribute)name;
0N/A
0N/A /*
0N/A * HTML.Attribute.ALIGN needs special processing.
0N/A * It can map to to 1 of many(3) possible CSS attributes
0N/A * depending on the nature of the tag the attribute is
0N/A * part off and depending on the value of the attribute.
0N/A */
0N/A if (key == HTML.Attribute.ALIGN) {
0N/A String htmlAttrValue = (String)htmlAttrSet.getAttribute(HTML.Attribute.ALIGN);
0N/A if (htmlAttrValue != null) {
0N/A CSS.Attribute cssAttr = getCssAlignAttribute(tag, htmlAttrSet);
0N/A if (cssAttr != null) {
0N/A Object o = getCssValue(cssAttr, htmlAttrValue);
0N/A if (o != null) {
0N/A cssAttrSet.addAttribute(cssAttr, o);
0N/A }
0N/A }
0N/A }
0N/A } else {
0N/A if (key == HTML.Attribute.SIZE && !isHTMLFontTag(tag)) {
4379N/A /*
4379N/A * The html size attribute has a mapping in the CSS world only
4379N/A * if it is par of a font or base font tag.
4379N/A */
4379N/A } else if (tag == HTML.Tag.TABLE && key == HTML.Attribute.BORDER) {
4379N/A int borderWidth = getTableBorder(htmlAttrSet);
4379N/A
4379N/A if (borderWidth > 0) {
4379N/A translateAttribute(HTML.Attribute.BORDER, Integer.toString(borderWidth), cssAttrSet);
4379N/A }
4379N/A } else {
4379N/A translateAttribute(key, (String) htmlAttrSet.getAttribute(key), cssAttrSet);
0N/A }
0N/A }
0N/A } else if (name instanceof CSS.Attribute) {
0N/A cssAttrSet.addAttribute(name, htmlAttrSet.getAttribute(name));
0N/A }
0N/A }
0N/A }
0N/A
0N/A private void translateAttribute(HTML.Attribute key,
4379N/A String htmlAttrValue,
0N/A MutableAttributeSet cssAttrSet) {
0N/A /*
0N/A * In the case of all remaining HTML.Attribute's they
0N/A * map to 1 or more CCS.Attribute.
0N/A */
0N/A CSS.Attribute[] cssAttrList = getCssAttribute(key);
0N/A
0N/A if (cssAttrList == null || htmlAttrValue == null) {
0N/A return;
0N/A }
611N/A for (Attribute cssAttr : cssAttrList) {
611N/A Object o = getCssValue(cssAttr, htmlAttrValue);
0N/A if (o != null) {
611N/A cssAttrSet.addAttribute(cssAttr , o);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Given a CSS.Attribute object and its corresponding HTML.Attribute's
0N/A * value, this method returns a CssValue object to associate with the
0N/A * CSS attribute.
0N/A *
0N/A * @param the CSS.Attribute
0N/A * @param a String containing the value associated HTML.Attribtue.
0N/A */
0N/A Object getCssValue(CSS.Attribute cssAttr, String htmlAttrValue) {
0N/A CssValue value = (CssValue)valueConvertor.get(cssAttr);
0N/A Object o = value.parseHtmlValue(htmlAttrValue);
0N/A return o;
0N/A }
0N/A
0N/A /**
0N/A * Maps an HTML.Attribute object to its appropriate CSS.Attributes.
0N/A *
0N/A * @param HTML.Attribute
0N/A * @return CSS.Attribute[]
0N/A */
0N/A private CSS.Attribute[] getCssAttribute(HTML.Attribute hAttr) {
611N/A return htmlAttrToCssAttrMap.get(hAttr);
0N/A }
0N/A
0N/A /**
0N/A * Maps HTML.Attribute.ALIGN to either:
0N/A * CSS.Attribute.TEXT_ALIGN
0N/A * CSS.Attribute.FLOAT
0N/A * CSS.Attribute.VERTICAL_ALIGN
0N/A * based on the tag associated with the attribute and the
0N/A * value of the attribute.
0N/A *
0N/A * @param AttributeSet containing HTML attributes.
0N/A * @return CSS.Attribute mapping for HTML.Attribute.ALIGN.
0N/A */
0N/A private CSS.Attribute getCssAlignAttribute(HTML.Tag tag,
0N/A AttributeSet htmlAttrSet) {
0N/A return CSS.Attribute.TEXT_ALIGN;
0N/A/*
0N/A String htmlAttrValue = (String)htmlAttrSet.getAttribute(HTML.Attribute.ALIGN);
0N/A CSS.Attribute cssAttr = CSS.Attribute.TEXT_ALIGN;
0N/A if (htmlAttrValue != null && htmlAttrSet instanceof Element) {
0N/A Element elem = (Element)htmlAttrSet;
0N/A if (!elem.isLeaf() && tag.isBlock() && validTextAlignValue(htmlAttrValue)) {
0N/A return CSS.Attribute.TEXT_ALIGN;
0N/A } else if (isFloater(htmlAttrValue)) {
0N/A return CSS.Attribute.FLOAT;
0N/A } else if (elem.isLeaf()) {
0N/A return CSS.Attribute.VERTICAL_ALIGN;
0N/A }
0N/A }
0N/A return null;
0N/A */
0N/A }
0N/A
0N/A /**
0N/A * Fetches the tag associated with the HTML AttributeSet.
0N/A *
0N/A * @param AttributeSet containing the HTML attributes.
0N/A * @return HTML.Tag
0N/A */
0N/A private HTML.Tag getHTMLTag(AttributeSet htmlAttrSet) {
0N/A Object o = htmlAttrSet.getAttribute(StyleConstants.NameAttribute);
0N/A if (o instanceof HTML.Tag) {
0N/A HTML.Tag tag = (HTML.Tag) o;
0N/A return tag;
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A
0N/A private boolean isHTMLFontTag(HTML.Tag tag) {
0N/A return (tag != null && ((tag == HTML.Tag.FONT) || (tag == HTML.Tag.BASEFONT)));
0N/A }
0N/A
0N/A
0N/A private boolean isFloater(String alignValue) {
0N/A return (alignValue.equals("left") || alignValue.equals("right"));
0N/A }
0N/A
0N/A private boolean validTextAlignValue(String alignValue) {
0N/A return (isFloater(alignValue) || alignValue.equals("center"));
0N/A }
0N/A
0N/A /**
0N/A * Base class to CSS values in the attribute sets. This
0N/A * is intended to act as a convertor to/from other attribute
0N/A * formats.
0N/A * <p>
0N/A * The CSS parser uses the parseCssValue method to convert
0N/A * a string to whatever format is appropriate a given key
0N/A * (i.e. these convertors are stored in a map using the
0N/A * CSS.Attribute as a key and the CssValue as the value).
0N/A * <p>
0N/A * The HTML to CSS conversion process first converts the
0N/A * HTML.Attribute to a CSS.Attribute, and then calls
0N/A * the parseHtmlValue method on the value of the HTML
0N/A * attribute to produce the corresponding CSS value.
0N/A * <p>
0N/A * The StyleConstants to CSS conversion process first
0N/A * converts the StyleConstants attribute to a
0N/A * CSS.Attribute, and then calls the fromStyleConstants
0N/A * method to convert the StyleConstants value to a
0N/A * CSS value.
0N/A * <p>
0N/A * The CSS to StyleConstants conversion process first
0N/A * converts the StyleConstants attribute to a
0N/A * CSS.Attribute, and then calls the toStyleConstants
0N/A * method to convert the CSS value to a StyleConstants
0N/A * value.
0N/A */
0N/A static class CssValue implements Serializable {
0N/A
0N/A /**
0N/A * Convert a CSS value string to the internal format
0N/A * (for fast processing) used in the attribute sets.
0N/A * The fallback storage for any value that we don't
0N/A * have a special binary format for is a String.
0N/A */
0N/A Object parseCssValue(String value) {
0N/A return value;
0N/A }
0N/A
0N/A /**
0N/A * Convert an HTML attribute value to a CSS attribute
0N/A * value. If there is no conversion, return null.
0N/A * This is implemented to simply forward to the CSS
0N/A * parsing by default (since some of the attribute
0N/A * values are the same). If the attribute value
0N/A * isn't recognized as a CSS value it is generally
0N/A * returned as null.
0N/A */
0N/A Object parseHtmlValue(String value) {
0N/A return parseCssValue(value);
0N/A }
0N/A
0N/A /**
0N/A * Converts a <code>StyleConstants</code> attribute value to
0N/A * a CSS attribute value. If there is no conversion,
0N/A * returns <code>null</code>. By default, there is no conversion.
0N/A *
0N/A * @param key the <code>StyleConstants</code> attribute
0N/A * @param value the value of a <code>StyleConstants</code>
0N/A * attribute to be converted
0N/A * @return the CSS value that represents the
0N/A * <code>StyleConstants</code> value
0N/A */
0N/A Object fromStyleConstants(StyleConstants key, Object value) {
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Converts a CSS attribute value to a
0N/A * <code>StyleConstants</code>
0N/A * value. If there is no conversion, returns
0N/A * <code>null</code>.
0N/A * By default, there is no conversion.
0N/A *
0N/A * @param key the <code>StyleConstants</code> attribute
0N/A * @param v the view containing <code>AttributeSet</code>
0N/A * @return the <code>StyleConstants</code> attribute value that
0N/A * represents the CSS attribute value
0N/A */
0N/A Object toStyleConstants(StyleConstants key, View v) {
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Return the CSS format of the value
0N/A */
0N/A public String toString() {
0N/A return svalue;
0N/A }
0N/A
0N/A /**
0N/A * The value as a string... before conversion to a
0N/A * binary format.
0N/A */
0N/A String svalue;
0N/A }
0N/A
0N/A /**
0N/A * By default CSS attributes are represented as simple
0N/A * strings. They also have no conversion to/from
0N/A * StyleConstants by default. This class represents the
0N/A * value as a string (via the superclass), but
0N/A * provides StyleConstants conversion support for the
0N/A * CSS attributes that are held as strings.
0N/A */
0N/A static class StringValue extends CssValue {
0N/A
0N/A /**
0N/A * Convert a CSS value string to the internal format
0N/A * (for fast processing) used in the attribute sets.
0N/A * This produces a StringValue, so that it can be
0N/A * used to convert from CSS to StyleConstants values.
0N/A */
0N/A Object parseCssValue(String value) {
0N/A StringValue sv = new StringValue();
0N/A sv.svalue = value;
0N/A return sv;
0N/A }
0N/A
0N/A /**
0N/A * Converts a <code>StyleConstants</code> attribute value to
0N/A * a CSS attribute value. If there is no conversion
0N/A * returns <code>null</code>.
0N/A *
0N/A * @param key the <code>StyleConstants</code> attribute
0N/A * @param value the value of a <code>StyleConstants</code>
0N/A * attribute to be converted
0N/A * @return the CSS value that represents the
0N/A * <code>StyleConstants</code> value
0N/A */
0N/A Object fromStyleConstants(StyleConstants key, Object value) {
0N/A if (key == StyleConstants.Italic) {
0N/A if (value.equals(Boolean.TRUE)) {
0N/A return parseCssValue("italic");
0N/A }
0N/A return parseCssValue("");
0N/A } else if (key == StyleConstants.Underline) {
0N/A if (value.equals(Boolean.TRUE)) {
0N/A return parseCssValue("underline");
0N/A }
0N/A return parseCssValue("");
0N/A } else if (key == StyleConstants.Alignment) {
0N/A int align = ((Integer)value).intValue();
0N/A String ta;
0N/A switch(align) {
0N/A case StyleConstants.ALIGN_LEFT:
0N/A ta = "left";
0N/A break;
0N/A case StyleConstants.ALIGN_RIGHT:
0N/A ta = "right";
0N/A break;
0N/A case StyleConstants.ALIGN_CENTER:
0N/A ta = "center";
0N/A break;
0N/A case StyleConstants.ALIGN_JUSTIFIED:
0N/A ta = "justify";
0N/A break;
0N/A default:
0N/A ta = "left";
0N/A }
0N/A return parseCssValue(ta);
0N/A } else if (key == StyleConstants.StrikeThrough) {
0N/A if (value.equals(Boolean.TRUE)) {
0N/A return parseCssValue("line-through");
0N/A }
0N/A return parseCssValue("");
0N/A } else if (key == StyleConstants.Superscript) {
0N/A if (value.equals(Boolean.TRUE)) {
0N/A return parseCssValue("super");
0N/A }
0N/A return parseCssValue("");
0N/A } else if (key == StyleConstants.Subscript) {
0N/A if (value.equals(Boolean.TRUE)) {
0N/A return parseCssValue("sub");
0N/A }
0N/A return parseCssValue("");
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Converts a CSS attribute value to a
0N/A * <code>StyleConstants</code> value.
0N/A * If there is no conversion, returns <code>null</code>.
0N/A * By default, there is no conversion.
0N/A *
0N/A * @param key the <code>StyleConstants</code> attribute
0N/A * @return the <code>StyleConstants</code> attribute value that
0N/A * represents the CSS attribute value
0N/A */
0N/A Object toStyleConstants(StyleConstants key, View v) {
0N/A if (key == StyleConstants.Italic) {
0N/A if (svalue.indexOf("italic") >= 0) {
0N/A return Boolean.TRUE;
0N/A }
0N/A return Boolean.FALSE;
0N/A } else if (key == StyleConstants.Underline) {
0N/A if (svalue.indexOf("underline") >= 0) {
0N/A return Boolean.TRUE;
0N/A }
0N/A return Boolean.FALSE;
0N/A } else if (key == StyleConstants.Alignment) {
0N/A if (svalue.equals("right")) {
0N/A return new Integer(StyleConstants.ALIGN_RIGHT);
0N/A } else if (svalue.equals("center")) {
0N/A return new Integer(StyleConstants.ALIGN_CENTER);
0N/A } else if (svalue.equals("justify")) {
0N/A return new Integer(StyleConstants.ALIGN_JUSTIFIED);
0N/A }
0N/A return new Integer(StyleConstants.ALIGN_LEFT);
0N/A } else if (key == StyleConstants.StrikeThrough) {
0N/A if (svalue.indexOf("line-through") >= 0) {
0N/A return Boolean.TRUE;
0N/A }
0N/A return Boolean.FALSE;
0N/A } else if (key == StyleConstants.Superscript) {
0N/A if (svalue.indexOf("super") >= 0) {
0N/A return Boolean.TRUE;
0N/A }
0N/A return Boolean.FALSE;
0N/A } else if (key == StyleConstants.Subscript) {
0N/A if (svalue.indexOf("sub") >= 0) {
0N/A return Boolean.TRUE;
0N/A }
0N/A return Boolean.FALSE;
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A // Used by ViewAttributeSet
0N/A boolean isItalic() {
0N/A return (svalue.indexOf("italic") != -1);
0N/A }
0N/A
0N/A boolean isStrike() {
0N/A return (svalue.indexOf("line-through") != -1);
0N/A }
0N/A
0N/A boolean isUnderline() {
0N/A return (svalue.indexOf("underline") != -1);
0N/A }
0N/A
0N/A boolean isSub() {
0N/A return (svalue.indexOf("sub") != -1);
0N/A }
0N/A
0N/A boolean isSup() {
0N/A return (svalue.indexOf("sup") != -1);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Represents a value for the CSS.FONT_SIZE attribute.
0N/A * The binary format of the value can be one of several
0N/A * types. If the type is Float,
0N/A * the value is specified in terms of point or
0N/A * percentage, depending upon the ending of the
0N/A * associated string.
0N/A * If the type is Integer, the value is specified
0N/A * in terms of a size index.
0N/A */
0N/A class FontSize extends CssValue {
0N/A
0N/A /**
0N/A * Returns the size in points. This is ultimately
0N/A * what we need for the purpose of creating/fetching
0N/A * a Font object.
0N/A *
0N/A * @param a the attribute set the value is being
0N/A * requested from. We may need to walk up the
0N/A * resolve hierarchy if it's relative.
0N/A */
0N/A int getValue(AttributeSet a, StyleSheet ss) {
0N/A ss = getStyleSheet(ss);
0N/A if (index) {
0N/A // it's an index, translate from size table
0N/A return Math.round(getPointSize((int) value, ss));
0N/A }
0N/A else if (lu == null) {
0N/A return Math.round(value);
0N/A }
0N/A else {
0N/A if (lu.type == 0) {
0N/A boolean isW3CLengthUnits = (ss == null) ? false : ss.isW3CLengthUnits();
0N/A return Math.round(lu.getValue(isW3CLengthUnits));
0N/A }
0N/A if (a != null) {
0N/A AttributeSet resolveParent = a.getResolveParent();
0N/A
0N/A if (resolveParent != null) {
0N/A int pValue = StyleConstants.getFontSize(resolveParent);
0N/A
0N/A float retValue;
0N/A if (lu.type == 1 || lu.type == 3) {
0N/A retValue = lu.value * (float)pValue;
0N/A }
0N/A else {
0N/A retValue = lu.value + (float)pValue;
0N/A }
0N/A return Math.round(retValue);
0N/A }
0N/A }
0N/A // a is null, or no resolve parent.
0N/A return 12;
0N/A }
0N/A }
0N/A
0N/A Object parseCssValue(String value) {
0N/A FontSize fs = new FontSize();
0N/A fs.svalue = value;
0N/A try {
0N/A if (value.equals("xx-small")) {
0N/A fs.value = 1;
0N/A fs.index = true;
0N/A } else if (value.equals("x-small")) {
0N/A fs.value = 2;
0N/A fs.index = true;
0N/A } else if (value.equals("small")) {
0N/A fs.value = 3;
0N/A fs.index = true;
0N/A } else if (value.equals("medium")) {
0N/A fs.value = 4;
0N/A fs.index = true;
0N/A } else if (value.equals("large")) {
0N/A fs.value = 5;
0N/A fs.index = true;
0N/A } else if (value.equals("x-large")) {
0N/A fs.value = 6;
0N/A fs.index = true;
0N/A } else if (value.equals("xx-large")) {
0N/A fs.value = 7;
0N/A fs.index = true;
0N/A } else {
0N/A fs.lu = new LengthUnit(value, (short)1, 1f);
0N/A }
0N/A // relative sizes, larger | smaller (adjust from parent by
0N/A // 1.5 pixels)
0N/A // em, ex refer to parent sizes
0N/A // lengths: pt, mm, cm, pc, in, px
0N/A // em (font height 3em would be 3 times font height)
0N/A // ex (height of X)
0N/A // lengths are (+/-) followed by a number and two letter
0N/A // unit identifier
0N/A } catch (NumberFormatException nfe) {
0N/A fs = null;
0N/A }
0N/A return fs;
0N/A }
0N/A
0N/A Object parseHtmlValue(String value) {
0N/A if ((value == null) || (value.length() == 0)) {
0N/A return null;
0N/A }
0N/A FontSize fs = new FontSize();
0N/A fs.svalue = value;
0N/A
0N/A try {
0N/A /*
0N/A * relative sizes in the size attribute are relative
0N/A * to the <basefont>'s size.
0N/A */
0N/A int baseFontSize = getBaseFontSize();
0N/A if (value.charAt(0) == '+') {
0N/A int relSize = Integer.valueOf(value.substring(1)).intValue();
0N/A fs.value = baseFontSize + relSize;
0N/A fs.index = true;
0N/A } else if (value.charAt(0) == '-') {
0N/A int relSize = -Integer.valueOf(value.substring(1)).intValue();
0N/A fs.value = baseFontSize + relSize;
0N/A fs.index = true;
0N/A } else {
0N/A fs.value = Integer.parseInt(value);
0N/A if (fs.value > 7) {
0N/A fs.value = 7;
0N/A } else if (fs.value < 0) {
0N/A fs.value = 0;
0N/A }
0N/A fs.index = true;
0N/A }
0N/A
0N/A } catch (NumberFormatException nfe) {
0N/A fs = null;
0N/A }
0N/A return fs;
0N/A }
0N/A
0N/A /**
0N/A * Converts a <code>StyleConstants</code> attribute value to
0N/A * a CSS attribute value. If there is no conversion
0N/A * returns <code>null</code>. By default, there is no conversion.
0N/A *
0N/A * @param key the <code>StyleConstants</code> attribute
0N/A * @param value the value of a <code>StyleConstants</code>
0N/A * attribute to be converted
0N/A * @return the CSS value that represents the
0N/A * <code>StyleConstants</code> value
0N/A */
0N/A Object fromStyleConstants(StyleConstants key, Object value) {
0N/A if (value instanceof Number) {
0N/A FontSize fs = new FontSize();
0N/A
0N/A fs.value = getIndexOfSize(((Number)value).floatValue(), StyleSheet.sizeMapDefault);
0N/A fs.svalue = Integer.toString((int)fs.value);
0N/A fs.index = true;
0N/A return fs;
0N/A }
0N/A return parseCssValue(value.toString());
0N/A }
0N/A
0N/A /**
0N/A * Converts a CSS attribute value to a <code>StyleConstants</code>
0N/A * value. If there is no conversion, returns <code>null</code>.
0N/A * By default, there is no conversion.
0N/A *
0N/A * @param key the <code>StyleConstants</code> attribute
0N/A * @return the <code>StyleConstants</code> attribute value that
0N/A * represents the CSS attribute value
0N/A */
0N/A Object toStyleConstants(StyleConstants key, View v) {
0N/A if (v != null) {
0N/A return Integer.valueOf(getValue(v.getAttributes(), null));
0N/A }
0N/A return Integer.valueOf(getValue(null, null));
0N/A }
0N/A
0N/A float value;
0N/A boolean index;
0N/A LengthUnit lu;
0N/A }
0N/A
0N/A static class FontFamily extends CssValue {
0N/A
0N/A /**
0N/A * Returns the font family to use.
0N/A */
0N/A String getValue() {
0N/A return family;
0N/A }
0N/A
0N/A Object parseCssValue(String value) {
0N/A int cIndex = value.indexOf(',');
0N/A FontFamily ff = new FontFamily();
0N/A ff.svalue = value;
0N/A ff.family = null;
0N/A
0N/A if (cIndex == -1) {
0N/A setFontName(ff, value);
0N/A }
0N/A else {
0N/A boolean done = false;
0N/A int lastIndex;
0N/A int length = value.length();
0N/A cIndex = 0;
0N/A while (!done) {
0N/A // skip ws.
0N/A while (cIndex < length &&
0N/A Character.isWhitespace(value.charAt(cIndex)))
0N/A cIndex++;
0N/A // Find next ','
0N/A lastIndex = cIndex;
0N/A cIndex = value.indexOf(',', cIndex);
0N/A if (cIndex == -1) {
0N/A cIndex = length;
0N/A }
0N/A if (lastIndex < length) {
0N/A if (lastIndex != cIndex) {
0N/A int lastCharIndex = cIndex;
0N/A if (cIndex > 0 && value.charAt(cIndex - 1) == ' '){
0N/A lastCharIndex--;
0N/A }
0N/A setFontName(ff, value.substring
0N/A (lastIndex, lastCharIndex));
0N/A done = (ff.family != null);
0N/A }
0N/A cIndex++;
0N/A }
0N/A else {
0N/A done = true;
0N/A }
0N/A }
0N/A }
0N/A if (ff.family == null) {
0N/A ff.family = Font.SANS_SERIF;
0N/A }
0N/A return ff;
0N/A }
0N/A
0N/A private void setFontName(FontFamily ff, String fontName) {
0N/A ff.family = fontName;
0N/A }
0N/A
0N/A Object parseHtmlValue(String value) {
0N/A // TBD
0N/A return parseCssValue(value);
0N/A }
0N/A
0N/A /**
0N/A * Converts a <code>StyleConstants</code> attribute value to
0N/A * a CSS attribute value. If there is no conversion
0N/A * returns <code>null</code>. By default, there is no conversion.
0N/A *
0N/A * @param key the <code>StyleConstants</code> attribute
0N/A * @param value the value of a <code>StyleConstants</code>
0N/A * attribute to be converted
0N/A * @return the CSS value that represents the
0N/A * <code>StyleConstants</code> value
0N/A */
0N/A Object fromStyleConstants(StyleConstants key, Object value) {
0N/A return parseCssValue(value.toString());
0N/A }
0N/A
0N/A /**
0N/A * Converts a CSS attribute value to a <code>StyleConstants</code>
0N/A * value. If there is no conversion, returns <code>null</code>.
0N/A * By default, there is no conversion.
0N/A *
0N/A * @param key the <code>StyleConstants</code> attribute
0N/A * @return the <code>StyleConstants</code> attribute value that
0N/A * represents the CSS attribute value
0N/A */
0N/A Object toStyleConstants(StyleConstants key, View v) {
0N/A return family;
0N/A }
0N/A
0N/A String family;
0N/A }
0N/A
0N/A static class FontWeight extends CssValue {
0N/A
0N/A int getValue() {
0N/A return weight;
0N/A }
0N/A
0N/A Object parseCssValue(String value) {
0N/A FontWeight fw = new FontWeight();
0N/A fw.svalue = value;
0N/A if (value.equals("bold")) {
0N/A fw.weight = 700;
0N/A } else if (value.equals("normal")) {
0N/A fw.weight = 400;
0N/A } else {
0N/A // PENDING(prinz) add support for relative values
0N/A try {
0N/A fw.weight = Integer.parseInt(value);
0N/A } catch (NumberFormatException nfe) {
0N/A fw = null;
0N/A }
0N/A }
0N/A return fw;
0N/A }
0N/A
0N/A /**
0N/A * Converts a <code>StyleConstants</code> attribute value to
0N/A * a CSS attribute value. If there is no conversion
0N/A * returns <code>null</code>. By default, there is no conversion.
0N/A *
0N/A * @param key the <code>StyleConstants</code> attribute
0N/A * @param value the value of a <code>StyleConstants</code>
0N/A * attribute to be converted
0N/A * @return the CSS value that represents the
0N/A * <code>StyleConstants</code> value
0N/A */
0N/A Object fromStyleConstants(StyleConstants key, Object value) {
0N/A if (value.equals(Boolean.TRUE)) {
0N/A return parseCssValue("bold");
0N/A }
0N/A return parseCssValue("normal");
0N/A }
0N/A
0N/A /**
0N/A * Converts a CSS attribute value to a <code>StyleConstants</code>
0N/A * value. If there is no conversion, returns <code>null</code>.
0N/A * By default, there is no conversion.
0N/A *
0N/A * @param key the <code>StyleConstants</code> attribute
0N/A * @return the <code>StyleConstants</code> attribute value that
0N/A * represents the CSS attribute value
0N/A */
0N/A Object toStyleConstants(StyleConstants key, View v) {
0N/A return (weight > 500) ? Boolean.TRUE : Boolean.FALSE;
0N/A }
0N/A
0N/A boolean isBold() {
0N/A return (weight > 500);
0N/A }
0N/A
0N/A int weight;
0N/A }
0N/A
0N/A static class ColorValue extends CssValue {
0N/A
0N/A /**
0N/A * Returns the color to use.
0N/A */
0N/A Color getValue() {
0N/A return c;
0N/A }
0N/A
0N/A Object parseCssValue(String value) {
0N/A
0N/A Color c = stringToColor(value);
0N/A if (c != null) {
0N/A ColorValue cv = new ColorValue();
0N/A cv.svalue = value;
0N/A cv.c = c;
0N/A return cv;
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A Object parseHtmlValue(String value) {
0N/A return parseCssValue(value);
0N/A }
0N/A
0N/A /**
0N/A * Converts a <code>StyleConstants</code> attribute value to
0N/A * a CSS attribute value. If there is no conversion
0N/A * returns <code>null</code>. By default, there is no conversion.
0N/A *
0N/A * @param key the <code>StyleConstants</code> attribute
0N/A * @param value the value of a <code>StyleConstants</code>
0N/A * attribute to be converted
0N/A * @return the CSS value that represents the
0N/A * <code>StyleConstants</code> value
0N/A */
0N/A Object fromStyleConstants(StyleConstants key, Object value) {
0N/A ColorValue colorValue = new ColorValue();
0N/A colorValue.c = (Color)value;
0N/A colorValue.svalue = colorToHex(colorValue.c);
0N/A return colorValue;
0N/A }
0N/A
0N/A /**
0N/A * Converts a CSS attribute value to a <code>StyleConstants</code>
0N/A * value. If there is no conversion, returns <code>null</code>.
0N/A * By default, there is no conversion.
0N/A *
0N/A * @param key the <code>StyleConstants</code> attribute
0N/A * @return the <code>StyleConstants</code> attribute value that
0N/A * represents the CSS attribute value
0N/A */
0N/A Object toStyleConstants(StyleConstants key, View v) {
0N/A return c;
0N/A }
0N/A
0N/A Color c;
0N/A }
0N/A
0N/A static class BorderStyle extends CssValue {
0N/A
0N/A CSS.Value getValue() {
0N/A return style;
0N/A }
0N/A
0N/A Object parseCssValue(String value) {
0N/A CSS.Value cssv = CSS.getValue(value);
0N/A if (cssv != null) {
0N/A if ((cssv == CSS.Value.INSET) ||
0N/A (cssv == CSS.Value.OUTSET) ||
0N/A (cssv == CSS.Value.NONE) ||
0N/A (cssv == CSS.Value.DOTTED) ||
0N/A (cssv == CSS.Value.DASHED) ||
0N/A (cssv == CSS.Value.SOLID) ||
0N/A (cssv == CSS.Value.DOUBLE) ||
0N/A (cssv == CSS.Value.GROOVE) ||
0N/A (cssv == CSS.Value.RIDGE)) {
0N/A
0N/A BorderStyle bs = new BorderStyle();
0N/A bs.svalue = value;
0N/A bs.style = cssv;
0N/A return bs;
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A private void writeObject(java.io.ObjectOutputStream s)
0N/A throws IOException {
0N/A s.defaultWriteObject();
0N/A if (style == null) {
0N/A s.writeObject(null);
0N/A }
0N/A else {
0N/A s.writeObject(style.toString());
0N/A }
0N/A }
0N/A
0N/A private void readObject(ObjectInputStream s)
0N/A throws ClassNotFoundException, IOException {
0N/A s.defaultReadObject();
0N/A Object value = s.readObject();
0N/A if (value != null) {
0N/A style = CSS.getValue((String)value);
0N/A }
0N/A }
0N/A
0N/A // CSS.Values are static, don't archive it.
0N/A transient private CSS.Value style;
0N/A }
0N/A
0N/A static class LengthValue extends CssValue {
0N/A
0N/A /**
0N/A * if this length value may be negative.
0N/A */
0N/A boolean mayBeNegative;
0N/A
0N/A LengthValue() {
0N/A this(false);
0N/A }
0N/A
0N/A LengthValue(boolean mayBeNegative) {
0N/A this.mayBeNegative = mayBeNegative;
0N/A }
0N/A
0N/A /**
0N/A * Returns the length (span) to use.
0N/A */
0N/A float getValue() {
0N/A return getValue(false);
0N/A }
0N/A
0N/A float getValue(boolean isW3CLengthUnits) {
0N/A return getValue(0, isW3CLengthUnits);
0N/A }
0N/A
0N/A /**
0N/A * Returns the length (span) to use. If the value represents
0N/A * a percentage, it is scaled based on <code>currentValue</code>.
0N/A */
0N/A float getValue(float currentValue) {
0N/A return getValue(currentValue, false);
0N/A }
0N/A float getValue(float currentValue, boolean isW3CLengthUnits) {
0N/A if (percentage) {
0N/A return span * currentValue;
0N/A }
0N/A return LengthUnit.getValue(span, units, isW3CLengthUnits);
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the length represents a percentage of the
0N/A * containing box.
0N/A */
0N/A boolean isPercentage() {
0N/A return percentage;
0N/A }
0N/A
0N/A Object parseCssValue(String value) {
0N/A LengthValue lv;
0N/A try {
0N/A // Assume pixels
0N/A float absolute = Float.valueOf(value).floatValue();
0N/A lv = new LengthValue();
0N/A lv.span = absolute;
0N/A } catch (NumberFormatException nfe) {
0N/A // Not pixels, use LengthUnit
0N/A LengthUnit lu = new LengthUnit(value,
0N/A LengthUnit.UNINITALIZED_LENGTH,
0N/A 0);
0N/A
0N/A // PENDING: currently, we only support absolute values and
0N/A // percentages.
0N/A switch (lu.type) {
0N/A case 0:
0N/A // Absolute
0N/A lv = new LengthValue();
0N/A lv.span =
0N/A (mayBeNegative) ? lu.value : Math.max(0, lu.value);
0N/A lv.units = lu.units;
0N/A break;
0N/A case 1:
0N/A // %
0N/A lv = new LengthValue();
0N/A lv.span = Math.max(0, Math.min(1, lu.value));
0N/A lv.percentage = true;
0N/A break;
0N/A default:
0N/A return null;
0N/A }
0N/A }
0N/A lv.svalue = value;
0N/A return lv;
0N/A }
0N/A
0N/A Object parseHtmlValue(String value) {
0N/A if (value.equals(HTML.NULL_ATTRIBUTE_VALUE)) {
0N/A value = "1";
0N/A }
0N/A return parseCssValue(value);
0N/A }
0N/A /**
0N/A * Converts a <code>StyleConstants</code> attribute value to
0N/A * a CSS attribute value. If there is no conversion,
0N/A * returns <code>null</code>. By default, there is no conversion.
0N/A *
0N/A * @param key the <code>StyleConstants</code> attribute
0N/A * @param value the value of a <code>StyleConstants</code>
0N/A * attribute to be converted
0N/A * @return the CSS value that represents the
0N/A * <code>StyleConstants</code> value
0N/A */
0N/A Object fromStyleConstants(StyleConstants key, Object value) {
0N/A LengthValue v = new LengthValue();
0N/A v.svalue = value.toString();
0N/A v.span = ((Float)value).floatValue();
0N/A return v;
0N/A }
0N/A
0N/A /**
0N/A * Converts a CSS attribute value to a <code>StyleConstants</code>
0N/A * value. If there is no conversion, returns <code>null</code>.
0N/A * By default, there is no conversion.
0N/A *
0N/A * @param key the <code>StyleConstants</code> attribute
0N/A * @return the <code>StyleConstants</code> attribute value that
0N/A * represents the CSS attribute value
0N/A */
0N/A Object toStyleConstants(StyleConstants key, View v) {
0N/A return new Float(getValue(false));
0N/A }
0N/A
0N/A /** If true, span is a percentage value, and that to determine
0N/A * the length another value needs to be passed in. */
0N/A boolean percentage;
0N/A /** Either the absolute value (percentage == false) or
0N/A * a percentage value. */
0N/A float span;
0N/A
0N/A String units = null;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * BorderWidthValue is used to model BORDER_XXX_WIDTH and adds support
0N/A * for the thin/medium/thick values.
0N/A */
0N/A static class BorderWidthValue extends LengthValue {
0N/A BorderWidthValue(String svalue, int index) {
0N/A this.svalue = svalue;
0N/A span = values[index];
0N/A percentage = false;
0N/A }
0N/A
0N/A Object parseCssValue(String value) {
0N/A if (value != null) {
0N/A if (value.equals("thick")) {
0N/A return new BorderWidthValue(value, 2);
0N/A }
0N/A else if (value.equals("medium")) {
0N/A return new BorderWidthValue(value, 1);
0N/A }
0N/A else if (value.equals("thin")) {
0N/A return new BorderWidthValue(value, 0);
0N/A }
0N/A }
0N/A // Assume its a length.
0N/A return super.parseCssValue(value);
0N/A }
0N/A
0N/A Object parseHtmlValue(String value) {
0N/A if (value == HTML.NULL_ATTRIBUTE_VALUE) {
0N/A return parseCssValue("medium");
0N/A }
0N/A return parseCssValue(value);
0N/A }
0N/A
0N/A /** Values used to represent border width. */
0N/A private static final float[] values = { 1, 2, 4 };
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Handles uniquing of CSS values, like lists, and background image
0N/A * repeating.
0N/A */
0N/A static class CssValueMapper extends CssValue {
0N/A Object parseCssValue(String value) {
0N/A Object retValue = cssValueToInternalValueMap.get(value);
0N/A if (retValue == null) {
0N/A retValue = cssValueToInternalValueMap.get(value.toLowerCase());
0N/A }
0N/A return retValue;
0N/A }
0N/A
0N/A
0N/A Object parseHtmlValue(String value) {
0N/A Object retValue = htmlValueToCssValueMap.get(value);
0N/A if (retValue == null) {
0N/A retValue = htmlValueToCssValueMap.get(value.toLowerCase());
0N/A }
0N/A return retValue;
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Used for background images, to represent the position.
0N/A */
0N/A static class BackgroundPosition extends CssValue {
0N/A float horizontalPosition;
0N/A float verticalPosition;
0N/A // bitmask: bit 0, horizontal relative, bit 1 horizontal relative to
0N/A // font size, 2 vertical relative to size, 3 vertical relative to
0N/A // font size.
0N/A //
0N/A short relative;
0N/A
0N/A Object parseCssValue(String value) {
0N/A // 'top left' and 'left top' both mean the same as '0% 0%'.
0N/A // 'top', 'top center' and 'center top' mean the same as '50% 0%'.
0N/A // 'right top' and 'top right' mean the same as '100% 0%'.
0N/A // 'left', 'left center' and 'center left' mean the same as
0N/A // '0% 50%'.
0N/A // 'center' and 'center center' mean the same as '50% 50%'.
0N/A // 'right', 'right center' and 'center right' mean the same as
0N/A // '100% 50%'.
0N/A // 'bottom left' and 'left bottom' mean the same as '0% 100%'.
0N/A // 'bottom', 'bottom center' and 'center bottom' mean the same as
0N/A // '50% 100%'.
0N/A // 'bottom right' and 'right bottom' mean the same as '100% 100%'.
0N/A String[] strings = CSS.parseStrings(value);
0N/A int count = strings.length;
0N/A BackgroundPosition bp = new BackgroundPosition();
0N/A bp.relative = 5;
0N/A bp.svalue = value;
0N/A
0N/A if (count > 0) {
0N/A // bit 0 for vert, 1 hor, 2 for center
0N/A short found = 0;
0N/A int index = 0;
0N/A while (index < count) {
0N/A // First, check for keywords
0N/A String string = strings[index++];
0N/A if (string.equals("center")) {
0N/A found |= 4;
0N/A continue;
0N/A }
0N/A else {
0N/A if ((found & 1) == 0) {
0N/A if (string.equals("top")) {
0N/A found |= 1;
0N/A }
0N/A else if (string.equals("bottom")) {
0N/A found |= 1;
0N/A bp.verticalPosition = 1;
0N/A continue;
0N/A }
0N/A }
0N/A if ((found & 2) == 0) {
0N/A if (string.equals("left")) {
0N/A found |= 2;
0N/A bp.horizontalPosition = 0;
0N/A }
0N/A else if (string.equals("right")) {
0N/A found |= 2;
0N/A bp.horizontalPosition = 1;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A if (found != 0) {
0N/A if ((found & 1) == 1) {
0N/A if ((found & 2) == 0) {
0N/A // vert and no horiz.
0N/A bp.horizontalPosition = .5f;
0N/A }
0N/A }
0N/A else if ((found & 2) == 2) {
0N/A // horiz and no vert.
0N/A bp.verticalPosition = .5f;
0N/A }
0N/A else {
0N/A // no horiz, no vert, but center
0N/A bp.horizontalPosition = bp.verticalPosition = .5f;
0N/A }
0N/A }
0N/A else {
0N/A // Assume lengths
0N/A LengthUnit lu = new LengthUnit(strings[0], (short)0, 0f);
0N/A
0N/A if (lu.type == 0) {
0N/A bp.horizontalPosition = lu.value;
0N/A bp.relative = (short)(1 ^ bp.relative);
0N/A }
0N/A else if (lu.type == 1) {
0N/A bp.horizontalPosition = lu.value;
0N/A }
0N/A else if (lu.type == 3) {
0N/A bp.horizontalPosition = lu.value;
0N/A bp.relative = (short)((1 ^ bp.relative) | 2);
0N/A }
0N/A if (count > 1) {
0N/A lu = new LengthUnit(strings[1], (short)0, 0f);
0N/A
0N/A if (lu.type == 0) {
0N/A bp.verticalPosition = lu.value;
0N/A bp.relative = (short)(4 ^ bp.relative);
0N/A }
0N/A else if (lu.type == 1) {
0N/A bp.verticalPosition = lu.value;
0N/A }
0N/A else if (lu.type == 3) {
0N/A bp.verticalPosition = lu.value;
0N/A bp.relative = (short)((4 ^ bp.relative) | 8);
0N/A }
0N/A }
0N/A else {
0N/A bp.verticalPosition = .5f;
0N/A }
0N/A }
0N/A }
0N/A return bp;
0N/A }
0N/A
0N/A boolean isHorizontalPositionRelativeToSize() {
0N/A return ((relative & 1) == 1);
0N/A }
0N/A
0N/A boolean isHorizontalPositionRelativeToFontSize() {
0N/A return ((relative & 2) == 2);
0N/A }
0N/A
0N/A float getHorizontalPosition() {
0N/A return horizontalPosition;
0N/A }
0N/A
0N/A boolean isVerticalPositionRelativeToSize() {
0N/A return ((relative & 4) == 4);
0N/A }
0N/A
0N/A boolean isVerticalPositionRelativeToFontSize() {
0N/A return ((relative & 8) == 8);
0N/A }
0N/A
0N/A float getVerticalPosition() {
0N/A return verticalPosition;
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Used for BackgroundImages.
0N/A */
0N/A static class BackgroundImage extends CssValue {
0N/A private boolean loadedImage;
0N/A private ImageIcon image;
0N/A
0N/A Object parseCssValue(String value) {
0N/A BackgroundImage retValue = new BackgroundImage();
0N/A retValue.svalue = value;
0N/A return retValue;
0N/A }
0N/A
0N/A Object parseHtmlValue(String value) {
0N/A return parseCssValue(value);
0N/A }
0N/A
0N/A // PENDING: this base is wrong for linked style sheets.
0N/A ImageIcon getImage(URL base) {
0N/A if (!loadedImage) {
0N/A synchronized(this) {
0N/A if (!loadedImage) {
0N/A URL url = CSS.getURL(base, svalue);
0N/A loadedImage = true;
0N/A if (url != null) {
0N/A image = new ImageIcon();
0N/A Image tmpImg = Toolkit.getDefaultToolkit().createImage(url);
0N/A if (tmpImg != null) {
0N/A image.setImage(tmpImg);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A return image;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Parses a length value, this is used internally, and never added
0N/A * to an AttributeSet or returned to the developer.
0N/A */
0N/A static class LengthUnit implements Serializable {
611N/A static Hashtable<String, Float> lengthMapping = new Hashtable<String, Float>(6);
611N/A static Hashtable<String, Float> w3cLengthMapping = new Hashtable<String, Float>(6);
0N/A static {
0N/A lengthMapping.put("pt", new Float(1f));
0N/A // Not sure about 1.3, determined by experiementation.
0N/A lengthMapping.put("px", new Float(1.3f));
0N/A lengthMapping.put("mm", new Float(2.83464f));
0N/A lengthMapping.put("cm", new Float(28.3464f));
0N/A lengthMapping.put("pc", new Float(12f));
0N/A lengthMapping.put("in", new Float(72f));
0N/A int res = 72;
0N/A try {
0N/A res = Toolkit.getDefaultToolkit().getScreenResolution();
0N/A } catch (HeadlessException e) {
0N/A }
0N/A // mapping according to the CSS2 spec
0N/A w3cLengthMapping.put("pt", new Float(res/72f));
0N/A w3cLengthMapping.put("px", new Float(1f));
0N/A w3cLengthMapping.put("mm", new Float(res/25.4f));
0N/A w3cLengthMapping.put("cm", new Float(res/2.54f));
0N/A w3cLengthMapping.put("pc", new Float(res/6f));
0N/A w3cLengthMapping.put("in", new Float(res));
0N/A }
0N/A
0N/A LengthUnit(String value, short defaultType, float defaultValue) {
0N/A parse(value, defaultType, defaultValue);
0N/A }
0N/A
0N/A void parse(String value, short defaultType, float defaultValue) {
0N/A type = defaultType;
0N/A this.value = defaultValue;
0N/A
0N/A int length = value.length();
0N/A if (length > 0 && value.charAt(length - 1) == '%') {
0N/A try {
0N/A this.value = Float.valueOf(value.substring(0, length - 1)).
0N/A floatValue() / 100.0f;
0N/A type = 1;
0N/A }
0N/A catch (NumberFormatException nfe) { }
0N/A }
0N/A if (length >= 2) {
0N/A units = value.substring(length - 2, length);
611N/A Float scale = lengthMapping.get(units);
0N/A if (scale != null) {
0N/A try {
0N/A this.value = Float.valueOf(value.substring(0,
0N/A length - 2)).floatValue();
0N/A type = 0;
0N/A }
0N/A catch (NumberFormatException nfe) { }
0N/A }
0N/A else if (units.equals("em") ||
0N/A units.equals("ex")) {
0N/A try {
0N/A this.value = Float.valueOf(value.substring(0,
0N/A length - 2)).floatValue();
0N/A type = 3;
0N/A }
0N/A catch (NumberFormatException nfe) { }
0N/A }
0N/A else if (value.equals("larger")) {
0N/A this.value = 2f;
0N/A type = 2;
0N/A }
0N/A else if (value.equals("smaller")) {
0N/A this.value = -2;
0N/A type = 2;
0N/A }
0N/A else {
0N/A // treat like points.
0N/A try {
0N/A this.value = Float.valueOf(value).floatValue();
0N/A type = 0;
0N/A } catch (NumberFormatException nfe) {}
0N/A }
0N/A }
0N/A else if (length > 0) {
0N/A // treat like points.
0N/A try {
0N/A this.value = Float.valueOf(value).floatValue();
0N/A type = 0;
0N/A } catch (NumberFormatException nfe) {}
0N/A }
0N/A }
0N/A
0N/A float getValue(boolean w3cLengthUnits) {
611N/A Hashtable<String, Float> mapping = (w3cLengthUnits) ? w3cLengthMapping : lengthMapping;
0N/A float scale = 1;
0N/A if (units != null) {
611N/A Float scaleFloat = mapping.get(units);
0N/A if (scaleFloat != null) {
0N/A scale = scaleFloat.floatValue();
0N/A }
0N/A }
0N/A return this.value * scale;
0N/A
0N/A }
0N/A
0N/A static float getValue(float value, String units, Boolean w3cLengthUnits) {
611N/A Hashtable<String, Float> mapping = (w3cLengthUnits) ? w3cLengthMapping : lengthMapping;
0N/A float scale = 1;
0N/A if (units != null) {
611N/A Float scaleFloat = mapping.get(units);
0N/A if (scaleFloat != null) {
0N/A scale = scaleFloat.floatValue();
0N/A }
0N/A }
0N/A return value * scale;
0N/A }
0N/A
0N/A public String toString() {
0N/A return type + " " + value;
0N/A }
0N/A
0N/A // 0 - value indicates real value
0N/A // 1 - % value, value relative to depends upon key.
0N/A // 50% will have a value = .5
0N/A // 2 - add value to parent value.
0N/A // 3 - em/ex relative to font size of element (except for
0N/A // font-size, which is relative to parent).
0N/A short type;
0N/A float value;
0N/A String units = null;
0N/A
0N/A
0N/A static final short UNINITALIZED_LENGTH = (short)10;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Class used to parse font property. The font property is shorthand
0N/A * for the other font properties. This expands the properties, placing
0N/A * them in the attributeset.
0N/A */
0N/A static class ShorthandFontParser {
0N/A /**
0N/A * Parses the shorthand font string <code>value</code>, placing the
0N/A * result in <code>attr</code>.
0N/A */
0N/A static void parseShorthandFont(CSS css, String value,
0N/A MutableAttributeSet attr) {
0N/A // font is of the form:
0N/A // [ <font-style> || <font-variant> || <font-weight> ]? <font-size>
0N/A // [ / <line-height> ]? <font-family>
0N/A String[] strings = CSS.parseStrings(value);
0N/A int count = strings.length;
0N/A int index = 0;
0N/A // bitmask, 1 for style, 2 for variant, 3 for weight
0N/A short found = 0;
0N/A int maxC = Math.min(3, count);
0N/A
0N/A // Check for font-style font-variant font-weight
0N/A while (index < maxC) {
0N/A if ((found & 1) == 0 && isFontStyle(strings[index])) {
0N/A css.addInternalCSSValue(attr, CSS.Attribute.FONT_STYLE,
0N/A strings[index++]);
0N/A found |= 1;
0N/A }
0N/A else if ((found & 2) == 0 && isFontVariant(strings[index])) {
0N/A css.addInternalCSSValue(attr, CSS.Attribute.FONT_VARIANT,
0N/A strings[index++]);
0N/A found |= 2;
0N/A }
0N/A else if ((found & 4) == 0 && isFontWeight(strings[index])) {
0N/A css.addInternalCSSValue(attr, CSS.Attribute.FONT_WEIGHT,
0N/A strings[index++]);
0N/A found |= 4;
0N/A }
0N/A else if (strings[index].equals("normal")) {
0N/A index++;
0N/A }
0N/A else {
0N/A break;
0N/A }
0N/A }
0N/A if ((found & 1) == 0) {
0N/A css.addInternalCSSValue(attr, CSS.Attribute.FONT_STYLE,
0N/A "normal");
0N/A }
0N/A if ((found & 2) == 0) {
0N/A css.addInternalCSSValue(attr, CSS.Attribute.FONT_VARIANT,
0N/A "normal");
0N/A }
0N/A if ((found & 4) == 0) {
0N/A css.addInternalCSSValue(attr, CSS.Attribute.FONT_WEIGHT,
0N/A "normal");
0N/A }
0N/A
0N/A // string at index should be the font-size
0N/A if (index < count) {
0N/A String fontSize = strings[index];
0N/A int slashIndex = fontSize.indexOf('/');
0N/A
0N/A if (slashIndex != -1) {
0N/A fontSize = fontSize.substring(0, slashIndex);
0N/A strings[index] = strings[index].substring(slashIndex);
0N/A }
0N/A else {
0N/A index++;
0N/A }
0N/A css.addInternalCSSValue(attr, CSS.Attribute.FONT_SIZE,
0N/A fontSize);
0N/A }
0N/A else {
0N/A css.addInternalCSSValue(attr, CSS.Attribute.FONT_SIZE,
0N/A "medium");
0N/A }
0N/A
0N/A // Check for line height
0N/A if (index < count && strings[index].startsWith("/")) {
0N/A String lineHeight = null;
0N/A if (strings[index].equals("/")) {
0N/A if (++index < count) {
0N/A lineHeight = strings[index++];
0N/A }
0N/A }
0N/A else {
0N/A lineHeight = strings[index++].substring(1);
0N/A }
0N/A // line height
0N/A if (lineHeight != null) {
0N/A css.addInternalCSSValue(attr, CSS.Attribute.LINE_HEIGHT,
0N/A lineHeight);
0N/A }
0N/A else {
0N/A css.addInternalCSSValue(attr, CSS.Attribute.LINE_HEIGHT,
0N/A "normal");
0N/A }
0N/A }
0N/A else {
0N/A css.addInternalCSSValue(attr, CSS.Attribute.LINE_HEIGHT,
0N/A "normal");
0N/A }
0N/A
0N/A // remainder of strings are font-family
0N/A if (index < count) {
0N/A String family = strings[index++];
0N/A
0N/A while (index < count) {
0N/A family += " " + strings[index++];
0N/A }
0N/A css.addInternalCSSValue(attr, CSS.Attribute.FONT_FAMILY,
0N/A family);
0N/A }
0N/A else {
0N/A css.addInternalCSSValue(attr, CSS.Attribute.FONT_FAMILY,
0N/A Font.SANS_SERIF);
0N/A }
0N/A }
0N/A
0N/A private static boolean isFontStyle(String string) {
0N/A return (string.equals("italic") ||
0N/A string.equals("oblique"));
0N/A }
0N/A
0N/A private static boolean isFontVariant(String string) {
0N/A return (string.equals("small-caps"));
0N/A }
0N/A
0N/A private static boolean isFontWeight(String string) {
0N/A if (string.equals("bold") || string.equals("bolder") ||
0N/A string.equals("italic") || string.equals("lighter")) {
0N/A return true;
0N/A }
0N/A // test for 100-900
0N/A return (string.length() == 3 &&
0N/A string.charAt(0) >= '1' && string.charAt(0) <= '9' &&
0N/A string.charAt(1) == '0' && string.charAt(2) == '0');
0N/A }
0N/A
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Parses the background property into its intrinsic values.
0N/A */
0N/A static class ShorthandBackgroundParser {
0N/A /**
0N/A * Parses the shorthand font string <code>value</code>, placing the
0N/A * result in <code>attr</code>.
0N/A */
0N/A static void parseShorthandBackground(CSS css, String value,
0N/A MutableAttributeSet attr) {
0N/A String[] strings = parseStrings(value);
0N/A int count = strings.length;
0N/A int index = 0;
0N/A // bitmask: 0 for image, 1 repeat, 2 attachment, 3 position,
0N/A // 4 color
0N/A short found = 0;
0N/A
0N/A while (index < count) {
0N/A String string = strings[index++];
0N/A if ((found & 1) == 0 && isImage(string)) {
0N/A css.addInternalCSSValue(attr, CSS.Attribute.
0N/A BACKGROUND_IMAGE, string);
0N/A found |= 1;
0N/A }
0N/A else if ((found & 2) == 0 && isRepeat(string)) {
0N/A css.addInternalCSSValue(attr, CSS.Attribute.
0N/A BACKGROUND_REPEAT, string);
0N/A found |= 2;
0N/A }
0N/A else if ((found & 4) == 0 && isAttachment(string)) {
0N/A css.addInternalCSSValue(attr, CSS.Attribute.
0N/A BACKGROUND_ATTACHMENT, string);
0N/A found |= 4;
0N/A }
0N/A else if ((found & 8) == 0 && isPosition(string)) {
0N/A if (index < count && isPosition(strings[index])) {
0N/A css.addInternalCSSValue(attr, CSS.Attribute.
0N/A BACKGROUND_POSITION,
0N/A string + " " +
0N/A strings[index++]);
0N/A }
0N/A else {
0N/A css.addInternalCSSValue(attr, CSS.Attribute.
0N/A BACKGROUND_POSITION, string);
0N/A }
0N/A found |= 8;
0N/A }
0N/A else if ((found & 16) == 0 && isColor(string)) {
0N/A css.addInternalCSSValue(attr, CSS.Attribute.
0N/A BACKGROUND_COLOR, string);
0N/A found |= 16;
0N/A }
0N/A }
0N/A if ((found & 1) == 0) {
0N/A css.addInternalCSSValue(attr, CSS.Attribute.BACKGROUND_IMAGE,
0N/A null);
0N/A }
0N/A if ((found & 2) == 0) {
0N/A css.addInternalCSSValue(attr, CSS.Attribute.BACKGROUND_REPEAT,
0N/A "repeat");
0N/A }
0N/A if ((found & 4) == 0) {
0N/A css.addInternalCSSValue(attr, CSS.Attribute.
0N/A BACKGROUND_ATTACHMENT, "scroll");
0N/A }
0N/A if ((found & 8) == 0) {
0N/A css.addInternalCSSValue(attr, CSS.Attribute.
0N/A BACKGROUND_POSITION, null);
0N/A }
0N/A // Currently, there is no good way to express this.
0N/A /*
0N/A if ((found & 16) == 0) {
0N/A css.addInternalCSSValue(attr, CSS.Attribute.BACKGROUND_COLOR,
0N/A null);
0N/A }
0N/A */
0N/A }
0N/A
0N/A static boolean isImage(String string) {
0N/A return (string.startsWith("url(") && string.endsWith(")"));
0N/A }
0N/A
0N/A static boolean isRepeat(String string) {
0N/A return (string.equals("repeat-x") || string.equals("repeat-y") ||
0N/A string.equals("repeat") || string.equals("no-repeat"));
0N/A }
0N/A
0N/A static boolean isAttachment(String string) {
0N/A return (string.equals("fixed") || string.equals("scroll"));
0N/A }
0N/A
0N/A static boolean isPosition(String string) {
0N/A return (string.equals("top") || string.equals("bottom") ||
0N/A string.equals("left") || string.equals("right") ||
0N/A string.equals("center") ||
0N/A (string.length() > 0 &&
0N/A Character.isDigit(string.charAt(0))));
0N/A }
0N/A
0N/A static boolean isColor(String string) {
0N/A return (CSS.stringToColor(string) != null);
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Used to parser margin and padding.
0N/A */
0N/A static class ShorthandMarginParser {
0N/A /**
0N/A * Parses the shorthand margin/padding/border string
0N/A * <code>value</code>, placing the result in <code>attr</code>.
0N/A * <code>names</code> give the 4 instrinsic property names.
0N/A */
0N/A static void parseShorthandMargin(CSS css, String value,
0N/A MutableAttributeSet attr,
0N/A CSS.Attribute[] names) {
0N/A String[] strings = parseStrings(value);
0N/A int count = strings.length;
0N/A int index = 0;
0N/A switch (count) {
0N/A case 0:
0N/A // empty string
0N/A return;
0N/A case 1:
0N/A // Identifies all values.
0N/A for (int counter = 0; counter < 4; counter++) {
0N/A css.addInternalCSSValue(attr, names[counter], strings[0]);
0N/A }
0N/A break;
0N/A case 2:
0N/A // 0 & 2 = strings[0], 1 & 3 = strings[1]
0N/A css.addInternalCSSValue(attr, names[0], strings[0]);
0N/A css.addInternalCSSValue(attr, names[2], strings[0]);
0N/A css.addInternalCSSValue(attr, names[1], strings[1]);
0N/A css.addInternalCSSValue(attr, names[3], strings[1]);
0N/A break;
0N/A case 3:
0N/A css.addInternalCSSValue(attr, names[0], strings[0]);
0N/A css.addInternalCSSValue(attr, names[1], strings[1]);
0N/A css.addInternalCSSValue(attr, names[2], strings[2]);
0N/A css.addInternalCSSValue(attr, names[3], strings[1]);
0N/A break;
0N/A default:
0N/A for (int counter = 0; counter < 4; counter++) {
0N/A css.addInternalCSSValue(attr, names[counter],
0N/A strings[counter]);
0N/A }
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A
0N/A static class ShorthandBorderParser {
0N/A static Attribute[] keys = {
0N/A Attribute.BORDER_TOP, Attribute.BORDER_RIGHT,
0N/A Attribute.BORDER_BOTTOM, Attribute.BORDER_LEFT,
0N/A };
0N/A
0N/A static void parseShorthandBorder(MutableAttributeSet attributes,
0N/A CSS.Attribute key, String value) {
0N/A Object[] parts = new Object[CSSBorder.PARSERS.length];
0N/A String[] strings = parseStrings(value);
0N/A for (String s : strings) {
0N/A boolean valid = false;
0N/A for (int i = 0; i < parts.length; i++) {
0N/A Object v = CSSBorder.PARSERS[i].parseCssValue(s);
0N/A if (v != null) {
0N/A if (parts[i] == null) {
0N/A parts[i] = v;
0N/A valid = true;
0N/A }
0N/A break;
0N/A }
0N/A }
0N/A if (!valid) {
0N/A // Part is non-parseable or occured more than once.
0N/A return;
0N/A }
0N/A }
0N/A
0N/A // Unspecified parts get default values.
0N/A for (int i = 0; i < parts.length; i++) {
0N/A if (parts[i] == null) {
0N/A parts[i] = CSSBorder.DEFAULTS[i];
0N/A }
0N/A }
0N/A
0N/A // Dispatch collected values to individual properties.
0N/A for (int i = 0; i < keys.length; i++) {
0N/A if ((key == Attribute.BORDER) || (key == keys[i])) {
0N/A for (int k = 0; k < parts.length; k++) {
0N/A attributes.addAttribute(
0N/A CSSBorder.ATTRIBUTES[k][i], parts[k]);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Calculate the requirements needed to tile the requirements
0N/A * given by the iterator that would be tiled. The calculation
0N/A * takes into consideration margin and border spacing.
0N/A */
0N/A static SizeRequirements calculateTiledRequirements(LayoutIterator iter, SizeRequirements r) {
0N/A long minimum = 0;
0N/A long maximum = 0;
0N/A long preferred = 0;
0N/A int lastMargin = 0;
0N/A int totalSpacing = 0;
0N/A int n = iter.getCount();
0N/A for (int i = 0; i < n; i++) {
0N/A iter.setIndex(i);
0N/A int margin0 = lastMargin;
0N/A int margin1 = (int) iter.getLeadingCollapseSpan();
611N/A totalSpacing += Math.max(margin0, margin1);
0N/A preferred += (int) iter.getPreferredSpan(0);
0N/A minimum += iter.getMinimumSpan(0);
0N/A maximum += iter.getMaximumSpan(0);
0N/A
0N/A lastMargin = (int) iter.getTrailingCollapseSpan();
0N/A }
0N/A totalSpacing += lastMargin;
0N/A totalSpacing += 2 * iter.getBorderWidth();
0N/A
0N/A // adjust for the spacing area
0N/A minimum += totalSpacing;
0N/A preferred += totalSpacing;
0N/A maximum += totalSpacing;
0N/A
0N/A // set return value
0N/A if (r == null) {
0N/A r = new SizeRequirements();
0N/A }
0N/A r.minimum = (minimum > Integer.MAX_VALUE) ? Integer.MAX_VALUE : (int)minimum;
0N/A r.preferred = (preferred > Integer.MAX_VALUE) ? Integer.MAX_VALUE :(int) preferred;
0N/A r.maximum = (maximum > Integer.MAX_VALUE) ? Integer.MAX_VALUE :(int) maximum;
0N/A return r;
0N/A }
0N/A
0N/A /**
0N/A * Calculate a tiled layout for the given iterator.
0N/A * This should be done collapsing the neighboring
0N/A * margins to be a total of the maximum of the two
0N/A * neighboring margin areas as described in the CSS spec.
0N/A */
0N/A static void calculateTiledLayout(LayoutIterator iter, int targetSpan) {
0N/A
0N/A /*
0N/A * first pass, calculate the preferred sizes, adjustments needed because
0N/A * of margin collapsing, and the flexibility to adjust the sizes.
0N/A */
0N/A long preferred = 0;
611N/A long currentPreferred;
0N/A int lastMargin = 0;
0N/A int totalSpacing = 0;
0N/A int n = iter.getCount();
0N/A int adjustmentWeightsCount = LayoutIterator.WorstAdjustmentWeight + 1;
0N/A //max gain we can get adjusting elements with adjustmentWeight <= i
0N/A long gain[] = new long[adjustmentWeightsCount];
0N/A //max loss we can get adjusting elements with adjustmentWeight <= i
0N/A long loss[] = new long[adjustmentWeightsCount];
0N/A
0N/A for (int i = 0; i < adjustmentWeightsCount; i++) {
0N/A gain[i] = loss[i] = 0;
0N/A }
0N/A for (int i = 0; i < n; i++) {
0N/A iter.setIndex(i);
0N/A int margin0 = lastMargin;
0N/A int margin1 = (int) iter.getLeadingCollapseSpan();
0N/A
0N/A iter.setOffset(Math.max(margin0, margin1));
0N/A totalSpacing += iter.getOffset();
0N/A
0N/A currentPreferred = (long)iter.getPreferredSpan(targetSpan);
0N/A iter.setSpan((int) currentPreferred);
0N/A preferred += currentPreferred;
0N/A gain[iter.getAdjustmentWeight()] +=
0N/A (long)iter.getMaximumSpan(targetSpan) - currentPreferred;
0N/A loss[iter.getAdjustmentWeight()] +=
0N/A currentPreferred - (long)iter.getMinimumSpan(targetSpan);
0N/A lastMargin = (int) iter.getTrailingCollapseSpan();
0N/A }
0N/A totalSpacing += lastMargin;
0N/A totalSpacing += 2 * iter.getBorderWidth();
0N/A
0N/A for (int i = 1; i < adjustmentWeightsCount; i++) {
0N/A gain[i] += gain[i - 1];
0N/A loss[i] += loss[i - 1];
0N/A }
0N/A
0N/A /*
0N/A * Second pass, expand or contract by as much as possible to reach
0N/A * the target span. This takes the margin collapsing into account
0N/A * prior to adjusting the span.
0N/A */
0N/A
0N/A // determine the adjustment to be made
0N/A int allocated = targetSpan - totalSpacing;
0N/A long desiredAdjustment = allocated - preferred;
0N/A long adjustmentsArray[] = (desiredAdjustment > 0) ? gain : loss;
0N/A desiredAdjustment = Math.abs(desiredAdjustment);
0N/A int adjustmentLevel = 0;
0N/A for (;adjustmentLevel <= LayoutIterator.WorstAdjustmentWeight;
0N/A adjustmentLevel++) {
0N/A // adjustmentsArray[] is sorted. I do not bother about
0N/A // binary search though
0N/A if (adjustmentsArray[adjustmentLevel] >= desiredAdjustment) {
0N/A break;
0N/A }
0N/A }
0N/A float adjustmentFactor = 0.0f;
0N/A if (adjustmentLevel <= LayoutIterator.WorstAdjustmentWeight) {
0N/A desiredAdjustment -= (adjustmentLevel > 0) ?
0N/A adjustmentsArray[adjustmentLevel - 1] : 0;
0N/A if (desiredAdjustment != 0) {
0N/A float maximumAdjustment =
0N/A adjustmentsArray[adjustmentLevel] -
0N/A ((adjustmentLevel > 0) ?
0N/A adjustmentsArray[adjustmentLevel - 1] : 0
0N/A );
0N/A adjustmentFactor = desiredAdjustment / maximumAdjustment;
0N/A }
0N/A }
0N/A // make the adjustments
611N/A int totalOffset = (int)iter.getBorderWidth();
0N/A for (int i = 0; i < n; i++) {
0N/A iter.setIndex(i);
0N/A iter.setOffset( iter.getOffset() + totalOffset);
0N/A if (iter.getAdjustmentWeight() < adjustmentLevel) {
0N/A iter.setSpan((int)
0N/A ((allocated > preferred) ?
0N/A Math.floor(iter.getMaximumSpan(targetSpan)) :
0N/A Math.ceil(iter.getMinimumSpan(targetSpan))
0N/A )
0N/A );
0N/A } else if (iter.getAdjustmentWeight() == adjustmentLevel) {
0N/A int availableSpan = (allocated > preferred) ?
0N/A (int) iter.getMaximumSpan(targetSpan) - iter.getSpan() :
0N/A iter.getSpan() - (int) iter.getMinimumSpan(targetSpan);
0N/A int adj = (int)Math.floor(adjustmentFactor * availableSpan);
0N/A iter.setSpan(iter.getSpan() +
0N/A ((allocated > preferred) ? adj : -adj));
0N/A }
0N/A totalOffset = (int) Math.min((long) iter.getOffset() +
0N/A (long) iter.getSpan(),
0N/A Integer.MAX_VALUE);
0N/A }
0N/A
0N/A // while rounding we could lose several pixels.
0N/A int roundError = targetSpan - totalOffset -
0N/A (int)iter.getTrailingCollapseSpan() -
0N/A (int)iter.getBorderWidth();
0N/A int adj = (roundError > 0) ? 1 : -1;
0N/A roundError *= adj;
0N/A
0N/A boolean canAdjust = true;
0N/A while (roundError > 0 && canAdjust) {
0N/A // check for infinite loop
0N/A canAdjust = false;
0N/A int offsetAdjust = 0;
0N/A // try to distribute roundError. one pixel per cell
0N/A for (int i = 0; i < n; i++) {
0N/A iter.setIndex(i);
0N/A iter.setOffset(iter.getOffset() + offsetAdjust);
0N/A int curSpan = iter.getSpan();
0N/A if (roundError > 0) {
0N/A int boundGap = (adj > 0) ?
0N/A (int)Math.floor(iter.getMaximumSpan(targetSpan)) - curSpan :
0N/A curSpan - (int)Math.ceil(iter.getMinimumSpan(targetSpan));
0N/A if (boundGap >= 1) {
0N/A canAdjust = true;
0N/A iter.setSpan(curSpan + adj);
0N/A offsetAdjust += adj;
0N/A roundError--;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * An iterator to express the requirements to use when computing
0N/A * layout.
0N/A */
0N/A interface LayoutIterator {
0N/A
0N/A void setOffset(int offs);
0N/A
0N/A int getOffset();
0N/A
0N/A void setSpan(int span);
0N/A
0N/A int getSpan();
0N/A
0N/A int getCount();
0N/A
0N/A void setIndex(int i);
0N/A
0N/A float getMinimumSpan(float parentSpan);
0N/A
0N/A float getPreferredSpan(float parentSpan);
0N/A
0N/A float getMaximumSpan(float parentSpan);
0N/A
0N/A int getAdjustmentWeight(); //0 is the best weight WorstAdjustmentWeight is a worst one
0N/A
0N/A //float getAlignment();
0N/A
0N/A float getBorderWidth();
0N/A
0N/A float getLeadingCollapseSpan();
0N/A
0N/A float getTrailingCollapseSpan();
0N/A public static final int WorstAdjustmentWeight = 2;
0N/A }
0N/A
0N/A //
0N/A // Serialization support
0N/A //
0N/A
0N/A private void writeObject(java.io.ObjectOutputStream s)
0N/A throws IOException
0N/A {
0N/A s.defaultWriteObject();
0N/A
0N/A // Determine what values in valueConvertor need to be written out.
0N/A Enumeration keys = valueConvertor.keys();
0N/A s.writeInt(valueConvertor.size());
0N/A if (keys != null) {
0N/A while (keys.hasMoreElements()) {
0N/A Object key = keys.nextElement();
0N/A Object value = valueConvertor.get(key);
0N/A if (!(key instanceof Serializable) &&
0N/A (key = StyleContext.getStaticAttributeKey(key)) == null) {
0N/A // Should we throw an exception here?
0N/A key = null;
0N/A value = null;
0N/A }
0N/A else if (!(value instanceof Serializable) &&
0N/A (value = StyleContext.getStaticAttributeKey(value)) == null){
0N/A // Should we throw an exception here?
0N/A key = null;
0N/A value = null;
0N/A }
0N/A s.writeObject(key);
0N/A s.writeObject(value);
0N/A }
0N/A }
0N/A }
0N/A
0N/A private void readObject(ObjectInputStream s)
0N/A throws ClassNotFoundException, IOException
0N/A {
0N/A s.defaultReadObject();
0N/A // Reconstruct the hashtable.
0N/A int numValues = s.readInt();
611N/A valueConvertor = new Hashtable<Object, Object>(Math.max(1, numValues));
0N/A while (numValues-- > 0) {
0N/A Object key = s.readObject();
0N/A Object value = s.readObject();
0N/A Object staticKey = StyleContext.getStaticAttribute(key);
0N/A if (staticKey != null) {
0N/A key = staticKey;
0N/A }
0N/A Object staticValue = StyleContext.getStaticAttribute(value);
0N/A if (staticValue != null) {
0N/A value = staticValue;
0N/A }
0N/A if (key != null && value != null) {
0N/A valueConvertor.put(key, value);
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A /*
0N/A * we need StyleSheet for resolving lenght units. (see
0N/A * isW3CLengthUnits)
0N/A * we can not pass stylesheet for handling relative sizes. (do not
0N/A * think changing public API is necessary)
0N/A * CSS is not likely to be accessed from more then one thread.
0N/A * Having local storage for StyleSheet for resolving relative
0N/A * sizes is safe
0N/A *
0N/A * idk 08/30/2004
0N/A */
0N/A private StyleSheet getStyleSheet(StyleSheet ss) {
0N/A if (ss != null) {
0N/A styleSheet = ss;
0N/A }
0N/A return styleSheet;
0N/A }
0N/A //
0N/A // Instance variables
0N/A //
0N/A
0N/A /** Maps from CSS key to CssValue. */
611N/A private transient Hashtable<Object, Object> valueConvertor;
0N/A
0N/A /** Size used for relative units. */
0N/A private int baseFontSize;
0N/A
0N/A private transient StyleSheet styleSheet = null;
0N/A
0N/A static int baseFontSizeIndex = 3;
0N/A}