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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
/*
* Copyright 2011 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.sass.handler;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Stack;
import org.w3c.css.sac.CSSException;
import org.w3c.css.sac.InputSource;
import org.w3c.css.sac.LexicalUnit;
import org.w3c.css.sac.SACMediaList;
import org.w3c.css.sac.SelectorList;
import com.vaadin.sass.ScssStylesheet;
import com.vaadin.sass.parser.LexicalUnitImpl;
import com.vaadin.sass.tree.BlockNode;
import com.vaadin.sass.tree.CommentNode;
import com.vaadin.sass.tree.ExtendNode;
import com.vaadin.sass.tree.FontFaceNode;
import com.vaadin.sass.tree.ForNode;
import com.vaadin.sass.tree.ImportNode;
import com.vaadin.sass.tree.ListRemoveNode;
import com.vaadin.sass.tree.MediaNode;
import com.vaadin.sass.tree.MicrosoftRuleNode;
import com.vaadin.sass.tree.MixinDefNode;
import com.vaadin.sass.tree.MixinNode;
import com.vaadin.sass.tree.NestPropertiesNode;
import com.vaadin.sass.tree.Node;
import com.vaadin.sass.tree.RuleNode;
import com.vaadin.sass.tree.VariableNode;
import com.vaadin.sass.tree.WhileNode;
import com.vaadin.sass.tree.controldirective.EachDefNode;
import com.vaadin.sass.tree.controldirective.ElseNode;
import com.vaadin.sass.tree.controldirective.IfElseDefNode;
import com.vaadin.sass.tree.controldirective.IfNode;
public class SCSSDocumentHandlerImpl implements SCSSDocumentHandler {
private final ScssStylesheet styleSheet;
Stack<Node> nodeStack = new Stack<Node>();
public SCSSDocumentHandlerImpl() {
this(new ScssStylesheet());
}
public SCSSDocumentHandlerImpl(ScssStylesheet styleSheet) {
this.styleSheet = styleSheet;
nodeStack.push(styleSheet);
}
@Override
public ScssStylesheet getStyleSheet() {
return styleSheet;
}
@Override
public void startDocument(InputSource source) throws CSSException {
nodeStack.push(styleSheet);
}
@Override
public void endDocument(InputSource source) throws CSSException {
}
@Override
public void variable(String name, LexicalUnitImpl value, boolean guarded) {
VariableNode node = new VariableNode(name, value, guarded);
nodeStack.peek().appendChild(node);
}
@Override
public void debugDirective() {
}
@Override
public ForNode forDirective(String var, String from, String to,
boolean exclusive, String body) {
ForNode node = new ForNode(var, from, to, exclusive, body);
System.out.println(node);
return node;
}
@Override
public EachDefNode startEachDirective(String var, ArrayList<String> list) {
EachDefNode node = new EachDefNode(var, list);
nodeStack.peek().appendChild(node);
nodeStack.push(node);
return node;
}
@Override
public EachDefNode startEachDirective(String var, String listVariable) {
EachDefNode node = new EachDefNode(var, listVariable);
nodeStack.peek().appendChild(node);
nodeStack.push(node);
return node;
}
@Override
public void endEachDirective() {
nodeStack.pop();
}
@Override
public WhileNode whileDirective(String condition, String body) {
WhileNode node = new WhileNode(condition, body);
System.out.println(node);
return node;
}
@Override
public void comment(String text) throws CSSException {
CommentNode node = new CommentNode(text);
nodeStack.peek().appendChild(node);
}
@Override
public void ignorableAtRule(String atRule) throws CSSException {
System.out.println("ignorableAtRule(String atRule): " + atRule);
}
@Override
public void namespaceDeclaration(String prefix, String uri)
throws CSSException {
System.out.println("namespaceDeclaration(String prefix, String uri): "
+ prefix + ", " + uri);
}
@Override
public void importStyle(String uri, SACMediaList media,
String defaultNamespaceURI) throws CSSException {
}
@Override
public void startMedia(SACMediaList media) throws CSSException {
MediaNode node = new MediaNode(media);
nodeStack.peek().appendChild(node);
nodeStack.push(node);
}
@Override
public void endMedia(SACMediaList media) throws CSSException {
nodeStack.pop();
}
@Override
public void startPage(String name, String pseudo_page) throws CSSException {
System.out.println("startPage(String name, String pseudo_page): "
+ name + ", " + pseudo_page);
}
@Override
public void endPage(String name, String pseudo_page) throws CSSException {
System.out.println("endPage(String name, String pseudo_page): " + name
+ ", " + pseudo_page);
}
@Override
public void startFontFace() throws CSSException {
FontFaceNode node = new FontFaceNode();
nodeStack.peek().appendChild(node);
nodeStack.push(node);
}
@Override
public void endFontFace() throws CSSException {
nodeStack.pop();
}
@Override
public void startSelector(ArrayList<String> selectors) throws CSSException {
BlockNode node = new BlockNode(selectors);
nodeStack.peek().appendChild(node);
nodeStack.push(node);
}
@Override
public void endSelector() throws CSSException {
nodeStack.pop();
}
@Override
public void property(String name, LexicalUnit value, boolean important)
throws CSSException {
property(name, (LexicalUnitImpl) value, important, null);
}
@Override
public void property(String name, LexicalUnitImpl value, boolean important,
String comment) {
RuleNode node = new RuleNode(name, value, important, comment);
nodeStack.peek().appendChild(node);
}
@Override
public void extendDirective(ArrayList<String> list) {
ExtendNode node = new ExtendNode(list);
nodeStack.peek().appendChild(node);
}
@Override
public void startNestedProperties(String name) {
NestPropertiesNode node = new NestPropertiesNode(name);
nodeStack.peek().appendChild(node);
nodeStack.push(node);
}
@Override
public void endNestedProperties(String name) {
nodeStack.pop();
}
@Override
public void startMixinDirective(String name, Collection<VariableNode> args) {
MixinDefNode node = new MixinDefNode(name.trim(), args);
nodeStack.peek().appendChild(node);
nodeStack.push(node);
}
@Override
public void endMixinDirective(String name, Collection<VariableNode> args) {
nodeStack.pop();
}
@Override
public void includeDirective(String name, Collection<LexicalUnitImpl> args) {
MixinNode node = new MixinNode(name, args);
nodeStack.peek().appendChild(node);
}
@Override
public void importStyle(String uri, SACMediaList media, boolean isURL) {
ImportNode node = new ImportNode(uri, media, isURL);
nodeStack.peek().appendChild(node);
}
@Override
public void startIfElseDirective() {
final IfElseDefNode node = new IfElseDefNode();
nodeStack.peek().appendChild(node);
nodeStack.push(node);
}
@Override
public void ifDirective(String evaluator) {
if (nodeStack.peek() instanceof IfNode) {
nodeStack.pop();
}
IfNode node = new IfNode(evaluator);
nodeStack.peek().appendChild(node);
nodeStack.push(node);
}
@Override
public void elseDirective() {
if (nodeStack.peek() instanceof IfNode) {
nodeStack.pop();
}
ElseNode node = new ElseNode();
nodeStack.peek().appendChild(node);
nodeStack.push(node);
}
@Override
public void endIfElseDirective() {
if ((nodeStack.peek() instanceof ElseNode)
|| (nodeStack.peek() instanceof IfNode)) {
nodeStack.pop();
}
nodeStack.pop();
}
@Override
public void microsoftDirective(String name, String value) {
MicrosoftRuleNode node = new MicrosoftRuleNode(name, value);
nodeStack.peek().appendChild(node);
}
@Override
public void endSelector(SelectorList arg0) throws CSSException {
// TODO Auto-generated method stub
}
@Override
public void startSelector(SelectorList arg0) throws CSSException {
// TODO Auto-generated method stub
}
@Override
public void removeDirective(String variable, String list, String remove,
String separator) {
ListRemoveNode node = new ListRemoveNode(variable, list, remove,
separator);
nodeStack.peek().appendChild(node);
}
}
|