KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * CSSStyleSheetImpl.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: CSSStyleSheetImpl.java,v 1.2 2005/04/26 21:13:28 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  * TODO: Setting the media list
44  *
45  * @author David Schweinsberg
46  * @version $Release$
47  */

48 public class CSSStyleSheetImpl implements CSSStyleSheet, Serializable JavaDoc {
49
50     private boolean _disabled = false;
51     private Node _ownerNode = null;
52     private StyleSheet _parentStyleSheet = null;
53     private String JavaDoc _href = null;
54     private String JavaDoc _title = null;
55     private MediaList _media = null;
56     private CSSRule _ownerRule = null;
57     private boolean _readOnly = false;
58     private CSSRuleListImpl _rules = null;
59
60     public CSSStyleSheetImpl() {
61     }
62
63     public String JavaDoc getType() {
64         return "text/css";
65     }
66
67     public boolean getDisabled() {
68         return _disabled;
69     }
70
71     /**
72      * We will need to respond more fully if a stylesheet is disabled, probably
73      * by generating an event for the main application.
74      */

75     public void setDisabled(boolean disabled) {
76         _disabled = disabled;
77     }
78
79     public Node getOwnerNode() {
80         return _ownerNode;
81     }
82
83     public StyleSheet getParentStyleSheet() {
84         return _parentStyleSheet;
85     }
86
87     public String JavaDoc getHref() {
88         return _href;
89     }
90
91     public String JavaDoc getTitle() {
92         return _title;
93     }
94
95     public MediaList getMedia() {
96         return _media;
97     }
98
99     public CSSRule getOwnerRule() {
100         return _ownerRule;
101     }
102
103     public CSSRuleList getCssRules() {
104         return _rules;
105     }
106
107     public int insertRule(String JavaDoc rule, int index) throws DOMException {
108         if (_readOnly) {
109             throw new DOMExceptionImpl(
110                 DOMException.NO_MODIFICATION_ALLOWED_ERR,
111                 DOMExceptionImpl.READ_ONLY_STYLE_SHEET);
112         }
113
114         try {
115             InputSource is = new InputSource(new StringReader JavaDoc(rule));
116             CSSOMParser parser = new CSSOMParser();
117             parser.setParentStyleSheet(this);
118             CSSRule r = parser.parseRule(is);
119
120             if (getCssRules().getLength() > 0) {
121
122                 // We need to check that this type of rule can legally go into
123
// the requested position.
124
int msg = -1;
125                 if (r.getType() == CSSRule.CHARSET_RULE) {
126
127                     // Index must be 0, and there can be only one charset rule
128
if (index != 0) {
129                         msg = DOMExceptionImpl.CHARSET_NOT_FIRST;
130                     } else if (getCssRules().item(0).getType()
131                             == CSSRule.CHARSET_RULE) {
132                         msg = DOMExceptionImpl.CHARSET_NOT_UNIQUE;
133                     }
134                 } else if (r.getType() == CSSRule.IMPORT_RULE) {
135
136                     // Import rules must preceed all other rules (except
137
// charset rules)
138
if (index <= getCssRules().getLength()) {
139                         for (int i = 0; i < index; i++) {
140                             int rt = getCssRules().item(i).getType();
141                             if ((rt != CSSRule.CHARSET_RULE)
142                                     || (rt != CSSRule.IMPORT_RULE)) {
143                                 msg = DOMExceptionImpl.IMPORT_NOT_FIRST;
144                                 break;
145                             }
146                         }
147                     }
148                 }
149
150                 if (msg > -1) {
151                     throw new DOMExceptionImpl(
152                         DOMException.HIERARCHY_REQUEST_ERR,
153                         msg);
154                 }
155             }
156
157             // Insert the rule into the list of rules
158
((CSSRuleListImpl)getCssRules()).insert(r, index);
159
160         } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
161             throw new DOMExceptionImpl(
162                 DOMException.INDEX_SIZE_ERR,
163                 DOMExceptionImpl.ARRAY_OUT_OF_BOUNDS,
164                 e.getMessage());
165         } catch (CSSException e) {
166             throw new DOMExceptionImpl(
167                 DOMException.SYNTAX_ERR,
168                 DOMExceptionImpl.SYNTAX_ERROR,
169                 e.getMessage());
170         } catch (IOException JavaDoc e) {
171             throw new DOMExceptionImpl(
172                 DOMException.SYNTAX_ERR,
173                 DOMExceptionImpl.SYNTAX_ERROR,
174                 e.getMessage());
175         }
176         return index;
177     }
178
179     public void deleteRule(int index) throws DOMException {
180         if (_readOnly) {
181             throw new DOMExceptionImpl(
182                 DOMException.NO_MODIFICATION_ALLOWED_ERR,
183                 DOMExceptionImpl.READ_ONLY_STYLE_SHEET);
184         }
185
186         try {
187             ((CSSRuleListImpl)getCssRules()).delete(index);
188         } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
189             throw new DOMExceptionImpl(
190                 DOMException.INDEX_SIZE_ERR,
191                 DOMExceptionImpl.ARRAY_OUT_OF_BOUNDS,
192                 e.getMessage());
193         }
194     }
195
196     public boolean isReadOnly() {
197         return _readOnly;
198     }
199
200     public void setReadOnly(boolean b) {
201         _readOnly = b;
202     }
203
204     public void setOwnerNode(Node ownerNode) {
205         _ownerNode = ownerNode;
206     }
207
208     public void setParentStyleSheet(StyleSheet parentStyleSheet) {
209         _parentStyleSheet = parentStyleSheet;
210     }
211
212     public void setHref(String JavaDoc href) {
213         _href = href;
214     }
215
216     public void setTitle(String JavaDoc title) {
217         _title = title;
218     }
219
220     public void setMedia(String JavaDoc mediaText) {
221         InputSource source = new InputSource(new StringReader JavaDoc(mediaText));
222         try
223         {
224             this._media = new MediaListImpl(new SACParser().parseMedia(source));
225         }
226         catch (IOException JavaDoc e)
227         {
228             // TODO handle exception
229
}
230     }
231
232     public void setOwnerRule(CSSRule ownerRule) {
233         _ownerRule = ownerRule;
234     }
235     
236     public void setRuleList(CSSRuleListImpl rules) {
237         _rules = rules;
238     }
239     
240     public String JavaDoc toString() {
241         return _rules.toString();
242     }
243     
244     /**
245      * Imports referenced CSSStyleSheets.
246      *
247      * @param recursive <code>true</code> if the import should be done
248      * recursively, <code>false</code> otherwise
249      */

