KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ParseTest.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: ParseTest.java,v 1.1.1.1 2003/12/28 21:23:10 davidsch Exp $
29  */

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

42 public class ParseTest extends HandlerBase {
43
44     private static final String JavaDoc PARSER = "com.steadystate.css.parser.SACParser";
45
46     private int _propertyCounter = 0;
47     private int _indentSize = 0;
48   
49     public ParseTest() {
50         try {
51             CSSOMParser.setProperty("org.w3c.css.sac.parser", PARSER);
52             ParserFactory factory = new ParserFactory();
53             Parser parser = factory.makeParser();
54 // Parser parser = new SACParser();
55
parser.setDocumentHandler(this);
56
57 // Reader r = new FileReader("d:\\working\\CSS2Parser\\primary.css");
58
// Reader r = new FileReader("d:\\project\\css2\\html40.css");
59
// Reader r = new FileReader("d:\\project\\css2\\test.css");
60
// BufferedReader r = new BufferedReader(new InputStreamReader(
61
// new FileInputStream("c:\\working\\CSS2Parser\\stylesheets\\test-unicode.css"),
62
// "UTF-16"));
63
// Reader r = new FileReader("d:\\project\\css2\\single-color.css");
64
Reader r = new FileReader("c:\\working\\css2parser\\stylesheets\\page_test.css");
65             
66             InputSource is = new InputSource(r);
67 // InputSource is = new InputSource("file:///d:/project/css2/test-unicode.css");
68
// InputSource is = new InputSource("file:///d:/project/css2/test.css");
69
parser.parseStyleSheet(is);
70
71         } catch (Exception JavaDoc e) {
72             System.err.println("Exception: " + e.getMessage());
73         }
74     }
75
76     public static void main(String JavaDoc[] args) {
77         new ParseTest();
78     }
79
80     public void startDocument(InputSource source)
81         throws CSSException {
82         System.out.println("startDocument");
83     }
84     
85     public void endDocument(InputSource source) throws CSSException {
86         System.out.println("endDocument");
87     }
88
89     public void comment(String JavaDoc text) throws CSSException {
90     }
91
92     public void ignorableAtRule(String JavaDoc atRule) throws CSSException {
93         System.out.println(atRule);
94     }
95
96     public void namespaceDeclaration(String JavaDoc prefix, String JavaDoc uri)
97         throws CSSException {
98     }
99
100     public void importStyle(String JavaDoc uri, SACMediaList media,
101             String JavaDoc defaultNamespaceURI)
102         throws CSSException {
103         System.out.print("@import url(" + uri + ")");
104         if (media.getLength() > 0) {
105           System.out.println(" " + media.toString() + ";");
106         } else {
107           System.out.println(";");
108         }
109     }
110
111     public void startMedia(SACMediaList media) throws CSSException {
112         System.out.println(indent() + "@media " + media.toString() + " {");
113         incIndent();
114     }
115
116     public void endMedia(SACMediaList media) throws CSSException {
117         decIndent();
118         System.out.println(indent() + "}");
119     }
120
121     public void startPage(String JavaDoc name, String JavaDoc pseudo_page) throws CSSException {
122         System.out.print(indent() + "@page");
123         if (name != null) {
124             System.out.print(" " + name);
125         }
126         if (pseudo_page != null) {
127             System.out.println(" " + pseudo_page);
128         }
129         System.out.println(" {");
130         _propertyCounter = 0;
131         incIndent();
132     }
133
134     public void endPage(String JavaDoc name, String JavaDoc pseudo_page) throws CSSException {
135         System.out.println();
136         decIndent();
137         System.out.println(indent() + "}");
138     }
139
140     public void startFontFace() throws CSSException {
141         System.out.println(indent() + "@font-face {");
142         _propertyCounter = 0;
143         incIndent();
144     }
145
146     public void endFontFace() throws CSSException {
147         System.out.println();
148         decIndent();
149         System.out.println(indent() + "}");
150     }
151
152     public void startSelector(SelectorList selectors) throws CSSException {
153         System.out.println(indent() + selectors.toString() + " {");
154         _propertyCounter = 0;
155         incIndent();
156     }
157
158     public void endSelector(SelectorList selectors) throws CSSException {
159         System.out.println();
160         decIndent();
161         System.out.println(indent() + "}");
162     }
163
164     public void property(String JavaDoc name, LexicalUnit value, boolean important)
165             throws CSSException {
166         if (_propertyCounter++ > 0) {
167             System.out.println(";");
168         }
169         System.out.print(indent() + name + ":");
170
171         // Iterate through the chain of lexical units
172
LexicalUnit nextVal = value;
173         while (nextVal != null) {
174 // System.out.print(" " + nextVal.toString());
175
System.out.print(" " + ((LexicalUnitImpl)nextVal).toDebugString());
176             nextVal = nextVal.getNextLexicalUnit();
177         }
178
179         // Is it important?
180
if (important) {
181             System.out.print(" !important");
182         }
183     }
184     
185     private String JavaDoc indent() {
186         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(16);
187         for (int i = 0; i < _indentSize; i++) {
188             sb.append(" ");
189         }
190         return sb.toString();
191     }
192     
193     private void incIndent() {
194         _indentSize += 4;
195     }
196
197     private void decIndent() {
198         _indentSize -= 4;
199     }
200 }
201
Popular Tags