KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > steadystate > css > dom > CSSMediaRuleImpl


1 /*
2  * CSSMediaRuleImpl.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: CSSMediaRuleImpl.java,v 1.2 2005/04/28 20:57:20 waldbaer Exp $
29  */

30
31 package com.steadystate.css.dom;
32
33 import java.io.IOException JavaDoc;
34 import java.io.Serializable JavaDoc;
35 import java.io.StringReader JavaDoc;
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  *
44  * @author David Schweinsberg
45  * @version $Release$
46  */

47 public class CSSMediaRuleImpl implements CSSMediaRule, Serializable JavaDoc {
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 JavaDoc getCssText() {
68         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("@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 JavaDoc 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 JavaDoc(cssText));
87             CSSOMParser parser = new CSSOMParser();
88             CSSRule r = parser.parseRule(is);
89
90             // The rule must be a media rule
91
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 JavaDoc 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 JavaDoc 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 JavaDoc(rule));
137             CSSOMParser parser = new CSSOMParser();
138             parser.setParentStyleSheet(_parentStyleSheet);
139             // parser._parentRule is never read
140
//parser.setParentRule(_parentRule);
141
CSSRule r = parser.parseRule(is);
142
143             // Insert the rule into the list of rules
144
((CSSRuleListImpl)getCssRules()).insert(r, index);
145
146         } catch (ArrayIndexOutOfBoundsException JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc toString() {
186         return getCssText();
187     }
188 }
189
Popular Tags