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 49 public class CSSImportRuleImpl implements CSSImportRule, Serializable { 50 51 CSSStyleSheetImpl _parentStyleSheet = null; 52 CSSRule _parentRule = null; 53 String _href = null; 54 MediaList _media = null; 55 56 public CSSImportRuleImpl( 57 CSSStyleSheetImpl parentStyleSheet, 58 CSSRule parentRule, 59 String href, 60 MediaList media) { 61 _parentStyleSheet = parentStyleSheet; 62 _parentRule = parentRule; 63 _href = href; 64 _media = media; 65 } 66 67 public short getType() { 68 return IMPORT_RULE; 69 } 70 71 public String getCssText() { 72 StringBuffer sb = new StringBuffer (); 73 sb.append("@import url(") 74 .append(getHref()) 75 .append(")"); 76 if (getMedia().getLength() > 0) { 77 sb.append(" ").append(getMedia().toString()); 78 } 79 sb.append(";"); 80 return sb.toString(); 81 } 82 83 public void setCssText(String cssText) throws DOMException { 84 if (_parentStyleSheet != null && _parentStyleSheet.isReadOnly()) { 85 throw new DOMExceptionImpl( 86 DOMException.NO_MODIFICATION_ALLOWED_ERR, 87 DOMExceptionImpl.READ_ONLY_STYLE_SHEET); 88 } 89 90 try { 91 InputSource is = new InputSource(new StringReader (cssText)); 92 CSSOMParser parser = new CSSOMParser(); 93 CSSRule r = parser.parseRule(is); 94 95 if (r.getType() == CSSRule.IMPORT_RULE) { 97 _href = ((CSSImportRuleImpl)r)._href; 98 _media = ((CSSImportRuleImpl)r)._media; 99 } else { 100 throw new DOMExceptionImpl( 101 DOMException.INVALID_MODIFICATION_ERR, 102 DOMExceptionImpl.EXPECTING_IMPORT_RULE); 103 } 104 } catch (CSSException e) { 105 throw new DOMExceptionImpl( 106 DOMException.SYNTAX_ERR, 107 DOMExceptionImpl.SYNTAX_ERROR, 108 e.getMessage()); 109 } catch (IOException e) { 110 throw new DOMExceptionImpl( 111 DOMException.SYNTAX_ERR, 112 DOMExceptionImpl.SYNTAX_ERROR, 113 e.getMessage()); 114 } 115 } 116 117 public CSSStyleSheet getParentStyleSheet() { 118 return _parentStyleSheet; 119 } 120 121 public CSSRule getParentRule() { 122 return _parentRule; 123 } 124 125 public String getHref() { 126 return _href; 127 } 128 129 public MediaList getMedia() { 130 return _media; 131 } 132 133 public CSSStyleSheet getStyleSheet() { 134 return null; 135 } 136 137 public String toString() { 138 return getCssText(); 139 } 140 } 141 | Popular Tags |