KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * CSSStyleDeclarationImpl.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: CSSStyleDeclarationImpl.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.Serializable JavaDoc;
34 import java.io.StringReader JavaDoc;
35 import java.util.*;
36 import org.w3c.css.sac.*;
37 import org.w3c.dom.*;
38 import org.w3c.dom.css.*;
39 import com.steadystate.css.parser.*;
40
41 /**
42  * @author David Schweinsberg
43  * @version $Release$
44  */

45 public class CSSStyleDeclarationImpl implements CSSStyleDeclaration, Serializable JavaDoc {
46
47     private CSSRule _parentRule;
48     private Vector _properties = new Vector();
49     
50     public CSSStyleDeclarationImpl(CSSRule parentRule) {
51         _parentRule = parentRule;
52     }
53
54     public String JavaDoc getCssText() {
55         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
56         sb.append("{");
57         //if newlines requested in text
58
//sb.append("\n");
59
for (int i = 0; i < _properties.size(); ++i) {
60             Property p = (Property) _properties.elementAt(i);
61             if (p != null) {
62                 sb.append(p.toString());
63             }
64             if (i < _properties.size() - 1) {
65                 sb.append("; ");
66             }
67             //if newlines requested in text
68
//sb.append("\n");
69
}
70         sb.append("}");
71         return sb.toString();
72     }
73
74     public void setCssText(String JavaDoc cssText) throws DOMException {
75         try {
76             InputSource is = new InputSource(new StringReader JavaDoc(cssText));
77             CSSOMParser parser = new CSSOMParser();
78             _properties.removeAllElements();
79             parser.parseStyleDeclaration(this, is);
80         } catch (Exception JavaDoc e) {
81             throw new DOMExceptionImpl(
82                 DOMException.SYNTAX_ERR,
83                 DOMExceptionImpl.SYNTAX_ERROR,
84                 e.getMessage());
85         }
86     }
87
88     public String JavaDoc getPropertyValue(String JavaDoc propertyName) {
89         Property p = getPropertyDeclaration(propertyName);
90         return (p != null) ? p.getValue().toString() : "";
91     }
92
93     public CSSValue getPropertyCSSValue(String JavaDoc propertyName) {
94         Property p = getPropertyDeclaration(propertyName);
95         return (p != null) ? p.getValue() : null;
96     }
97
98     public String JavaDoc removeProperty(String JavaDoc propertyName) throws DOMException {
99         for (int i = 0; i < _properties.size(); i++) {
100             Property p = (Property) _properties.elementAt(i);
101             if (p.getName().equalsIgnoreCase(propertyName)) {
102                 _properties.removeElementAt(i);
103                 return p.getValue().toString();
104             }
105         }
106         return "";
107     }
108
109     public String JavaDoc getPropertyPriority(String JavaDoc propertyName) {
110         Property p = getPropertyDeclaration(propertyName);
111         if (p != null) {
112             return p.isImportant() ? "important" : "";
113         }
114         return "";
115     }
116
117     public void setProperty(
118             String JavaDoc propertyName,
119             String JavaDoc value,
120             String JavaDoc priority ) throws DOMException {
121         try {
122             InputSource is = new InputSource(new StringReader JavaDoc(value));
123             CSSOMParser parser = new CSSOMParser();
124             CSSValue expr = parser.parsePropertyValue(is);
125             Property p = getPropertyDeclaration(propertyName);
126             boolean important = (priority != null)
127                 ? priority.equalsIgnoreCase("important")
128                 : false;
129             if (p == null) {
130                 p = new Property(propertyName, expr, important);
131                 addProperty(p);
132             } else {
133                 p.setValue(expr);
134                 p.setImportant(important);
135             }
136         } catch (Exception JavaDoc e) {
137             throw new DOMExceptionImpl(
138             DOMException.SYNTAX_ERR,
139             DOMExceptionImpl.SYNTAX_ERROR,
140             e.getMessage());
141         }
142     }
143     
144     public int getLength() {
145         return _properties.size();
146     }
147
148     public String JavaDoc item(int index) {
149         Property p = (Property) _properties.elementAt(index);
150         return (p != null) ? p.getName() : "";
151     }
152
153     public CSSRule getParentRule() {
154         return _parentRule;
155     }
156
157     public void addProperty(Property p) {
158         _properties.addElement(p);
159     }
160
161     private Property getPropertyDeclaration(String JavaDoc name) {
162         for (int i = 0; i < _properties.size(); i++) {
163             Property p = (Property) _properties.elementAt(i);
164             if (p.getName().equalsIgnoreCase(name)) {
165                 return p;
166             }
167         }
168         return null;
169     }
170
171     public String JavaDoc toString() {
172         return getCssText();
173     }
174 }
175
Popular Tags