List of usage examples for org.w3c.dom.css CSSStyleDeclaration getLength
public int getLength();
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 w w w .jav a 2s . com 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:com.edgenius.wiki.ext.textnut.NutParser.java
/** * Remove tags outside body.//from w w w . j a v a 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.css.ComputedCSSStyleDeclaration.java
/** * Makes a local, "computed", modification to this CSS style. * * @param declaration the style declaration * @param selector the selector determining that the style applies to this element *//*from www .j a v a 2 s . c om*/ public void applyStyleFromSelector(final org.w3c.dom.css.CSSStyleDeclaration declaration, final Selector selector) { final SelectorSpecificity specificity = new SelectorSpecificity(selector); for (int k = 0; k < declaration.getLength(); k++) { final String name = declaration.item(k); final String value = declaration.getPropertyValue(name); final String priority = declaration.getPropertyPriority(name); applyLocalStyleAttribute(name, value, priority, specificity); } }
From source file:org.akrogen.tkui.css.core.dom.properties.providers.CSSPropertyHandlerLazyProviderImpl.java
@Override protected CSSStyleDeclaration getDefaultCSSStyleDeclaration(CSSEngine engine, CSSStylableElement stylableElement, CSSStyleDeclaration newStyle, String pseudoE) throws Exception { if (stylableElement.getDefaultStyleDeclaration(pseudoE) != null) return stylableElement.getDefaultStyleDeclaration(pseudoE); if (newStyle != null) { StringBuffer style = null; int length = newStyle.getLength(); for (int i = 0; i < length; i++) { String propertyName = newStyle.item(i); String[] compositePropertiesNames = engine.getCSSCompositePropertiesNames(propertyName); if (compositePropertiesNames != null) { for (int j = 0; j < compositePropertiesNames.length; j++) { propertyName = compositePropertiesNames[j]; String s = getCSSPropertyStyle(engine, stylableElement, propertyName); if (s != null) { if (style == null) style = new StringBuffer(); style.append(s); }/* ww w.j a v a 2 s .c o m*/ } } else { String s = getCSSPropertyStyle(engine, stylableElement, propertyName); if (s != null) { if (style == null) style = new StringBuffer(); style.append(s); } } } if (style != null) { CSSStyleDeclaration defaultStyleDeclaration = engine.parseStyleDeclaration(style.toString()); stylableElement.setDefaultStyleDeclaration(pseudoE, defaultStyleDeclaration); return defaultStyleDeclaration; } } return stylableElement.getDefaultStyleDeclaration(pseudoE); }
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 a v a 2s .c om } 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
private static StyleDefinition parseStyleDefinition(final CSSStyleDeclaration declaration) { final StyleDefinitionImpl.Builder builder = new StyleDefinitionImpl.Builder(); boolean isFillNone = false; boolean isStrokeNone = false; for (int j = 0; j < declaration.getLength(); j++) { final String property = declaration.item(j).trim(); final String value = declaration.getPropertyValue(property).trim(); switch (property) { case OPACITY: builder.setAlpha(SVGAttributeParser.toPixelValue(value)); break; case FILL: if (ATTR_VALUE_NONE.equals(value)) { isFillNone = true;//from www .j a va2s . c o m } else { builder.setFillColor(SVGAttributeParser.toHexColorString(value)); } break; case FILL_OPACITY: builder.setFillAlpha(SVGAttributeParser.toPixelValue(value)); break; case STROKE: if (ATTR_VALUE_NONE.equals(value)) { isStrokeNone = true; } else { builder.setStrokeColor(SVGAttributeParser.toHexColorString(value)); } break; case STROKE_OPACITY: builder.setStrokeAlpha(SVGAttributeParser.toPixelValue(value)); break; case STROKE_WIDTH: builder.setStrokeWidth(SVGAttributeParser.toPixelValue(value)); break; case STROKE_DASHARRAY: builder.setStrokeDashArray(value); break; case FONT_FAMILY: builder.setFontFamily(value.trim()); break; case FONT_SIZE: builder.setFontSize(SVGAttributeParser.toPixelValue(value)); break; } } if (isFillNone) { builder.setFillAlpha(0); } if (isStrokeNone) { builder.setStrokeAlpha(0); } return builder.build(); }
From source file:org.kie.workbench.common.stunner.svg.gen.translator.css.SVGStyleTranslatorHelper.java
private static StyleDefinition parseStyleDefinition(final CSSStyleDeclaration declaration) { final StyleDefinitionImpl.Builder builder = new StyleDefinitionImpl.Builder(); boolean isFillNone = false; boolean isStrokeNone = false; for (int j = 0; j < declaration.getLength(); j++) { final String property = declaration.item(j).trim(); final String value = declaration.getPropertyValue(property).trim(); switch (property) { case OPACITY: builder.setAlpha(SVGAttributeParserUtils.toPixelValue(value)); break; case FILL: if (ATTR_VALUE_NONE.equals(value)) { isFillNone = true;//from w w w .java2 s. c o m } else { builder.setFillColor(SVGAttributeParserUtils.toHexColorString(value)); } break; case FILL_OPACITY: builder.setFillAlpha(SVGAttributeParserUtils.toPixelValue(value)); break; case STROKE: if (ATTR_VALUE_NONE.equals(value)) { isStrokeNone = true; } else { builder.setStrokeColor(SVGAttributeParserUtils.toHexColorString(value)); } break; case STROKE_OPACITY: builder.setStrokeAlpha(SVGAttributeParserUtils.toPixelValue(value)); break; case STROKE_WIDTH: builder.setStrokeWidth(SVGAttributeParserUtils.toPixelValue(value)); break; } } if (isFillNone) { builder.setFillAlpha(0); } if (isStrokeNone) { builder.setStrokeAlpha(0); } return builder.build(); }