List of usage examples for org.w3c.dom.css CSSRuleList item
public CSSRule item(int index);
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 {//from ww w. j a v a2 s . 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 w w. jav a 2 s. c o 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 ww w. j ava 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:com.gargoylesoftware.htmlunit.javascript.host.Stylesheet.java
/** * Modifies the specified style object by adding any style rules which apply to the specified * element./*from w w w . ja va 2 s . com*/ * * @param style the style to modify * @param element the element to which style rules must apply in order for them to be added to * the specified style */ void modifyIfNecessary(final ComputedCSSStyleDeclaration style, final HTMLElement element) { final HtmlElement e = element.getDomNodeOrDie(); final CSSRuleList rules = getWrappedSheet().getCssRules(); if (rules == null) { return; } for (int i = 0; i < rules.getLength(); i++) { final CSSRule rule = rules.item(i); if (rule.getType() == CSSRule.STYLE_RULE) { final CSSStyleRuleImpl styleRule = (CSSStyleRuleImpl) rule; final SelectorList selectors = styleRule.getSelectors(); for (int j = 0; j < selectors.getLength(); j++) { final Selector selector = selectors.item(j); final boolean selected = selects(selector, e); if (selected) { final org.w3c.dom.css.CSSStyleDeclaration dec = styleRule.getStyle(); style.applyStyleFromSelector(dec, selector); } } } else if (rule.getType() == CSSRule.IMPORT_RULE) { final CSSImportRuleImpl importRule = (CSSImportRuleImpl) rule; Stylesheet sheet = imports_.get(importRule); if (sheet == null) { // TODO: surely wrong: in which case is it null and why? final String uri = (uri_ != null) ? uri_ : e.getPage().getWebResponse().getRequestSettings().getUrl().toExternalForm(); final String href = importRule.getHref(); final String url = UrlUtils.resolveUrl(uri, href); sheet = loadStylesheet(getWindow(), ownerNode_, null, url); imports_.put(importRule, sheet); } sheet.modifyIfNecessary(style, element); } } }
From source file:com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleSheet.java
private void modifyIfNecessary(final ComputedCSSStyleDeclaration style, final Element element, final CSSRuleList rules, final Set<String> alreadyProcessing) { if (rules == null) { return;//from w ww.j a va 2 s . c o m } final BrowserVersion browser = getBrowserVersion(); final DomElement e = element.getDomNodeOrDie(); final int rulesLength = rules.getLength(); for (int i = 0; i < rulesLength; i++) { final CSSRule rule = rules.item(i); final short ruleType = rule.getType(); if (CSSRule.STYLE_RULE == ruleType) { final CSSStyleRuleImpl styleRule = (CSSStyleRuleImpl) rule; final SelectorList selectors = styleRule.getSelectors(); for (int j = 0; j < selectors.getLength(); j++) { final Selector selector = selectors.item(j); final boolean selected = selects(browser, selector, e); if (selected) { final org.w3c.dom.css.CSSStyleDeclaration dec = styleRule.getStyle(); style.applyStyleFromSelector(dec, selector); } } } else if (CSSRule.IMPORT_RULE == ruleType) { final CSSImportRuleImpl importRule = (CSSImportRuleImpl) rule; final MediaList mediaList = importRule.getMedia(); if (isActive(this, mediaList)) { CSSStyleSheet sheet = imports_.get(importRule); if (sheet == null) { // TODO: surely wrong: in which case is it null and why? final String uri = (uri_ != null) ? uri_ : e.getPage().getUrl().toExternalForm(); final String href = importRule.getHref(); final String url = UrlUtils.resolveUrl(uri, href); sheet = loadStylesheet(getWindow(), ownerNode_, null, url); imports_.put(importRule, sheet); } if (!alreadyProcessing.contains(sheet.getUri())) { final CSSRuleList sheetRules = sheet.getWrappedSheet().getCssRules(); alreadyProcessing.add(getUri()); sheet.modifyIfNecessary(style, element, sheetRules, alreadyProcessing); } } } else if (CSSRule.MEDIA_RULE == ruleType) { final CSSMediaRuleImpl mediaRule = (CSSMediaRuleImpl) rule; final MediaList mediaList = mediaRule.getMedia(); if (isActive(this, mediaList)) { final CSSRuleList internalRules = mediaRule.getCssRules(); modifyIfNecessary(style, element, internalRules, alreadyProcessing); } } } }
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 www .ja va 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.kie.workbench.common.stunner.svg.gen.translator.css.SVGStyleTranslator.java
public static StyleSheetDefinition parseStyleSheetDefinition(final String cssPath, final InputStream cssStream) throws TranslatorException { final CSSStyleSheetImpl sheet = parseStyleSheet(new InputSource(new InputStreamReader(cssStream))); final CSSRuleList cssRules = sheet.getCssRules(); final StyleSheetDefinition result = new StyleSheetDefinition(cssPath); for (int i = 0; i < cssRules.getLength(); i++) { final CSSRule item = cssRules.item(i); if (CSSRule.STYLE_RULE == item.getType()) { final CSSStyleRuleImpl rule = (CSSStyleRuleImpl) item; final String selectorText = rule.getSelectorText(); final CSSStyleDeclaration declaration = rule.getStyle(); final StyleDefinition styleDefinition = parseStyleDefinition(declaration); result.addStyle(selectorText, styleDefinition); }//from w ww.j a v a 2 s . c om } return result; }
From source file:org.kie.workbench.common.stunner.svg.gen.translator.css.SVGStyleTranslator.java
private static StyleDefinition parseElementStyleDefinition(final String styleRaw) throws TranslatorException { final CSSStyleSheetImpl sheet = parseElementStyleSheet(styleRaw); final CSSRuleList cssRules = sheet.getCssRules(); for (int i = 0; i < cssRules.getLength(); i++) { final CSSRule item = cssRules.item(i); if (CSSRule.STYLE_RULE == item.getType()) { final CSSStyleRuleImpl rule = (CSSStyleRuleImpl) item; final CSSStyleDeclaration declaration = rule.getStyle(); return parseStyleDefinition(declaration); }/*from w ww . ja va 2s . c o m*/ } return null; }
From source file:org.kie.workbench.common.stunner.svg.gen.translator.css.SVGStyleTranslatorHelper.java
public static StyleDefinition parseStyleDefinition(final String styleRaw) throws TranslatorException { final CSSStyleSheetImpl sheet = parseStyleSheet(styleRaw); final CSSRuleList cssRules = sheet.getCssRules(); for (int i = 0; i < cssRules.getLength(); i++) { final CSSRule item = cssRules.item(i); if (CSSRule.STYLE_RULE == item.getType()) { final CSSStyleRuleImpl rule = (CSSStyleRuleImpl) item; String selectorText = rule.getSelectorText(); final CSSStyleDeclaration declaration = rule.getStyle(); return parseStyleDefinition(declaration); }/*from ww w .j av a2 s.c o m*/ } return null; }
From source file:org.xwiki.portlet.view.CSSStreamFilter.java
/** * {@inheritDoc}// w w w .ja va2s. co m * * @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); } }