javax.swing.text.html
Class CSS

java.lang.Object
  extended byjavax.swing.text.html.CSS
All Implemented Interfaces:
Serializable

public class CSS
extends Object
implements Serializable

Defines a set of CSS attributes as a typesafe enumeration. The HTML View implementations use CSS attributes to determine how they will render. This also defines methods to map between CSS/HTML/StyleConstants. Any shorthand properties, such as font, are mapped to the intrinsic properties.

The following describes the CSS properties that are suppored by the rendering engine:

The following are modeled, but currently not rendered.

Note: for the time being we do not fully support relative units, unless noted, so that p { margin-top: 10% } will be treated as if no margin-top was specified.

Author:
Timothy Prinzing, Scott Violet
See Also:
StyleSheet, Serialized Form

Nested Class Summary
static class CSS.Attribute
          Definitions to be used as a key on AttributeSet's that might hold CSS attributes.
(package private) static class CSS.BackgroundImage
          Used for BackgroundImages.
(package private) static class CSS.BackgroundPosition
          Used for background images, to represent the position.
(package private) static class CSS.BorderStyle
           
(package private) static class CSS.BorderWidthValue
          BorderWidthValue is used to model BORDER_XXX_WIDTH and adds support for the thin/medium/thick values.
(package private) static class CSS.ColorValue
           
(package private) static class CSS.CssValue
          Base class to CSS values in the attribute sets.
(package private) static class CSS.CssValueMapper
          Handles uniquing of CSS values, like lists, and background image repeating.
(package private) static class CSS.FontFamily
           
(package private)  class CSS.FontSize
          Represents a value for the CSS.FONT_SIZE attribute.
(package private) static class CSS.FontWeight
           
(package private) static interface CSS.LayoutIterator
          An iterator to express the requirements to use when computing layout.
(package private) static class CSS.LengthUnit
          Parses a length value, this is used internally, and never added to an AttributeSet or returned to the developer.
(package private) static class CSS.LengthValue
           
(package private) static class CSS.ShorthandBackgroundParser
          Parses the background property into its intrinsic values.
(package private) static class CSS.ShorthandFontParser
          Class used to parse font property.
(package private) static class CSS.ShorthandMarginParser
          Used to parser margin and padding.
(package private) static class CSS.StringValue
          By default CSS attributes are represented as simple strings.
(package private) static class CSS.Value
           
 
Field Summary
private static Hashtable attributeMap
           
private  int baseFontSize
          Size used for relative units.
private static Hashtable cssValueToInternalValueMap
          Maps from CSS value (string) to internal value.
private static Hashtable fontMapping
          Used to indicate if a font family name is valid.
private static Object fontMappingLock
           
private static Hashtable htmlAttrToCssAttrMap
          The hashtable and the static initalization block below, set up a mapping from well-known HTML attributes to CSS attributes.
private static Hashtable htmlValueToCssValueMap
          Maps from HTML value to a CSS value.
(package private) static int[] sizeMap
          The HTML/CSS size model has seven slots that one can assign sizes to.
private static Hashtable styleConstantToCssMap
          The hashtable and static initialization that follows sets up a translation from StyleConstants (i.e. the well known attributes) to the associated CSS attributes.
private  Hashtable valueConvertor
          Maps from CSS key to CssValue.
private static Hashtable valueMap
           
 
Constructor Summary
CSS()
           
 
Method Summary
(package private)  void addInternalCSSValue(MutableAttributeSet attr, CSS.Attribute key, String value)
          Parses the CSS property key with value value placing the result in att.
(package private) static void calculateTiledLayout(CSS.LayoutIterator iter, int targetSpan)
          Calculate a tiled layout for the given iterator.
(package private) static SizeRequirements calculateTiledRequirements(CSS.LayoutIterator iter, SizeRequirements r)
          Calculate the requirements needed to tile the requirements given by the iterator that would be tiled.
(package private) static String colorToHex(Color color)
          Converts a type Color to a hex string in the format "#RRGGBB"
(package private)  Object cssValueToStyleConstantsValue(StyleConstants key, Object value)
          Converts the passed in CSS value to a StyleConstants value.
static CSS.Attribute[] getAllAttributeKeys()
          Return the set of all possible CSS attribute keys.
static CSS.Attribute getAttribute(String name)
          Translates a string to a CSS.Attribute object.
