org.w3c.mwi.mobileok.basic
Class CSSUtils

java.lang.Object
  extended by org.w3c.mwi.mobileok.basic.CSSUtils

final class CSSUtils
extends java.lang.Object

Static helper class that defines useful methods to parse and validate CSS resources using the W3C CSS Validator.

Version:
$Revision: 1.1 $
Author:
The W3C mobileOK Checker Task Force
See Also:
AbstractCSSTestImplementation, CssContent

Field Summary
private static java.util.regex.Pattern atMediaPattern
          Regular expression to find CSS properties that use the at-media rules.
private static java.util.regex.Pattern backgroundImagePattern
          Regular expression to find the URI of images in background-image CSS properties.
private static java.util.regex.Pattern backgroundPattern
          Regular expression to find the URI of images in background CSS properties.
private static java.util.regex.Pattern commentPattern
          Regular expression to extract CSS comments.
private static java.util.regex.Pattern importPattern
          Regular expression to find the URI of external stylesheets imported by a CSS resource using quotes instead of a url(foo) pattern.
private static java.util.regex.Pattern importUrlPattern
          Regular expression to find the URI of external stylesheets imported by a CSS resource using a url(foo) pattern.
private static java.util.regex.Pattern listImagePattern
          Regular expression to find the URI of images in list-style-image CSS properties.
 
Constructor Summary
private CSSUtils()
          Private constructor to prevent instanciation, since the class only defined static helper methods.
 
Method Summary
static int countExtraneousChars(java.lang.String css)
          Computes the number of "useless" characters in the CSS, i.e.
static void extractCSSErrors(java.util.Collection<ValidationLineAndColumnMessage> cssErrorMessageList, org.w3c.css.css.StyleSheet styleSheet)
          Extracts the CSS validation errors from the stylesheet returned by the W3C CSS validator.
static java.util.List<java.net.URI> extractCSSImageResources(java.net.URI baseURI, java.lang.String body)
          Extracts the list of absolute URIs of the images referenced by the CSS.
static void extractCSSWarnings(java.util.Collection<ValidationLineAndColumnMessage> cssWarningMessageList, org.w3c.css.css.StyleSheet styleSheet)
          Extracts the CSS validation warnings from the stylesheet returned by the W3C CSS validator.
static org.w3c.css.css.StyleSheet getStylesheet(java.net.URI cssUri, CssContent cssContent, RetrievalElement retrieved)
          Parses a CSS content with the W3C CSS validator and returns the result.
static java.lang.String preprocessBody(java.net.URI baseURI, java.lang.String cssBody, java.util.Collection<java.net.URI> extractedCSSResources)
          Parses a CSS string and only keeps the parts that need to be validated by the W3C CSS Validator.
private static java.lang.String stripToLineBreaks(java.lang.String atMediaBody)
          Returns a copy of the input string that only contains the line breaks, i.e.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

importUrlPattern

private static final java.util.regex.Pattern importUrlPattern
Regular expression to find the URI of external stylesheets imported by a CSS resource using a url(foo) pattern.


importPattern

private static final java.util.regex.Pattern importPattern
Regular expression to find the URI of external stylesheets imported by a CSS resource using quotes instead of a url(foo) pattern.


commentPattern

private static final java.util.regex.Pattern commentPattern
Regular expression to extract CSS comments.


listImagePattern

private static final java.util.regex.Pattern listImagePattern
Regular expression to find the URI of images in list-style-image CSS properties.


backgroundImagePattern

private static final java.util.regex.Pattern backgroundImagePattern
Regular expression to find the URI of images in background-image CSS properties.


backgroundPattern

private static final java.util.regex.Pattern backgroundPattern
Regular expression to find the URI of images in background CSS properties.


atMediaPattern

private static final java.util.regex.Pattern atMediaPattern
Regular expression to find CSS properties that use the at-media rules.

Constructor Detail

CSSUtils

private CSSUtils()
Private constructor to prevent instanciation, since the class only defined static helper methods.

Method Detail

getStylesheet

public static org.w3c.css.css.StyleSheet getStylesheet(java.net.URI cssUri,
                                                       CssContent cssContent,
                                                       RetrievalElement retrieved)
Parses a CSS content with the W3C CSS validator and returns the result.

Validation is performed against CSS Level 1, and only concerns the CSS properties that apply to the handheld media type.

Parameters:
cssUri - URI of the CSS to parse. Cannot be null.
cssContent - CSS Content to parse. Cannot be null.
Returns:
the result of the validation as a W3C CSS stylesheet, null when validation fails.

extractCSSErrors

public static void extractCSSErrors(java.util.Collection<ValidationLineAndColumnMessage> cssErrorMessageList,
                                    org.w3c.css.css.StyleSheet styleSheet)
Extracts the CSS validation errors from the stylesheet returned by the W3C CSS validator.

Parameters:
cssErrorMessageList - the list of CSS validation errors to complete.
styleSheet - the stylesheet returned by the W3C CSS validator.

extractCSSWarnings

public static void extractCSSWarnings(java.util.Collection<ValidationLineAndColumnMessage> cssWarningMessageList,
                                      org.w3c.css.css.StyleSheet styleSheet)
Extracts the CSS validation warnings from the stylesheet returned by the W3C CSS validator.

Parameters:
cssWarningMessageList - the list of CSS validation warnings to complete.
styleSheet - the stylesheet returned by the W3C CSS validator.

extractCSSImageResources

public static java.util.List<java.net.URI> extractCSSImageResources(java.net.URI baseURI,
                                                                    java.lang.String body)
Extracts the list of absolute URIs of the images referenced by the CSS.

URIs appear only once in the list, even though they may appear multiple times in the CSS.

Parameters:
baseURI - base URI of the CSS, used to resolve relative URIs.
body - CSS content to parse.
Returns:
the list of distinct absolute images URIs.

preprocessBody

public static java.lang.String preprocessBody(java.net.URI baseURI,
                                              java.lang.String cssBody,
                                              java.util.Collection<java.net.URI> extractedCSSResources)
Parses a CSS string and only keeps the parts that need to be validated by the W3C CSS Validator.

See the CSS Style and Validity sections in the W3C mobileOK Basic Tests 1.0 standard for more details about CSS Style in mobileOK.

The method removes sections that do not need to be checked or that would trigger CSS Level 1 validation errors whereas they are valid in mobileOK. In short, the method:

Line breaks are kept so that the position lines of potential errors and warnings in the processed CSS matches the original CSS as well.

Through the extractedCSSResources parameter, the method may also return the list of CSS resources imported by the CSS and that applies to no media type or to the all or handheld media types.

Parameters:
baseURI - base URI to use to resolve relative URIs.
cssBody - CSS to process.
extractedCSSResources - the list to fill of absolute URIs of CSS resources imported by the CSS being parsed. May be null. URIs appear only once in the list.
Returns:
a copy of the CSS prepared for validation.

stripToLineBreaks

private static java.lang.String stripToLineBreaks(java.lang.String atMediaBody)
Returns a copy of the input string that only contains the line breaks, i.e. remove all characters from the original string except line breaks.

Parameters:
atMediaBody - string to strip.
Returns:
a copy of the string that only contains line breaks.

countExtraneousChars

public static int countExtraneousChars(java.lang.String css)
Computes the number of "useless" characters in the CSS, i.e. the number of non significant whitespaces plus the number of characters that are comments.

Parameters:
css - CSS to parse.
Returns:
the number of useless characters in the CSS.