KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > TestDOM


1 /*
2  * TestDOM.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.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 com.steadystate.css.*;
35 import org.w3c.dom.*;
36 import org.w3c.dom.css.*;
37
38 /**
39  * Tests the CSS DOM implementation by loading a stylesheet and performing a
40  * few operations upon it.
41  *
42  * @author David Schweinsberg
43  * @version $Release$
44  */

45 public class TestDOM {
46
47     public static void main(String JavaDoc[] args) throws ParseException {
48         try {
49             Reader r = new FileReader("c:\\working\\css2parser\\stylesheets\\basic.css");
50             CSS2Parser parser = new CSS2Parser(r);
51 /*
52             CSSStyleSheet stylesheet = parser.styleSheet();
53             CSSRuleList rules = stylesheet.getCssRules();
54
55             for (int i = 0; i < rules.getLength(); i++) {
56                 CSSRule rule = rules.item(i);
57                 System.out.println(rule.getCssText());
58             }
59 */

60             CSSStyleDeclaration n = parser.styleDeclaration();
61
62             // Enumerate the properties and retrieve their values
63
System.out.println("No. of properties: " + n.getLength());
64
65             for (int i = 0; i < n.getLength(); i++) {
66                 String JavaDoc name = n.item(i);
67                 System.out.println(name + " : " + n.getPropertyValue(name));
68             }
69
70             // Get the style declaration as a single lump of text
71
System.out.println("\ngetCssText");
72             System.out.println(n.getCssText());
73
74             // Directly set the CSS style declaration
75
// NOTE: We must include the braces!
76
System.out.println("\nsetCssText");
77             n.setCssText("{ alpha: 2; beta: 20px; gamma: 40em; delta: 1mm; epsilon: 24pt }");
78
79             System.out.println(n.getCssText());
80
81             // Remove some properties, from the middle, beginning, and end
82
System.out.println();
83             System.out.println("Removing 'gamma'");
84             System.out.println(n.removeProperty("gamma"));
85             System.out.println("Removing 'alpha'");
86             System.out.println(n.removeProperty("alpha"));
87             System.out.println("Removing 'epsilon'");
88             System.out.println(n.removeProperty("epsilon"));
89
90             // Let's see what remains
91
System.out.println(n.getCssText());
92
93             // Use the setProperty method to modify an existing property,
94
// and add a new one.
95
System.out.println();
96             System.out.println("setting 'beta' to 40px");
97             n.setProperty("beta", "40px", null);
98             System.out.println("setting 'omega' to 1 with 'important' priority");
99             n.setProperty("omega", "1", "important");
100
101             // Let's look at the changes
102
System.out.println(n.getCssText());
103
104             // Work with CSSValues
105
System.out.println();
106             System.out.println("Retrieving 'beta' as a CSSPrimitiveValue");
107             CSSPrimitiveValue value = (CSSPrimitiveValue) n.getPropertyCSSValue("beta");
108             System.out.println("getCssText: 'beta' = " + value.getCssText());
109             System.out.println("getFloatValue: 'beta' = " + value.getFloatValue(CSSPrimitiveValue.CSS_PX));
110             System.out.println("Setting 'beta' to 100px");
111             value.setFloatValue(CSSPrimitiveValue.CSS_PX, 100);
112
113             System.out.println("Adding a new value, which should end-up as a CSSValueList.\nSetting 'list-test' to 100 200 300");
114             n.setProperty("list-test", "100 200 300", null);
115             value = (CSSPrimitiveValue) n.getPropertyCSSValue("list-test");
116             System.out.println("getValueType: 'list-test' = " + value.getCssValueType());
117
118             CSSValueList vl = (CSSValueList) n.getPropertyCSSValue("list-test");
119             for (int i = 0; i < vl.getLength(); i++) {
120                 System.out.println(
121                 "getFloatValue: 'list-test[" +
122                 String.valueOf( i ) +
123                 "]' = " +
124                 ((CSSPrimitiveValue)vl.item(i)).getFloatValue(CSSPrimitiveValue.CSS_NUMBER));
125             }
126
127             // When a CSSValue is modified, it modifies the declaration
128
System.out.println("Let's see the change within the entire declaration");
129             System.out.println(n.getCssText());
130
131             // Using the setCssText method, we can change the type of value
132
System.out.println("Setting 'list-test' to something completely different, the string 'bogus'.");
133             vl.setCssText("bogus");
134
135             System.out.println("What happens...");
136             System.out.println("getValueType: 'list-test' = " + value.getCssValueType());
137             System.out.println(n.getCssText());
138         } catch (Exception JavaDoc e) {
139             System.out.println("Error.");
140             System.out.println(e.getMessage());
141             e.printStackTrace();
142         }
143     }
144 }
145
Popular Tags