(package private)  int getBaseFontSize()
          Returns the base font size.
(package private)  Color getColor(AttributeSet a, CSS.Attribute key)
          Takes a set of attributes and turn it into a color specification.
private static int getColorComponent(String string, int[] index)
          Returns the next integer value from string starting at index[0].
private  CSS.Attribute getCssAlignAttribute(HTML.Tag tag, AttributeSet htmlAttrSet)
          Maps HTML.Attribute.ALIGN to either: CSS.Attribute.TEXT_ALIGN CSS.Attribute.FLOAT CSS.Attribute.VERTICAL_ALIGN based on the tag associated with the attribute and the value of the attribute.
private  CSS.Attribute[] getCssAttribute(HTML.Attribute hAttr)
          Maps an HTML.Attribute object to its appropriate CSS.Attributes.
(package private)  Object getCssValue(CSS.Attribute cssAttr, String htmlAttrValue)
          Given a CSS.Attribute object and its corresponding HTML.Attribute's value, this method returns a CssValue object to associate with the CSS attribute.
(package private)  Font getFont(StyleContext sc, AttributeSet a, int defaultSize)
          Returns the font for the values in the passed in AttributeSet.
(package private) static int getFontSize(AttributeSet attr, int defaultSize)
           
private  HTML.Tag getHTMLTag(AttributeSet htmlAttrSet)
          Fetches the tag associated with the HTML AttributeSet.
(package private) static int getIndexOfSize(float pt)
           
(package private)  Object getInternalCSSValue(CSS.Attribute key, String value)
          Gets the internal CSS representation of value which is a CSS value of the CSS attribute named key.
(package private)  float getLength(AttributeSet a, CSS.Attribute key)
          Returns the length of the attribute in a with key key.
(package private)  float getPointSize(int index)
          Return the point size, given a size index.
(package private)  float getPointSize(String size)
          Returns the size of a font from the passed in string.
(package private) static URL getURL(URL base, String cssString)
          Returns a URL for the given CSS url string.
(package private) static Hashtable getValidFontNameMapping()
          Returns a hashtable whose contents are used to indicate the valid fonts.
(package private) static CSS.Value getValue(String name)
          Translates a string to a CSS.Value object.
(package private) static Color hexToColor(String value)
          Convert a "#FFFFFF" hex string to a Color.
private  boolean isFloater(String alignValue)
           
private  boolean isHTMLFontTag(HTML.Tag tag)
           
private static Color parseRGB(String string)
          Parses a String in the format rgb(r, g, b) where each of the Color components is either an integer, or a floating number with a % after indicating a percentage value of 255.
(package private) static String[] parseStrings(String value)
           
private  void readObject(ObjectInputStream s)
           
(package private)  void setBaseFontSize(int sz)
          Sets the base font size.
(package private)  void setBaseFontSize(String size)
          Sets the base font size from the passed in string.
(package private) static Color stringToColor(String str)
          Convert a color string such as "RED" or "#NNNNNN" or "rgb(r, g, b)" to a Color.
(package private)  CSS.Attribute styleConstantsKeyToCSSKey(StyleConstants sc)
          Maps from a StyleConstants to a CSS Attribute.
(package private)  Object styleConstantsValueToCSSValue(StyleConstants sc, Object styleValue)
          Maps from a StyleConstants value to a CSS value.
private  void translateAttribute(HTML.Attribute key, AttributeSet htmlAttrSet, MutableAttributeSet cssAttrSet)
           
private  void translateAttributes(HTML.Tag tag, AttributeSet htmlAttrSet, MutableAttributeSet cssAttrSet)
           
private  void translateEmbeddedAttributes(AttributeSet htmlAttrSet, MutableAttributeSet cssAttrSet)
           
(package private)  AttributeSet translateHTMLToCSS(AttributeSet htmlAttrSet)
          Convert a set of HTML attributes to an equivalent set of CSS attributes.
private  boolean validTextAlignValue(String alignValue)
           
private  void writeObject(ObjectOutputStream s)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

attributeMap

private static final Hashtable attributeMap

valueMap

private static final Hashtable valueMap

sizeMap

static int[] sizeMap
The HTML/CSS size model has seven slots that one can assign sizes to.


htmlAttrToCssAttrMap

