1 30 31 package com.steadystate.css.dom; 32 33 import java.io.IOException ; 34 import java.io.Serializable ; 35 import java.io.StringReader ; 36 import org.w3c.dom.*; 37 import org.w3c.dom.stylesheets.*; 38 import org.w3c.dom.css.*; 39 import org.w3c.css.sac.*; 40 import com.steadystate.css.parser.*; 41 42 48 public class CSSStyleSheetImpl implements CSSStyleSheet, Serializable { 49 50 private boolean _disabled = false; 51 private Node _ownerNode = null; 52 private StyleSheet _parentStyleSheet = null; 53 private String _href = null; 54 private String _title = null; 55 private MediaList _media = null; 56 private CSSRule _ownerRule = null; 57 private boolean _readOnly = false; 58 private CSSRuleListImpl _rules = null; 59 60 public CSSStyleSheetImpl() { 61 } 62 63 public String getType() { 64 return "text/css"; 65 } 66 67 public boolean getDisabled() { 68 return _disabled; 69 } 70 71 75 public void setDisabled(boolean disabled) { 76 _disabled = disabled; 77 } 78 79 public Node getOwnerNode() { 80 return _ownerNode; 81 } 82 83 public StyleSheet getParentStyleSheet() { 84 return _parentStyleSheet; 85 } 86 87 public String getHref() { 88 return _href; 89 } 90 91 public String getTitle() { 92 return _title; 93 } 94 95 public MediaList getMedia() { 96 return _media; 97 } 98 99 public CSSRule getOwnerRule() { 100 return _ownerRule; 101 } 102 103 public CSSRuleList getCssRules() { 104 return _rules; 105 } 106 107 public int insertRule(String rule, int index) throws DOMException { 108 if (_readOnly) { 109 throw new DOMExceptionImpl( 110 DOMException.NO_MODIFICATION_ALLOWED_ERR, 111 DOMExceptionImpl.READ_ONLY_STYLE_SHEET); 112 } 113 114 try { 115 InputSource is = new InputSource(new StringReader (rule)); 116 CSSOMParser parser = new CSSOMParser(); 117 parser.setParentStyleSheet(this); 118 CSSRule r = parser.parseRule(is); 119 120 if (getCssRules().getLength() > 0) { 121 122 int msg = -1; 125 if (r.getType() == CSSRule.CHARSET_RULE) { 126 127 if (index != 0) { 129 msg = DOMExceptionImpl.CHARSET_NOT_FIRST; 130 } else if (getCssRules().item(0).getType() 131 == CSSRule.CHARSET_RULE) { 132 msg = DOMExceptionImpl.CHARSET_NOT_UNIQUE; 133 } 134 } else if (r.getType() == CSSRule.IMPORT_RULE) { 135 136 if (index <= getCssRules().getLength()) { 139 for (int i = 0; i < index; i++) { 140 int rt = getCssRules().item(i).getType(); 141 if ((rt != CSSRule.CHARSET_RULE) 142 || (rt != CSSRule.IMPORT_RULE)) { 143 msg = DOMExceptionImpl.IMPORT_NOT_FIRST; 144 break; 145 } 146 } 147 } 148 } 149 150 if (msg > -1) { 151 throw new DOMExceptionImpl( 152 DOMException.HIERARCHY_REQUEST_ERR, 153 msg); 154 } 155 } 156 157 ((CSSRuleListImpl)getCssRules()).insert(r, index); 159 160 } catch (ArrayIndexOutOfBoundsException e) { 161 throw new DOMExceptionImpl( 162 DOMException.INDEX_SIZE_ERR, 163 DOMExceptionImpl.ARRAY_OUT_OF_BOUNDS, 164 e.getMessage()); 165 } catch (CSSException e) { 166 throw new DOMExceptionImpl( 167 DOMException.SYNTAX_ERR, 168 DOMExceptionImpl.SYNTAX_ERROR, 169 e.getMessage()); 170 } catch (IOException e) { 171 throw new DOMExceptionImpl( 172 DOMException.SYNTAX_ERR, 173 DOMExceptionImpl.SYNTAX_ERROR, 174 e.getMessage()); 175 } 176 return index; 177 } 178 179 public void deleteRule(int index) throws DOMException { 180 if (_readOnly) { 181 throw new DOMExceptionImpl( 182 DOMException.NO_MODIFICATION_ALLOWED_ERR, 183 DOMExceptionImpl.READ_ONLY_STYLE_SHEET); 184 } 185 186 try { 187 ((CSSRuleListImpl)getCssRules()).delete(index); 188 } catch (ArrayIndexOutOfBoundsException e) { 189 throw new DOMExceptionImpl( 190 DOMException.INDEX_SIZE_ERR, 191 DOMExceptionImpl.ARRAY_OUT_OF_BOUNDS, 192 e.getMessage()); 193 } 194 } 195 196 public boolean isReadOnly() { 197 return _readOnly; 198 } 199 200 public void setReadOnly(boolean b) { 201 _readOnly = b; 202 } 203 204 public void setOwnerNode(Node ownerNode) { 205 _ownerNode = ownerNode; 206 } 207 208 public void setParentStyleSheet(StyleSheet parentStyleSheet) { 209 _parentStyleSheet = parentStyleSheet; 210 } 211 212 public void setHref(String href) { 213 _href = href; 214 } 215 216 public void setTitle(String title) { 217 _title = title; 218 } 219 220 public void setMedia(String mediaText) { 221 InputSource source = new InputSource(new StringReader (mediaText)); 222 try 223 { 224 this._media = new MediaListImpl(new SACParser().parseMedia(source)); 225 } 226 catch (IOException e) 227 { 228 } 230 } 231 232 public void setOwnerRule(CSSRule ownerRule) { 233 _ownerRule = ownerRule; 234 } 235 236 public void setRuleList(CSSRuleListImpl rules) { 237 _rules = rules; 238 } 239 240 public String toString() { 241 return _rules.toString(); 242 } 243 244 250 public void importImports(boolean recursive) 251 throws DOMException 252 { 253 for (int i = 0; i < this.getCssRules().getLength(); i++) 254 { 255 CSSRule cssRule = this.getCssRules().item(i); 256 if (cssRule.getType() == CSSRule.IMPORT_RULE) 257 { 258 CSSImportRule cssImportRule = (CSSImportRule) cssRule; 259 try 260 { 261 java.net.URI importURI = new java.net.URI (this.getHref()) 262 .resolve(cssImportRule.getHref()); 263 CSSStyleSheetImpl importedCSS = (CSSStyleSheetImpl) 264 new CSSOMParser().parseStyleSheet(new InputSource( 265 importURI.toString())); 266 if (recursive) 267 { 268 importedCSS.importImports(recursive); 269 } 270 MediaList mediaList = cssImportRule.getMedia(); 271 if (mediaList.getLength() == 0) 272 { 273 mediaList.appendMedium("all"); 274 } 275 CSSMediaRuleImpl cssMediaRule = 276 new CSSMediaRuleImpl(this, null, mediaList); 277 cssMediaRule.setRuleList( 278 (CSSRuleListImpl) importedCSS.getCssRules()); 279 this.deleteRule(i); 280 ((CSSRuleListImpl) this.getCssRules()).insert(cssMediaRule, i); 281 } 282 catch (java.net.URISyntaxException e) 283 { 284 throw new DOMException(DOMException.SYNTAX_ERR, 286 e.getLocalizedMessage()); 287 } 288 catch (IOException e) 289 { 290 } 292 } 293 } 294 } 295 } 296 | Popular Tags |