1 package org.joshy.html.css; 2 3 import java.awt.Point ; 4 import java.util.ArrayList ; 5 import org.joshy.u; 6 import org.joshy.html.Border; 7 import java.awt.Color ; 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 prop, boolean inherit); 13 14 public float getFloatProperty(Element elem, String prop, float parent_value) { 16 return this.getFloatProperty(elem,prop,parent_value,true); 17 } 18 19 public float getFloatProperty(Node elem, String 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 prop) { 28 return getFloatProperty(elem,prop,true); 29 } 30 31 32 public boolean hasProperty(Element elem, String prop) { 33 return hasProperty(elem,prop,true); 34 } 35 36 public boolean hasProperty(Node elem, String 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 getStringProperty(Element elem, String prop) { 45 return getStringProperty(elem,prop,true); 46 } 47 48 public String getStringProperty(Node elem, String prop) { 49 return getStringProperty(elem,prop,true); 50 } 51 52 53 54 55 public float getFloatProperty(Element elem, String prop, float parent_value, boolean inherit) { 56 CSSValue val = getProperty(elem,prop,inherit); 57 if(val == null) { 59 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 if(pval.getPrimitiveType() == pval.CSS_PERCENTAGE) { 72 float retval = (pval.getFloatValue(pval.CSS_PERCENTAGE)/100) * parent_value; 73 return retval; 75 } 76 if(pval.getPrimitiveType() == pval.CSS_EMS) { 77 float retval = (pval.getFloatValue(pval.CSS_PERCENTAGE)) * parent_value; 79 return retval; 80 } 81 return pval.getFloatValue(pval.CSS_PX); 83 } 84 85 86 public float getFloatProperty(Element elem, String prop, boolean inherit) { 87 CSSValue val = getProperty(elem,prop,inherit); 89 if(val == null) { 91 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 return pval.getFloatValue(pval.CSS_PX); 101 } 102 103 104 public boolean hasProperty(Element elem, String 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 getStringProperty(Node elem, String 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 getStringProperty(Element elem, String 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 getFloatPairProperty(Element elem, String 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 pt = new Point (); 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 [] getStringArrayProperty(Element elem, String prop) { 153 CSSValue val = getProperty(elem,prop,true); 154 if(val == null) { 155 u.p("not array found"); 156 return null; 157 } 158 ArrayList al = new ArrayList (); 159 if(val.getCssValueType() == val.CSS_VALUE_LIST) { 160 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 al.add(((CSSPrimitiveValue)val).getStringValue()); 168 } 169 return u.list_to_strings(al); 170 } 171 172 173 174 public Color 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 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 getColor(Element elem) { 191 return getColor(elem,true); 192 } 193 public Color getColor(Element elem, boolean inherit) { 194 CSSValue val = getProperty(elem,"color",inherit); 196 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 return rgbToColor(rgbcol); 203 } 204 } 205 return null; 206 } 207 208 private Color rgbToColor(RGBColor rgbcol) { 209 return new java.awt.Color (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 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 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 |