@Override public void apply(String style) { RuleMap rulemap = new RuleMap(); setup(rulemap); try { InputSource source = new InputSource(new StringReader(style)); CSSOMParser parser = new CSSOMParser(new SACParserCSS3()); CSSStyleDeclaration decl = parser.parseStyleDeclaration(source); for (int i = 0; i < decl.getLength(); i++) { final String propName = decl.item(i); Rule rule = rulemap.get(propName); if (rule == null) { System.out.println("Unknown CSS property: " + propName); continue; } rule.consumer.accept(StylerValueConverter.convert(decl.getPropertyCSSValue(propName), rule.type)); } } catch(Exception e) { e.printStackTrace(); } }
/** * {@inheritDoc} */ @Override public String getCssText(final CSSFormat format) { final StringBuilder sb = new StringBuilder(); final String sel = getSelectorText(); sb.append("@page ").append(sel); if (sel.length() > 0) { sb.append(" "); } sb.append("{"); final CSSStyleDeclaration style = getStyle(); if (null != style) { sb.append(style.getCssText()); } sb.append("}"); return sb.toString(); }
/** * {@inheritDoc} */ @Override public String getCssText(final CSSFormat format) { final CSSStyleDeclaration style = getStyle(); if (null == style) { return ""; } final String selectorText = ((CSSFormatable) selectors_).getCssText(format); final String styleText = ((CSSFormatable) style).getCssText(format); if (null == styleText || styleText.length() == 0) { return selectorText + " { }"; } if (format != null && format.getPropertiesInSeparateLines()) { return selectorText + " {" + styleText + "}"; } return selectorText + " { " + styleText + " }"; }
private boolean equalsProperties(final CSSStyleDeclaration csd) { if ((csd == null) || (getLength() != csd.getLength())) { return false; } for (int i = 0; i < getLength(); i++) { final String propertyName = item(i); // CSSValue propertyCSSValue1 = getPropertyCSSValue(propertyName); // CSSValue propertyCSSValue2 = csd.getPropertyCSSValue(propertyName); final String propertyValue1 = getPropertyValue(propertyName); final String propertyValue2 = csd.getPropertyValue(propertyName); if (!LangUtils.equals(propertyValue1, propertyValue2)) { return false; } final String propertyPriority1 = getPropertyPriority(propertyName); final String propertyPriority2 = csd.getPropertyPriority(propertyName); if (!LangUtils.equals(propertyPriority1, propertyPriority2)) { return false; } } return true; }
/** * Adds the style declaration. * * @param styleDeclaration * the style declaration */ public void addStyleDeclaration(CSSStyleDeclaration styleDeclaration) { synchronized (this) { Collection<CSSStyleDeclaration> sd = this.styleDeclarations; if (sd == null) { sd = new LinkedList<CSSStyleDeclaration>(); this.styleDeclarations = sd; } sd.add(styleDeclaration); int length = styleDeclaration.getLength(); for (int i = 0; i < length; i++) { String propertyName = styleDeclaration.item(i); String propertyValue = styleDeclaration.getPropertyValue(propertyName); String priority = styleDeclaration.getPropertyPriority(propertyName); boolean important = "important".equals(priority); this.setPropertyValueProcessed(propertyName.toLowerCase(), propertyValue, styleDeclaration, important); } } }
/** * put style declarations * * @param elementRules * @param styleDeclarations * @param element * @param pseudoNames * @return */ private Collection<CSSStyleDeclaration> putStyleDeclarations(Collection<StyleRuleInfo> elementRules, Collection<CSSStyleDeclaration> styleDeclarations, HTMLElementImpl element, Set pseudoNames) { Iterator<StyleRuleInfo> i = elementRules.iterator(); while (i.hasNext()) { StyleRuleInfo styleRuleInfo = i.next(); if (styleRuleInfo.isSelectorMatch(element, pseudoNames)) { CSSStyleRule styleRule = styleRuleInfo.getStyleRule(); CSSStyleSheet styleSheet = styleRule.getParentStyleSheet(); if (styleSheet != null && styleSheet.getDisabled()) { continue; } if (styleDeclarations == null) { styleDeclarations = new LinkedList<CSSStyleDeclaration>(); } styleDeclarations.add(styleRule.getStyle()); } } return styleDeclarations; }
/** * Parses and creates a style declaration. * * @param value * The style declaration text. */ public CSSStyleDeclaration parseStyleDeclaration( String value ) { styleDeclarationBuilder.styleDeclaration = new StyleDeclaration( this ); try { parser.setDocumentHandler( styleDeclarationBuilder ); parser.parseStyleDeclaration( new InputSource( new StringReader( value ) ) ); } catch ( Exception e ) { /** @todo: logout the error message */ } return styleDeclarationBuilder.styleDeclaration; }
/** * Gets all the name/value pairs from a CSS declaration and puts them into a * <code>LinkedHashMap</code>. All the name and value is of string type. * * @param declaration * the declaration of the style rule * @param errors * the error list of the declaration * @return all the supported name/value pairs */ LinkedHashMap<String, ? extends Object> buildProperties( CSSStyleDeclaration declaration, List<StyleSheetParserException> errors ) { LinkedHashMap<String, String> properties = new LinkedHashMap<String, String>( ); for ( int i = 0; i < declaration.getLength( ); i++ ) { String cssName = declaration.item( i ); String cssValue = declaration.getPropertyValue( cssName ); if ( StringUtil.isBlank( cssName ) || StringUtil.isBlank( cssValue ) ) continue; properties.put( cssName, cssValue ); } return buildProperties( properties, errors ); }
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); } } return result; }
public static String escapeIFrameCss(String orig) { String rule = ""; CSSOMParser parser = new CSSOMParser(); try { List<String> rules = new ArrayList<>(); CSSStyleDeclaration decl = parser.parseStyleDeclaration(new InputSource(new StringReader(orig))); for (int i = 0; i < decl.getLength(); i++) { String property = decl.item(i); String value = decl.getPropertyValue(property); if (StringUtils.isBlank(property) || StringUtils.isBlank(value)) { continue; } if (ALLOWED_IFRAME_CSS_RULES.contains(property) && StringUtils.containsNone(value, FORBIDDEN_CSS_RULE_CHARACTERS)) { rules.add(property + ":" + decl.getPropertyValue(property) + ";"); } } rule = StringUtils.join(rules, ""); } catch (Exception e) { log.error(e.getMessage(), e); } return rule; }
public static String escapeImgCss(String orig) { String rule = ""; CSSOMParser parser = new CSSOMParser(); try { List<String> rules = new ArrayList<>(); CSSStyleDeclaration decl = parser.parseStyleDeclaration(new InputSource(new StringReader(orig))); for (int i = 0; i < decl.getLength(); i++) { String property = decl.item(i); String value = decl.getPropertyValue(property); if (StringUtils.isBlank(property) || StringUtils.isBlank(value)) { continue; } if (ALLOWED_IMG_CSS_RULES.contains(property) && StringUtils.containsNone(value, FORBIDDEN_CSS_RULE_CHARACTERS)) { rules.add(property + ":" + decl.getPropertyValue(property) + ";"); } } rule = StringUtils.join(rules, ""); } catch (Exception e) { log.error(e.getMessage(), e); } return rule; }
/** * <b>DOM</b>: Implements {@link * org.w3c.dom.css.ViewCSS#getComputedStyle(Element,String)}. */ public CSSStyleDeclaration getComputedStyle(Element elt, String pseudoElt) { if (elt instanceof CSSStylableElement) { return new CSSOMSVGComputedStyle(cssEngine, (CSSStylableElement)elt, pseudoElt); } return null; }
/** * <b>DOM</b>: Implements {@link * org.w3c.dom.css.ViewCSS#getComputedStyle(Element,String)}. */ public CSSStyleDeclaration getComputedStyle(Element elt, String pseudoElt) { if (elt instanceof CSSStylableElement) { return new CSSOMComputedStyle(cssEngine, (CSSStylableElement)elt, pseudoElt); } return null; }
/** * Returns an AnimatableValue for the underlying value of a CSS property. */ public AnimatableValue getUnderlyingCSSValue(Element animElt, AnimationTarget target, String pn) { ValueManager[] vms = cssEngine.getValueManagers(); int idx = cssEngine.getPropertyIndex(pn); if (idx != -1) { int type = vms[idx].getPropertyType(); Factory factory = factories[type]; if (factory == null) { throw new BridgeException (ctx, animElt, "attribute.not.animatable", new Object[] { target.getElement().getNodeName(), pn }); } SVGStylableElement e = (SVGStylableElement) target.getElement(); CSSStyleDeclaration over = e.getOverrideStyle(); String oldValue = over.getPropertyValue(pn); if (oldValue != null) { over.removeProperty(pn); } Value v = cssEngine.getComputedStyle(e, null, idx); if (oldValue != null && !oldValue.equals("")) { over.setProperty(pn, oldValue, null); } return factories[type].createValue(target, pn, v); } // XXX Doesn't handle shorthands. return null; }
/** * Returns the override style declaration for this element. */ public CSSStyleDeclaration getOverrideStyle() { if (overrideStyleDeclaration == null) { CSSEngine eng = ((SVGOMDocument) getOwnerDocument()).getCSSEngine(); overrideStyleDeclaration = new OverrideStyleDeclaration(eng); } return overrideStyleDeclaration; }
/** * Updates a property value in this target. */ public void updatePropertyValue(String pn, AnimatableValue val) { CSSStyleDeclaration over = getOverrideStyle(); if (val == null) { over.removeProperty(pn); } else { over.setProperty(pn, val.getCssText(), ""); } }
/** * <b>DOM</b>: Implements {@link org.w3c.dom.svg.SVGStylable#getStyle()}. */ public CSSStyleDeclaration getStyle() { if (style == null) { CSSEngine eng = ((SVGOMDocument)getOwnerDocument()).getCSSEngine(); style = new StyleDeclaration(eng); putLiveAttributeValue(null, SVG_STYLE_ATTRIBUTE, style); } return style; }
/** * <b>DOM</b>: Implements * {@link DocumentCSS#getOverrideStyle(Element,String)}. */ public CSSStyleDeclaration getOverrideStyle(Element elt, String pseudoElt) { if (elt instanceof SVGStylableElement && pseudoElt == null) { return ((SVGStylableElement) elt).getOverrideStyle(); } return null; }
protected int getCssPropertyValuePixels(CSSStyleDeclaration cssProperties, String propName) { CSSPrimitiveValue cssValue = (CSSPrimitiveValue) cssProperties .getPropertyCSSValue(propName); return (int) ((CSSPrimitiveValue) cssValue) .getFloatValue(cssValue.getPrimitiveType()); }
public static Color toSWTColor(Element element, String attributeName) { Document document = element.getOwnerDocument(); ViewCSS viewCSS = (ViewCSS) document.getDocumentElement(); CSSStyleDeclaration computedStyle = viewCSS.getComputedStyle(element, null); SVGPaint svgPaint = (SVGPaint) computedStyle.getPropertyCSSValue(attributeName); if (svgPaint.getPaintType() == SVGPaint.SVG_PAINTTYPE_RGBCOLOR) { RGBColor rgb = svgPaint.getRGBColor(); float red = rgb.getRed().getFloatValue(CSSValue.CSS_PRIMITIVE_VALUE); float green = rgb.getGreen().getFloatValue(CSSValue.CSS_PRIMITIVE_VALUE); float blue = rgb.getBlue().getFloatValue(CSSValue.CSS_PRIMITIVE_VALUE); return SWTResourceManager.getColor((int) red, (int) green, (int) blue); } return null; }
public void parseStyleDeclaration(final CSSStyleDeclaration sd, final InputSource source) throws IOException { final Stack<Object> nodeStack = new Stack<Object>(); nodeStack.push(sd); final CSSOMHandler handler = new CSSOMHandler(nodeStack); parser_.setDocumentHandler(handler); parser_.parseStyleDeclaration(source); }
/** * {@inheritDoc} */ @Override public String getCssText(final CSSFormat format) { final StringBuilder sb = new StringBuilder(); sb.append("@font-face {"); final CSSStyleDeclaration style = getStyle(); if (null != style) { sb.append(style.getCssText()); } sb.append("}"); return sb.toString(); }
@Override public boolean equals(final Object obj) { if (this == obj) { return true; } if (!(obj instanceof CSSStyleDeclaration)) { return false; } final CSSStyleDeclaration csd = (CSSStyleDeclaration) obj; // don't use parentRule in equals() // recursive loop -> stack overflow! return equalsProperties(csd); }
@Override public void changeValue(AbstractCSSProperties properties, String newValue, CSSStyleDeclaration declaration, boolean important) { properties.setPropertyValueLCAlt(BORDER, newValue, important); properties.setPropertyValueProcessed(BORDER_TOP, newValue, declaration, important); properties.setPropertyValueProcessed(BORDER_LEFT, newValue, declaration, important); properties.setPropertyValueProcessed(BORDER_BOTTOM, newValue, declaration, important); properties.setPropertyValueProcessed(BORDER_RIGHT, newValue, declaration, important); }
/** * Gets the local style object associated with the element. The properties * object returned only includes properties from the local style attribute. * It may return null only if the type of element does not handle * stylesheets. * * @return the style */ @Override public AbstractCSSProperties getStyle() { AbstractCSSProperties sds; synchronized (this) { sds = this.localStyleDeclarationState; if (sds != null) { return sds; } sds = new LocalCSSProperties(this); // Add any declarations in style attribute (last takes precedence). String style = this.getAttribute(STYLE_HTML); if (style != null && style.length() != 0) { CSSOMParser parser = new CSSOMParser(new SACParserCSS3()); InputSource inputSource = this.getCssInputSourceForDecl(style); try { CSSStyleDeclaration sd = parser.parseStyleDeclaration(inputSource); sd.setCssText(style); sds.addStyleDeclaration(sd); } catch (Exception err) { String id = this.getId(); String withId = id == null ? "" : " with ID '" + id + "'"; logger.error("Unable to parse style attribute value for element " + this.getTagName() + withId + " in " + this.getDocumentURL() + ".", err); } } this.localStyleDeclarationState = sds; } // Synchronization note: Make sure getStyle() does not return multiple // values. return sds; }
/** * add a style definition into the report. * * @param style * style definition. */ public void addStyle( String name, CSSStyleDeclaration style ) { assert ( style != null ); this.styles.add( style ); this.styleTable.put( name, style ); }