Skip to content

Commit

Permalink
For #180 - Big cleanup of style related code
Browse files Browse the repository at this point in the history
Also beginning of style caching with which conditions will disable it.
  • Loading branch information
danfickle committed Apr 8, 2018
1 parent f031a4a commit 8b09e42
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;

Expand Down Expand Up @@ -56,36 +54,18 @@ public class StyleReference {
* resolution.
*/
private SharedContext _context;

/**
* Description of the Field
*/
private NamespaceHandler _nsh;

/**
* Description of the Field
*/
private Document _doc;

/**
* Description of the Field
*/
private StylesheetFactoryImpl _stylesheetFactory;
private final StylesheetFactoryImpl _stylesheetFactory;

/**
* Instance of our element-styles matching class. Will be null if new rules
* have been added since last match.
*/
private com.openhtmltopdf.css.newmatch.Matcher _matcher;

/** */
private UserAgentCallback _uac;

/**
* Default constructor for initializing members.
*
* @param userAgent PARAM
*/
public StyleReference(UserAgentCallback userAgent) {
_uac = userAgent;
_stylesheetFactory = new StylesheetFactoryImpl(userAgent);
Expand All @@ -105,8 +85,10 @@ public void setDocumentContext(SharedContext context, NamespaceHandler nsh, Docu
_doc = doc;
AttributeResolver attRes = new StandardAttributeResolver(_nsh, _uac, ui);

List infos = getStylesheets();
List<StylesheetInfo> infos = getStylesheets();

XRLog.match("media = " + _context.getMedia());

_matcher = new com.openhtmltopdf.css.newmatch.Matcher(
new DOMTreeResolver(),
attRes,
Expand All @@ -115,18 +97,18 @@ public void setDocumentContext(SharedContext context, NamespaceHandler nsh, Docu
_context.getMedia());
}

private List readAndParseAll(List infos, String medium) {
List result = new ArrayList(infos.size() + 15);
for (Iterator i = infos.iterator(); i.hasNext(); ) {
StylesheetInfo info = (StylesheetInfo)i.next();
private List<Stylesheet> readAndParseAll(List<StylesheetInfo> infos, String medium) {
List<Stylesheet> result = new ArrayList<Stylesheet>(infos.size() + 15);

for (StylesheetInfo info : infos) {
if (info.appliesToMedia(medium)) {
Stylesheet sheet = info.getStylesheet();

if (sheet == null) {
sheet = _stylesheetFactory.getStylesheet(info);
}

if (sheet!=null) {
if (sheet != null) {
if (sheet.getImportRules().size() > 0) {
result.addAll(readAndParseAll(sheet.getImportRules(), medium));
}
Expand All @@ -140,13 +122,7 @@ private List readAndParseAll(List infos, String medium) {

return result;
}

/**
* Description of the Method
*
* @param e PARAM
* @return Returns
*/

public boolean isHoverStyled(Element e) {
return _matcher.isHoverStyled(e);
}
Expand All @@ -155,22 +131,25 @@ public boolean isHoverStyled(Element e) {
* Returns a Map keyed by CSS property names (e.g. 'border-width'), and the
* assigned value as a SAC CSSValue instance. The properties should have
* been matched to the element when the Context was established for this
* StyleReference on the Document to which the Element belongs. See {@link
* com.openhtmltopdf.swing.BasicPanel#setDocument(Document, java.net.URL)}
* for an example of how to establish a StyleReference and associate to a
* Document.
* StyleReference on the Document to which the Element belongs.
*
* Only used by broken DOM inspector.
*
* @param e The DOM Element for which to find properties
* @return Map of CSS property names to CSSValue instance assigned to it.
*/
@Deprecated
public java.util.Map<String, org.w3c.dom.css.CSSPrimitiveValue> getCascadedPropertiesMap(Element e) {
CascadedStyle cs = _matcher.getCascadedStyle(e, false);//this is only for debug, I think
CascadedStyle cs = _matcher.getCascadedStyle(e, false);

java.util.Map<String, org.w3c.dom.css.CSSPrimitiveValue> props = new java.util.LinkedHashMap<String, org.w3c.dom.css.CSSPrimitiveValue>();

for (PropertyDeclaration pd : cs.getCascadedPropertyDeclarations()) {
String propName = pd.getPropertyName();
CSSName cssName = CSSName.getByPropertyName(propName);
props.put(propName, cs.propertyByName(cssName).getValue());
}

return props;
}

Expand Down Expand Up @@ -210,7 +189,9 @@ public PageInfo getPageStyle(String pageName, String pseudoPage) {

/**
* Flushes any stylesheet associated with this stylereference (based on the user agent callback) that are in cache.
* Deprecated for now, until we fix caching, use a new <code>StylesheetFactory</code> each run.
*/
@Deprecated
public void flushStyleSheets() {
String uri = _uac.getBaseURL();
StylesheetInfo info = new StylesheetInfo();
Expand All @@ -225,14 +206,15 @@ public void flushStyleSheets() {
}
}

@Deprecated
public void flushAllStyleSheets() {
_stylesheetFactory.flushCachedStylesheets();
}

/**
* Gets StylesheetInfos for all stylesheets and inline styles associated
* with the current document. Default (user agent) stylesheet and the inline
* style for the current media are loaded and cached in the
* style for the current media are loaded in the
* StyleSheetFactory by URI.
*
* @return The stylesheets value
Expand Down Expand Up @@ -274,6 +256,7 @@ private List<StylesheetInfo> getStylesheets() {
return infos;
}

@Deprecated
public void removeStyle(Element e) {
if (_matcher != null) {
_matcher.removeStyle(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,20 @@ public class StylesheetFactoryImpl implements StylesheetFactory {
*/
private UserAgentCallback _userAgentCallback;

private int _cacheCapacity = 16;
private final int _cacheCapacity = 16;

/**
* an LRU cache
*/
private java.util.LinkedHashMap _cache =
new java.util.LinkedHashMap(_cacheCapacity, 0.75f, true) {
private final java.util.LinkedHashMap<String, Stylesheet> _cache =
new java.util.LinkedHashMap<String, Stylesheet>(_cacheCapacity, 0.75f, true) {
private static final long serialVersionUID = 1L;

protected boolean removeEldestEntry(java.util.Map.Entry eldest) {
protected boolean removeEldestEntry(java.util.Map.Entry<String, Stylesheet> eldest) {
return size() > _cacheCapacity;
}
};

private CSSParser _cssParser;

public StylesheetFactoryImpl(UserAgentCallback userAgentCallback) {
Expand All @@ -70,12 +71,11 @@ public void error(String uri, String message) {
});
}

public synchronized Stylesheet parse(Reader reader, StylesheetInfo info) {
public Stylesheet parse(Reader reader, StylesheetInfo info) {
try {
return _cssParser.parseStylesheet(info.getUri(), info.getOrigin(), reader);
} catch (IOException e) {
XRLog.cssParse(Level.WARNING, "Couldn't parse stylesheet at URI " + info.getUri() + ": " + e.getMessage(), e);
e.printStackTrace();
return new Stylesheet(info.getUri(), info.getOrigin());
}
}
Expand Down Expand Up @@ -105,7 +105,7 @@ private Stylesheet parse(StylesheetInfo info) {
}
}

public synchronized Ruleset parseStyleDeclaration(int origin, String styleDeclaration) {
public Ruleset parseStyleDeclaration(int origin, String styleDeclaration) {
return _cssParser.parseDeclaration(origin, styleDeclaration);
}

Expand All @@ -117,7 +117,8 @@ public synchronized Ruleset parseStyleDeclaration(int origin, String styleDeclar
* factory.
* @param sheet The sheet to cache.
*/
public synchronized void putStylesheet(Object key, Stylesheet sheet) {
@Deprecated
public void putStylesheet(String key, Stylesheet sheet) {
_cache.put(key, sheet);
}

Expand All @@ -127,7 +128,8 @@ public synchronized void putStylesheet(Object key, Stylesheet sheet) {
* Note that the Stylesheet may be null.
*/
//TODO: work out how to handle caching properly, with cache invalidation
public synchronized boolean containsStylesheet(Object key) {
@Deprecated
public boolean containsStylesheet(String key) {
return _cache.containsKey(key);
}

Expand All @@ -138,8 +140,9 @@ public synchronized boolean containsStylesheet(Object key) {
* putStylesheet();
* @return The stylesheet
*/
public synchronized Stylesheet getCachedStylesheet(Object key) {
return (Stylesheet) _cache.get(key);
@Deprecated
public Stylesheet getCachedStylesheet(String key) {
return _cache.get(key);
}

/**
Expand All @@ -148,11 +151,13 @@ public synchronized Stylesheet getCachedStylesheet(Object key) {
* @param key The key for this sheet; same as key passed to
* putStylesheet();
*/
public synchronized Object removeCachedStylesheet(Object key) {
@Deprecated
public Object removeCachedStylesheet(String key) {
return _cache.remove(key);
}

public synchronized void flushCachedStylesheets() {
@Deprecated
public void flushCachedStylesheets() {
_cache.clear();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public static CascadedStyle createLayoutStyle(PropertyDeclaration[] decls) {
return new CascadedStyle(Arrays.asList(decls).iterator());
}

public static CascadedStyle createLayoutStyle(List decls) {
public static CascadedStyle createLayoutStyle(List<PropertyDeclaration> decls) {
return new CascadedStyle(decls.iterator());
}

Expand Down
Loading

0 comments on commit 8b09e42

Please sign in to comment.