List of usage examples for org.w3c.dom.css CSSStyleSheet getCssRules
public CSSRuleList getCssRules();
From source file:com.pagecrumb.proxy.util.filter.CssProxyTransformParser.java
@Override public void parse(String document) { log.info("Parsing CSS: " + document); this.document = document; InputSource source = new InputSource(new StringReader(this.document)); try {// w w w.ja v a 2s . c o m CSSStyleSheet stylesheet = parser.parseStyleSheet(source, null, null); CSSRuleList ruleList = stylesheet.getCssRules(); log.info("Number of rules: " + ruleList.getLength()); // lets examine the stylesheet contents for (int i = 0; i < ruleList.getLength(); i++) { CSSRule rule = ruleList.item(i); if (rule instanceof CSSStyleRule) { CSSStyleRule styleRule = (CSSStyleRule) rule; log.info("selector: " + styleRule.getSelectorText()); CSSStyleDeclaration styleDeclaration = styleRule.getStyle(); //assertEquals(1, styleDeclaration.getLength()); for (int j = 0; j < styleDeclaration.getLength(); j++) { String property = styleDeclaration.item(j); log.info("property: " + property); log.info("value: " + styleDeclaration.getPropertyCSSValue(property).getCssText()); } } } } catch (IOException e) { e.printStackTrace(); } }
From source file:net.sf.nmedit.jtheme.store.StorageContext.java
public CSSStyleRule getStyleRule(String name) { CSSStyleRule rule = styleRuleMap.get(name); if (rule != null) return FakeRule.isFake(rule) ? null : rule; CSSStyleSheet css = getStyleSheet(); CSSRuleList list = css.getCssRules(); rule = null;/*from w ww .ja v a2 s . co m*/ for (int i = 0; i < list.getLength(); i++) { CSSRule arule = list.item(i); if (arule.getType() == CSSRule.STYLE_RULE) { CSSStyleRule srule = (CSSStyleRule) arule; if (name.equals(srule.getSelectorText())) { // we found the rule rule = srule; break; } } } if (rule == null) { styleRuleMap.put(name, FakeRule.instance()); return null; } styleRuleMap.put(name, rule); return rule; }
From source file:com.edgenius.wiki.ext.textnut.NutParser.java
/** * Remove tags outside body./*from w w w. ja va 2 s.c o m*/ * Apply HTML CSS into tag style. * Remove default attribute in tag. * * @param htmlContent * @return */ public String convertNutHTMLToPageHTML(String htmlContent) { //Parse whole HTML, and separator out CSS and tags list inside body. HtmlParser htmlParser = new HtmlParser(); HtmlNodeListenerImpl listener = new HtmlNodeListenerImpl(); htmlParser.scan(htmlContent, listener); StringBuffer css = new StringBuffer(); HTMLNodeContainer body = new HTMLNodeContainer(); //First, exact CSS style part, then remove all element outside body tag int require = 0; //1=css;2=body HTMLNode endNode = null; for (HTMLNode node : listener.getHtmlNode()) { if (!node.isTextNode() && "style".equals(node.getTagName()) && !node.isCloseTag()) { endNode = node.getPair(); require = 1; continue; } if (!node.isTextNode() && "body".equals(node.getTagName()) && !node.isCloseTag()) { endNode = node.getPair(); require = 2; continue; } if (endNode != null && require == 1) { if (endNode == node) { endNode = null; } else { if (node.isTextNode()) css.append(node.getText()); } continue; } if (endNode != null && require == 2) { if (endNode == node) { break; } else { body.add(node); } } } // parse and create a stylesheet composition CSSOMParser parser = new CSSOMParser(); //I don't know how to use CSSParser API to quick located style by selector, so blow map will be used for this purpose //its key is selector text, value is style list separated by ";" which is able to use in tag "class" attribute. Map<String, String> selectors = new HashMap<String, String>(); try { CSSStyleSheet stylesheet = parser .parseStyleSheet(new InputSource(new StringReader(css.toString().trim())), null, null); CSSRuleList ruleList = stylesheet.getCssRules(); for (int idx = 0; idx < ruleList.getLength(); idx++) { CSSRule rule = ruleList.item(idx); if (rule instanceof CSSStyleRule) { CSSStyleDeclaration style = ((CSSStyleRule) rule).getStyle(); StringBuffer buf = new StringBuffer(); int len = style.getLength(); for (int idj = 0; idj < len; idj++) { String name = style.item(idj); String value = style.getPropertyCSSValue(name).getCssText(); if ("margin".equals(name) && value.equals("0px 0px 0px 0px")) { continue; } else if ("font".equals(name) && value.equals("18px Helvetica")) { //This value must coordinate with TextNut default font setting. continue; } buf.append(name).append(":").append(value).append(";"); } //buf could be blank, even so, we still need put it into map so that class attribute can locate style by name selectors.put(((CSSStyleRule) rule).getSelectorText(), buf.toString()); } } //debug use: only for display: // for (int idx = 0; idx < ruleList.getLength(); idx++) { // CSSRule rule = ruleList.item(idx); // if (rule instanceof CSSStyleRule){ // CSSStyleRule styleRule=(CSSStyleRule)rule; // CSSStyleDeclaration style = styleRule.getStyle(); // System.out.println(styleRule.getSelectorText()); // for (int idj = 0; idj < style.getLength(); idj++){ // String name = style.item(idj); // System.out.println(name +"=" + style.getPropertyCSSValue(name).getCssText()); // } // } // } //apply CSS to body tags HTMLNode node; for (Iterator<HTMLNode> iter = body.iterator(); iter.hasNext();) { node = iter.next(); if (node.isTextNode()) continue; if (node.getAttributes() != null && !StringUtils.isBlank(node.getAttributes().get("class"))) { //we only support "tagName.styleName" format selector String style = selectors.get(node.getTagName() + "." + node.getAttributes().get("class")); if (style != null) { if (!"".equals(style)) { //could be blank, if style all removed because they are default value. node.setAttribute("style", style); } node.removeAttribute("class"); } } //embedded files object - <object data="file:///index_1.html">index_1.html</object> if ("object".equals(node.getTagName()) && node.getAttributes() != null && StringUtils.startsWith(node.getAttributes().get("data"), "file://")) { } } return body.toString().trim(); } catch (IOException e) { log.error("Parse CSS failed", e); } return htmlContent; }
From source file:org.castafiore.ecm.Main.java
public static String Parse(File css, String name) throws Exception { String result = ""; InputStream stream = new FileInputStream(css); InputSource source = new InputSource(new InputStreamReader(stream)); CSSOMParser parser = new CSSOMParser(); CSSStyleSheet stylesheet = parser.parseStyleSheet(source, null, null); CSSRuleList ruleList = stylesheet.getCssRules(); for (int i = 0; i < ruleList.getLength(); i++) { CSSRule rule = ruleList.item(i); if (rule instanceof CSSStyleRule) { CSSStyleRule styleRule = (CSSStyleRule) rule; String selector = styleRule.getSelectorText().replace("*", "").replace("html", "").trim(); StringBuilder b = new StringBuilder(); if (selector.startsWith("body")) { selector = "." + name; b.append(selector);/*from w w w . j av a 2 s. c o m*/ } else { String[] asSelector = StringUtils.split(selector, ","); for (String s : asSelector) { if (b.length() > 0) { b.append(" , "); } // if(s.trim().startsWith("a:")){ // // } s = s.replace("#", ".").trim(); s = "." + name + " " + s; b.append(s); } } System.out.println(b.toString() + "{"); result = result + b.toString() + "{\n"; CSSStyleDeclaration styleDeclaration = styleRule.getStyle(); for (int j = 0; j < styleDeclaration.getLength(); j++) { String property = styleDeclaration.item(j); String value = styleDeclaration.getPropertyValue(property); System.out.println("\t" + property + ":" + value); result = result + "\t" + property + ":" + value + ";\n"; } result = result + "}\n"; System.out.println("}"); } } if (stream != null) stream.close(); return result; }
From source file:org.xwiki.portlet.view.CSSStreamFilter.java
/** * {@inheritDoc}//from w w w.j a v a2s. com * * @see StreamFilter#filter(Reader, Writer) */ public void filter(Reader reader, Writer writer) { try { // Parse the CSS. CSSStyleSheet styleSheet = cssParser.parseStyleSheet(new InputSource(reader), null, null); // Filter the CSS. styleSheetFilter.filter(styleSheet); // Serialize the CSS. CSSRuleList rules = styleSheet.getCssRules(); for (int i = 0; i < rules.getLength(); i++) { writer.write(rules.item(i).getCssText()); } } catch (IOException e) { LOG.error("Failed to rewrite servlet CSS.", e); } }