1 package org.unicode.cldr.test;
2 
3 import java.util.HashSet;
4 import java.util.List;
5 import java.util.Set;
6 
7 import org.unicode.cldr.test.CheckCLDR.CheckStatus.Subtype;
8 import org.unicode.cldr.util.CLDRFile;
9 
10 public class CheckAlt extends CheckCLDR {
11 
12     Set<String> seenSoFar = new HashSet<>();
13 
14     // determine if we have an alt=...proposed
15     // if we have one, and there is not a non-proposed version -- in this same file, unaliased, there's a problem.
16     @Override
handleCheck(String path, String fullPath, String value, Options options, List<CheckStatus> result)17     public CheckCLDR handleCheck(String path, String fullPath, String value,
18         Options options, List<CheckStatus> result) {
19         if (fullPath == null) return this; // skip paths that we don't have
20 
21         // quick checks
22         if (path.indexOf("[@alt=") <= 0) {
23             return this;
24         }
25         if (path.indexOf("proposed") <= 0) {
26             return this;
27         }
28 
29         String strippedPath = CLDRFile.getNondraftNonaltXPath(path);
30         if (strippedPath.equals(path)) {
31             return this; // paths equal, skip
32         }
33 
34         String otherValue = getCldrFileToCheck().getStringValue(strippedPath);
35         if (otherValue != null) {
36             return this;
37         }
38         // String localeID = getCldrFileToCheck().getSourceLocaleID(path, null);
39         // if (!localeID.equals(getCldrFileToCheck().getLocaleID())) {
40         // return this; // must be same file
41         // }
42         // if (!status.pathWhereFound.equals(path)) return this; // must be unaliased
43 
44         // if (fullPath.contains("x999")) {
45         // result.add(new CheckStatus().setCause(this).setType(CheckStatus.warningType)
46         // .setMessage("There was a conflict introduced as a result of fixing default contents: please pick among the values or add a corrected value.",
47         // new Object[]{}));
48         // }
49 
50         // String strippedPath = removeProposed(path);
51         // if (strippedPath.equals(path)) return this; // happened to match "proposed" but wasn't in 'alt';
52         //
53         // localeID = getCldrFileToCheck().getSourceLocaleID(strippedPath, null);
54         // // if localeID is null, or if it is CODE_FALLBACK_ID or root, we have a potential problem.
55         // if (localeID == null || localeID.equals(XMLSource.CODE_FALLBACK_ID)) { // || localeID.equals("root")
56         // String message = strippedPath;
57         // boolean checkOnSubmit = true;
58 
59         // if (seenSoFar.contains(strippedPath)) {
60         // message += "MULTIPLE! ";
61         // checkOnSubmit = false;
62         // }
63         result.add(new CheckStatus().setCause(this).setMainType(CheckStatus.warningType)
64             .setSubtype(Subtype.noUnproposedVariant)
65             .setCheckOnSubmit(false)
66             .setMessage("Proposed item but no unproposed variant", new Object[] {}));
67         seenSoFar.add(strippedPath);
68 
69         return this;
70     }
71 
72     // private String removeProposed(String path) {
73     // parts.set(path);
74     // for (int i = 0; i < parts.size(); ++i) {
75     // Map attributes = parts.getAttributes(i);
76     // for (Iterator it = attributes.keySet().iterator(); it.hasNext();) {
77     // String attribute = (String) it.next();
78     // if (!attribute.equals("alt")) continue;
79     // String attributeValue = (String) attributes.get(attribute);
80     // int pos = attributeValue.indexOf("proposed");
81     // if (pos < 0) continue;
82     // if (pos > 0 && attributeValue.charAt(pos-1) == '-') --pos; // backup for "...-proposed"
83     // if (pos == 0) {
84     // attributes.remove(attribute);
85     // continue;
86     // }
87     // attributeValue = attributeValue.substring(0,pos); // strip it off
88     // attributes.put(attribute, attributeValue);
89     // }
90     // }
91     // String strippedPath = parts.toString();
92     // return strippedPath;
93     // }
94 
95     @Override
setCldrFileToCheck(CLDRFile cldrFileToCheck, Options options, List<CheckStatus> possibleErrors)96     public CheckCLDR setCldrFileToCheck(CLDRFile cldrFileToCheck, Options options,
97         List<CheckStatus> possibleErrors) {
98         if (cldrFileToCheck == null) return this;
99         // Skip if the phase is not final testing
100         if (Phase.FINAL_TESTING == getPhase() || Phase.BUILD == getPhase()) {
101             setSkipTest(false); // ok
102         } else {
103             setSkipTest(true);
104             return this;
105         }
106 
107         super.setCldrFileToCheck(cldrFileToCheck, options, possibleErrors);
108         seenSoFar.clear();
109         return this;
110     }
111     // Matcher myLocalePlus = PatternCache.get(cldrFileToCheck.getLocaleID() + "_[^_]*").matcher("");
112     // Set children = cldrFileToCheck.getAvailableLocales();
113     // List iChildren = new ArrayList();
114     // for (Iterator it = children.iterator(); it.hasNext();) {
115     // String locale = (String)it.next();
116     // if (!myLocalePlus.reset(locale).matches()) continue;
117     // CLDRFile child = cldrFileToCheck.make(locale, true);
118     // if (child == null) {
119     // CheckStatus item = new CheckStatus().setCause(this).setType(CheckStatus.errorType)
120     // .setMessage("Null file from: {0}", new Object[]{locale});
121     // possibleErrors.add(item);
122     // } else {
123     // iChildren.add(child);
124     // }
125     // }
126     // if (iChildren.size() == 0) immediateChildren = null;
127     // else {
128     // immediateChildren = new CLDRFile[iChildren.size()];
129     // immediateChildren = (CLDRFile[]) iChildren.toArray(immediateChildren);
130     // }
131     // return this;
132     // }
133 
134 }
135