1 30 31 package com.steadystate.css.dom; 32 33 import java.io.Serializable ; 34 import java.io.StringReader ; 35 import java.util.*; 36 import org.w3c.css.sac.*; 37 import org.w3c.dom.*; 38 import org.w3c.dom.css.*; 39 import com.steadystate.css.parser.*; 40 41 45 public class CSSStyleDeclarationImpl implements CSSStyleDeclaration, Serializable { 46 47 private CSSRule _parentRule; 48 private Vector _properties = new Vector(); 49 50 public CSSStyleDeclarationImpl(CSSRule parentRule) { 51 _parentRule = parentRule; 52 } 53 54 public String getCssText() { 55 StringBuffer sb = new StringBuffer (); 56 sb.append("{"); 57 for (int i = 0; i < _properties.size(); ++i) { 60 Property p = (Property) _properties.elementAt(i); 61 if (p != null) { 62 sb.append(p.toString()); 63 } 64 if (i < _properties.size() - 1) { 65 sb.append("; "); 66 } 67 } 70 sb.append("}"); 71 return sb.toString(); 72 } 73 74 public void setCssText(String cssText) throws DOMException { 75 try { 76 InputSource is = new InputSource(new StringReader (cssText)); 77 CSSOMParser parser = new CSSOMParser(); 78 _properties.removeAllElements(); 79 parser.parseStyleDeclaration(this, is); 80 } catch (Exception e) { 81 throw new DOMExceptionImpl( 82 DOMException.SYNTAX_ERR, 83 DOMExceptionImpl.SYNTAX_ERROR, 84 e.getMessage()); 85 } 86 } 87 88 public String getPropertyValue(String propertyName) { 89 Property p = getPropertyDeclaration(propertyName); 90 return (p != null) ? p.getValue().toString() : ""; 91 } 92 93 public CSSValue getPropertyCSSValue(String propertyName) { 94 Property p = getPropertyDeclaration(propertyName); 95 return (p != null) ? p.getValue() : null; 96 } 97 98 public String removeProperty(String propertyName) throws DOMException { 99 for (int i = 0; i < _properties.size(); i++) { 100 Property p = (Property) _properties.elementAt(i); 101 if (p.getName().equalsIgnoreCase(propertyName)) { 102 _properties.removeElementAt(i); 103 return p.getValue().toString(); 104 } 105 } 106 return ""; 107 } 108 109 public String getPropertyPriority(String propertyName) { 110 Property p = getPropertyDeclaration(propertyName); 111 if (p != null) { 112 return p.isImportant() ? "important" : ""; 113 } 114 return ""; 115 } 116 117 public void setProperty( 118 String propertyName, 119 String value, 120 String priority ) throws DOMException { 121 try { 122 InputSource is = new InputSource(new StringReader (value)); 123 CSSOMParser parser = new CSSOMParser(); 124 CSSValue expr = parser.parsePropertyValue(is); 125 Property p = getPropertyDeclaration(propertyName); 126 boolean important = (priority != null) 127 ? priority.equalsIgnoreCase("important") 128 : false; 129 if (p == null) { 130 p = new Property(propertyName, expr, important); 131 addProperty(p); 132 } else { 133 p.setValue(expr); 134 p.setImportant(important); 135 } 136 } catch (Exception e) { 137 throw new DOMExceptionImpl( 138 DOMException.SYNTAX_ERR, 139 DOMExceptionImpl.SYNTAX_ERROR, 140 e.getMessage()); 141 } 142 } 143 144 public int getLength() { 145 return _properties.size(); 146 } 147 148 public String item(int index) { 149 Property p = (Property) _properties.elementAt(index); 150 return (p != null) ? p.getName() : ""; 151 } 152 153 public CSSRule getParentRule() { 154 return _parentRule; 155 } 156 157 public void addProperty(Property p) { 158 _properties.addElement(p); 159 } 160 161 private Property getPropertyDeclaration(String name) { 162 for (int i = 0; i < _properties.size(); i++) { 163 Property p = (Property) _properties.elementAt(i); 164 if (p.getName().equalsIgnoreCase(name)) { 165 return p; 166 } 167 } 168 return null; 169 } 170 171 public String toString() { 172 return getCssText(); 173 } 174 } 175 | Popular Tags |