1 package org.unicode.cldr.test; 2 3 import java.util.List; 4 import java.util.regex.Matcher; 5 import java.util.regex.Pattern; 6 7 import org.unicode.cldr.test.CheckCLDR.CheckStatus.Subtype; 8 import org.unicode.cldr.util.PatternCache; 9 10 public class CheckPlaceHolders extends CheckCLDR { 11 12 private static final Pattern PLACEHOLDER_PATTERN = PatternCache.get("([0-9]|[1-9][0-9]+)"); 13 private static final Pattern SKIP_PATH_LIST = Pattern 14 .compile("//ldml/characters/(exemplarCharacters|parseLenient).*"); 15 16 @Override handleCheck(String path, String fullPath, String value, Options options, List<CheckStatus> result)17 public CheckCLDR handleCheck(String path, String fullPath, String value, Options options, 18 List<CheckStatus> result) { 19 if (SKIP_PATH_LIST.matcher(path).matches()) { 20 return this; 21 } 22 int startPlaceHolder = 0; 23 int endPlaceHolder; 24 if (value == null) { 25 return this; 26 } 27 while (startPlaceHolder != -1 && startPlaceHolder < value.length()) { 28 startPlaceHolder = value.indexOf('{', startPlaceHolder + 1); 29 if (startPlaceHolder != -1) { 30 endPlaceHolder = value.indexOf('}', startPlaceHolder + 1); 31 if (endPlaceHolder == -1) { 32 result.add(new CheckStatus().setCause(this) 33 .setMainType(CheckStatus.errorType) 34 .setSubtype(Subtype.invalidPlaceHolder) 35 .setMessage("Invalid placeholder in value \"" + value + "\"")); 36 } else { 37 String placeHolderString = value.substring(startPlaceHolder + 1, endPlaceHolder); 38 Matcher matcher = PLACEHOLDER_PATTERN.matcher(placeHolderString); 39 if (!matcher.matches()) { 40 result.add(new CheckStatus().setCause(this) 41 .setMainType(CheckStatus.errorType) 42 .setSubtype(Subtype.invalidPlaceHolder) 43 .setMessage("Invalid placeholder in value \"" + value + "\"")); 44 } 45 startPlaceHolder = endPlaceHolder; 46 } 47 } 48 } 49 return this; 50 } 51 } 52