private static final Hashtable htmlAttrToCssAttrMap
The hashtable and the static initalization block below, set up a mapping from well-known HTML attributes to CSS attributes. For the most part, there is a 1-1 mapping between the two. However in the case of certain HTML attributes for example HTML.Attribute.VSPACE or HTML.Attribute.HSPACE, end up mapping to two CSS.Attribute's. Therefore, the value associated with each HTML.Attribute. key ends up being an array of CSS.Attribute.* objects.


styleConstantToCssMap

private static final Hashtable styleConstantToCssMap
The hashtable and static initialization that follows sets up a translation from StyleConstants (i.e. the well known attributes) to the associated CSS attributes.


htmlValueToCssValueMap

private static final Hashtable htmlValueToCssValueMap
Maps from HTML value to a CSS value. Used in internal mapping.


cssValueToInternalValueMap

private static final Hashtable cssValueToInternalValueMap
Maps from CSS value (string) to internal value.


fontMapping

private static Hashtable fontMapping
Used to indicate if a font family name is valid.


fontMappingLock

private static final Object fontMappingLock

valueConvertor

private transient Hashtable valueConvertor
Maps from CSS key to CssValue.


baseFontSize

private int baseFontSize
Size used for relative units.

Constructor Detail

CSS

public CSS()
Method Detail

setBaseFontSize

void setBaseFontSize(int sz)
Sets the base font size. sz is a CSS value, and is not necessarily the point size. Use getPointSize to determine the point size corresponding to sz.


setBaseFontSize

void setBaseFontSize(String size)
Sets the base font size from the passed in string.


getBaseFontSize

int getBaseFontSize()
Returns the base font size.


addInternalCSSValue

void addInternalCSSValue(MutableAttributeSet attr,
                         CSS.Attribute key,
                         String value)
Parses the CSS property key with value value placing the result in att.


getInternalCSSValue

Object getInternalCSSValue(CSS.Attribute key,
                           String value)
Gets the internal CSS representation of value which is a CSS value of the CSS attribute named key. The receiver should not modify value, and the first count strings are valid.


styleConstantsKeyToCSSKey

CSS.Attribute styleConstantsKeyToCSSKey(StyleConstants sc)
Maps from a StyleConstants to a CSS Attribute.


styleConstantsValueToCSSValue

Object styleConstantsValueToCSSValue(StyleConstants sc,
                                     Object styleValue)
Maps from a StyleConstants value to a CSS value.


cssValueToStyleConstantsValue

Object cssValueToStyleConstantsValue(StyleConstants key,
                                     Object value)
Converts the passed in CSS value to a StyleConstants value. key identifies the CSS attribute being mapped.


getFont

Font getFont(StyleContext sc,
             AttributeSet a,
             int defaultSize)
Returns the font for the values in the passed in AttributeSet. It is assumed the keys will be CSS.Attribute keys. sc is the StyleContext that will be messaged to get the font once the size, name and style have been determined.


getFontSize

static int getFontSize(AttributeSet attr,
                       int defaultSize)

getColor

Color getColor(AttributeSet a,
               CSS.Attribute key)
Takes a set of attributes and turn it into a color specification. This might be used to specify things like brighter, more hue, etc. This will return null if there is no value for key.

Parameters:
key - CSS.Attribute identifying where color is stored.
a - the set of attributes
Returns:
the color

getPointSize

float getPointSize(String size)
Returns the size of a font from the passed in string.

Parameters:
size - CSS string describing font size

getLength

float getLength(AttributeSet a,
                CSS.Attribute key)
Returns the length of the attribute in a with key key.


translateHTMLToCSS

AttributeSet translateHTMLToCSS(AttributeSet htmlAttrSet)
Convert a set of HTML attributes to an equivalent set of CSS attributes.

Returns:
AttributeSet containing the corresponding CSS attributes. The AttributeSet will be empty if there are no mapping CSS attributes.

getValidFontNameMapping

static Hashtable getValidFontNameMapping()
Returns a hashtable whose contents are used to indicate the valid fonts.


getAllAttributeKeys

public static CSS.Attribute[] getAllAttributeKeys()
Return the set of all possible CSS attribute keys.


getAttribute

public static final CSS.Attribute getAttribute(String name)
Translates a string to a CSS.Attribute object. This will return null if there is no attribute by the given name.

Parameters:
name - the name of the CSS attribute to fetch the typesafe enumeration for
Returns:
the CSS.Attribute object, or null if the string doesn't represent a valid attribute key

getValue

