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 CSSCharsetRuleImpl implements CSSCharsetRule, Serializable { 47 48 CSSStyleSheetImpl _parentStyleSheet = null; 49 CSSRule _parentRule = null; 50 String _encoding = null; 51 52 public CSSCharsetRuleImpl( 53 CSSStyleSheetImpl parentStyleSheet, 54 CSSRule parentRule, 55 String encoding) { 56 _parentStyleSheet = parentStyleSheet; 57 _parentRule = parentRule; 58 _encoding = encoding; 59 } 60 61 public short getType() { 62 return CHARSET_RULE; 63 } 64 65 public String getCssText() { 66 return "@charset \"" + getEncoding() + "\";"; 67 } 68 69 public void setCssText(String cssText) throws DOMException { 70 if (_parentStyleSheet != null && _parentStyleSheet.isReadOnly()) { 71 throw new DOMExceptionImpl( 72 DOMException.NO_MODIFICATION_ALLOWED_ERR, 73 DOMExceptionImpl.READ_ONLY_STYLE_SHEET); 74 } 75 76 try { 77 InputSource is = new InputSource(new StringReader (cssText)); 78 CSSOMParser parser = new CSSOMParser(); 79 CSSRule r = parser.parseRule(is); 80 81 if (r.getType() == CSSRule.CHARSET_RULE) { 83 _encoding = ((CSSCharsetRuleImpl)r)._encoding; 84 } else { 85 throw new DOMExceptionImpl( 86 DOMException.INVALID_MODIFICATION_ERR, 87 DOMExceptionImpl.EXPECTING_CHARSET_RULE); 88 } 89 } catch (CSSException e) { 90 throw new DOMExceptionImpl( 91 DOMException.SYNTAX_ERR, 92 DOMExceptionImpl.SYNTAX_ERROR, 93 e.getMessage()); 94 } catch (IOException e) { 95 throw new DOMExceptionImpl( 96 DOMException.SYNTAX_ERR, 97 DOMExceptionImpl.SYNTAX_ERROR, 98 e.getMessage()); 99 } 100 } 101 102 public CSSStyleSheet getParentStyleSheet() { 103 return _parentStyleSheet; 104 } 105 106 public CSSRule getParentRule() { 107 return _parentRule; 108 } 109 110 public String getEncoding() { 111 return _encoding; 112 } 113 114 public void setEncoding(String encoding) throws DOMException { 115 _encoding = encoding; 116 } 117 } 118 | Popular Tags |