250     public void importImports(boolean recursive)
251         throws DOMException
252     {
253         for (int i = 0; i < this.getCssRules().getLength(); i++)
254         {
255             CSSRule cssRule = this.getCssRules().item(i);
256             if (cssRule.getType() == CSSRule.IMPORT_RULE)
257             {
258                 CSSImportRule cssImportRule = (CSSImportRule) cssRule;
259                 try
260                 {
261                     java.net.URI JavaDoc importURI = new java.net.URI JavaDoc(this.getHref())
262                         .resolve(cssImportRule.getHref());
263                     CSSStyleSheetImpl importedCSS = (CSSStyleSheetImpl)
264                         new CSSOMParser().parseStyleSheet(new InputSource(
265                             importURI.toString()));
266                     if (recursive)
267                     {
268                         importedCSS.importImports(recursive);
269                     }
270                     MediaList mediaList = cssImportRule.getMedia();
271                     if (mediaList.getLength() == 0)
272                     {
273                         mediaList.appendMedium("all");
274                     }
275                     CSSMediaRuleImpl cssMediaRule =
276                         new CSSMediaRuleImpl(this, null, mediaList);
277                     cssMediaRule.setRuleList(
278                         (CSSRuleListImpl) importedCSS.getCssRules());
279                     this.deleteRule(i);
280                     ((CSSRuleListImpl) this.getCssRules()).insert(cssMediaRule, i);
281                 }
282                 catch (java.net.URISyntaxException JavaDoc e)
283                 {
284                     // TODO handle exception
285
throw new DOMException(DOMException.SYNTAX_ERR,
286                         e.getLocalizedMessage());
287                 }
288                 catch (IOException JavaDoc e)
289                 {
290                     // TODO handle exception
291
}
292             }
293         }
294     }
295 }
296
Popular Tags