/
ParserDemo.java
87 lines (77 loc) · 2.53 KB
/
ParserDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* ParserDemo.java
*
* Created on 8. 7. 2014, 13:02:07 by burgetr
*/
package test;
import java.io.IOException;
import cz.vutbr.web.css.CSSException;
import cz.vutbr.web.css.CSSFactory;
import cz.vutbr.web.css.Declaration;
import cz.vutbr.web.css.RuleBlock;
import cz.vutbr.web.css.RuleMedia;
import cz.vutbr.web.css.RuleSet;
import cz.vutbr.web.css.StyleSheet;
import cz.vutbr.web.css.Term;
/**
* A simple demo showing how to parse and process a style sheet with no DOM analysis involved.
*
* @author burgetr
*/
public class ParserDemo
{
private static String css =
"a { color: #abc; }"
+ " b { font-weight: bold; }"
+ " @media screen {"
+ " em { font-style: italic; }"
+ "}";
/**
* @param args
*/
public static void main(String[] args)
{
try
{
StyleSheet style = CSSFactory.parseString(css, null);
//obtain the number of rules
System.out.println("#of rules parsed: " + style.size());
//go through the rules
for (RuleBlock<?> rule : style)
{
if (rule instanceof RuleSet)
{
RuleSet set = (RuleSet) rule;
System.out.println("Selectors: " + set.getSelectors());
System.out.println("Declarations:");
for (Declaration decl : set)
{
System.out.println(" Property: " + decl.getProperty());
System.out.println(" Values: ");
for (Term<?> term : decl)
System.out.println(" " + term);
}
}
else if (rule instanceof RuleMedia)
{
RuleMedia media = (RuleMedia) rule;
System.out.println("Media: " + media.getMediaQueries());
for (RuleSet set : media)
{
//process similarly to the RuleSet processing above
System.out.println(" Rule: " + set);
}
}
else
{
//other rules such as @page, @viewport etc.
System.out.println("Other rule " + rule);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (CSSException e) {
e.printStackTrace();
}
}
}