1 18 package org.apache.batik.css.dom; 19 20 import java.util.HashMap ; 21 import java.util.Map ; 22 23 import org.apache.batik.css.engine.value.Value; 24 import org.w3c.dom.DOMException ; 25 import org.w3c.dom.css.CSSRule; 26 import org.w3c.dom.css.CSSStyleDeclaration; 27 import org.w3c.dom.css.CSSValue; 28 29 35 public class CSSOMStyleDeclaration implements CSSStyleDeclaration { 36 37 40 protected ValueProvider valueProvider; 41 42 45 protected ModificationHandler handler; 46 47 50 protected CSSRule parentRule; 51 52 55 protected Map values; 56 57 60 public CSSOMStyleDeclaration(ValueProvider vp, CSSRule parent) { 61 valueProvider = vp; 62 parentRule = parent; 63 } 64 65 68 public void setModificationHandler(ModificationHandler h) { 69 handler = h; 70 } 71 72 76 public String getCssText() { 77 return valueProvider.getText(); 78 } 79 80 84 public void setCssText(String cssText) throws DOMException { 85 if (handler == null) { 86 throw new DOMException 87 (DOMException.NO_MODIFICATION_ALLOWED_ERR, ""); 88 } else { 89 values = null; 90 handler.textChanged(cssText); 91 } 92 } 93 94 98 public String getPropertyValue(String propertyName) { 99 Value value = valueProvider.getValue(propertyName); 100 if (value == null) { 101 return ""; 102 } 103 return value.getCssText(); 104 } 105 106 110 public CSSValue getPropertyCSSValue(String propertyName) { 111 Value value = valueProvider.getValue(propertyName); 112 if (value == null) { 113 return null; 114 } 115 return getCSSValue(propertyName); 116 } 117 118 122 public String removeProperty(String propertyName) throws DOMException { 123 String result = getPropertyValue(propertyName); 124 if (result.length() > 0) { 125 if (handler == null) { 126 throw new DOMException 127 (DOMException.NO_MODIFICATION_ALLOWED_ERR, ""); 128 } else { 129 if (values != null) { 130 values.remove(propertyName); 131 } 132 handler.propertyRemoved(propertyName); 133 } 134 } 135 return result; 136 } 137 138 142 public String getPropertyPriority(String propertyName) { 143 return (valueProvider.isImportant(propertyName)) ? "important" : ""; 144 } 145 146 150 public void setProperty(String propertyName, String value, String prio) 151 throws DOMException { 152 if (handler == null) { 153 throw new DOMException 154 (DOMException.NO_MODIFICATION_ALLOWED_ERR, ""); 155 } else { 156 handler.propertyChanged(propertyName, value, prio); 157 } 158 } 159 160 164 public int getLength() { 165 return valueProvider.getLength(); 166 } 167 168 172 public String item(int index) { 173 return valueProvider.item(index); 174 } 175 176 180 public CSSRule getParentRule() { 181 return parentRule; 182 } 183 184 187 protected CSSValue getCSSValue(String name) { 188 CSSValue result = null; 189 if (values != null) { 190 result = (CSSValue)values.get(name); 191 } 192 if (result == null) { 193 result = createCSSValue(name); 194 if (values == null) { 195 values = new HashMap (11); 196 } 197 values.put(name, result); 198 } 199 return result; 200 } 201 202 205 protected CSSValue createCSSValue(String name) { 206 return new StyleDeclarationValue(name); 207 } 208 209 212 public interface ValueProvider { 213 214 217 Value getValue(String name); 218 219 222 boolean isImportant(String name); 223 224 227 String getText(); 228 229 232 int getLength(); 233 234 237 String item(int idx); 238 239 } 240 241 244 public interface ModificationHandler { 245 246 249 void textChanged(String text) throws DOMException ; 250 251 254 void propertyRemoved(String name) throws DOMException ; 255 256 259 void propertyChanged(String name, String value, String prio) 260 throws DOMException ; 261 262 } 263 264 267 public class StyleDeclarationValue 268 extends CSSOMValue 269 implements CSSOMValue.ValueProvider { 270 271 274 protected String property; 275 276 279 public StyleDeclarationValue(String prop) { 280 super(null); 281 this.valueProvider = this; 282 this.setModificationHandler(new AbstractModificationHandler() { 283 protected Value getValue() { 284 return StyleDeclarationValue.this.getValue(); 285 } 286 public void textChanged(String text) throws DOMException { 287 if (values == null || 288 values.get(this) == null || 289 StyleDeclarationValue.this.handler == null) { 290 throw new DOMException 291 (DOMException.NO_MODIFICATION_ALLOWED_ERR, ""); 292 } 293 String prio = getPropertyPriority(property); 294 CSSOMStyleDeclaration.this. 295 handler.propertyChanged(property, text, prio); 296 } 297 }); 298 299 property = prop; 300 } 301 302 304 307 public Value getValue() { 308 return CSSOMStyleDeclaration.this.valueProvider.getValue(property); 309 } 310 311 } 312 } 313 | Popular Tags |