KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > TestDOM_1


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

30
31 package test;
32
33 import java.io.*;
34 import org.w3c.dom.*;
35 import org.w3c.dom.css.*;
36 import org.w3c.css.sac.*;
37 import com.steadystate.css.parser.*;
38
39 /**
40  * Tests the CSS DOM implementation by loading a stylesheet and performing a
41  * few operations upon it.
42  *
43  * @author David Schweinsberg
44  * @version $Release$
45  */

46 public class TestDOM_1 {
47
48     public static void main(String JavaDoc[] args) throws ParseException {
49         try {
50             Reader r = new FileReader("c:\\working\\css2parser\\stylesheets\\test.css");
51             CSSOMParser parser = new CSSOMParser();
52             InputSource is = new InputSource(r);
53
54             CSSStyleSheet stylesheet = parser.parseStyleSheet(is);
55             CSSRuleList rules = stylesheet.getCssRules();
56
57             for (int i = 0; i < rules.getLength(); i++) {
58                 CSSRule rule = rules.item(i);
59                 System.out.println(rule.getCssText());
60             }
61             
62             // Do some modifications and output the results
63

64             // Style Rules
65
rules.item(9).setCssText("apple { color: green }"); // GOOD
66
// rules.item(9).setCssText("@font-face { src: url(null) }"); // BAD
67

68             CSSRule rule = rules.item(9);
69             System.out.println(rule.getCssText());
70
71             ((CSSStyleRule)rules.item(9)).setSelectorText("banana"); // GOOD
72

73             System.out.println(rule.getCssText());
74
75             ((CSSStyleRule)rules.item(9)).setSelectorText("banana, orange tangerine, grapefruit"); // GOOD
76

77             System.out.println(rule.getCssText());
78
79             ((CSSStyleRule)rules.item(9)).getStyle().setCssText("{ color: red green brown; smell: sweet, sour; taste: sweet/tart }"); // GOOD
80

81             System.out.println(rule.getCssText());
82             
83             // Import rules
84
stylesheet.insertRule("@import \"thing.css\";", 0); // GOOD
85
// stylesheet.insertRule("@import \"thing.css\";", 10); // BAD
86

87             rule = rules.item(0);
88             System.out.println(rule.getCssText());
89             
90             ((CSSImportRule)rules.item(0)).setCssText("@import \"thing-hack.css\";");
91
92             System.out.println(rule.getCssText());
93
94             // Font-face rules
95
stylesheet.insertRule("@font-face { src: \"#foo\" }", 10); // GOOD
96

97             rule = rules.item(10);
98             System.out.println(rule.getCssText());
99
100             ((CSSFontFaceRule)rules.item(10)).setCssText("@font-face { src: \"#bar\" }"); // GOOD
101
// ((CSSFontFaceRule)rules.item(10)).setCssText("@import \"thing-hack.css\";"); // BAD
102

103             System.out.println(rule.getCssText());
104             
105             // Media rules
106

107         } catch (Exception JavaDoc e) {
108             System.out.println("Error.");
109             System.out.println(e.getMessage());
110             e.printStackTrace();
111         }
112     }
113 }
114
Popular Tags