1 package org.unicode.cldr.test; 2 3 import java.util.List; 4 5 import org.unicode.cldr.test.CheckCLDR.CheckStatus.Subtype; 6 import org.unicode.cldr.test.CheckCLDR.CheckStatus.Type; 7 import org.unicode.cldr.util.CLDRFile; 8 import org.unicode.cldr.util.XPathParts; 9 import org.unicode.cldr.util.personname.PersonNameFormatter.SampleType; 10 11 import com.google.common.collect.ImmutableMultimap; 12 13 public class CheckPersonNames extends CheckCLDR { 14 15 static final String MISSING = "∅∅∅"; 16 /** 17 * @internal, public for testing 18 */ 19 public static final ImmutableMultimap<SampleType, String> REQUIRED = ImmutableMultimap.<SampleType, String> builder() 20 .putAll(SampleType.givenOnly, "given") 21 .putAll(SampleType.givenSurnameOnly, "given", "surname") 22 .putAll(SampleType.given12Surname, "given", "given2", "surname") 23 .putAll(SampleType.full, "prefix", "given", "given-informal", "given2", "surname", "surname2", "suffix") 24 .build(); 25 /** 26 * @internal, public for testing 27 */ 28 public static final ImmutableMultimap<SampleType, String> REQUIRED_EMPTY = ImmutableMultimap.<SampleType, String> builder() 29 .putAll(SampleType.givenOnly, "prefix", "given-informal", "given2", "surname", "surname-prefix", "surname-core", "surname2", "suffix") 30 .putAll(SampleType.givenSurnameOnly, "prefix", "given2", "surname-prefix", "surname-core", "surname2", "suffix") 31 .build(); 32 33 boolean isRoot = false; 34 35 @Override setCldrFileToCheck(CLDRFile cldrFileToCheck, Options options, List<CheckStatus> possibleErrors)36 public CheckCLDR setCldrFileToCheck(CLDRFile cldrFileToCheck, Options options, List<CheckStatus> possibleErrors) { 37 isRoot = cldrFileToCheck.getLocaleID().equals("root"); 38 return super.setCldrFileToCheck(cldrFileToCheck, options, possibleErrors); 39 } 40 41 @Override handleCheck(String path, String fullPath, String value, Options options, List<CheckStatus> result)42 public CheckCLDR handleCheck(String path, String fullPath, String value, Options options, 43 List<CheckStatus> result) { 44 if (value == null || isRoot || !path.startsWith("//ldml/personNames/")) { 45 return this; 46 } 47 XPathParts parts = XPathParts.getFrozenInstance(path); 48 String category = parts.getElement(2); 49 if (category.equals("sampleName")) { 50 Type status = CheckStatus.warningType; 51 String message = null; 52 SampleType item = SampleType.valueOf(parts.getAttributeValue(2, "item")); 53 String modifiedField = parts.getAttributeValue(3, "type"); 54 boolean isMissing = value.equals(MISSING); 55 if (isMissing) { 56 if (REQUIRED.get(item).contains(modifiedField)) { 57 message = "This value must not be empty (" + MISSING + ")"; 58 } else if (modifiedField.equals("surname")) { 59 String surname2 = getCldrFileToCheck().getStringValue(path.replace("surname", "surname2")); 60 if (!surname2.equals(MISSING)) { 61 message = "The value for 'surname' must not be empty (" + MISSING + ") unless 'surname2' is."; 62 } 63 } 64 } else { // not missing, so... 65 status = CheckStatus.errorType; 66 if (REQUIRED_EMPTY.get(item).contains(modifiedField)) { 67 message = "This value must be empty (" + MISSING + ")"; 68 } else if (modifiedField.equals("multiword") && !value.contains(" ")) { 69 message = "All multiword fields must have 2 (or more) words separated by spaces"; 70 } 71 } 72 if (message != null) { 73 result.add(new CheckStatus().setCause(this) 74 .setMainType(status) 75 .setSubtype(Subtype.badSamplePersonName) 76 .setMessage(message)); 77 } 78 } 79 return this; 80 } 81 } 82