com.mslinn.JBrowCaps
Class StyleSheet

java.lang.Object
  |
  +--com.mslinn.JBrowCaps.StyleSheet

public class StyleSheet
extends Object

The StyleSheet class breaks down a CSS style sheet into equivalent HTML for older browsers and stores the style sheet rules as a HashMap of HashMaps.  For any given browser capability, this class builds a separate HashMap (e.g. cssTrue is the HashMap containing markup for browsers that support CSS1, cssFalse is the HashMap containing markup for browsers that don't support CSS).

Each HashMap contains keys who's associated values are themselves HashMaps.  In this way an entire set of rules for how to render HTML markup is associated with the tags defined in the stylesheet represented by this class.

For each entry in a rule's HashMap, this class translates it to equivalent HTML and accumulates three strings; one string to turn on formatting, one to turn off formatting, and one to provide for inline CSS formatting via the style tag.

This means that in order to support browsers that don't undertand CSS, browsers that do, and WML or i-mode browsers, the HashMap corresponding to each browser capability will have three key/value entries stored (one to turn formatting on, one to turn formatting off, and one for inline formatting).

This class is an application-level object (like all classes in the package save BrowserDetector); only one is required for a servlet application.


Field Summary
 HashMap cssFalse
          Contains map of format strings for browsers that don't support CSS.  Add more HashMaps for each targeted type of browser (such as i-mode or WML).
 HashMap cssTrue
          Contains map of css-compatible format strings.
 boolean debug
          Controls debug output sent to System.out.
 
Constructor Summary
StyleSheet()
          Default constructor, requires subsequent initialization by calls to load(String) and buildTargetMaps() before the HashMaps have useful values.  Should only be called during servlet initialization.
StyleSheet(String cssFileName, HttpServletRequest request)
          One-step constructor, reads from default CSS style sheet in cssFileName.  Should only be called during servlet initialization.
 
Method Summary
protected  void addCssRule(CssRule cssRule)
          Merge CssRule rule into a StyleSheet.  Should only be called during servlet initialization.  Declared as protected for debugging purposes; should actually be private.
protected  void addCssRule(String ruleString)
          Merge CssRule into a StyleSheet.  Should only be called during servlet initialization.
 boolean browserSupportsCss()
          Detect browser capability.  Should only be called at the start of each user session.
 void buildTargetMaps()
          Create cssTrue and cssFalse, which define all the formatting strings for JSP pages that want to use the stylesheet.  Should only be called during servlet initialization.
 boolean getError()
          Used to discover if there is an error condition in this class.
 String getErrorMsg()
          Used to obtain the error message.
 Set getSelectors()
          Used to discover the selectors defined in a stylesheet with a minimum of overhead.
 HashMap getStylesheetMap()
          Call after load(String) and buildTargetMaps() have executed.
 StyleSheet load(String id)
          Read style sheet file and return a StyleSheet.
static void main(String[] args)
          For testing only, runs from command line.
protected  String read(String id, boolean removeComments)
          Read style sheet from file and return as String (including any error message).  Should only be called during servlet initialization.  Declared as protected for debugging purposes; should actually be private.
protected  String ruleToString(HashMap rule)
          Print the rule Hashmap so it conforms to css formatting.  Duplicated in CssRule because I'm tired.
protected  void storeHtml(String selector, HashMap rule, HashMap targetMap)
          This method break down CSS formatting into equivalent HTML for older browsers.
 void test()
          Just for testing.
 String toString()
          Format StyleSheet for printing, not useful for serializing.  Definitions are printed in sorted order.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

cssTrue

public HashMap cssTrue
Contains map of css-compatible format strings.

cssFalse

public HashMap cssFalse
Contains map of format strings for browsers that don't support CSS.  Add more HashMaps for each targeted type of browser (such as i-mode or WML).

debug

public boolean debug
Controls debug output sent to System.out.
Constructor Detail

StyleSheet

public StyleSheet()

Default constructor, requires subsequent initialization by calls to load(String) and buildTargetMaps() before the HashMaps have useful values.  Should only be called during servlet initialization.


StyleSheet

public StyleSheet(String cssFileName,
                  HttpServletRequest request)

One-step constructor, reads from default CSS style sheet in cssFileName.  Should only be called during servlet initialization.

Method Detail

addCssRule

protected void addCssRule(CssRule cssRule)

Merge CssRule rule into a StyleSheet.  Should only be called during servlet initialization.  Declared as protected for debugging purposes; should actually be private.

Parameters:
cssRule - CssRule containing the HashMap of selectors to add.

addCssRule

protected void addCssRule(String ruleString)
                   throws JspException

Merge CssRule into a StyleSheet.  Should only be called during servlet initialization.

Parameters:
ruleString - String containing textual version of rule, which will be parsed prior to adding to the HashMap.  Declared as protected for debugging purposes; should actually be private.
Throws:
JspException - if there is a problem parsing the rule or adding the rule to the HashMap.

browserSupportsCss

public boolean browserSupportsCss()

Detect browser capability.  Should only be called at the start of each user session.

Returns:
boolean indicating if this browser can support stylesheets.

buildTargetMaps

public void buildTargetMaps()

Create cssTrue and cssFalse, which define all the formatting strings for JSP pages that want to use the stylesheet.  Should only be called during servlet initialization.

this stylesheet should have the default HTML 2.0 rules merged before calling buildTargetMaps() so that display:block is set for all block mode tags. If display:block is not set, then tags are always emitted instead of

tags.

getStylesheetMap

public HashMap getStylesheetMap()

Call after load(String) and buildTargetMaps() have executed.

Returns:
HashMap of format strings appropriate to the detected browser.

getError

public boolean getError()

Used to discover if there is an error condition in this class.

Returns:
boolean containing error status set by constructor.

getErrorMsg

public String getErrorMsg()

Used to obtain the error message.

Returns:
String containing error message set by constructor.

getSelectors

public Set getSelectors()

Used to discover the selectors defined in a stylesheet with a minimum of overhead.

Returns:
Set containing keys in StyleSheet hashmap.

load

public StyleSheet load(String id)
                throws JspException

Read style sheet file and return a StyleSheet.

Parameters:
id - String containing name of stylesheet to read.
Returns:
StyleSheet with contents of file added (if StyleSheet was not empty, it is augmented).
Throws:
JspException - if any error was encountered.

main

public static void main(String[] args)
                 throws JspException

For testing only, runs from command line.


read

protected String read(String id,
                      boolean removeComments)
               throws JspException

Read style sheet from file and return as String (including any error message).  Should only be called during servlet initialization.  Declared as protected for debugging purposes; should actually be private.

Parameters:
boolean - removeComments controls whether comments are to be removed or not.
id - String containing name of stylesheet to read.
Returns:
contents of stylesheet
Throws:
JspException - if any error was encountered.

ruleToString

protected String ruleToString(HashMap rule)

Print the rule Hashmap so it conforms to css formatting.  Duplicated in CssRule because I'm tired.

Declared as protected for debugging purposes; should actually be private.

Parameters:
rule - HashMap to print.
Returns:
String representation of rule HashMap.

storeHtml

protected void storeHtml(String selector,
                         HashMap rule,
                         HashMap targetMap)

This method break down CSS formatting into equivalent HTML for older browsers. Recall that a stylesheet is a HashMap of HashMaps. For any given browser capability, this routine is called once for every selector (e.g. <h1>, <tt> or <tag class=myclass>).  Each selector has an associated HashMap containing a collection of definitions stored as a HashMap. This method walks the HashMap of definitions.

For each entry in a rule's HashMap, this method translates it to equivalent HTML and accumulates three strings; one string to turn on formatting, one to turn off formatting, and one to provide for inline CSS formatting via the style tag.

For example, to support browsers that don't undertand CSS, browsers that do, and i-mode or WML browsers, this method will be called once for each type of browser, with three different targetMaps (one for each browser type), times the number of selectors in the stylesheet.

The method is declared as protected for debugging purposes; should actually be private.

Parameters:
selector - String containing name of rule.
rule - HashMap containing collection of definitions.
targetMap - HashMap that will receive three new entries.

test

public void test()
          throws JspException
Just for testing.
Throws:
JspException - if uncommented.

toString

public String toString()

Format StyleSheet for printing, not useful for serializing.  Definitions are printed in sorted order.

Overrides:
toString in class Object


Written 2001 by Mike Slinn.  All rights reserved.
4230 Wooster Ave, San Mateo, CA 94403.
Tel: 650-571-8436   www.mslinn.com