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 47 public class CSSMediaRuleImpl implements CSSMediaRule, Serializable { 48 49 private CSSStyleSheetImpl _parentStyleSheet = null; 50 private CSSRule _parentRule = null; 51 private MediaList _media = null; 52 private CSSRuleList _rules = null; 53 54 public CSSMediaRuleImpl( 55 CSSStyleSheetImpl parentStyleSheet, 56 CSSRule parentRule, 57 MediaList media) { 58 _parentStyleSheet = parentStyleSheet; 59 _parentRule = parentRule; 60 _media = media; 61 } 62 63 public short getType() { 64 return MEDIA_RULE; 65 } 66 67 public String getCssText() { 68 StringBuffer sb = new StringBuffer ("@media "); 69 sb.append(getMedia().toString()).append(" {"); 70 for (int i = 0; i < getCssRules().getLength(); i++) { 71 CSSRule rule = getCssRules().item(i); 72 sb.append(rule.getCssText()).append(" "); 73 } 74 sb.append("}"); 75 return sb.toString(); 76 } 77 78 public void setCssText(String cssText) throws DOMException { 79 if (_parentStyleSheet != null && _parentStyleSheet.isReadOnly()) { 80 throw new DOMExceptionImpl( 81 DOMException.NO_MODIFICATION_ALLOWED_ERR, 82 DOMExceptionImpl.READ_ONLY_STYLE_SHEET); 83 } 84 85 try { 86 InputSource is = new InputSource(new StringReader (cssText)); 87 CSSOMParser parser = new CSSOMParser(); 88 CSSRule r = parser.parseRule(is); 89 90 if (r.getType() == CSSRule.MEDIA_RULE) { 92 _media = ((CSSMediaRuleImpl)r)._media; 93 _rules = ((CSSMediaRuleImpl)r)._rules; 94 } else { 95 throw new DOMExceptionImpl( 96 DOMException.INVALID_MODIFICATION_ERR, 97 DOMExceptionImpl.EXPECTING_MEDIA_RULE); 98 } 99 } catch (CSSException e) { 100 throw new DOMExceptionImpl( 101 DOMException.SYNTAX_ERR, 102 DOMExceptionImpl.SYNTAX_ERROR, 103 e.getMessage()); 104 } catch (IOException e) { 105 throw new DOMExceptionImpl( 106 DOMException.SYNTAX_ERR, 107 DOMExceptionImpl.SYNTAX_ERROR, 108 e.getMessage()); 109 } 110 } 111 112 public CSSStyleSheet getParentStyleSheet() { 113 return _parentStyleSheet; 114 } 115 116 public CSSRule getParentRule() { 117 return _parentRule; 118 } 119 120 public MediaList getMedia() { 121 return _media; 122 } 123 124 public CSSRuleList getCssRules() { 125 return _rules; 126 } 127 128 public int insertRule(String rule, int index) throws DOMException { 129 if (_parentStyleSheet != null && _parentStyleSheet.isReadOnly()) { 130 throw new DOMExceptionImpl( 131 DOMException.NO_MODIFICATION_ALLOWED_ERR, 132 DOMExceptionImpl.READ_ONLY_STYLE_SHEET); 133 } 134 135 try { 136 InputSource is = new InputSource(new StringReader (rule)); 137 CSSOMParser parser = new CSSOMParser(); 138 parser.setParentStyleSheet(_parentStyleSheet); 139 CSSRule r = parser.parseRule(is); 142 143 ((CSSRuleListImpl)getCssRules()).insert(r, index); 145 146 } catch (ArrayIndexOutOfBoundsException e) { 147 throw new DOMExceptionImpl( 148 DOMException.INDEX_SIZE_ERR, 149 DOMExceptionImpl.ARRAY_OUT_OF_BOUNDS, 150 e.getMessage()); 151 } catch (CSSException e) { 152 throw new DOMExceptionImpl( 153 DOMException.SYNTAX_ERR, 154 DOMExceptionImpl.SYNTAX_ERROR, 155 e.getMessage()); 156 } catch (IOException e) { 157 throw new DOMExceptionImpl( 158 DOMException.SYNTAX_ERR, 159 DOMExceptionImpl.SYNTAX_ERROR, 160 e.getMessage()); 161 } 162 return index; 163 } 164 165 public void deleteRule(int index) throws DOMException { 166 if (_parentStyleSheet != null && _parentStyleSheet.isReadOnly()) { 167 throw new DOMExceptionImpl( 168 DOMException.NO_MODIFICATION_ALLOWED_ERR, 169 DOMExceptionImpl.READ_ONLY_STYLE_SHEET); 170 } 171 try { 172 ((CSSRuleListImpl)getCssRules()).delete(index); 173 } catch (ArrayIndexOutOfBoundsException e) { 174 throw new DOMExceptionImpl( 175 DOMException.INDEX_SIZE_ERR, 176 DOMExceptionImpl.ARRAY_OUT_OF_BOUNDS, 177 e.getMessage()); 178 } 179 } 180 181 public void setRuleList(CSSRuleListImpl rules) { 182 _rules = rules; 183 } 184 185 public String toString() { 186 return getCssText(); 187 } 188 } 189 | Popular Tags |