xref: /aosp_15_r20/external/cldr/tools/cldr-code/src/main/java/org/unicode/cldr/test/CheckQuotes.java (revision 912701f9769bb47905792267661f0baf2b85bed5)
1 package org.unicode.cldr.test;
2 
3 import com.ibm.icu.text.UnicodeSet;
4 import java.util.List;
5 import java.util.regex.Matcher;
6 import java.util.regex.Pattern;
7 import org.unicode.cldr.test.CheckCLDR.CheckStatus.Subtype;
8 import org.unicode.cldr.util.PatternCache;
9 
10 public class CheckQuotes extends CheckCLDR {
11     public static final String VALID_DELIMITER_URL =
12             "https://cldr.unicode.org/translation/characters";
13 
14     private static final Pattern ASCII_QUOTES = PatternCache.get("[\'\"]");
15     private static final Pattern UNITS = PatternCache.get("//ldml/units/.*");
16     private static final Pattern DELIMITERS = PatternCache.get("//ldml/delimiters/.*");
17     private static final UnicodeSet VALID_DELIMITERS =
18             new UnicodeSet()
19                     .add(0x2018, 0x201A)
20                     .add(0x201C, 0x201E)
21                     .add(0x300C, 0x300F)
22                     .add(0x2039, 0x203A)
23                     .add(0x00AB)
24                     .add(0x00BB);
25 
26     @Override
handleCheck( String path, String fullPath, String value, Options options, List<CheckStatus> result)27     public CheckCLDR handleCheck(
28             String path, String fullPath, String value, Options options, List<CheckStatus> result) {
29         if (value == null) {
30             return this;
31         }
32 
33         if (UNITS.matcher(path).matches()) {
34             if (!accept(result)) return this;
35             Matcher matcher = ASCII_QUOTES.matcher(value);
36             CheckStatus.Type type = CheckStatus.warningType;
37             if (this.getCldrFileToCheck().getLocaleID().equals("en")) {
38                 type = CheckStatus.errorType;
39             }
40             if (matcher.find()) {
41                 result.add(
42                         new CheckStatus()
43                                 .setCause(this)
44                                 .setMainType(type)
45                                 .setSubtype(Subtype.asciiQuotesNotAllowed)
46                                 .setMessage(
47                                         "Use of ASCII quote marks (' \") is discouraged. Use primes for units (′ ″) and curly quotes for text (‘ ’ “ ” …)"));
48             }
49         }
50         if (DELIMITERS.matcher(path).matches()) {
51             if (!accept(result)) return this;
52             if (!VALID_DELIMITERS.contains(value)) {
53                 result.add(
54                         new CheckStatus()
55                                 .setCause(this)
56                                 .setMainType(CheckStatus.errorType)
57                                 .setSubtype(Subtype.invalidDelimiter)
58                                 .setMessage(
59                                         "Invalid delimiter. See "
60                                                 + VALID_DELIMITER_URL
61                                                 + " for a list of valid delimiters."));
62             }
63         }
64         return this;
65     }
66 }
67