|
5 | 5 | package org.w3c.css.selectors.pseudofunctions;
|
6 | 6 |
|
7 | 7 | import org.w3c.css.selectors.PseudoFunctionSelector;
|
| 8 | +import org.w3c.css.util.ApplContext; |
| 9 | +import org.w3c.css.util.InvalidParamException; |
| 10 | + |
| 11 | +import java.util.IllformedLocaleException; |
| 12 | +import java.util.Locale; |
| 13 | +import java.util.Locale.Builder; |
8 | 14 |
|
9 | 15 | /**
|
10 | 16 | * PseudoFunctionLang<br />
|
11 | 17 | * Created: Sep 2, 2005 4:24:48 PM<br />
|
12 | 18 | */
|
13 | 19 | public class PseudoFunctionLang extends PseudoFunctionSelector {
|
14 | 20 |
|
15 |
| - public PseudoFunctionLang(String name, String lang) { |
| 21 | + public PseudoFunctionLang(String name, String lang, ApplContext ac) |
| 22 | + throws InvalidParamException { |
16 | 23 | setName(name);
|
| 24 | + parseLang(ac, lang, name); |
17 | 25 | setParam(lang);
|
18 | 26 | }
|
19 | 27 |
|
| 28 | + /** |
| 29 | + * verify a language tag per BCP47 |
| 30 | + * |
| 31 | + * @param ac the ApplContext |
| 32 | + * @param lang the language tag |
| 33 | + * @param caller the property/selector/context calling for verification |
| 34 | + * @throws InvalidParamException if invalid |
| 35 | + */ |
| 36 | + public static final void parseLang(ApplContext ac, String lang, String caller) |
| 37 | + throws InvalidParamException { |
| 38 | + try { |
| 39 | + String lang_tag = lang; |
| 40 | + if (lang.charAt(0) == '"' || lang.charAt(0) == '\'') { |
| 41 | + // trim the string |
| 42 | + lang_tag = lang.substring(1, lang.lastIndexOf(lang.charAt(0))); |
| 43 | + } |
| 44 | + // use Locale builder parsing to check BCP 47 values |
| 45 | + Builder builder = new Builder(); |
| 46 | + builder.setLanguageTag(lang_tag); |
| 47 | + Locale l = builder.build(); |
| 48 | + } catch (IllformedLocaleException ex) { |
| 49 | + throw new InvalidParamException("value", lang, caller, ac); |
| 50 | + } |
| 51 | + } |
| 52 | + |
20 | 53 | }
|
0 commit comments