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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//
// $Id$
// From Philippe Le Hegaret (Philippe.Le_Hegaret@sophia.inria.fr)
//
// (c) COPYRIGHT MIT and INRIA, 1997.
// Please first read the full copyright statement in file COPYRIGHT.html

package org.w3c.css.css;

import org.w3c.css.atrules.css.AtRuleMedia;
import org.w3c.css.atrules.css.AtRulePage;
import org.w3c.css.parser.AtRule;
import org.w3c.css.parser.CssError;
import org.w3c.css.parser.CssFouffa;
import org.w3c.css.parser.CssParseException;
import org.w3c.css.parser.CssSelectors;
import org.w3c.css.parser.CssValidatorListener;
import org.w3c.css.parser.Errors;
import org.w3c.css.parser.analyzer.ParseException;
import org.w3c.css.parser.analyzer.TokenMgrError;
import org.w3c.css.properties.css.CssProperty;
import org.w3c.css.selectors.IdSelector;
import org.w3c.css.util.ApplContext;
import org.w3c.css.util.CssVersion;
import org.w3c.css.util.InvalidParamException;
import org.w3c.css.util.Messages;
import org.w3c.css.util.Util;
import org.w3c.css.util.Warning;
import org.w3c.css.util.Warnings;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Constructor;
import java.net.URL;
import java.util.ArrayList;
import java.util.StringTokenizer;

/**
 * @version $Revision$
 */
