Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/steadystate/css/dom/CSSMediaRuleImpl.java


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.1.1.1 2003/12/28 21:22:49 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   *
44   * @author  David Schweinsberg
45   * @version $Release$
46   */
47  public class CSSMediaRuleImpl implements CSSMediaRule, Serializable {
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 getCssText() {
68          StringBuffer sb = new StringBuffer("@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 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(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 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 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(rule));
137             CSSOMParser parser = new CSSOMParser();
138             parser.setParentStyleSheet(_parentStyleSheet);
139             parser.setParentRule(_parentRule);
140             CSSRule r = parser.parseRule(is);
141 
142             // Insert the rule into the list of rules
143             ((CSSRuleListImpl)getCssRules()).insert(r, index);
144 
145         } catch (ArrayIndexOutOfBoundsException e) {
146             throw new DOMExceptionImpl(
147                 DOMException.INDEX_SIZE_ERR,
148                 DOMExceptionImpl.ARRAY_OUT_OF_BOUNDS,
149                 e.getMessage());
150         } catch (CSSException e) {
151             throw new DOMExceptionImpl(
152                 DOMException.SYNTAX_ERR,
153                 DOMExceptionImpl.SYNTAX_ERROR,
154                 e.getMessage());
155         } catch (IOException e) {
156             throw new DOMExceptionImpl(
157                 DOMException.SYNTAX_ERR,
158                 DOMExceptionImpl.SYNTAX_ERROR,
159                 e.getMessage());
160         }
161         return index;
162     }
163 
164     public void deleteRule(int index) throws DOMException {
165         if (_parentStyleSheet != null && _parentStyleSheet.isReadOnly()) {
166             throw new DOMExceptionImpl(
167                 DOMException.NO_MODIFICATION_ALLOWED_ERR,
168                 DOMExceptionImpl.READ_ONLY_STYLE_SHEET);
169         }
170         try {
171             ((CSSRuleListImpl)getCssRules()).delete(index);
172         } catch (ArrayIndexOutOfBoundsException e) {
173             throw new DOMExceptionImpl(
174                 DOMException.INDEX_SIZE_ERR,
175                 DOMExceptionImpl.ARRAY_OUT_OF_BOUNDS,
176                 e.getMessage());
177         }
178     }
179 
180     public void setRuleList(CSSRuleListImpl rules) {
181         _rules = rules;
182     }
183     
184     public String toString() {
185         return getCssText();
186     }
187 }