KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * CSSStyleRuleImpl.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: CSSStyleRuleImpl.java,v 1.1.1.1 2003/12/28 21:22:51 davidsch 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.css.*;
38 import org.w3c.css.sac.*;
39 import com.steadystate.css.parser.*;
40
41 /**
42  *
43  * @author David Schweinsberg
44  * @version $Release$
45  */

46 public class CSSStyleRuleImpl implements CSSStyleRule, Serializable JavaDoc {
47
48     private CSSStyleSheetImpl _parentStyleSheet = null;
49     private CSSRule _parentRule = null;
50     private SelectorList _selectors = null;
51     private CSSStyleDeclaration _style = null;
52
53     public CSSStyleRuleImpl(CSSStyleSheetImpl parentStyleSheet, CSSRule parentRule, SelectorList selectors) {
54         _parentStyleSheet = parentStyleSheet;
55         _parentRule = parentRule;
56         _selectors = selectors;
57     }
58
59     public short getType() {
60         return STYLE_RULE;
61     }
62
63     public String JavaDoc getCssText() {
64         return getSelectorText() + " " + getStyle().toString();
65     }
66
67     public void setCssText(String JavaDoc cssText) throws DOMException {
68         if (_parentStyleSheet != null && _parentStyleSheet.isReadOnly()) {
69             throw new DOMExceptionImpl(
70                 DOMException.NO_MODIFICATION_ALLOWED_ERR,
71                 DOMExceptionImpl.READ_ONLY_STYLE_SHEET);
72         }
73
74         try {
75             InputSource is = new InputSource(new StringReader JavaDoc(cssText));
76             CSSOMParser parser = new CSSOMParser();
77             CSSRule r = parser.parseRule(is);
78
79             // The rule must be a style rule
80
if (r.getType() == CSSRule.STYLE_RULE) {
81                 _selectors = ((CSSStyleRuleImpl)r)._selectors;
82                 _style = ((CSSStyleRuleImpl)r)._style;
83             } else {
84                 throw new DOMExceptionImpl(
85                     DOMException.INVALID_MODIFICATION_ERR,
86                     DOMExceptionImpl.EXPECTING_STYLE_RULE);
87             }
88         } catch (CSSException e) {
89             throw new DOMExceptionImpl(
90                 DOMException.SYNTAX_ERR,
91                 DOMExceptionImpl.SYNTAX_ERROR,
92                 e.getMessage());
93         } catch (IOException JavaDoc e) {
94             throw new DOMExceptionImpl(
95                 DOMException.SYNTAX_ERR,
96                 DOMExceptionImpl.SYNTAX_ERROR,
97                 e.getMessage());
98         }
99     }
100
101     public CSSStyleSheet getParentStyleSheet() {
102         return _parentStyleSheet;
103     }
104
105     public CSSRule getParentRule() {
106         return _parentRule;
107     }
108
109     public String JavaDoc getSelectorText() {
110         return _selectors.toString();
111     }
112
113     public void setSelectorText(String JavaDoc selectorText) throws DOMException {
114         if (_parentStyleSheet != null && _parentStyleSheet.isReadOnly()) {
115             throw new DOMExceptionImpl(
116                 DOMException.NO_MODIFICATION_ALLOWED_ERR,
117                 DOMExceptionImpl.READ_ONLY_STYLE_SHEET );
118         }
119
120         try {
121             InputSource is = new InputSource(new StringReader JavaDoc(selectorText));
122             CSSOMParser parser = new CSSOMParser();
123             _selectors = parser.parseSelectors(is);
124         } catch (CSSException e) {
125             throw new DOMExceptionImpl(
126                 DOMException.SYNTAX_ERR,
127                 DOMExceptionImpl.SYNTAX_ERROR,
128                 e.getMessage());
129         } catch (IOException JavaDoc e) {
130             throw new DOMExceptionImpl(
131                 DOMException.SYNTAX_ERR,
132                 DOMExceptionImpl.SYNTAX_ERROR,
133                 e.getMessage());
134         }
135     }
136
137     public CSSStyleDeclaration getStyle() {
138         return _style;
139     }
140
141     public void setStyle(CSSStyleDeclarationImpl style) {
142         _style = style;
143     }
144     
145     public String JavaDoc toString() {
146         return getCssText();
147     }
148     
149     public SelectorList getSelectorList() {
150         return _selectors;
151     }
152 }
153
Popular Tags