KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > browser > StyleBrowserTreeBuilder


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

30 package browser;
31
32 import javax.swing.tree.*;
33 import org.w3c.dom.*;
34 import org.w3c.dom.stylesheets.*;
35 import org.w3c.dom.css.*;
36
37 /**
38  * @author David Schweinsberg
39  * @version $Release$
40  */

41 class StyleBrowserTreeBuilder {
42
43   private static DefaultMutableTreeNode createNode(String JavaDoc name) {
44     return new DefaultMutableTreeNode(new StyleData(name));
45   }
46
47   private static void addStyleDeclaration(
48     DefaultMutableTreeNode parent,
49     CSSStyleDeclaration decl) {
50
51     DefaultMutableTreeNode node = createNode("CSSStyleDeclaration");
52     parent.add(node);
53
54     // Add each style declaration
55
for (int i = 0; i < decl.getLength(); i++) {
56       String JavaDoc name = decl.item(i);
57       String JavaDoc value = decl.getPropertyValue(name);
58       String JavaDoc prio = decl.getPropertyPriority(name);
59
60       if (prio.equals(""))
61         node.add(createNode(name + " : " + value));
62       else
63         node.add(createNode(name + " : " + value + " ! " + prio));
64     }
65   }
66
67   private static void addMediaList(
68     DefaultMutableTreeNode parent,
69     MediaList mediaList) {
70
71     // WORKAROUND
72
// CSSImportRule.media shouldn't ever be null, but is if the media list
73
// is empty - this is a bug.
74
if (mediaList == null)
75       return;
76
77     DefaultMutableTreeNode node = createNode("MediaList");
78     parent.add(node);
79
80     // Add each media type
81
for (int i = 0; i < mediaList.getLength(); i++)
82       node.add(createNode(mediaList.item(i)));
83   }
84
85   private static void addUnknownRule(
86     DefaultMutableTreeNode parent,
87     CSSUnknownRule rule) {
88
89     DefaultMutableTreeNode node = createNode("CSSUnknownRule");
90     parent.add(node);
91
92     // Add the href
93
node.add(createNode(rule.getCssText()));
94   }
95
96   private static void addStyleRule(
97     DefaultMutableTreeNode parent,
98     CSSStyleRule rule) {
99
100     DefaultMutableTreeNode node = createNode("CSSStyleRule");
101     parent.add(node);
102
103     // Add the selector text
104
node.add(createNode(rule.getSelectorText()));
105
106     addStyleDeclaration(node, rule.getStyle());
107   }
108
109   private static void addCharsetRule(
110     DefaultMutableTreeNode parent,
111     CSSCharsetRule rule) {
112
113     DefaultMutableTreeNode node = createNode("CSSCharsetRule");
114     parent.add(node);
115
116     // Add the encoding
117
node.add(createNode(rule.getEncoding()));
118   }
119
120   private static void addImportRule(
121     DefaultMutableTreeNode parent,
122     CSSImportRule rule) {
123
124     DefaultMutableTreeNode node = createNode("CSSImportRule");
125     parent.add(node);
126
127     // Add the href
128
node.add(createNode(rule.getHref()));
129
130     addMediaList(node, rule.getMedia());
131   }
132
133   private static void addMediaRule(
134     DefaultMutableTreeNode parent,
135     CSSMediaRule rule) {
136
137     DefaultMutableTreeNode node = createNode("CSSMediaRule");
138     parent.add(node);
139
140     addMediaList(node, rule.getMedia());
141     addRuleList(node, rule.getCssRules());
142   }
143
144   private static void addFontFaceRule(
145     DefaultMutableTreeNode parent,
146     CSSFontFaceRule rule) {
147
148     DefaultMutableTreeNode node = createNode("CSSFontFaceRule");
149     parent.add(node);
150
151     addStyleDeclaration(node, rule.getStyle());
152   }
153
154   private static void addPageRule(
155     DefaultMutableTreeNode parent,
156     CSSPageRule rule) {
157
158     DefaultMutableTreeNode node = createNode("CSSPageRule");
159     parent.add(node);
160
161     // Add the selector text
162
node.add(createNode(rule.getSelectorText()));
163
164     addStyleDeclaration(node, rule.getStyle());
165   }
166
167   private static void addRuleList(
168     DefaultMutableTreeNode parent,
169     CSSRuleList rules) {
170
171     DefaultMutableTreeNode node = createNode("CSSRuleList");
172     parent.add(node);
173
174     for (int i = 0; i < rules.getLength(); i++) {
175       CSSRule rule = rules.item(i);
176
177       switch (rule.getType()) {
178       case CSSRule.UNKNOWN_RULE:
179         addUnknownRule(node, (CSSUnknownRule) rule);
180         break;
181       case CSSRule.STYLE_RULE:
182         addStyleRule(node, (CSSStyleRule) rule);
183         break;
184       case CSSRule.CHARSET_RULE:
185         addCharsetRule(node, (CSSCharsetRule) rule);
186         break;
187       case CSSRule.IMPORT_RULE:
188         addImportRule(node, (CSSImportRule) rule);
189         break;
190       case CSSRule.MEDIA_RULE:
191         addMediaRule(node, (CSSMediaRule) rule);
192         break;
193       case CSSRule.FONT_FACE_RULE:
194         addFontFaceRule(node, (CSSFontFaceRule) rule);
195         break;
196       case CSSRule.PAGE_RULE:
197         addPageRule(node, (CSSPageRule) rule);
198         break;
199       }
200     }
201   }
202
203   public static TreeModel createStyleSheetTree(CSSStyleSheet stylesheet) {
204     DefaultMutableTreeNode node = createNode("CSSStyleSheet");
205     TreeModel treeModel = new DefaultTreeModel(node);
206
207     addRuleList(node, stylesheet.getCssRules());
208
209     return treeModel;
210   }
211 }
212
Popular Tags