KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > steadystate > css > parser > CSSOMParser


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

30  
31 package com.steadystate.css.parser;
32
33 import java.io.*;
34 import java.util.*;
35 import org.w3c.dom.css.*;
36 import org.w3c.css.sac.*;
37 import org.w3c.css.sac.helpers.ParserFactory;
38 import com.steadystate.css.dom.*;
39
40 /**
41  *
42  * @author David Schweinsberg
43  * @version $Release$
44  */

45 public class CSSOMParser {
46     
47     private static final String JavaDoc PARSER = "com.steadystate.css.parser.SACParser";
48
49     private Parser _parser = null;
50     private CSSStyleSheetImpl _parentStyleSheet = null;
51     // TODO what is this _parentRule for? It is not read locally.
52
//private CSSRule _parentRule = null;
53

54     /** Creates new CSSOMParser */
55     public CSSOMParser() {
56         try {
57             // use the direct method if we already failed once before
58
if(use_internal) {
59                 _parser = new SACParser();
60             } else {
61                 setProperty("org.w3c.css.sac.parser", PARSER);
62                 ParserFactory factory = new ParserFactory();
63                 _parser = factory.makeParser();
64             }
65         } catch (Exception JavaDoc e) {
66             use_internal = true;
67             System.err.println(e.getMessage());
68             e.printStackTrace();
69             System.err.println("using the default parser instead");
70             _parser = new SACParser();
71         }
72     }
73     
74     private static boolean use_internal = false;
75
76     /**
77      * Creates new CSSOMParser
78      *
79      * @param parser the SAC Parser
80      */

81     public CSSOMParser(Parser parser)
82     {
83         this._parser = parser;
84     }
85
86     public CSSStyleSheet parseStyleSheet(InputSource source) throws IOException {
87         CSSOMHandler handler = new CSSOMHandler();
88         _parser.setDocumentHandler(handler);
89         _parser.parseStyleSheet(source);
90         return (CSSStyleSheet) handler.getRoot();
91     }
92     
93     public CSSStyleDeclaration parseStyleDeclaration(InputSource source)
94             throws IOException {
95         CSSStyleDeclarationImpl sd = new CSSStyleDeclarationImpl(null);
96         parseStyleDeclaration(sd, source);
97         return sd;
98     }
99     
100     public void parseStyleDeclaration(CSSStyleDeclaration sd, InputSource source)
101             throws IOException {
102         Stack nodeStack = new Stack();
103         nodeStack.push(sd);
104         CSSOMHandler handler = new CSSOMHandler(nodeStack);
105         _parser.setDocumentHandler(handler);
106         _parser.parseStyleDeclaration(source);
107     }
108     
109     public CSSValue parsePropertyValue(InputSource source) throws IOException {
110         CSSOMHandler handler = new CSSOMHandler();
111         _parser.setDocumentHandler(handler);
112         return new CSSValueImpl(_parser.parsePropertyValue(source));
113     }
114     
115     public CSSRule parseRule(InputSource source) throws IOException {
116         CSSOMHandler handler = new CSSOMHandler();
117         _parser.setDocumentHandler(handler);
118         _parser.parseRule(source);
119         return (CSSRule) handler.getRoot();
120     }
121     
122     public SelectorList parseSelectors(InputSource source) throws IOException {
123         HandlerBase handler = new HandlerBase();
124         _parser.setDocumentHandler(handler);
125         return _parser.parseSelectors(source);
126     }
127
128     public void setParentStyleSheet(CSSStyleSheetImpl parentStyleSheet) {
129         _parentStyleSheet = parentStyleSheet;
130     }
131
132     // See _parentRule
133
/*
134     public void setParentRule(CSSRule parentRule) {
135         _parentRule = parentRule;
136     }
137     */

138     
139     class CSSOMHandler implements DocumentHandler {
140         
141         private Stack _nodeStack;
142         private Object JavaDoc _root = null;
143
144         public CSSOMHandler(Stack nodeStack) {
145             _nodeStack = nodeStack;
146         }
147         
148         public CSSOMHandler() {
149             _nodeStack = new Stack();
150         }
151         
152         public Object JavaDoc getRoot() {
153             return _root;
154         }
155         
156         public void startDocument(InputSource source) throws CSSException {
157             if (_nodeStack.empty()) {
158                 CSSStyleSheetImpl ss = new CSSStyleSheetImpl();
159                 _parentStyleSheet = ss;
160                 ss.setHref(source.getURI());
161                 ss.setMedia(source.getMedia());
162                 ss.setTitle(source.getTitle());
163                 // Create the rule list
164
CSSRuleListImpl rules = new CSSRuleListImpl();
165                 ss.setRuleList(rules);
166                 _nodeStack.push(ss);
167                 _nodeStack.push(rules);
168             } else {
169                 // Error
170
}
171         }
172
173         public void endDocument(InputSource source) throws CSSException {
174
175             // Pop the rule list and style sheet nodes
176
_nodeStack.pop();
177             _root = _nodeStack.pop();
178         }
179
180         public void comment(String JavaDoc text) throws CSSException {
181         }
182
183         public void ignorableAtRule(String JavaDoc atRule) throws CSSException {
184
185             // Create the unknown rule and add it to the rule list
186
CSSUnknownRuleImpl ir = new CSSUnknownRuleImpl(
187                 _parentStyleSheet,
188                 null,
189                 atRule);
190             if (!_nodeStack.empty()) {
191                 ((CSSRuleListImpl)_nodeStack.peek()).add(ir);
192             } else {
193 // _nodeStack.push(ir);
194
_root = ir;
195             }
196         }
197
198         public void namespaceDeclaration(String JavaDoc prefix, String JavaDoc uri)
199                 throws CSSException {
200         }
201
202         public void importStyle(
203                 String JavaDoc uri,
204                 SACMediaList media,
205                 String JavaDoc defaultNamespaceURI) throws CSSException {
206
207             // Create the import rule and add it to the rule list
208
CSSImportRuleImpl ir = new CSSImportRuleImpl(
209                 _parentStyleSheet,
210                 null,
211                 uri,
212                 new MediaListImpl(media));
213             if (!_nodeStack.empty()) {
214                 ((CSSRuleListImpl)_nodeStack.peek()).add(ir);
215             } else {
216 // _nodeStack.push(ir);
217
_root = ir;
218             }
219         }
220
221         public void startMedia(SACMediaList media) throws CSSException {
222
223             // Create the media rule and add it to the rule list
224
CSSMediaRuleImpl mr = new CSSMediaRuleImpl(
225                 _parentStyleSheet,
226                 null,
227                 new MediaListImpl(media));
228             if (!_nodeStack.empty()) {
229                 ((CSSRuleListImpl)_nodeStack.peek()).add(mr);
230             }
231
232             // Create the rule list
233
CSSRuleListImpl rules = new CSSRuleListImpl();
234             mr.setRuleList(rules);
235             _nodeStack.push(mr);
236             _nodeStack.push(rules);
237         }
238
239         public void endMedia(SACMediaList media) throws CSSException {
240
241             // Pop the rule list and media rule nodes
242
_nodeStack.pop();
243             _root = _nodeStack.pop();
244         }
245
246         public void startPage(String JavaDoc name, String JavaDoc pseudo_page) throws CSSException {
247
248             // Create the page rule and add it to the rule list
249
CSSPageRuleImpl pr = new CSSPageRuleImpl(_parentStyleSheet, null, name, pseudo_page);
250             if (!_nodeStack.empty()) {
251                 ((CSSRuleListImpl)_nodeStack.peek()).add(pr);
252             }
253
254             // Create the style declaration
255
CSSStyleDeclarationImpl decl = new CSSStyleDeclarationImpl(pr);
256             pr.setStyle(decl);
257             _nodeStack.push(pr);
258             _nodeStack.push(decl);
259         }
260
261         public void endPage(String JavaDoc name, String JavaDoc pseudo_page) throws CSSException {
262
263             // Pop both the style declaration and the page rule nodes
264
_nodeStack.pop();
265             _root = _nodeStack.pop();
266         }
267
268         public void startFontFace() throws CSSException {
269
270             // Create the font face rule and add it to the rule list
271
CSSFontFaceRuleImpl ffr = new CSSFontFaceRuleImpl(_parentStyleSheet, null);
272             if (!_nodeStack.empty()) {
273                 ((CSSRuleListImpl)_nodeStack.peek()).add(ffr);
274             }
275
276             // Create the style declaration
277
CSSStyleDeclarationImpl decl = new CSSStyleDeclarationImpl(ffr);
278             ffr.setStyle(decl);
279             _nodeStack.push(ffr);
280             _nodeStack.push(decl);
281         }
282
283         public void endFontFace() throws CSSException {
284
285             // Pop both the style declaration and the font face rule nodes
286
_nodeStack.pop();
287             _root = _nodeStack.pop();
288         }
289
290         public void startSelector(SelectorList selectors) throws CSSException {
291
292             // Create the style rule and add it to the rule list
293
CSSStyleRuleImpl sr = new CSSStyleRuleImpl(_parentStyleSheet, null, selectors);
294             if (!_nodeStack.empty()) {
295                 ((CSSRuleListImpl)_nodeStack.peek()).add(sr);
296             }
297             
298             // Create the style declaration
299
CSSStyleDeclarationImpl decl = new CSSStyleDeclarationImpl(sr);
300             sr.setStyle(decl);
301             _nodeStack.push(sr);
302             _nodeStack.push(decl);
303         }
304
305         public void endSelector(SelectorList selectors) throws CSSException {
306
307             // Pop both the style declaration and the style rule nodes
308
_nodeStack.pop();
309             _root = _nodeStack.pop();
310         }
311
312         public void property(String JavaDoc name, LexicalUnit value, boolean important)
313                 throws CSSException {
314             CSSStyleDeclarationImpl decl =
315                 (CSSStyleDeclarationImpl) _nodeStack.peek();
316             decl.addProperty(
317                 new Property(name, new CSSValueImpl(value), important));
318         }
319     }
320
321     public static void setProperty(String JavaDoc key, String JavaDoc val) {
322         Properties props = System.getProperties();
323         props.put(key, val);
324         System.setProperties(props);
325     }
326 }
327
Popular Tags