com.inet.html.css
Class StyleResolver

java.lang.Object
  extended by com.inet.html.css.StyleResolver
All Implemented Interfaces:
CSSChangeListener

public class StyleResolver
extends java.lang.Object
implements CSSChangeListener

The style resolver will handle all read operations on the current style sheets. It will fill the temporary styles into the elements attribute sets and handle the access to these style informations.
NOTE: This class does not supervise it's internal style sheet objects! You are free to change them at any time, but keep in mind that changing the stylesheets will not update the documents model.


Constructor Summary
StyleResolver()
           
 
Method Summary
 void clearStyles()
          Clears the currently set styles table
 void fillAttributesNonLocked(javax.swing.text.Element root, java.util.HashMap<TemporaryStyle.Attribute,java.lang.Object> parentSet)
          Sets the temporary style information for an element.
static
<T> T
getAttributeValue(javax.swing.text.Element elem, com.inet.html.finder.AttributeFinder<T> finder)
          Searches the correct style for the requested attribute.
static
<T> T
getAttributeValue(javax.swing.text.Element elem, com.inet.html.finder.AttributeFinder<T> finder, boolean useCache)
          Searches the correct style for the requested attribute.
 java.util.Enumeration<?> getStyleNames()
          Fetches a list of the currently used styles.
 Styles getStyles()
          Returns the current styles table
 Styles getStyleSheet(java.lang.String nm)
          Returns the style with the given name.
 void handleNewCSSEntry(Selector selector)
          Notifies the listener if a selector was added
static boolean hasRelevantInlineStyles(javax.swing.text.Element element)
          Checks whether this element has any (inline) attribute attached, which distinguishes it from it's parent.
static boolean matchesAttributes(javax.swing.text.Element element, javax.swing.text.AttributeSet requirements)
          Checks, whether the given element matches the defined attributes.
 void optimize()
          Creates an internal list of which styles are described by the registered stylesheets and which one will remain the default.
static void setAttribute(javax.swing.text.AttributeSet attrs, java.lang.Object name, java.lang.Object value)
          Adds the value to the attribute set if the value is not null.
 void setStyles(Styles styles)
          Sets the current styles table.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StyleResolver

public StyleResolver()
Method Detail

getAttributeValue

public static <T> T getAttributeValue(javax.swing.text.Element elem,
                                      com.inet.html.finder.AttributeFinder<T> finder)
Searches the correct style for the requested attribute. NOTE: Due to internal processing this method NEVER returns the value of the FontSize attribute, because this value is locally overwritten by the calculated font size.

Type Parameters:
T - Class type of the requested attribute
Parameters:
elem - the current HTML node
finder - Finder for the requested type of attribute
Returns:
the find attribute or null

getAttributeValue

public static <T> T getAttributeValue(javax.swing.text.Element elem,
                                      com.inet.html.finder.AttributeFinder<T> finder,
                                      boolean useCache)
Searches the correct style for the requested attribute. NOTE: Due to internal processing this method NEVER returns the value of the FontSize attribute, if useCache is ture, because this value is locally overwritten by the calculated font size.

Type Parameters:
T - Class type of the requested attribute
Parameters:
elem - the current HTML node
finder - Finder for the requested type of attribute
useCache - if true, the calculated styles will be used. If false, every styles will be calculated by the registered CSS files(Warning: VERY SLOW!), this mode will not drop the font size!
Returns:
the find attribute or null

matchesAttributes

public static boolean matchesAttributes(javax.swing.text.Element element,
                                        javax.swing.text.AttributeSet requirements)
Checks, whether the given element matches the defined attributes. It will be tested, if any attribute defined in requirements is set with the same value at the element. This comparison will check for inherited values as well!
NOTE: Only CSS attributes will be checked!

Parameters:
element - The element to check
requirements - The required attribute settings
Returns:
true, if element fulfills the requirements

fillAttributesNonLocked

public void fillAttributesNonLocked(javax.swing.text.Element root,
                                    java.util.HashMap<TemporaryStyle.Attribute,java.lang.Object> parentSet)
Sets the temporary style information for an element. These styles are the ones which are a result of the CSS rules, the HTML attributes and the inline styles. They carry the information which will be used to display these elements!
The temporary styles are stored as difference to the values of the parent. So if the parent of the root element has the same value for an attribute, this attribute will not appear in the attribute set of root. This has nothing to do with CSS-inheritance! It's just a way to keep the attribute sets small.

Parameters:
root - the element to fill
parentSet - the attribute set of the parent of root in non-diff form. If null these values will be calculated locally

setAttribute

public static void setAttribute(javax.swing.text.AttributeSet attrs,
                                java.lang.Object name,
                                java.lang.Object value)
Adds the value to the attribute set if the value is not null. This is likely to work only within a writeLock thread.

Parameters:
attrs - The attribute set to modify
name - The name of the attribute
value - The value of the attribute

getStyles

public Styles getStyles()
Returns the current styles table

Returns:
the current styles table

setStyles

public void setStyles(Styles styles)
Sets the current styles table. Can be used to completely replace the styles without changing the resolver

Parameters:
styles - the new styles

clearStyles

public void clearStyles()
Clears the currently set styles table


getStyleNames

public java.util.Enumeration<?> getStyleNames()
Fetches a list of the currently used styles. There are upt to three logical style-table names in use:
DEFAULT for the editors default values USERAGENT for the current user agent styles(which may override the defaults) INLINE for any style information described or linked by the current page

Returns:
a list with the names of all used style tables

getStyleSheet

public Styles getStyleSheet(java.lang.String nm)
Returns the style with the given name. The name is the logical name of the styles table, not the name of any linked stylesheet.
NOTE: It is recommended not to change the DEFAULT-Table! Use USERAGENT instead.

Parameters:
nm - the name to find
Returns:
a styles table or null

handleNewCSSEntry

public void handleNewCSSEntry(Selector selector)
Notifies the listener if a selector was added

Specified by:
handleNewCSSEntry in interface CSSChangeListener
Parameters:
selector - the new selector

hasRelevantInlineStyles

public static boolean hasRelevantInlineStyles(javax.swing.text.Element element)
Checks whether this element has any (inline) attribute attached, which distinguishes it from it's parent.
If this method returns true, it's widely safe to remove this element if there are no CSS-rules which require this element. Keep in Mind, that there remains a risk, that element might have styles by stylesheet rules.
NOTE: This method works only on spans because it's primary use is to check character attributes.

Parameters:
element - The element to check
Returns:
true, if the inline styles of this element effects the temporary styles

optimize

public void optimize()
Creates an internal list of which styles are described by the registered stylesheets and which one will remain the default. This may speed up the