de.infinityloop.util.style
Class CSS2Parser

java.lang.Object
  extended by de.infinityloop.util.style.CSS2Parser
All Implemented Interfaces:
org.w3c.css.sac.DocumentHandler, org.w3c.css.sac.ErrorHandler

public class CSS2Parser
extends java.lang.Object
implements org.w3c.css.sac.DocumentHandler, org.w3c.css.sac.ErrorHandler

Encapsulates a SAC CSS2 parser for convenient use in the using application.


Field Summary
static long ctor_calls
           
 java.lang.String errorContext
          Attempt to provide some context information for a potential error.
static long parse_calls
           
 
Constructor Summary
CSS2Parser()
          Creates and initializes a CSS2 parser object.
 
Method Summary
 void addMediaFilter(java.lang.String filterName)
          Adds a media filter.
 void comment(java.lang.String t)
          noop implementation, see SAC
 void endDocument(org.w3c.css.sac.InputSource is)
          noop implementation, see SAC
 void endFontFace()
          noop implementation, see SAC
 void endMedia(org.w3c.css.sac.SACMediaList l)
          endMedia implementation, see SAC Decrement skip counter, if needed.
 void endPage(java.lang.String name, java.lang.String pseudo)
          endPage implementation, see SAC
 void endSelector(org.w3c.css.sac.SelectorList selectors)
          endSelector implementation, see SAC
 void error(org.w3c.css.sac.CSSParseException e)
          Handle an error, see SAC
static java.lang.String extractPropertyValue(java.lang.String propString, java.lang.String propName, java.lang.String unit)
          Extracts a single value from a CSS property string's property with the name propName.
 void fatalError(org.w3c.css.sac.CSSParseException e)
          Handle a fatal error, see SAC
static java.util.Stack filterProperties(java.util.Stack props, CSSPropertyFilter f)
          Filters a Stack of properties against a property filter.
static java.lang.String filterPropertyString(java.lang.String propertyString, CSSPropertyFilter f)
          Filters a CSS string of properties against a custom defined property name filter.
 java.util.Stack getErrors()
          Retrieves a Stack of errors that may have been occurred during parsing.
 void ignorableAtRule(java.lang.String r)
          noop implementation, see SAC
 void importStyle(java.lang.String uri, org.w3c.css.sac.SACMediaList l, java.lang.String ns)
          import external stylesheet implementation, see SAC
static void main(java.lang.String[] args)
          Class testing, parses the stylesheet in args[0] and prints the result to System.out .
static java.lang.String makePropertyStringFromStack(java.util.Stack props, CSSPropertyDefaultUnits unitTable)
          Makes a property list string from a stack of properties.
 void namespaceDeclaration(java.lang.String pref, java.lang.String uri)
          noop implementation, see SAC
 java.util.Stack parsePropertyList(java.lang.String plist)
          Parse a style declaration, passed as string.
static org.w3c.css.sac.LexicalUnit parsePropertyValue(java.lang.String pval)
          Parse a property value, passed as string.
 java.util.Stack parseStylesheet(InputProxy ip)
          Parses a complete external stylesheet.
 void property(java.lang.String name, org.w3c.css.sac.LexicalUnit value, boolean important)
          property implementation, see SAC
 void startDocument(org.w3c.css.sac.InputSource is)
          noop implementation, see SAC
 void startFontFace()
          noop implementation, see SAC
 void startMedia(org.w3c.css.sac.SACMediaList l)
          startMedia implementation, see SAC We check if one of the media in the media list is in the mediaFilter.
 void startPage(java.lang.String name, java.lang.String pseudo)
          startPage implementation, see SAC
 void startSelector(org.w3c.css.sac.SelectorList selectors)
          startSelector implementation, see SAC
 void warning(org.w3c.css.sac.CSSParseException e)
          Handle a warning, see SAC
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ctor_calls

public static long ctor_calls

parse_calls

public static long parse_calls

errorContext

public java.lang.String errorContext
Attempt to provide some context information for a potential error.

Constructor Detail

CSS2Parser

public CSS2Parser()
Creates and initializes a CSS2 parser object.

Method Detail

addMediaFilter

public void addMediaFilter(java.lang.String filterName)
Adds a media filter. By default, all media is parsed and returned. Once one or several filters are added, only those declarations and rulesets are returned that are contained in that media rule, plus - of course - all declarations and rules outside of any media rule.

Parameters:
filterName - media identifier to filter (e.g. "print")

parseStylesheet

public java.util.Stack parseStylesheet(InputProxy ip)
                                throws java.lang.Exception
Parses a complete external stylesheet.

Parameters:
ip - the source of the stylesheet
Returns:
a Stack of Pair()s [CSSSelector selector, Stack properties] where the properties Stack is a Stack of property Pair()s [String propname, Object val]. If the stylesheet defines a list of selectors for a specific declaration block, this returns one entry for each individual selector in that list with the declaration block automatically duplicated for each one.
Throws:
java.lang.Exception - of appropriate type

parsePropertyList

public java.util.Stack parsePropertyList(java.lang.String plist)
Parse a style declaration, passed as string.

Parameters:
plist - style declaration as CSS string
Returns:
a Stack of Pair()s [CSSSelector selector, Stack properties] where the Stack is a Stack of property Pair()s [String propname, Object val].

parsePropertyValue

public static org.w3c.css.sac.LexicalUnit parsePropertyValue(java.lang.String pval)
                                                      throws ILException
Parse a property value, passed as string.

Parameters:
pval - the property value to parse
Returns:
a LexicalUnit describing the property value
Throws:
ILException

getErrors

