1 package org.joshy.html.css; 2 3 import java.util.Map ; 4 import java.util.HashMap ; 5 import java.util.List ; 6 import java.util.ArrayList ; 7 import java.util.Iterator ; 8 import java.io.*; 9 import java.awt.Color ; 10 import org.joshy.u; 11 import org.joshy.x; 12 import org.joshy.html.*; 13 14 import com.steadystate.css.*; 15 import com.steadystate.css.parser.*; 16 import org.w3c.dom.*; 17 import org.w3c.dom.css.*; 18 import org.w3c.css.sac.*; 19 20 public class CSSParser { 21 CSSBank bank; 22 public CSSParser(CSSBank bank) { 23 this.bank = bank; 24 init_color_map(); 25 } 26 27 28 29 public void parse(Reader reader) throws IOException { 30 CSSOMParser parser = new CSSOMParser(); 31 InputSource is = new InputSource(reader); 32 CSSStyleSheet style = parser.parseStyleSheet(is); 33 bank.sheets.add(style); 35 this.pullOutStyles(style); 36 } 37 public void parse(String reader) throws IOException { 38 CSSOMParser parser = new CSSOMParser(); 39 InputSource is = new InputSource(reader); 40 CSSStyleSheet style = parser.parseStyleSheet(is); 41 bank.sheets.add(style); 43 this.pullOutStyles(style); 44 } 45 46 47 public void parseInlineStyles(Element elem) throws IOException { 48 if(elem.getNodeName().equals("style")) { 50 if(!bank.style_nodes.contains(elem)) { 52 CSSOMParser parser = new CSSOMParser(); 54 CSSStyleSheet style = parser.parseStyleSheet(new InputSource(new StringReader(x.text(elem)))); 55 bank.sheets.add(style); 57 bank.style_nodes.add(elem); 60 this.pullOutStyles(style); 61 } 62 } 63 NodeList nl = elem.getChildNodes(); 65 for(int i=0; i<nl.getLength(); i++) { 66 Node n = nl.item(i); 67 if(n.getNodeType() == n.ELEMENT_NODE) { 68 parseInlineStyles((Element)n); 69 } 70 } 71 } 72 73 76 public void pullOutStyles(CSSStyleSheet sheet) throws IOException { 77 CSSRuleList rules = sheet.getCssRules(); 79 for(int i=0; i<rules.getLength(); i++) { 80 CSSRule rule = rules.item(i); 81 if(rule.getType() == rule.STYLE_RULE) { 82 CSSStyleRule style_rule = (CSSStyleRule) rule; 83 style_rule = normalize(style_rule); 84 JStyle style_holder = new JStyle(); 85 style_holder.rule = style_rule; 86 style_holder.sheet = sheet; 87 String selector = style_rule.getSelectorText(); 88 CSSOMParser parser = new CSSOMParser(); 89 SelectorList list = parser.parseSelectors(new InputSource(new StringReader(selector))); 90 style_holder.selector_list = list; 91 style_holder.declaration = style_rule.getStyle(); 92 bank.styles.add(style_holder); 93 } 94 } 95 } 96 97 public CSSStyleRule normalize(CSSStyleRule rule) { 98 for(int i=0; i<rule.getStyle().getLength(); i++) { 99 String prop = rule.getStyle().item(i); 100 if(prop.equals("color") || 101 prop.equals("background-color") || 102 prop.equals("border-color")) { 103 String value = rule.getStyle().getPropertyValue(prop); 104 rule.getStyle().setProperty(prop,getColorHex(value),null); 105 } 106 if(prop.equals("padding")) { 107 expand(prop,rule); 108 } 109 if(prop.equals("margin")) { 110 expand(prop,rule); 111 } 112 if(prop.equals("border-width")) { 113 expand("border-width",rule,"border","-width"); 114 } 115 if(prop.equals("background-position")) { 116 expandBackgroundPosition(rule); 117 } 118 } 119 return rule; 120 } 121 122 public void expand(String prop, CSSStyleRule rule) { 123 expand(prop,rule,prop,""); 124 } 125 126 public void expandBackgroundPosition(CSSStyleRule rule) { 127 CSSStyleDeclaration dec = rule.getStyle(); 128 CSSValue val = dec.getPropertyCSSValue("background-position"); 129 if(val.getCssValueType() == val.CSS_VALUE_LIST) { 130 return; 132 } 133 if(val.getCssValueType() == val.CSS_PRIMITIVE_VALUE) { 134 String str = val.getCssText(); 136 if(str.startsWith("top")) { dec.setProperty("background-position","50% 0%",null); } 137 if(str.startsWith("bottom")) { dec.setProperty("background-position","50% 100%",null); } 138 if(str.startsWith("left")) { dec.setProperty("background-position","0% 50%",null); } 139 if(str.startsWith("right")) { dec.setProperty("background-position","100% 50%",null); } 140 return; 141 } 142 143 } 144 145 public void expand(String prop, CSSStyleRule rule, String before, String after) { 146 CSSStyleDeclaration dec = rule.getStyle(); 149 CSSValue val = dec.getPropertyCSSValue(prop); 150 CSSValueList list = (CSSValueList)val; 152 CSSValue top, bottom, left, right; 153 top = bottom = left = right = null; 154 155 if(val.getCssValueType() == val.CSS_VALUE_LIST) { 156 if(list.getLength() == 2) { 157 top = list.item(0); 159 bottom = list.item(0); 160 left = list.item(1); 161 right = list.item(1); 162 } 163 if(list.getLength() == 3) { 164 top = list.item(0); 166 left = list.item(1); 167 right = list.item(1); 168 bottom = list.item(2); 169 } 170 if(list.getLength() == 4) { 171 top = list.item(0); 173 right = list.item(1); 174 bottom = list.item(2); 175 left = list.item(3); 176 } 177 } else { 178 top = list; 180 bottom = list; left = list; right = list; } 184 dec.setProperty(before+"-top"+after,top.getCssText(),null); 185 dec.setProperty(before+"-bottom"+after,bottom.getCssText(),null); 186 dec.setProperty(before+"-left"+after,left.getCssText(),null); 187 dec.setProperty(before+"-right"+after,right.getCssText(),null); 188 } 189 190 public String getColorHex(String value) { 191 if(value.indexOf("rgb")>=0) { 192 return value; 193 } 194 String retval = (String )color_map.get(value.toLowerCase()); 195 return retval; 196 } 197 198 199 Map color_map; 200 private void init_color_map() { 201 color_map = new HashMap (); 202 color_map.put("black","#000000"); 203 color_map.put("white","#FFFFFF"); 204 color_map.put("red","#FF0000"); 205 color_map.put("yellow","#FFFF00"); 206 color_map.put("lime","#00ff00"); 207 color_map.put("aqua","#00ffff"); 208 color_map.put("blue","#0000ff"); 209 color_map.put("fuchsia","#ff00ff"); 210 color_map.put("gray","#808080"); 211 color_map.put("silver","#c0c0c0"); 212 color_map.put("maroon","#800000"); 213 color_map.put("olive","#808000"); 214 color_map.put("green","#008000"); 215 color_map.put("teal","#008080"); 216 color_map.put("navy","#000080"); 217 color_map.put("purple","#800080"); 218 } 219 220 221 222 } 223 | Popular Tags |