KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.joshy.html.css;
2
3 import java.awt.Point JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import org.joshy.u;
6 import org.joshy.html.Border;
7 import java.awt.Color JavaDoc;
8 import org.w3c.dom.*;
9 import org.w3c.dom.css.*;
10
11 public abstract class CSSAccessor {
12     public abstract CSSValue getProperty(Element elem, String JavaDoc prop, boolean inherit);
13
14     // easy accessors
15
public float getFloatProperty(Element elem, String JavaDoc prop, float parent_value) {
16         return this.getFloatProperty(elem,prop,parent_value,true);
17     }
18
19     public float getFloatProperty(Node elem, String JavaDoc prop, float parent_value, boolean inherit) {
20         if(elem instanceof Element) {
21             return getFloatProperty((Element)elem, prop, parent_value, inherit);
22         } else {
23             return getFloatProperty((Element)(elem.getParentNode()), prop, parent_value, inherit);
24         }
25     }
26     
27     public float getFloatProperty(Element elem, String JavaDoc prop) {
28         return getFloatProperty(elem,prop,true);
29     }
30     
31     
32     public boolean hasProperty(Element elem, String JavaDoc prop) {
33         return hasProperty(elem,prop,true);
34     }
35
36     public boolean hasProperty(Node elem, String JavaDoc prop, boolean inherit) {
37         if(elem instanceof Element) {
38             return hasProperty((Element)elem, prop, inherit);
39         } else {
40             return hasProperty((Element)(elem.getParentNode()), prop, inherit);
41         }
42     }
43     
44     public String JavaDoc getStringProperty(Element elem, String JavaDoc prop) {
45         return getStringProperty(elem,prop,true);
46     }
47
48     public String JavaDoc getStringProperty(Node elem, String JavaDoc prop) {
49         return getStringProperty(elem,prop,true);
50     }
51
52
53     
54     
55     public float getFloatProperty(Element elem, String JavaDoc prop, float parent_value, boolean inherit) {
56         CSSValue val = getProperty(elem,prop,inherit);
57         //u.p("potential float value = " + val);
58
if(val == null) {
59             //u.dump_stack();
60
//u.p("elem " + elem.getNodeName() + " doesn't have the property: " + prop);
61
return -1;
62         }
63
64         if(val.getCssValueType() != val.CSS_PRIMITIVE_VALUE) {
65             u.p(val + " isn't a primitive value");
66             return -1;
67         }
68
69         CSSPrimitiveValue pval = (CSSPrimitiveValue)val;
70         //u.p("pval = " + pval);
71
if(pval.getPrimitiveType() == pval.CSS_PERCENTAGE) {
72             float retval = (pval.getFloatValue(pval.CSS_PERCENTAGE)/100) * parent_value;
73             //u.p("parent: " + parent_value + " %: " + pval.getFloatValue(pval.CSS_PERCENTAGE) + " return: " + retval);
74
return retval;
75         }
76         if(pval.getPrimitiveType() == pval.CSS_EMS) {
77             //u.p("val = " + pval);
78
float retval = (pval.getFloatValue(pval.CSS_PERCENTAGE)) * parent_value;
79             return retval;
80         }
81         //u.p("returning: " + pval.getFloatValue(pval.CSS_PX));
82
return pval.getFloatValue(pval.CSS_PX);
83     }
84
85
86     public float getFloatProperty(Element elem, String JavaDoc prop, boolean inherit) {
87         //u.p("get float property " + elem.getNodeName() + " " + prop);
88
CSSValue val = getProperty(elem,prop,inherit);
89         //u.p("potential float value = " + val);
90
if(val == null) {
91             //u.p("elem " + elem.getNodeName() + " doesn't have the property: " + prop);
92
return -1;
93         }
94         if(val.getCssValueType() != val.CSS_PRIMITIVE_VALUE) {
95             u.p(val + " isn't a primitive value");
96             return -1;
97         }
98         CSSPrimitiveValue pval = (CSSPrimitiveValue)val;
99         //u.p("returning: " + pval.getFloatValue(pval.CSS_PX));
100
return pval.getFloatValue(pval.CSS_PX);
101     }
102
103
104     public boolean hasProperty(Element elem, String JavaDoc prop, boolean inherit) {
105         CSSValue val = getProperty(elem,prop,inherit);
106         if(val == null) {
107             return false;
108         }
109         return true;
110     }
111
112
113     public String JavaDoc getStringProperty(Node elem, String JavaDoc prop, boolean inherit) {
114         if(elem instanceof Element) {
115             return getStringProperty((Element)elem,prop,true);
116         } else {
117             return getStringProperty((Element)(elem.getParentNode()), prop, inherit);
118         }
119     }
120     
121     public String JavaDoc getStringProperty(Element elem, String JavaDoc prop, boolean inherit) {
122         CSSValue val = getProperty(elem,prop,inherit);
123         if(val == null) {
124             return null;
125         }
126         if(val.getCssValueType() != val.CSS_PRIMITIVE_VALUE) {
127             u.p(val + " isn't a primitive value");
128             return null;
129         }
130         CSSPrimitiveValue pval = (CSSPrimitiveValue)val;
131         return pval.getStringValue();
132     }
133
134     public Point JavaDoc getFloatPairProperty(Element elem, String JavaDoc prop, boolean inherit) {
135         CSSValue val = getProperty(elem,prop,inherit);
136         if(val == null) {
137             return null;
138         }
139         if(val.getCssValueType() == val.CSS_VALUE_LIST) {
140             CSSValueList vl = (CSSValueList)val;
141             CSSPrimitiveValue pval = (CSSPrimitiveValue)val;
142             Point JavaDoc pt = new Point JavaDoc();
143             pt.setLocation(
144                 ((CSSPrimitiveValue)vl.item(0)).getFloatValue(pval.CSS_PERCENTAGE),
145                 ((CSSPrimitiveValue)vl.item(1)).getFloatValue(pval.CSS_PERCENTAGE)
146                 );
147             return pt;
148         }
149         return null;
150     }
151
152     public String JavaDoc[] getStringArrayProperty(Element elem, String JavaDoc prop) {
153         CSSValue val = getProperty(elem,prop,true);
154         if(val == null) {
155             u.p("not array found");
156             return null;
157         }
158         ArrayList JavaDoc al = new ArrayList JavaDoc();
159         if(val.getCssValueType() == val.CSS_VALUE_LIST) {
160             //u.p("got a list");
161
CSSValueList vl = (CSSValueList)val;
162             for(int i=0; i<vl.getLength(); i++) {
163                 al.add(((CSSPrimitiveValue)vl.item(i)).getStringValue());
164             }
165         } else {
166             //u.p("it wasn't a value list!: " + val);
167
al.add(((CSSPrimitiveValue)val).getStringValue());
168         }
169         return u.list_to_strings(al);
170     }
171
172     /* ========== Color stuff ============= */
173
174     public Color JavaDoc getBackgroundColor(Element elem) {
175         CSSValue val = getProperty(elem,"background-color",false);
176         if(val == null) {
177             return null;
178         }
179         return rgbToColor(((CSSPrimitiveValue)val).getRGBColorValue());
180     }
181
182     public Color JavaDoc getBorderColor(Element elem) {
183         CSSValue val = getProperty(elem,"border-color",true);
184         if(val == null) {
185             return null;
186         }
187         return rgbToColor(((CSSPrimitiveValue)val).getRGBColorValue());
188     }
189
190     public Color JavaDoc getColor(Element elem) {
191         return getColor(elem,true);
192     }
193     public Color JavaDoc getColor(Element elem, boolean inherit) {
194         //u.p("CSSBank.getColor("+elem.getNodeName() + ")");
195
CSSValue val = getProperty(elem,"color",inherit);
196         //u.p("val = " + val);
197
if(val.getCssValueType() == val.CSS_PRIMITIVE_VALUE) {
198             CSSPrimitiveValue pval = (CSSPrimitiveValue)val;
199             if(pval.getPrimitiveType() == pval.CSS_RGBCOLOR) {
200                 RGBColor rgbcol = pval.getRGBColorValue();
201                 //u.p("returning color: " + rgbToColor(rgbcol));
202
return rgbToColor(rgbcol);
203             }
204         }
205         return null;
206     }
207
208     private Color JavaDoc rgbToColor(RGBColor rgbcol) {
209         return new java.awt.Color JavaDoc(rgbcol.getRed().getFloatValue(CSSPrimitiveValue.CSS_NUMBER)/255f,
210             rgbcol.getGreen().getFloatValue(CSSPrimitiveValue.CSS_NUMBER)/255f,
211             rgbcol.getBlue().getFloatValue(CSSPrimitiveValue.CSS_NUMBER)/255f);
212     }
213
214
215     /* ======= margins, border, padding ========== */
216
217     public Border getBorderWidth(Element elem) {
218         float top = getFloatProperty(elem,"border-top-width");
219         float bottom = getFloatProperty(elem,"border-bottom-width");
220         float left = getFloatProperty(elem,"border-left-width");
221         //u.p("left w/o inherit = " + left + " for elem: " + elem);
222
float right = getFloatProperty(elem,"border-right-width");
223         Border border = new Border();
224         border.top = (int)top;
225         border.bottom = (int)bottom;
226         border.left = (int)left;
227         border.right = (int)right;
228         return border;
229     }
230
231     public Border getPaddingWidth(Element elem) {
232         float top = getFloatProperty(elem,"padding-top");
233         float bottom = getFloatProperty(elem,"padding-bottom");
234         float left = getFloatProperty(elem,"padding-left");
235         float right = getFloatProperty(elem,"padding-right");
236         Border border = new Border();
237         border.top = (int)top;
238         border.bottom = (int)bottom;
239         border.left = (int)left;
240         border.right = (int)right;
241         return border;
242     }
243
244     public Border getMarginWidth(Element elem) {
245         float top = getFloatProperty(elem,"margin-top");
246         float bottom = getFloatProperty(elem,"margin-bottom");
247         float left = getFloatProperty(elem,"margin-left");
248         float right = getFloatProperty(elem,"margin-right");
249         Border border = new Border();
250         border.top = (int)top;
251         border.bottom = (int)bottom;
252         border.left = (int)left;
253         border.right = (int)right;
254         return border;
255     }
256
257
258 }
259
260
261
Popular Tags