diff --git a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/context/StyleReference.java b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/context/StyleReference.java index 7b1dc0689..08924f29f 100644 --- a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/context/StyleReference.java +++ b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/context/StyleReference.java @@ -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; @@ -56,21 +54,9 @@ 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 @@ -78,14 +64,8 @@ public class StyleReference { */ 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); @@ -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 infos = getStylesheets(); + XRLog.match("media = " + _context.getMedia()); + _matcher = new com.openhtmltopdf.css.newmatch.Matcher( new DOMTreeResolver(), attRes, @@ -115,10 +97,10 @@ 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 readAndParseAll(List infos, String medium) { + List result = new ArrayList(infos.size() + 15); + + for (StylesheetInfo info : infos) { if (info.appliesToMedia(medium)) { Stylesheet sheet = info.getStylesheet(); @@ -126,7 +108,7 @@ private List readAndParseAll(List infos, String medium) { sheet = _stylesheetFactory.getStylesheet(info); } - if (sheet!=null) { + if (sheet != null) { if (sheet.getImportRules().size() > 0) { result.addAll(readAndParseAll(sheet.getImportRules(), medium)); } @@ -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); } @@ -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 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 props = new java.util.LinkedHashMap(); + for (PropertyDeclaration pd : cs.getCascadedPropertyDeclarations()) { String propName = pd.getPropertyName(); CSSName cssName = CSSName.getByPropertyName(propName); props.put(propName, cs.propertyByName(cssName).getValue()); } + return props; } @@ -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 StylesheetFactory each run. */ + @Deprecated public void flushStyleSheets() { String uri = _uac.getBaseURL(); StylesheetInfo info = new StylesheetInfo(); @@ -225,6 +206,7 @@ public void flushStyleSheets() { } } + @Deprecated public void flushAllStyleSheets() { _stylesheetFactory.flushCachedStylesheets(); } @@ -232,7 +214,7 @@ public void flushAllStyleSheets() { /** * 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 @@ -274,6 +256,7 @@ private List getStylesheets() { return infos; } + @Deprecated public void removeStyle(Element e) { if (_matcher != null) { _matcher.removeStyle(e); diff --git a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/context/StylesheetFactoryImpl.java b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/context/StylesheetFactoryImpl.java index 5e750c78f..5d9ba5d2c 100644 --- a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/context/StylesheetFactoryImpl.java +++ b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/context/StylesheetFactoryImpl.java @@ -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 _cache = + new java.util.LinkedHashMap(_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 eldest) { return size() > _cacheCapacity; } }; + private CSSParser _cssParser; public StylesheetFactoryImpl(UserAgentCallback userAgentCallback) { @@ -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()); } } @@ -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); } @@ -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); } @@ -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); } @@ -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); } /** @@ -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(); } diff --git a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/newmatch/CascadedStyle.java b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/newmatch/CascadedStyle.java index 38e8caea4..4f6e6b72d 100644 --- a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/newmatch/CascadedStyle.java +++ b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/newmatch/CascadedStyle.java @@ -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 decls) { return new CascadedStyle(decls.iterator()); } diff --git a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/newmatch/Condition.java b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/newmatch/Condition.java index cb912770e..56aa5463f 100644 --- a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/newmatch/Condition.java +++ b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/newmatch/Condition.java @@ -24,8 +24,6 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.w3c.dom.Element; - import com.openhtmltopdf.css.extend.AttributeResolver; import com.openhtmltopdf.css.extend.TreeResolver; import com.openhtmltopdf.css.parser.CSSParseException; @@ -39,6 +37,7 @@ abstract class Condition { abstract boolean matches(Object e, AttributeResolver attRes, TreeResolver treeRes); + abstract boolean disablesStyleCache(); /** * the CSS condition [attribute] @@ -241,6 +240,11 @@ boolean matches(Object e, AttributeResolver attRes, TreeResolver treeRes) { protected boolean compare(String attrValue, String conditionValue) { throw new UnsupportedOperationException(); } + + @Override + boolean disablesStyleCache() { + return false; + } } private static class AttributeEqualsCondition extends AttributeCompareCondition { @@ -251,6 +255,11 @@ private static class AttributeEqualsCondition extends AttributeCompareCondition protected boolean compare(String attrValue, String conditionValue) { return attrValue.equals(conditionValue); } + + @Override + boolean disablesStyleCache() { + return false; + } } private static class AttributePrefixCondition extends AttributeCompareCondition { @@ -261,6 +270,11 @@ private static class AttributePrefixCondition extends AttributeCompareCondition protected boolean compare(String attrValue, String conditionValue) { return attrValue.startsWith(conditionValue); } + + @Override + boolean disablesStyleCache() { + return false; + } } private static class AttributeSuffixCondition extends AttributeCompareCondition { @@ -271,6 +285,11 @@ private static class AttributeSuffixCondition extends AttributeCompareCondition protected boolean compare(String attrValue, String conditionValue) { return attrValue.endsWith(conditionValue); } + + @Override + boolean disablesStyleCache() { + return false; + } } private static class AttributeSubstringCondition extends AttributeCompareCondition { @@ -281,6 +300,11 @@ private static class AttributeSubstringCondition extends AttributeCompareConditi protected boolean compare(String attrValue, String conditionValue) { return attrValue.indexOf(conditionValue) > -1; } + + @Override + boolean disablesStyleCache() { + return false; + } } private static class AttributeMatchesListCondition extends AttributeCompareCondition { @@ -298,6 +322,11 @@ protected boolean compare(String attrValue, String conditionValue) { } return matched; } + + @Override + boolean disablesStyleCache() { + return false; + } } private static class AttributeMatchesFirstPartCondition extends AttributeCompareCondition { @@ -312,6 +341,11 @@ protected boolean compare(String attrValue, String conditionValue) { } return false; } + + @Override + boolean disablesStyleCache() { + return false; + } } private static class ClassCondition extends Condition { @@ -336,7 +370,11 @@ boolean matches(Object e, AttributeResolver attRes, TreeResolver treeRes) { // in an XML DOM, space normalization in attributes is supposed to have happened already. return (" " + c + " ").indexOf(_paddedClassName) != -1; } - + + @Override + boolean disablesStyleCache() { + return false; + } } private static class IDCondition extends Condition { @@ -356,7 +394,11 @@ boolean matches(Object e, AttributeResolver attRes, TreeResolver treeRes) { } return true; } - + + @Override + boolean disablesStyleCache() { + return false; + } } private static class LangCondition extends Condition { @@ -384,6 +426,10 @@ boolean matches(Object e, AttributeResolver attRes, TreeResolver treeRes) { return false; } + @Override + boolean disablesStyleCache() { + return false; + } } private static class FirstChildCondition extends Condition { @@ -394,6 +440,11 @@ private static class FirstChildCondition extends Condition { boolean matches(Object e, AttributeResolver attRes, TreeResolver treeRes) { return treeRes.isFirstChildElement(e); } + + @Override + boolean disablesStyleCache() { + return true; + } } @@ -405,6 +456,11 @@ private static class LastChildCondition extends Condition { boolean matches(Object e, AttributeResolver attRes, TreeResolver treeRes) { return treeRes.isLastChildElement(e); } + + @Override + boolean disablesStyleCache() { + return true; + } } @@ -465,6 +521,12 @@ static NthChildCondition fromString(String number) { } } } + + @Override + boolean disablesStyleCache() { + // We support even and odd nth-child conditions. + return this.a != 2 || (this.b != 1 && this.b != 0); + } } private static class EvenChildCondition extends Condition { @@ -476,6 +538,11 @@ boolean matches(Object e, AttributeResolver attRes, TreeResolver treeRes) { int position = treeRes.getPositionOfElement(e); return position >= 0 && position % 2 == 0; } + + @Override + boolean disablesStyleCache() { + return false; + } } private static class OddChildCondition extends Condition { @@ -487,6 +554,11 @@ boolean matches(Object e, AttributeResolver attRes, TreeResolver treeRes) { int position = treeRes.getPositionOfElement(e); return position >= 0 && position % 2 == 1; } + + @Override + boolean disablesStyleCache() { + return false; + } } private static class LinkCondition extends Condition { @@ -498,6 +570,10 @@ boolean matches(Object e, AttributeResolver attRes, TreeResolver treeRes) { return attRes.isLink(e); } + @Override + boolean disablesStyleCache() { + return false; + } } /** @@ -511,14 +587,18 @@ private static class UnsupportedCondition extends Condition { boolean matches(Object e, AttributeResolver attRes, TreeResolver treeRes) { return false; } - + + @Override + boolean disablesStyleCache() { + return false; + } } private static String[] split(String s, char ch) { if (s.indexOf(ch) == -1) { return new String[] { s }; } else { - List result = new ArrayList(); + List result = new ArrayList(); int last = 0; int next = 0; @@ -534,7 +614,7 @@ private static String[] split(String s, char ch) { result.add(s.substring(last)); } - return (String[])result.toArray(new String[result.size()]); + return result.toArray(new String[result.size()]); } } } diff --git a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/newmatch/Matcher.java b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/newmatch/Matcher.java index 82d5f650a..755e28b59 100644 --- a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/newmatch/Matcher.java +++ b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/newmatch/Matcher.java @@ -24,8 +24,6 @@ import java.util.Collections; import java.util.Comparator; import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; @@ -58,8 +56,8 @@ public class Matcher { private Set _focusElements; private Set _visitElements; - private List _pageRules; - private List _fontFaceRules; + private final List _pageRules = new ArrayList(); + private final List _fontFaceRules = new ArrayList(); public Matcher( TreeResolver tr, AttributeResolver ar, StylesheetFactory factory, List stylesheets, String medium) { @@ -67,9 +65,7 @@ public Matcher( _treeRes = tr; _attRes = ar; _styleFactory = factory; - - _pageRules = new ArrayList(); - _fontFaceRules = new ArrayList(); + docMapper = createDocumentMapper(stylesheets, medium); } @@ -78,7 +74,6 @@ public void removeStyle(Object e) { } public CascadedStyle getCascadedStyle(Object e, boolean restyle) { - //synchronized (e) { Mapper em; if (!restyle) { em = getMapper(e); @@ -86,7 +81,6 @@ public CascadedStyle getCascadedStyle(Object e, boolean restyle) { em = matchElement(e); } return em.getCascadedStyle(e); - //} } /** @@ -142,7 +136,6 @@ public boolean isFocusStyled(Object e) { } protected Mapper matchElement(Object e) { - //synchronized (e) { Object parent = _treeRes.getParentElement(e); Mapper child; if (parent != null) { @@ -152,7 +145,6 @@ protected Mapper matchElement(Object e) { child = docMapper.mapChild(e); } return child; - //} } Mapper createDocumentMapper(List stylesheets, String medium) { @@ -227,53 +219,6 @@ private Mapper getMapper(Object e) { return m; } - private static java.util.Iterator getMatchedRulesets(final List mappedSelectors) { - return - new java.util.Iterator() { - java.util.Iterator selectors = mappedSelectors.iterator(); - - public boolean hasNext() { - return selectors.hasNext(); - } - - public Object next() { - if (hasNext()) { - return ((Selector) selectors.next()).getRuleset(); - } else { - throw new java.util.NoSuchElementException(); - } - } - - public void remove() { - throw new UnsupportedOperationException(); - } - }; - } - - private static java.util.Iterator getSelectedRulesets(java.util.List selectorList) { - final java.util.List sl = selectorList; - return - new java.util.Iterator() { - java.util.Iterator selectors = sl.iterator(); - - public boolean hasNext() { - return selectors.hasNext(); - } - - public Object next() { - if (hasNext()) { - return ((Selector) selectors.next()).getRuleset(); - } else { - throw new java.util.NoSuchElementException(); - } - } - - public void remove() { - throw new UnsupportedOperationException(); - } - }; - } - private com.openhtmltopdf.css.sheet.Ruleset getElementStyle(Object e) { //synchronized (e) { if (_attRes == null || _styleFactory == null) { @@ -309,7 +254,7 @@ private com.openhtmltopdf.css.sheet.Ruleset getNonCssStyle(Object e) { * @author Torbjoern Gannholm */ class Mapper { - java.util.List axes; + java.util.List axes; private HashMap> pseudoSelectors; private List mappedSelectors; private Map children; @@ -334,8 +279,7 @@ Mapper mapChild(Object e) { java.util.HashMap> pseudoSelectors = new java.util.HashMap>(); java.util.List mappedSelectors = new java.util.ArrayList(); StringBuilder key = new StringBuilder(); - for (Object axe : axes) { - Selector sel = (Selector) axe; + for (Selector sel : axes) { if (sel.getAxis() == Selector.DESCENDANT_AXIS) { //carry it forward to other descendants childAxes.add(sel); @@ -397,7 +341,7 @@ Mapper mapChild(Object e) { CascadedStyle getCascadedStyle(Object e) { CascadedStyle result; - //synchronized (e) { + CascadedStyle cs = null; com.openhtmltopdf.css.sheet.Ruleset elementStyling = getElementStyle(e); com.openhtmltopdf.css.sheet.Ruleset nonCssStyling = getNonCssStyle(e); @@ -407,9 +351,8 @@ CascadedStyle getCascadedStyle(Object e) { propList.addAll(nonCssStyling.getPropertyDeclarations()); } //these should have been returned in order of specificity - for (Iterator i = getMatchedRulesets(mappedSelectors); i.hasNext();) { - com.openhtmltopdf.css.sheet.Ruleset rs = (com.openhtmltopdf.css.sheet.Ruleset) i.next(); - propList.addAll(rs.getPropertyDeclarations()); + for (Selector sel : mappedSelectors) { + propList.addAll(sel.getRuleset().getPropertyDeclarations()); } //specificity 1,0,0,0 if (elementStyling != null) { @@ -422,7 +365,7 @@ CascadedStyle getCascadedStyle(Object e) { } result = cs; - //} + return result; } @@ -440,9 +383,8 @@ public CascadedStyle getPECascadedStyle(Object e, String pseudoElement) { if (pe == null) return null; java.util.List propList = new java.util.ArrayList(); - for (java.util.Iterator i = getSelectedRulesets(pe); i.hasNext();) { - com.openhtmltopdf.css.sheet.Ruleset rs = (com.openhtmltopdf.css.sheet.Ruleset) i.next(); - propList.addAll(rs.getPropertyDeclarations()); + for (Selector sel : pe) { + propList.addAll(sel.getRuleset().getPropertyDeclarations()); } if (propList.size() == 0) cs = CascadedStyle.emptyCascadedStyle;//already internalized diff --git a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/newmatch/Selector.java b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/newmatch/Selector.java index 1e4bfa5d4..f53a57f0e 100644 --- a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/newmatch/Selector.java +++ b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/newmatch/Selector.java @@ -51,7 +51,7 @@ public class Selector { private int _pos;//to distinguish between selectors of same specificity - private java.util.List conditions; + private java.util.List conditions; public final static int DESCENDANT_AXIS = 0; public final static int CHILD_AXIS = 1; @@ -89,8 +89,7 @@ public boolean matches(Object e, AttributeResolver attRes, TreeResolver treeRes) if (_name == null || treeRes.matchesElement(e, _namespaceURI, _name)) { if (conditions != null) { // all conditions need to be true - for (java.util.Iterator i = conditions.iterator(); i.hasNext();) { - Condition c = (Condition) i.next(); + for (Condition c : conditions) { if (!c.matches(e, attRes, treeRes)) { return false; } @@ -421,7 +420,7 @@ Object getAppropriateSibling(Object e, TreeResolver treeRes) { */ private void addCondition(Condition c) { if (conditions == null) { - conditions = new java.util.ArrayList(); + conditions = new java.util.ArrayList(); } if (_pe != null) { conditions.add(Condition.createUnsupportedCondition()); diff --git a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/sheet/Stylesheet.java b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/sheet/Stylesheet.java index 840b0ac0c..5fc4528cc 100644 --- a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/sheet/Stylesheet.java +++ b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/sheet/Stylesheet.java @@ -35,24 +35,27 @@ * @author Patrick Wright */ public class Stylesheet implements RulesetContainer { + + private final String _uri; + /** - * The info for this stylesheet + * user-agent, user or author from StylesheetInfo */ - private String _uri; + private final int _origin; + + private final List _fontFaceRules = new ArrayList(); + private final List _importRules = new ArrayList(); + /** - * Description of the Field + * May contain page rules, media rules or rulesets. */ - private int _origin; - - private List _fontFaceRules = new ArrayList(); - private List _importRules = new ArrayList(); - private List _contents = new ArrayList(); + private final List _contents = new ArrayList(); /** * Creates a new instance of Stylesheet * * @param uri - * @param origin + * @param origin user-agent, user or author from StylesheetInfo */ public Stylesheet(String uri, int origin) { _uri = uri; @@ -62,7 +65,7 @@ public Stylesheet(String uri, int origin) { /** * Gets the origin attribute of the Stylesheet object * - * @return The origin value + * @return The origin value: user-agent, user or author from StylesheetInfo */ public int getOrigin() { return _origin; @@ -89,7 +92,10 @@ public void addContent(PageRule rule) { _contents.add(rule); } - public List getContents() { + /** + * @return a list containing page rules, media rules and rulesets in encounter order. + */ + public List getContents() { return _contents; } @@ -97,7 +103,7 @@ public void addImportRule(StylesheetInfo info) { _importRules.add(info); } - public List getImportRules() { + public List getImportRules() { return _importRules; } @@ -108,8 +114,7 @@ public void addFontFaceRule(FontFaceRule rule) { public List getFontFaceRules() { return _fontFaceRules; } - -}// end class +} /* * $Id$ diff --git a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/sheet/StylesheetInfo.java b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/sheet/StylesheetInfo.java index 13fa54efc..0ebbb2b29 100644 --- a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/sheet/StylesheetInfo.java +++ b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/sheet/StylesheetInfo.java @@ -21,12 +21,13 @@ import java.util.ArrayList; import java.util.List; +import java.util.Locale; /** * A reference to a stylesheet. If no stylesheet is set, the matcher will try to - * find the stylesheet by uri, first from the StylesheetFactory cache, then by + * find the stylesheet by uri, first from the StylesheetFactory, then by * loading the uri if it is not cached.

* * Therefore, either a stylesheet must be set, or a uri must be set

@@ -37,19 +38,12 @@ */ public class StylesheetInfo { - /** Description of the Field */ private Stylesheet stylesheet = null;//just to be able to attach "dummy" stylesheets. Also might save a lookup if it's already looked up - /** Description of the Field */ private String title; - /** Description of the Field */ private String uri; - /** Description of the Field */ private int origin = USER_AGENT; - /** Description of the Field */ private String type; - - private List mediaTypes = new ArrayList(); - + private List mediaTypes = new ArrayList(); private String content; /** Origin of stylesheet - user agent */ @@ -67,8 +61,8 @@ public class StylesheetInfo { * @return true if the stylesheet referenced applies to the medium */ public boolean appliesToMedia(String m) { - return m.toLowerCase().equals("all") || - mediaTypes.contains("all") || mediaTypes.contains(m.toLowerCase()); + return m.toLowerCase(Locale.US).equals("all") || + mediaTypes.contains("all") || mediaTypes.contains(m.toLowerCase(Locale.US)); } /** @@ -76,7 +70,7 @@ public boolean appliesToMedia(String m) { * * @param uri The new uri value */ - public void setUri( String uri ) { + public void setUri(String uri) { this.uri = uri; } @@ -85,16 +79,16 @@ public void setUri( String uri ) { * * @param media The new media value */ - public void setMedia( String media ) { + public void setMedia(String media) { String[] mediaTypes = media.split(","); - List l = new ArrayList(mediaTypes.length); + this.mediaTypes.clear(); + for (int i = 0; i < mediaTypes.length; i++) { - l.add(mediaTypes[i].trim().toLowerCase()); + this.mediaTypes.add(mediaTypes[i].trim().toLowerCase(Locale.US)); } - this.mediaTypes = l; } - public void setMedia(List mediaTypes) { + public void setMedia(List mediaTypes) { this.mediaTypes = mediaTypes; } @@ -152,7 +146,7 @@ public String getUri() { * * @return The media value */ - public List getMedia() { + public List getMedia() { return mediaTypes; }