public final class StyleSheetParser
        implements CssValidatorListener, CssParser {

    private static Constructor co = null;

    static {
        try {
            Class c = java.lang.Exception.class;
            Class cp[] = {java.lang.Exception.class};
            co = c.getDeclaredConstructor(cp);
        } catch (NoSuchMethodException ex) {
            co = null;
        }
    }

    CssFouffa cssFouffa;
    StyleSheet style = new StyleSheet();

    public void reInit() {
        style = new StyleSheet();
    }

    public StyleSheet getStyleSheet() {
        return style;
    }

    public void setWarningLevel(int warningLevel) {
        style.setWarningLevel(warningLevel);
    }

    public void notifyErrors(Errors errors) {
        style.addErrors(errors);
    }

    public void notifyWarnings(Warnings warnings) {
        style.addWarnings(warnings);
    }

    /**
     * Adds a vector of properties to a selector.
     *
     * @param selector   the selector
     * @param properties Properties to associate with contexts
     */
    public void handleRule(ApplContext ac, CssSelectors selector,
                           ArrayList<CssProperty> properties) {
        if (selector.getAtRule() instanceof AtRulePage) {
            style.remove(selector);
        }
        for (CssProperty property : properties) {
            property.setSelectors(selector);
            style.addProperty(selector, property);
        }
    }

    // part added by Sijtsche de Jong

    public void addCharSet(String charset) {
        style.addCharSet(charset);
    }

    public void newAtRule(AtRule atRule) {
        style.newAtRule(atRule);
    }

    public void endOfAtRule() {
        style.endOfAtRule();
    }

    public void setImportant(boolean important) {
        style.setImportant(important);
    }

    public void setSelectorList(ArrayList<CssSelectors> selectors) {
        style.setSelectorList(selectors);
    }

    public void setProperty(ArrayList<CssProperty> properties) {
        style.setProperty(properties);
    }

    public void endOfRule() {
        style.endOfRule();
    }

    public void removeThisRule() {
        style.removeThisRule();
    }

    public void removeThisAtRule() {
        style.removeThisAtRule();
    }

    //end of part added by Sijtsche de Jong

    /**
     * Handles an at-rule.
     * <p/>
     * <p>The parameter <code>value</code> can be :
     * <DL>
     * <DT>CssString
     * <DD>The value coming from a string.
     * <DT>CssURL
     * <DD>The value coming from an URL.
     * <DT>Vector
     * <DD>The value is a vector of declarations (it contains properties).
     * This feature is not legal, so be careful.
     * </DL>
     *
     * @param ident  The ident for this at-rule (for example: 'font-face')
     * @param string The string representation if this at-rule
     */
    public void handleAtRule(ApplContext ac, String ident, String string) {
        style.getWarnings().addWarning(new Warning(cssFouffa.getSourceFile(),
                cssFouffa.getLine(),
                "at-rule",
                2,
                new String[]{ident, string},
                ac));
        //stylesheet.addAtRule(atRule);
    }

    /**
     * @param url    the URL containing the style sheet
     * @param title  the title of the stylesheet
     * @param kind   may be a stylesheet or an alternate stylesheet
     * @param media  the media to apply this
     * @param origin the origin of the style sheet
     * @throws IOException an IO error
     */
    public void parseURL(ApplContext ac, URL url, String title,
                         String kind, String media,
                         int origin) {
        boolean doneref = false;
        URL ref = ac.getReferrer();
        setWarningLevel(ac.getWarningLevel());
        if (Util.onDebug) {
            System.err.println("StyleSheet.parseURL(" + url + ", "
                    + title + ", "
                    + kind + ", " + media + ", "
                    + origin + ")");
        }
        if (kind != null) {
            kind = kind.trim().toLowerCase();
            if (!kind.equals("stylesheet") && !kind.equals("alternate stylesheet")) {
                return;
            }
        }
        try {
            ac.setOrigin(origin);
//	    if (cssFouffa == null) {
            cssFouffa = new CssFouffa(ac, url);
            cssFouffa.addListener(this);
//	    } else {
//		cssFouffa.ReInit(ac, url);
//	    }

            //	    cssFouffa.setResponse(res);

            // removed plh 2001-03-08
            // cssFouffa.setOrigin(origin);
            //	    cssFouffa.setDefaultMedium(defaultmedium);
            //	    cssFouffa.doConfig();
            if (media == null) {
                if (ac.getCssVersion() != CssVersion.CSS1) {
                    if (ac.getMedium() == null) {
                        media = "all";
                    } else {
                        media = ac.getMedium();
                    }
                }
            }
            AtRuleMedia m = AtRuleMedia.getInstance(ac.getCssVersion());
            try {
                if (media != null) {
                    addMedias(m, media, ac);
                }
                cssFouffa.setAtRule(m);
            } catch (org.w3c.css.util.InvalidParamException e) {
                Errors er = new Errors();
                er.addError(new org.w3c.css.parser.CssError(url.toString(),
                        -1, e));
                notifyErrors(er);
                return;
            }
            ac.setReferrer(url);
            doneref = true;
            cssFouffa.parseStyle();
        } catch (Exception e) {
            Errors er = new Errors();
            er.addError(new org.w3c.css.parser.CssError(Messages.escapeString(url.toString()),
                    // BUG: CWE-79 Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting')
                    // -1, e));
                    // FIXED:
                    -1, new Exception(Messages.escapeString(e.getMessage()))));
            notifyErrors(er);
        } finally {
            if (doneref) {
                ac.setReferrer(ref);
            }
        }
    }

    // add media, easy version for CSS version < 3, otherwise, reuse the parser
    private void addMedias(AtRuleMedia m, String medias, ApplContext ac) throws InvalidParamException {
        // before CSS3, let's parse it the easy way...
        if (ac.getCssVersion().compareTo(CssVersion.CSS3) < 0) {
            StringTokenizer tokens = new StringTokenizer(medias, ",");
            while (tokens.hasMoreTokens()) {
                m.addMedia(null, tokens.nextToken().trim(), ac);
            }
        } else {
            CssFouffa muP = new CssFouffa(ac, new StringReader(medias));
            try {
                AtRuleMedia arm = muP.parseMediaDeclaration();
                if (arm != null) {
                    m.allMedia = arm.allMedia;
                }
            } catch (ParseException pex) {
                // error already added, so nothing else to do
            }
        }
    }

    /**
     * Parse a style element. The Style element always comes from the user
     *
     * @param reader the reader containing the style data
     * @param url    the name of the file the style element was read in.
     * @throws IOException an IO error
     */
    public void parseStyleElement(ApplContext ac, Reader reader,
                                  String title, String media,
                                  URL url, int lineno) {
        boolean doneref = false;
        style.setWarningLevel(ac.getWarningLevel());
        if (Util.onDebug) {
            System.err.println("StyleSheet.parseStyleElement(" + title + ", "
                    + media + ", " + url
                    + "," + lineno + ")");
        }
        URL ref = ac.getReferrer();
        try {

//	    if (cssFouffa == null) {
            String charset = ac.getCharsetForURL(url);
            cssFouffa = new CssFouffa(ac, reader, url, lineno);
            cssFouffa.addListener(this);
//	    } else {
//		cssFouffa.ReInit(ac, input, url, lineno);
//	    }

            //	    cssFouffa.setResponse(res);
            //	    cssFouffa.setDefaultMedium(defaultmedium);
            //	    cssFouffa.doConfig();
            if (media == null && ac.getCssVersion() != CssVersion.CSS1) {
                media = "all";
            }

            AtRuleMedia m = AtRuleMedia.getInstance(ac.getCssVersion());
            try {
                if (media != null) {
                    addMedias(m, media, ac);
                }
                cssFouffa.setAtRule(m);
            } catch (org.w3c.css.util.InvalidParamException e) {
                Errors er = new Errors();
                er.addError(new org.w3c.css.parser.CssError(url.toString(),
                        -1, e));
                notifyErrors(er);
                return;
            }
            ac.setReferrer(url);
            doneref = true;
            cssFouffa.parseStyle();
        } catch (IOException e) {
            Errors er = new Errors();
            er.addError(new org.w3c.css.parser.CssError(url.toString(),
                    -1, e));
            notifyErrors(er);
        } catch (TokenMgrError e) {
            Errors er = new Errors();
            CssParseException cpe = null;
            if (co != null) {
                try {
                    Object o[] = new Object[1];
                    o[0] = e;
                    Exception new_e = (Exception) co.newInstance(o);
                    cpe = new CssParseException(new_e);
                } catch (Exception ex) {
                    cpe = null;
                }
            }
            if (cpe == null) {
                cpe = new CssParseException(new Exception(e.getMessage()));
            }
            er.addError(new org.w3c.css.parser.CssError(url.toString(),
                    -1,
                    //e.getErrorLine(),
                    cpe));
            notifyErrors(er);
        } catch (RuntimeException e) {
            Errors er = new Errors();
            er.addError(new org.w3c.css.parser.CssError(url.toString(),
                    cssFouffa.getLine(),
                    new CssParseException(e)));
            notifyErrors(er);
        } finally {
            if (doneref) {
                ac.setReferrer(ref);
            }
        }
    }

    /**
     * @param input the inputStream containing the style data
     * @param url   the name of the file the style element was read in.
     * @throws IOException an IO error
     * @see #parseStyleElement(ApplContext, InputStream, String, String, URL, int)
     * @deprecated Replaced by parseStyleElement
     */
    public void parseStyleElement(ApplContext ac, String input, URL url, int lineno) {
        parseStyleElement(ac, new StringReader(input), null, null, url, lineno);
    }

    /**
     * Parse a style element. The Style element always comes from the user
     *
     * @param input the input stream containing the style data
     * @param url   the name of the file the style element was read in.
     * @throws IOException an IO error
     */
    public void parseStyleElement(ApplContext ac, InputStream input,
                                  String title, String media,
                                  URL url, int lineno) {
        InputStreamReader reader = null;
        String charset = ac.getCharsetForURL(url);
        try {
            reader = new InputStreamReader(input, (charset == null) ?
                    "iso-8859-1" : charset);
        } catch (UnsupportedEncodingException uex) {
            Errors er = new Errors();
            er.addError(new org.w3c.css.parser.CssError(url.toString(),
                    -1, uex));
            notifyErrors(er);
        } catch (Exception ex) {
            // in case of error, ignore it.
            reader = null;
            if (Util.onDebug) {
                System.err.println("Error in StyleSheet.parseStyleElement(" + title + ","
                        + url + "," + lineno + ")");
            }
        }
        if (reader != null) {
            parseStyleElement(ac, reader, title, media, url, lineno);
        }
    }

    /**
     * Unify call to the parser for css doc as a reader.
     *
     * @param ac
     * @param reader
     * @param docref
     */
    public void parseStyleSheet(ApplContext ac, Reader reader, URL docref) {
        parseStyleElement(ac, reader, null, null, (docref == null) ? ac.getFakeURL() : docref, 0);
    }

    /**
     * Parse some declarations. All declarations always comes from the user
     *
     * @param input the inputStream containing the style data
     * @param id    the uniq id
     * @param url   the URL the style element was read in.
     * @throws IOException an IO error
     */
    public void parseStyleAttribute(ApplContext ac, InputStream input, String id,
                                    URL url, int lineno) {
        style.setWarningLevel(ac.getWarningLevel());
        lineno--; // why ?!?!
        if (Util.onDebug) {
            System.err.println("StyleSheet.parseStyleAttribute(" + id + ","
                    + url + "," + lineno + ")");
        }

        try {
            //	    if (cssFouffa == null) {
            String charset = ac.getCharsetForURL(url);
            cssFouffa = new CssFouffa(ac, input, charset, url, lineno);
            cssFouffa.addListener(this);
            //	    } else
//		cssFouffa.ReInit(ac, input, url, lineno);
            CssSelectors selector = new CssSelectors(ac);

            try {
                AtRuleMedia media = AtRuleMedia.getInstance(ac.getCssVersion());
                if (ac.getCssVersion() != CssVersion.CSS1) {
                    media.addMedia(null, "all", ac);
                }
                cssFouffa.setAtRule(media);
            } catch (InvalidParamException e) {
            } //ignore

            try {
                if (id == null || id.length() == 0) {
                    id = "nullId-" + Long.toHexString(System.currentTimeMillis());
                    // TODO add an error/warning ?
                }
                selector.addId(new IdSelector(id.substring(1)));
            } catch (InvalidParamException e) {
                style.removeThisRule();
                ac.getFrame().addError(new CssError(e));
            }
            cssFouffa.parseDeclarations(selector);
        } catch (IOException e) {
            Errors er = new Errors();
            er.addError(new org.w3c.css.parser.CssError(url.toString(),
                    -1, e));
            notifyErrors(er);
        }
    }

    /**
     * @param input the inputStream containing the style data
     * @param id    the uniq id
     * @param url   the name of the file the style element was read in.
     * @throws IOException an IO error
     * @see #parseStyleAttribute(ApplContext, InputStream, String, URL, int)
     * @deprecated Replaced by parseStyleAttribute
     */
    public void parseStyleAttribute(ApplContext ac, String input, String id,
                                    URL url, int lineno) {
        parseStyleAttribute(ac, new ByteArrayInputStream(input.getBytes()),
                id, url, lineno);
    }

    public void setStyle(Class style) {
        cssFouffa.setStyle(style);
    }

}