static final CSS.Value getValue(String name)
Translates a string to a CSS.Value object. This will return null if there is no value by the given name.

Parameters:
name - the name of the CSS value to fetch the typesafe enumeration for
Returns:
the CSS.Value object, or null if the string doesn't represent a valid CSS value name; this does not mean that it doesn't represent a valid CSS value

getURL

static URL getURL(URL base,
                  String cssString)
Returns a URL for the given CSS url string. If relative, base is used as the parent. If a valid URL can not be found, this will not throw a MalformedURLException, instead null will be returned.


colorToHex

static String colorToHex(Color color)
Converts a type Color to a hex string in the format "#RRGGBB"


hexToColor

static final Color hexToColor(String value)
Convert a "#FFFFFF" hex string to a Color. If the color specification is bad, an attempt will be made to fix it up.


stringToColor

static Color stringToColor(String str)
Convert a color string such as "RED" or "#NNNNNN" or "rgb(r, g, b)" to a Color.


parseRGB

private static Color parseRGB(String string)
Parses a String in the format rgb(r, g, b) where each of the Color components is either an integer, or a floating number with a % after indicating a percentage value of 255. Values are constrained to fit with 0-255. The resulting Color is returned.


getColorComponent

private static int getColorComponent(String string,
                                     int[] index)
Returns the next integer value from string starting at index[0]. The value can either can an integer, or a percentage (floating number ending with %), in which case it is multiplied by 255.


getIndexOfSize

static int getIndexOfSize(float pt)

parseStrings

static String[] parseStrings(String value)
Returns:
an array of all the strings in value that are separated by whitespace.

getPointSize

float getPointSize(int index)
Return the point size, given a size index. Legal HTML index sizes are 1-7.


translateEmbeddedAttributes

private void translateEmbeddedAttributes(AttributeSet htmlAttrSet,
                                         MutableAttributeSet cssAttrSet)

translateAttributes

private void translateAttributes(HTML.Tag tag,
                                 AttributeSet htmlAttrSet,
                                 MutableAttributeSet cssAttrSet)

translateAttribute

private void translateAttribute(HTML.Attribute key,
                                AttributeSet htmlAttrSet,
                                MutableAttributeSet cssAttrSet)

getCssValue

Object getCssValue(CSS.Attribute cssAttr,
                   String htmlAttrValue)
Given a CSS.Attribute object and its corresponding HTML.Attribute's value, this method returns a CssValue object to associate with the CSS attribute.


getCssAttribute

private CSS.Attribute[] getCssAttribute(HTML.Attribute hAttr)
Maps an HTML.Attribute object to its appropriate CSS.Attributes.

Returns:
CSS.Attribute[]

getCssAlignAttribute

private CSS.Attribute getCssAlignAttribute(HTML.Tag tag,
                                           AttributeSet htmlAttrSet)
Maps HTML.Attribute.ALIGN to either: CSS.Attribute.TEXT_ALIGN CSS.Attribute.FLOAT CSS.Attribute.VERTICAL_ALIGN based on the tag associated with the attribute and the value of the attribute.

Returns:
CSS.Attribute mapping for HTML.Attribute.ALIGN.

getHTMLTag

private HTML.Tag getHTMLTag(AttributeSet htmlAttrSet)
Fetches the tag associated with the HTML AttributeSet.

Returns:
HTML.Tag

isHTMLFontTag

private boolean isHTMLFontTag(HTML.Tag tag)

isFloater

private boolean isFloater(String alignValue)

validTextAlignValue

private boolean validTextAlignValue(String alignValue)

calculateTiledRequirements

static SizeRequirements calculateTiledRequirements(CSS.LayoutIterator iter,
                                                   SizeRequirements r)
Calculate the requirements needed to tile the requirements given by the iterator that would be tiled. The calculation takes into consideration margin collapsing.


calculateTiledLayout

static void calculateTiledLayout(CSS.LayoutIterator iter,
                                 int targetSpan)
Calculate a tiled layout for the given iterator. This should be done collapsing the neighboring margins to be a total of the maximum of the two neighboring margin areas as described in the CSS spec.


writeObject

private void writeObject(ObjectOutputStream s)
                  throws IOException
Throws:
IOException

readObject

private void readObject(ObjectInputStream s)
                 throws ClassNotFoundException,
                        IOException
Throws:
ClassNotFoundException
IOException