Source code: com/steadystate/css/dom/CSSImportRuleImpl.java
1 /*
2 * CSSImportRuleImpl.java
3 *
4 * Steady State CSS2 Parser
5 *
6 * Copyright (C) 1999, 2002 Steady State Software Ltd. All rights reserved.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 * To contact the authors of the library, write to Steady State Software Ltd.,
23 * 49 Littleworth, Wing, Buckinghamshire, LU7 0JX, England
24 *
25 * http://www.steadystate.com/css/
26 * mailto:css@steadystate.co.uk
27 *
28 * $Id: CSSImportRuleImpl.java,v 1.1.1.1 2003/12/28 21:22:48 davidsch Exp $
29 */
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 /**
43 * TODO:
44 * Implement getStyleSheet()
45 *
46 * @author David Schweinsberg
47 * @version $Release$
48 */
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 // The rule must be an import rule
96 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 }