public java.util.Stack getErrors()
Retrieves a Stack of errors that may have been occurred during parsing.

Returns:
null, when there are no errors or warnings, otherwise a Stack() of String()s.

startPage

public void startPage(java.lang.String name,
                      java.lang.String pseudo)
startPage implementation, see SAC

Specified by:
startPage in interface org.w3c.css.sac.DocumentHandler

endPage

public void endPage(java.lang.String name,
                    java.lang.String pseudo)
endPage implementation, see SAC

Specified by:
endPage in interface org.w3c.css.sac.DocumentHandler

startSelector

public void startSelector(org.w3c.css.sac.SelectorList selectors)
startSelector implementation, see SAC

Specified by:
startSelector in interface org.w3c.css.sac.DocumentHandler

endSelector

public void endSelector(org.w3c.css.sac.SelectorList selectors)
endSelector implementation, see SAC

Specified by:
endSelector in interface org.w3c.css.sac.DocumentHandler

property

public void property(java.lang.String name,
                     org.w3c.css.sac.LexicalUnit value,
                     boolean important)
property implementation, see SAC

Specified by:
property in interface org.w3c.css.sac.DocumentHandler

importStyle

public void importStyle(java.lang.String uri,
                        org.w3c.css.sac.SACMediaList l,
                        java.lang.String ns)
import external stylesheet implementation, see SAC

Specified by:
importStyle in interface org.w3c.css.sac.DocumentHandler

startMedia

public void startMedia(org.w3c.css.sac.SACMediaList l)
startMedia implementation, see SAC

We check if one of the media in the media list is in the mediaFilter. If it is, we parse it, otherwise we skip everything in it by incrementing the skip variable.

Specified by:
startMedia in interface org.w3c.css.sac.DocumentHandler

endMedia

public void endMedia(org.w3c.css.sac.SACMediaList l)
endMedia implementation, see SAC

Decrement skip counter, if needed.

Specified by:
endMedia in interface org.w3c.css.sac.DocumentHandler

error

public void error(org.w3c.css.sac.CSSParseException e)
Handle an error, see SAC

Specified by:
error in interface org.w3c.css.sac.ErrorHandler

warning

public void warning(org.w3c.css.sac.CSSParseException e)
Handle a warning, see SAC

Specified by:
warning in interface org.w3c.css.sac.ErrorHandler

fatalError

public void fatalError(org.w3c.css.sac.CSSParseException e)
Handle a fatal error, see SAC

Specified by:
fatalError in interface org.w3c.css.sac.ErrorHandler

comment

public void comment(java.lang.String t)
noop implementation, see SAC

Specified by:
comment in interface org.w3c.css.sac.DocumentHandler

ignorableAtRule

public void ignorableAtRule(java.lang.String r)
noop implementation, see SAC

Specified by:
ignorableAtRule in interface org.w3c.css.sac.DocumentHandler

namespaceDeclaration

public void namespaceDeclaration(java.lang.String pref,
                                 java.lang.String uri)
noop implementation, see SAC

Specified by:
namespaceDeclaration in interface org.w3c.css.sac.DocumentHandler

startDocument

public void startDocument(org.w3c.css.sac.InputSource is)
noop implementation, see SAC

Specified by:
startDocument in interface org.w3c.css.sac.DocumentHandler

endDocument

public void endDocument(org.w3c.css.sac.InputSource is)
noop implementation, see SAC

Specified by:
endDocument in interface org.w3c.css.sac.DocumentHandler

startFontFace

public void startFontFace()
noop implementation, see SAC

Specified by:
startFontFace in interface org.w3c.css.sac.DocumentHandler

endFontFace

public void endFontFace()
noop implementation, see SAC

Specified by:
endFontFace in interface org.w3c.css.sac.DocumentHandler

filterProperties

public static java.util.Stack filterProperties(java.util.Stack props,
                                               CSSPropertyFilter f)
Filters a Stack of properties against a property filter.

Parameters:
props - the Stack of properties
f - a CSSPropertyFilter object configured as desired
Returns:
the filtered property Stack
See Also:
CSSPropertyFilter

makePropertyStringFromStack

public static java.lang.String makePropertyStringFromStack(java.util.Stack props,
                                                           CSSPropertyDefaultUnits unitTable)
Makes a property list string from a stack of properties. This is the inverse of parsePropertyList().

Parameters:
props - the Stack of properties
unitTable - a CSSPropertyDefaultUnits object
Returns:
a property list String
See Also:
CSSPropertyDefaultUnits

filterPropertyString

public static java.lang.String filterPropertyString(java.lang.String propertyString,
                                                    CSSPropertyFilter f)
Filters a CSS string of properties against a custom defined property name filter.

Parameters:
propertyString - the String you wish to have filtered
f - the CSSPropertyFilter object describing which filter action should be performed.
Returns:
a filtered property string
See Also:
CSSPropertyFilter

extractPropertyValue

public static java.lang.String extractPropertyValue(java.lang.String propString,
                                                    java.lang.String propName,
                                                    java.lang.String unit)
Extracts a single value from a CSS property string's property with the name propName. You can also specify the unit the result should be returned in (incl. the resp. unit identifier). This will only take effect for properties having a unit as opposed to text content or keywords.

Parameters:
propString - the property string
propName - name of the property to extract, e.g. 'width' or 'list-style-type'.
unit - the unit name of the value should be converted to (only for properties of type length), e.g. "in" or "px"
Returns:
the value of the requested property or null, if it was not found

main

public static void main(java.lang.String[] args)
Class testing, parses the stylesheet in args[0] and prints the result to System.out .