-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathApplContext.java
More file actions
643 lines (548 loc) · 17.5 KB
/
ApplContext.java
File metadata and controls
643 lines (548 loc) · 17.5 KB
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
/*
* (c) COPYRIGHT 1999 World Wide Web Consortium
* (Massachusetts Institute of Technology, Institut National de Recherche
* en Informatique et en Automatique, Keio University).
* All Rights Reserved. http://www.w3.org/Consortium/Legal/
*
* $Id$
*/
package org.w3c.css.util;
import org.w3c.css.css.StyleSheet;
import org.w3c.css.parser.Frame;
import org.w3c.www.http.HttpAcceptCharset;
import org.w3c.www.http.HttpAcceptCharsetList;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
import java.util.ArrayList;
import java.util.HashMap;
/**
* @author Philippe Le Hegaret
* @version $Revision$
*/
public class ApplContext {
// the charset of the first source (url/uploaded/text)
public static Charset defaultCharset;
public static Charset utf8Charset;
static {
try {
defaultCharset = Charset.forName("iso-8859-1");
utf8Charset = Charset.forName("utf-8");
} catch (Exception ex) {
// we are in deep trouble here
defaultCharset = null;
utf8Charset = null;
}
}
private class BomEncoding {
Charset uriCharset = null;
boolean fromBom = false;
private BomEncoding(Charset c, boolean fb) {
uriCharset = c;
fromBom = fb;
}
private BomEncoding(Charset c) {
this(c, false);
}
private BomEncoding() {
}
}
// charset definition of traversed URLs
private HashMap<URL, BomEncoding> uricharsets = null;
// namespace definitions
private HashMap<URL, HashMap<String, String>> namespaces = null;
// default prefix
public static String defaultPrefix = "*defaultprefix*";
public static String noPrefix = "*noprefix*";
private ArrayList<URL> linkedmedia = new ArrayList<URL>();
int readTimeout = 60000; // ms
int connectTimeout = 5000; // ms
String credential = null;
String lang;
Messages msgs;
Frame frame;
StyleSheet styleSheet = null;
CssVersion version = CssVersion.getDefault();
CssProfile profile = CssProfile.NONE;
String input;
Class cssselectorstyle;
int origin = -1;
String medium;
private String link;
int warningLevel = 0;
boolean treatVendorExtensionsAsWarnings = false;
boolean treatCssHacksAsWarnings = false;
boolean suggestPropertyName = true;
private String propertyKey = null;
public boolean followlinks() {
return followlinks;
}
public void setFollowlinks(boolean followlinks) {
this.followlinks = followlinks;
}
boolean followlinks = true;
FakeFile fakefile = null;
String faketext = null;
Charset faketextcharset = null;
URL fakeurl = null;
URL referrer = null;
/**
* Creates a new ApplContext
*/
public ApplContext(String lang) {
this.lang = lang;
msgs = new Messages(lang);
}
public int getWarningLevel() {
return warningLevel;
}
public void setWarningLevel(int warningLevel) {
this.warningLevel = warningLevel;
}
public ArrayList<URL> getLinkedURIs() {
return linkedmedia;
}
public void addLinkedURI(URL url) {
if (url != null) {
linkedmedia.add(url);
}
}
// as ugly as everything else
public String getCredential() {
return credential;
}
public void setCredential(String credential) {
this.credential = credential;
}
public void setFrame(Frame frame) {
this.frame = frame;
frame.ac = this;
}
public Frame getFrame() {
return frame;
}
public void setStyleSheet(StyleSheet styleSheet) {
this.styleSheet = styleSheet;
}
public StyleSheet getStyleSheet() {
return styleSheet;
}
public Class getCssSelectorsStyle() {
return cssselectorstyle;
}
public void setCssSelectorsStyle(Class s) {
cssselectorstyle = s;
}
public Messages getMsg() {
return msgs;
}
public String getContentType() {
return (msgs != null) ? msgs.getString("content-type") : null;
}
public String getContentLanguage() {
return (msgs != null) ? msgs.getString("content-language") : null;
}
/**
* Searches the properties list for a content-encoding one. If it does not
* exist, searches for output-encoding-name. If it still does not exists,
* the method returns the default utf-8 value
*
* @return the output encoding of this ApplContext
*/
public String getContentEncoding() {
// return (msgs != null) ? msgs.getString("content-encoding") : null;
String res = null;
if (msgs != null) {
res = msgs.getString("content-encoding");
if (res == null) {
res = msgs.getString("output-encoding-name");
}
if (res != null) {
// if an encoding has been found, return it
return res;
}
}
// default encoding
return Utf8Properties.ENCODING;
}
public String getLang() {
return lang;
}
public void setCssVersion(String cssversion) {
version = CssVersion.resolve(this, cssversion);
propertyKey = null;
}
public void setCssVersion(CssVersion version) {
this.version = version;
propertyKey = null;
}
public String getCssVersionString() {
return version.toString();
}
public CssVersion getCssVersion() {
return version;
}
public void setProfile(String profile) {
this.profile = CssProfile.resolve(this, profile);
propertyKey = null;
}
/**
* get the String used to fetch the relevant property file
*/
public String getPropertyKey() {
if (propertyKey != null) {
return propertyKey;
}
if (profile == CssProfile.SVG && version == CssVersion.CSS3) {
propertyKey = version.toString() + profile.toString();
return propertyKey;
}
if (profile != CssProfile.EMPTY && profile != CssProfile.NONE) {
propertyKey = profile.toString();
} else {
propertyKey = version.toString();
}
return propertyKey;
}
public CssProfile getCssProfile() {
return profile;
}
public String getProfileString() {
return profile.toString();
}
public void setCssVersionAndProfile(String spec) {
// for things like SVG, version will be set to the default one
// CSS21 in that case, as defined in CssVersion
// and profile will be resolved to svg.
//
// if the version resolve then profile will default to NONE
// TODO should we check profile first and if SVG or MOBILE
// set specific version of CSS (like CSS2 and not CSS21 for MOBILE) ?
if ((spec == null) || spec.isEmpty()) {
version = CssVersion.getDefault();
profile = CssProfile.SVG;
} else {
String low = spec.toLowerCase();
version = CssVersion.resolve(this, low);
profile = CssProfile.resolve(this, low);
// some special cases...
// http://www.atsc.org/cms/index.php/standards/published-standards/71-atsc-a100-standard
if (profile.equals(CssProfile.ATSCTV)) {
version = CssVersion.CSS2;
}
}
}
public void setOrigin(int origin) {
this.origin = origin;
}
public int getOrigin() {
return origin;
}
public void setMedium(String medium) {
this.medium = medium;
}
public String getMedium() {
return medium;
}
public String getInput() {
return input;
}
public void setInput(String input) {
this.input = input;
}
public String getLink() {
return link;
}
public void setLink(String queryString) {
this.link = queryString;
}
public boolean getTreatVendorExtensionsAsWarnings() {
return treatVendorExtensionsAsWarnings;
}
/**
* Change the behaviour of error reporting for vendor extensions.
*
* @param treatVendorExtensionsAsWarnings
*/
public void setTreatVendorExtensionsAsWarnings(
boolean treatVendorExtensionsAsWarnings) {
this.treatVendorExtensionsAsWarnings = treatVendorExtensionsAsWarnings;
}
public boolean getTreatCssHacksAsWarnings() {
return treatCssHacksAsWarnings;
}
/**
* Change the behaviour of error reporting for CSS Hacks.
*
* @param treatCssHacksAsWarnings
*/
public void setTreatCssHacksAsWarnings(boolean treatCssHacksAsWarnings) {
this.treatCssHacksAsWarnings = treatCssHacksAsWarnings;
}
public boolean getSuggestPropertyName() {
return suggestPropertyName;
}
public void setSuggestPropertyName(boolean b) {
suggestPropertyName = b;
}
/**
* Sets the content encoding to the first charset that appears in
* <i>acceptCharset</i>. If the charset is not supported, the content
* encoding will be utf-8
*
* @param acceptCharset a String representing the Accept-Charset request parameter
*/
public void setContentEncoding(String acceptCharset) {
if (acceptCharset != null) {
// uses some Jigsaw classes to parse the Accept-Charset
// these classes need to load a lot of stuff, so it may be quite
// long the first time
HttpAcceptCharsetList charsetList = new HttpAcceptCharsetList();
charsetList.setString(acceptCharset);
HttpAcceptCharset[] charsets;
charsets = (HttpAcceptCharset[]) charsetList.getValue();
String encoding = null;
double quality = 0.0;
String biasedcharset = getMsg().getString("output-encoding-name");
for (int i = 0; i < charsets.length && quality < 1.0; i++) {
HttpAcceptCharset charset = charsets[i];
String currentCharset = charset.getCharset();
// checks that the charset is supported by Java
if (isCharsetSupported(currentCharset)) {
double currentQuality = charset.getQuality();
// we prefer utf-8
// FIXME (the bias value and the biased charset
// should be dependant on the language)
if ((biasedcharset != null) &&
!biasedcharset.equalsIgnoreCase(currentCharset)) {
currentQuality *= 0.5;
}
if (currentQuality > quality) {
quality = currentQuality;
encoding = charset.getCharset();
}
}
}
if (encoding != null) {
getMsg().properties.setProperty("content-encoding", encoding);
} else {
// no valid charset
getMsg().properties.remove("content-encoding");
}
} else {
// no Accept-Charset given
getMsg().properties.remove("content-encoding");
}
}
private boolean isCharsetSupported(String charset) {
if ("*".equals(charset)) {
return true;
}
try {
return Charset.isSupported(charset);
} catch (Exception e) {
return false;
}
}
/**
* used for storing the charset of the document in use
* and its update by a @charset statement, or through
* automatic discovery
*/
public void setCharsetForURL(URL url, String charset, boolean from_bom) {
if (uricharsets == null) {
uricharsets = new HashMap<>();
}
Charset c = null;
try {
c = Charset.forName(charset);
} catch (IllegalCharsetNameException icex) {
// FIXME add a warning in the CSS
} catch (UnsupportedCharsetException ucex) {
// FIXME inform about lack of support
}
if (c != null) {
uricharsets.put(url, new BomEncoding(c, from_bom));
}
}
/**
* used for storing the charset of the document in use
* and its update by a @charset statement, or through
* automatic discovery
*/
public void setCharsetForURL(URL url, Charset charset) {
if (uricharsets == null) {
uricharsets = new HashMap<URL, BomEncoding>();
}
uricharsets.put(url, new BomEncoding(charset));
}
public boolean isCharsetFromBOM(URL url) {
BomEncoding b;
if (uricharsets == null) {
return false;
}
b = uricharsets.get(url);
if (b != null) {
return b.fromBom;
}
return false;
}
/**
* used for storing the charset of the document in use
* and its update by a @charset statement, or through
* automatic discovery
*/
public String getCharsetForURL(URL url) {
BomEncoding b;
if (uricharsets == null) {
return null;
}
b = uricharsets.get(url);
if (b != null) {
return b.uriCharset.toString();
}
return null;
}
/**
* used for storing the charset of the document in use
* and its update by a @charset statement, or through
* automatic discovery
*/
public Charset getCharsetObjForURL(URL url) {
BomEncoding b;
if (uricharsets == null) {
return null;
}
b = uricharsets.get(url);
if (b == null) {
return null;
}
return b.uriCharset;
}
/**
* store content of uploaded file
*/
public void setFakeFile(FakeFile fakefile) {
this.fakefile = fakefile;
}
/**
* store content of entered text
*/
public void setFakeText(String faketext, Charset faketextcharset) {
this.faketext = faketext;
this.faketextcharset = faketextcharset;
}
public InputStream getFakeInputStream(URL source)
throws IOException {
InputStream is = null;
Charset c = null;
if (fakefile != null) {
is = fakefile.getInputStream();
}
if (faketext != null) {
is = new ByteArrayInputStream(faketext.getBytes(faketextcharset));
c = faketextcharset;
}
if (is == null) {
return null;
}
if (c == null) {
c = getCharsetObjForURL(source);
}
if (c == null) {
UnicodeInputStream uis = new UnicodeInputStream(is);
String guessedCharset = uis.getEncodingFromStream();
if (guessedCharset != null) {
setCharsetForURL(source, guessedCharset, true);
}
return uis;
} else {
if (utf8Charset.compareTo(c) == 0) {
return new UnicodeInputStream(is);
}
}
return is;
}
public boolean isInputFake() {
return ((faketext != null) || (fakefile != null));
}
public void setFakeURL(String fakeurl) {
try {
this.fakeurl = new URL(fakeurl);
} catch (Exception ex) {
}
}
public URL getFakeURL() {
return fakeurl;
}
/**
* support for namespaces
*/
public void setNamespace(URL url, String prefix, String nsname) {
if (namespaces == null) {
namespaces = new HashMap<URL, HashMap<String, String>>();
}
// reformat the prefix if null.
String realPrefix = ((prefix != null) && !prefix.isEmpty()) ? prefix : defaultPrefix;
HashMap<String, String> nsdefs = namespaces.get(url);
if (nsdefs == null) {
nsdefs = new HashMap<String, String>();
nsdefs.put(realPrefix, nsname);
namespaces.put(url, nsdefs);
} else {
// do we need to check if we have a redefinition ?
nsdefs.put(realPrefix, nsname);
}
}
// true if a namespace is defined in the document (CSS fragment)
// defined by the URL, with prefix "prefix"
public boolean isNamespaceDefined(URL url, String prefix) {
if (prefix == null) { // no prefix, always match
return true;
}
if (prefix.equals("*")) { // any ns, always true
return true;
}
String realPrefix = (!prefix.isEmpty()) ? prefix : defaultPrefix;
if (namespaces == null) { // no ns defined -> fail
return false;
}
HashMap<String, String> nsdefs = namespaces.get(url);
if (nsdefs == null) {
return false;
}
return nsdefs.containsKey(realPrefix);
}
/**
* Set the current referrer for possible linked style sheets
*
* @param referrer the referring URL
*/
public void setReferrer(URL referrer) {
this.referrer = referrer;
}
/**
* get the referrer URL (or null if not relevant)
*
* @return an URL
*/
public URL getReferrer() {
return referrer;
}
public int getReadTimeout() {
return readTimeout;
}
public void setReadTimeout(int timeout) {
readTimeout = timeout;
}
public int getConnectTimeout() {
return connectTimeout;
}
public void setConnectTimeout(int timeout) {
connectTimeout = timeout;
}
}