private void addMediaQuery(String query, CssBlock cssBlock) { System.out.println("adding media query " + query); String[] groups = cssBlock.getGroupNames(); for (int i = 0; i < groups.length; i++) { String group = groups[i]; AttributesGroup map = cssBlock.getGroupDeclarations(group); System.out.println("style " + group + " has " + map.size() + " attributes "); } }
/** * Adds a CSS block to this style sheet. A CSS block contains either a style or the colors, fonts, * borders or backgrounds areas. * * @param cssBlock the block containing CSS declarations */ public void addCssBlock(CssBlock cssBlock) { this.isInitialised = false; String selector = cssBlock.getSelector().toLowerCase(); String[] groupNames = cssBlock.getGroupNames(); AttributesGroup[] groups = new AttributesGroup[groupNames.length]; for (int i = 0; i < groups.length; i++) { groups[i] = cssBlock.getGroupDeclarations(groupNames[i]); } Map<String, AttributesGroup> target = null; if ("colors".equals(selector)) { target = this.colors; } else if ("fonts".equals(selector)) { target = this.fonts; } else if ("backgrounds".equals(selector)) { target = this.backgrounds; } else if ("borders".equals(selector)) { target = this.borders; } if (target != null) { for (int i = 0; i < groups.length; i++) { String name = groupNames[i]; AttributesGroup group = groups[i]; AttributesGroup targetGroup = (AttributesGroup) target.get(name); if (targetGroup == null) { target.put(name, group); } else { targetGroup.putAll(group); } } } else { // this is a style: String parent = null; int extendsPos = selector.indexOf(" extends "); int colonPos = selector.indexOf(':'); if (extendsPos != -1) { if (colonPos != -1) { throw new BuildException( "Invalid CSS: the CSS style \"" + selector + "\" is a pseudo style that uses an extends clause. Please either use a pseudo style (:hover, etc) or use the extends clause. Pseudo styles inherit from their base styles automatically. Please adjust your polish.css design settings."); } parent = selector.substring(extendsPos + 9).trim(); if (parent.charAt(0) == '.') { parent = parent.substring(1); } selector = selector.substring(0, extendsPos).trim(); if ("default".equals(selector)) { throw new BuildException( "Invalid CSS code: The style [default] must not extend any other style."); } } // check for the "[parentStyleName]:hover" "[parentStyleName]:pressed" etc syntax: if (colonPos != -1) { // allow any number of levels of pseudo styles, e.g. myStyle:hover:visited:pressed String subName = null; Style parentStyle = null; while (colonPos != -1) { subName = selector.substring(colonPos + 1); int subNameColonIndex = subName.indexOf(':'); if (subNameColonIndex != -1) { subName = subName.substring(0, subNameColonIndex).trim(); } parent = selector.substring(0, colonPos).trim(); if (parent.charAt(0) == '.') { parent = parent.substring(1); } parentStyle = getStyle(parent); if (parentStyle == null && this.mediaQueryCondition == null) { throw new BuildException( "Invalid CSS: the :" + subName + " CSS style \"" + selector + "\" needs to follow AFTER the referenced style definition. Please adjust your polish.css design settings."); } // found parent style, now set the implicit focused-style attribute: String newStyleName = subName; if (subName.equals("hover")) { newStyleName = "focused"; } selector = (selector.substring(0, colonPos).trim() + newStyleName + selector.substring(colonPos + subName.length() + 1).trim()) .trim(); subName = newStyleName; // System.out.println("selector: " + selector ); // System.out.println( "subname: [" + subName + "]"); colonPos = selector.indexOf(':'); } cssBlock.setSelector(selector); if (parentStyle != null) { AttributesGroup referenceMap = new AttributesGroup(parentStyle, subName, 1); referenceMap.put("style", parent + subName); parentStyle.addGroup(subName, referenceMap); } } boolean isDynamicStyle = false; // check if this style is dynamic: isDynamicStyle = (selector.indexOf(' ') != -1) || (selector.indexOf('\t') != -1) || (selector.indexOf('>') != -1) || (selector.indexOf('*') != -1); if (selector.charAt(0) == '.') { selector = selector.substring(1); if (PSEUDO_CLASSES.get(selector) != null) { throw new BuildException( "Invalid CSS code: The style [." + selector + "] uses a reserved name, please choose another one or remove the leading dot of the name."); } } else { // this could be a DYNAMIC style: if (PSEUDO_CLASSES.get(selector) == null && selector.indexOf(':') == -1) { isDynamicStyle = true; } } if (isDynamicStyle) { this.containsDynamicStyles = true; // System.out.println("project uses dynamic style: [" + selector + "]"); } // check for reserved names of the style-selector: if (KEYWORDS.get(selector) != null) { throw new BuildException( "Invalid CSS code: The style-selector [" + selector + "] uses a reserved keyword, please choose another name."); } if (selector.startsWith("@media ")) { addMediaQuery(selector.substring("@media ".length()).trim(), cssBlock); } else { // this is a traditional style: String styleName = StringUtil.replace(selector, '-', '_'); if (isDynamicStyle) { selector = StringUtil.replace(selector, ".", ""); selector = StringUtil.replace(selector, '\t', ' '); selector = StringUtil.replace(selector, " > ", ">"); selector = StringUtil.replace(selector, " > ", ">"); selector = StringUtil.replace(selector, " * ", "*"); selector = StringUtil.replace(selector, " ", " "); styleName = StringUtil.replace(selector, ' ', '_'); styleName = StringUtil.replace(styleName, ">", "__"); styleName = StringUtil.replace(styleName, "*", "___"); } // check style name for invalid characters: if ((styleName.indexOf('.') != -1) || (styleName.indexOf('"') != -1) || (styleName.indexOf('\'') != -1) || (styleName.indexOf('*') != -1) || (styleName.indexOf('+') != -1) || (styleName.indexOf('-') != -1) || (styleName.indexOf('/') != -1) || (styleName.indexOf(':') != -1) || (styleName.indexOf('=') != -1) || (styleName.indexOf('|') != -1) || (styleName.indexOf('&') != -1) || (styleName.indexOf('~') != -1) || (styleName.indexOf('!') != -1) || (styleName.indexOf('^') != -1) || (styleName.indexOf('(') != -1) || (styleName.indexOf(')') != -1) || (styleName.indexOf('%') != -1) || (styleName.indexOf('?') != -1) || (styleName.indexOf('#') != -1) || (styleName.indexOf('$') != -1) || (styleName.indexOf('@') != -1)) { throw new BuildException( "Invalid CSS code: The style-selector [" + selector + "] contains invalid characters, please use only alpha-numeric characters for style-names."); } Style style = (Style) this.stylesByName.get(styleName); if (style == null) { style = new Style(selector, styleName, isDynamicStyle, parent, cssBlock); this.styles.add(style); // System.out.println("added new style [" + style.getStyleName() + "]."); this.stylesByName.put(selector, style); } else { style.add(cssBlock); } } } }