KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joshy > html > css > CSSParser


1 package org.joshy.html.css;
2
3 import java.util.Map JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.io.*;
9 import java.awt.Color JavaDoc;
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     /* parsing and sorting */
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         //u.p("got style sheet: " + style);
34
bank.sheets.add(style);
35         this.pullOutStyles(style);
36     }
37     public void parse(String JavaDoc reader) throws IOException {
38         CSSOMParser parser = new CSSOMParser();
39         InputSource is = new InputSource(reader);
40         CSSStyleSheet style = parser.parseStyleSheet(is);
41         //u.p("got style sheet: " + style);
42
bank.sheets.add(style);
43         this.pullOutStyles(style);
44     }
45     
46     
47     public void parseInlineStyles(Element elem) throws IOException {
48         // if this is a style node
49
if(elem.getNodeName().equals("style")) {
50             // check if we've already imported it
51
if(!bank.style_nodes.contains(elem)) {
52                 // import the style
53
CSSOMParser parser = new CSSOMParser();
54                 CSSStyleSheet style = parser.parseStyleSheet(new InputSource(new StringReader(x.text(elem))));
55                 // save the new style to the list
56
bank.sheets.add(style);
57                 //u.p("parsed: " + style);
58
// add this node to the imported list
59
bank.style_nodes.add(elem);
60                 this.pullOutStyles(style);
61             }
62         }
63         // do all of the children
64
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     /*
74     
75     */

76     public void pullOutStyles(CSSStyleSheet sheet) throws IOException {
77         //u.p("pull out styles");
78
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 JavaDoc 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 JavaDoc prop = rule.getStyle().item(i);
100             if(prop.equals("color") ||
101                 prop.equals("background-color") ||
102                 prop.equals("border-color")) {
103                     String JavaDoc 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 JavaDoc 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             //u.p("val = " + val);
131
return;
132         }
133         if(val.getCssValueType() == val.CSS_PRIMITIVE_VALUE) {
134             //u.p("val = " + val);
135
String JavaDoc 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 JavaDoc prop, CSSStyleRule rule, String JavaDoc before, String JavaDoc after) {
146         //u.p("rule = " + rule);
147
//u.p("prop = " + prop);
148
CSSStyleDeclaration dec = rule.getStyle();
149         CSSValue val = dec.getPropertyCSSValue(prop);
150         //u.p("value = " + val);
151
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                 //u.p("turning two into four");
158
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                 //u.p("turning three into four");
165
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                 //u.p("turning three into four");
172
top = list.item(0);
173                 right = list.item(1);
174                 bottom = list.item(2);
175                 left = list.item(3);
176             }
177         } else {
178             //u.p("only one to transform");
179
top = list;
180             bottom = list;//.item(0);
181
left = list;//.item(0);
182
right = list;//.item(0);
183
}
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 JavaDoc getColorHex(String JavaDoc value) {
191         if(value.indexOf("rgb")>=0) {
192             return value;
193         }
194         String JavaDoc retval = (String JavaDoc)color_map.get(value.toLowerCase());
195         return retval;
196     }
197     
198         
199     Map JavaDoc color_map;
200     private void init_color_map() {
201         color_map = new HashMap JavaDoc();
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