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.css.*; 38 import org.w3c.css.sac.*; 39 import com.steadystate.css.parser.*; 40 41 46 public class CSSStyleRuleImpl implements CSSStyleRule, Serializable { 47 48 private CSSStyleSheetImpl _parentStyleSheet = null; 49 private CSSRule _parentRule = null; 50 private SelectorList _selectors = null; 51 private CSSStyleDeclaration _style = null; 52 53 public CSSStyleRuleImpl(CSSStyleSheetImpl parentStyleSheet, CSSRule parentRule, SelectorList selectors) { 54 _parentStyleSheet = parentStyleSheet; 55 _parentRule = parentRule; 56 _selectors = selectors; 57 } 58 59 public short getType() { 60 return STYLE_RULE; 61 } 62 63 public String getCssText() { 64 return getSelectorText() + " " + getStyle().toString(); 65 } 66 67 public void setCssText(String cssText) throws DOMException { 68 if (_parentStyleSheet != null && _parentStyleSheet.isReadOnly()) { 69 throw new DOMExceptionImpl( 70 DOMException.NO_MODIFICATION_ALLOWED_ERR, 71 DOMExceptionImpl.READ_ONLY_STYLE_SHEET); 72 } 73 74 try { 75 InputSource is = new InputSource(new StringReader (cssText)); 76 CSSOMParser parser = new CSSOMParser(); 77 CSSRule r = parser.parseRule(is); 78 79 if (r.getType() == CSSRule.STYLE_RULE) { 81 _selectors = ((CSSStyleRuleImpl)r)._selectors; 82 _style = ((CSSStyleRuleImpl)r)._style; 83 } else { 84 throw new DOMExceptionImpl( 85 DOMException.INVALID_MODIFICATION_ERR, 86 DOMExceptionImpl.EXPECTING_STYLE_RULE); 87 } 88 } catch (CSSException e) { 89 throw new DOMExceptionImpl( 90 DOMException.SYNTAX_ERR, 91 DOMExceptionImpl.SYNTAX_ERROR, 92 e.getMessage()); 93 } catch (IOException e) { 94 throw new DOMExceptionImpl( 95 DOMException.SYNTAX_ERR, 96 DOMExceptionImpl.SYNTAX_ERROR, 97 e.getMessage()); 98 } 99 } 100 101 public CSSStyleSheet getParentStyleSheet() { 102 return _parentStyleSheet; 103 } 104 105 public CSSRule getParentRule() { 106 return _parentRule; 107 } 108 109 public String getSelectorText() { 110 return _selectors.toString(); 111 } 112 113 public void setSelectorText(String selectorText) throws DOMException { 114 if (_parentStyleSheet != null && _parentStyleSheet.isReadOnly()) { 115 throw new DOMExceptionImpl( 116 DOMException.NO_MODIFICATION_ALLOWED_ERR, 117 DOMExceptionImpl.READ_ONLY_STYLE_SHEET ); 118 } 119 120 try { 121 InputSource is = new InputSource(new StringReader (selectorText)); 122 CSSOMParser parser = new CSSOMParser(); 123 _selectors = parser.parseSelectors(is); 124 } catch (CSSException e) { 125 throw new DOMExceptionImpl( 126 DOMException.SYNTAX_ERR, 127 DOMExceptionImpl.SYNTAX_ERROR, 128 e.getMessage()); 129 } catch (IOException e) { 130 throw new DOMExceptionImpl( 131 DOMException.SYNTAX_ERR, 132 DOMExceptionImpl.SYNTAX_ERROR, 133 e.getMessage()); 134 } 135 } 136 137 public CSSStyleDeclaration getStyle() { 138 return _style; 139 } 140 141 public void setStyle(CSSStyleDeclarationImpl style) { 142 _style = style; 143 } 144 145 public String toString() { 146 return getCssText(); 147 } 148 149 public SelectorList getSelectorList() { 150 return _selectors; 151 } 152 } 153 | Popular Tags |