下面列出了怎么用org.w3c.dom.css.CSSStyleSheet的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Parses a CSS resource and get the CSSStyleSheet as the output.
*
* @param source
* the source of the CSS resource
* @return the CSSStyleSheet if succeed
* @throws IOException
* if the resource is not well-located
*/
public CSSStyleSheet parseStyleSheet( InputSource source )
throws IOException
{
CssHandler handler = new CssHandler( );
parser.setDocumentHandler( handler );
parser.setErrorHandler( errorHandler );
try
{
parser.parseStyleSheet( source );
}
catch ( StringIndexOutOfBoundsException e )
{
throw new CSSException( CSSException.SAC_SYNTAX_ERR );
}
return (StyleSheet) handler.getRoot( );
}
private static String processStyles(String tag, String clazz, String style, List<CSSStyleSheet> sheets) {
for (CSSStyleSheet sheet : sheets)
if (isScreenMedia(sheet.getMedia())) {
style = processStyles(null, clazz, style, sheet.getCssRules(), Selector.SAC_ELEMENT_NODE_SELECTOR);
style = processStyles(tag, clazz, style, sheet.getCssRules(), Selector.SAC_ELEMENT_NODE_SELECTOR);
style = processStyles(tag, clazz, style, sheet.getCssRules(), Selector.SAC_CONDITIONAL_SELECTOR);
}
return style;
}
/**
* Returns the cached parsed version of the specified CSS snippet. If there is no
* corresponding cached stylesheet, this method returns {@code null}.
*
* @param css the CSS snippet whose cached stylesheet is sought
* @return the cached stylesheet corresponding to the specified CSS snippet
*/
public CSSStyleSheet getCachedStyleSheet(final String css) {
final Entry cachedEntry = entries_.get(css);
if (cachedEntry == null) {
return null;
}
synchronized (entries_) {
cachedEntry.touch();
}
return (CSSStyleSheet) cachedEntry.value_;
}
public CSSStyleSheet getParentStyleSheet( )
{
return null;
}
public CSSStyleSheet getParentStyleSheet( )
{
return null;
}
/**
* Caches the parsed version of the specified CSS snippet. We key the cache based on CSS snippets (rather
* than requests and responses as is done above) because a) this allows us to cache inline CSS, b) CSS is
* extremely expensive to parse, so we want to avoid it as much as possible, c) CSS files aren't usually
* nearly as large as JavaScript files, so memory bloat won't be too bad, and d) caching on requests and
* responses requires checking dynamicity (see {@link #isCacheableContent(WebResponse)}), and headers often
* aren't set up correctly, disallowing caching when in fact it should be allowed.
*
* @param css the CSS snippet from which <tt>styleSheet</tt> is derived
* @param styleSheet the parsed version of <tt>css</tt>
*/
public void cache(final String css, final CSSStyleSheet styleSheet) {
final Entry entry = new Entry(css, null, styleSheet);
entries_.put(entry.key_, entry);
deleteOverflow();
}