Skip to content

Commit 93eac57

Browse files
authored
CLDR-18697 Remove all PathStarrer methods except get and getWithPattern (#4899)
1 parent 741b3dd commit 93eac57

File tree

9 files changed

+75
-132
lines changed

9 files changed

+75
-132
lines changed

tools/cldr-code/src/main/java/org/unicode/cldr/tool/ChartDelta.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -591,19 +591,17 @@ private String getReformattedPath(
591591
return old.getSourceLocaleID(oldStylePath, oldStatus);
592592
}
593593

594-
PathStarrer starrer = new PathStarrer().setSubstitutionPattern("%A");
595-
596594
private PathHeader getPathHeader(String path) {
597595
try {
598596
PathHeader ph = phf.fromPath(path);
599597
if (ph.getPageId() == PageId.Unknown) {
600-
String star = starrer.set(path);
598+
String star = PathStarrer.getWithPattern(path, PathStarrer.PERCENT_A_PATTERN);
601599
badHeaders.add(star);
602600
return null;
603601
}
604602
return ph;
605603
} catch (Exception e) {
606-
String star = starrer.set(path);
604+
String star = PathStarrer.getWithPattern(path, PathStarrer.PERCENT_A_PATTERN);
607605
badHeaders.add(star);
608606
// System.err.println("Skipping path with bad PathHeader: " + path);
609607
return null;

tools/cldr-code/src/main/java/org/unicode/cldr/tool/ShowStarredCoverage.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,11 @@ public static void main(String[] args) {
166166
EnumSet.of(PageId.Fields, PageId.Gregorian, PageId.Generic);
167167

168168
private static String condense(PathHeader ph) {
169-
// Replace PathStarrer.STAR_PATTERN with simple "*" for compatibility with other
169+
// Use PathStarrer.SIMPLE_STAR_PATTERN ("*") for compatibility with other
170170
// code used by ShowStarredCoverage. Also, collapse alts.
171171
final String starredPath =
172-
PathStarrer.getWithPattern(ph.getOriginalPath(), "*").replace("[@alt=\"*\"]", "");
172+
PathStarrer.getWithPattern(ph.getOriginalPath(), PathStarrer.SIMPLE_STAR_PATTERN)
173+
.replace("[@alt=\"*\"]", "");
173174
SectionId sectionId = ph.getSectionId();
174175
PageId pageId = ph.getPageId();
175176
String category = sectionId + "|" + pageId;

tools/cldr-code/src/main/java/org/unicode/cldr/util/DtdPathIterator.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,11 @@ public static void main(String[] args) {
142142
CLDRConfig.getInstance().getCommonAndSeedAndMainAndAnnotationsFactory();
143143

144144
// get all the actual starred patterns
145-
146-
PathStarrer ps = new PathStarrer().setSubstitutionPattern("%A");
147145
Map<String, String> starredToSample = new TreeMap<>();
148146
for (String locale : Arrays.asList("en", "de", "zh", "ar", "ru")) {
149147
CLDRFile cfile = factory.make(locale, true);
150148
for (String path : cfile.fullIterable()) {
151-
String starred = ps.set(path);
149+
String starred = PathStarrer.getWithPattern(path, PathStarrer.PERCENT_A_PATTERN);
152150
starred = starred.replace("[@alt=\"%A\"]", "");
153151
if (!starredToSample.containsKey(starred)
154152
&& !starred.endsWith("/alias")
Lines changed: 3 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
package org.unicode.cldr.util;
22

3-
import com.google.common.base.Joiner;
4-
import com.ibm.icu.impl.Utility;
5-
import java.util.ArrayList;
6-
import java.util.List;
7-
import java.util.Set;
83
import java.util.concurrent.ConcurrentHashMap;
9-
import java.util.regex.Matcher;
10-
import java.util.regex.Pattern;
114

125
/**
136
* Transforms a path by replacing attributes with .*
147
*
158
* @author markdavis
169
*/
1710
public class PathStarrer {
11+
public static final String PERCENT_A_PATTERN = "%A";
12+
public static final String SIMPLE_STAR_PATTERN = "*";
13+
1814
/**
1915
* The default pattern to replace attribute values in paths to make starred paths. This pattern
2016
* is assumed never to occur literally in an ordinary non-starred path. Cached starred paths use
@@ -24,11 +20,6 @@ public class PathStarrer {
2420
*/
2521
private static final String STAR_PATTERN = "([^\"]*+)";
2622

27-
private final List<String> attributes = new ArrayList<>();
28-
private String substitutionPattern = STAR_PATTERN;
29-
30-
private static final Pattern ATTRIBUTE_PATTERN_OLD = PatternCache.get("=\"([^\"]*)\"");
31-
3223
private static final ConcurrentHashMap<String, String> STAR_CACHE = new ConcurrentHashMap<>();
3324

3425
/**
@@ -60,73 +51,4 @@ public static String get(String path) {
6051
public static String getWithPattern(String path, String pattern) {
6152
return get(path).replace(STAR_PATTERN, pattern);
6253
}
63-
64-
// TODO: make this method thread-safe, or remove it and use get/getWithPattern instead.
65-
// Reference: https://unicode-org.atlassian.net/browse/CLDR-18697
66-
public String set(String path) {
67-
XPathParts parts = XPathParts.getFrozenInstance(path).cloneAsThawed();
68-
attributes.clear();
69-
for (int i = 0; i < parts.size(); ++i) {
70-
for (String key : parts.getAttributeKeys(i)) {
71-
attributes.add(parts.getAttributeValue(i, key));
72-
parts.setAttribute(i, key, substitutionPattern);
73-
}
74-
}
75-
return parts.toString();
76-
}
77-
78-
/**
79-
* Sets the path starrer attributes, and returns the string.
80-
*
81-
* @param parts
82-
* @return
83-
*/
84-
public String setSkippingAttributes(XPathParts parts, Set<String> skipAttributes) {
85-
attributes.clear();
86-
for (int i = 0; i < parts.size(); ++i) {
87-
for (String key : parts.getAttributeKeys(i)) {
88-
if (!skipAttributes.contains(key)) {
89-
attributes.add(parts.getAttributeValue(i, key));
90-
parts.setAttribute(i, key, substitutionPattern);
91-
}
92-
}
93-
}
94-
return parts.toString();
95-
}
96-
97-
public String setOld(String path) {
98-
Matcher starAttributeMatcher = ATTRIBUTE_PATTERN_OLD.matcher(path);
99-
StringBuilder starredPathOld = new StringBuilder();
100-
attributes.clear();
101-
int lastEnd = 0;
102-
while (starAttributeMatcher.find()) {
103-
int start = starAttributeMatcher.start(1);
104-
int end = starAttributeMatcher.end(1);
105-
starredPathOld.append(path, lastEnd, start);
106-
starredPathOld.append(substitutionPattern);
107-
108-
attributes.add(path.substring(start, end));
109-
lastEnd = end;
110-
}
111-
starredPathOld.append(path.substring(lastEnd));
112-
return starredPathOld.toString();
113-
}
114-
115-
public String getAttributesString(String separator) {
116-
return Joiner.on(separator).join(attributes);
117-
}
118-
119-
public PathStarrer setSubstitutionPattern(String substitutionPattern) {
120-
this.substitutionPattern = substitutionPattern;
121-
return this;
122-
}
123-
124-
// Used for coverage lookups - strips off the leading ^ and trailing $ from regexp pattern.
125-
public String transform2(String source) {
126-
String result = Utility.unescape(setOld(source));
127-
if (result.startsWith("^") && result.endsWith("$")) {
128-
result = result.substring(1, result.length() - 1);
129-
}
130-
return result;
131-
}
13254
}

tools/cldr-code/src/main/java/org/unicode/cldr/util/RegexLookup.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.unicode.cldr.util;
22

33
import com.google.common.base.Splitter;
4+
import com.ibm.icu.impl.Utility;
45
import com.ibm.icu.text.Transform;
56
import com.ibm.icu.util.Output;
67
import java.util.ArrayList;
@@ -38,7 +39,6 @@ public class RegexLookup<T> implements Iterable<Map.Entry<Finder, T>> {
3839
private Transform<String, ? extends T> valueTransform;
3940
private Merger<T> valueMerger;
4041
private final boolean allowNull = false;
41-
private static PathStarrer pathStarrer = new PathStarrer().setSubstitutionPattern("*");
4242

4343
public enum LookupType {
4444
STAR_PATTERN_LOOKUP,
@@ -642,8 +642,11 @@ public int size() {
642642
public void put(Finder pattern, T value) {
643643
// System.out.println("pattern.toString() is => "+pattern.toString());
644644
String starPattern =
645-
pathStarrer.transform2(
646-
pattern.toString().replaceAll("\\(\\[\\^\"\\]\\*\\)", "*"));
645+
starPatternTransform(
646+
pattern.toString()
647+
.replaceAll(
648+
"\\(\\[\\^\"\\]\\*\\)",
649+
PathStarrer.SIMPLE_STAR_PATTERN));
647650
// System.out.println("Putting => "+starPattern);
648651
List<SPNode> candidates = _spmap.get(starPattern);
649652
if (candidates == null) {
@@ -657,7 +660,7 @@ public void put(Finder pattern, T value) {
657660

658661
@Override
659662
public T get(Finder finder) {
660-
String starPattern = pathStarrer.transform2(finder.toString());
663+
String starPattern = starPatternTransform(finder.toString());
661664
List<SPNode> candidates = _spmap.get(starPattern);
662665
if (candidates == null) {
663666
return null;
@@ -679,7 +682,7 @@ public List<T> getAll(
679682
List<SPNode> list = new ArrayList<>();
680683
List<T> retList = new ArrayList<>();
681684

682-
String starPattern = pathStarrer.transform2(pattern);
685+
String starPattern = starPatternTransform(pattern);
683686
List<SPNode> candidates = _spmap.get(starPattern);
684687
if (candidates == null) {
685688
return retList;
@@ -763,6 +766,27 @@ public String toString() {
763766
return this._finder.toString();
764767
}
765768
}
769+
770+
// Used for coverage lookups - strips off the leading ^ and trailing $ from regexp pattern.
771+
private String starPatternTransform(String path) {
772+
final Pattern ATTRIBUTE_PATTERN_OLD = PatternCache.get("=\"([^\"]*)\"");
773+
Matcher starAttributeMatcher = ATTRIBUTE_PATTERN_OLD.matcher(path);
774+
StringBuilder starredPathOld = new StringBuilder();
775+
int lastEnd = 0;
776+
while (starAttributeMatcher.find()) {
777+
int start = starAttributeMatcher.start(1);
778+
int end = starAttributeMatcher.end(1);
779+
starredPathOld.append(path, lastEnd, start);
780+
starredPathOld.append(PathStarrer.SIMPLE_STAR_PATTERN);
781+
lastEnd = end;
782+
}
783+
starredPathOld.append(path.substring(lastEnd));
784+
String result = Utility.unescape(starredPathOld.toString());
785+
if (result.startsWith("^") && result.endsWith("$")) {
786+
result = result.substring(1, result.length() - 1);
787+
}
788+
return result;
789+
}
766790
}
767791

768792
/**

tools/cldr-code/src/test/java/org/unicode/cldr/unittest/TestAlt.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
import com.ibm.icu.impl.Row;
77
import com.ibm.icu.impl.Row.R4;
88
import com.ibm.icu.util.Output;
9+
import java.util.ArrayList;
910
import java.util.Collection;
10-
import java.util.Collections;
1111
import java.util.HashMap;
1212
import java.util.LinkedHashSet;
13+
import java.util.List;
1314
import java.util.Map.Entry;
1415
import java.util.Set;
1516
import org.unicode.cldr.icu.dev.test.TestFmwk;
@@ -21,7 +22,6 @@
2122
import org.unicode.cldr.util.XPathParts;
2223

2324
public class TestAlt extends TestFmwk {
24-
private static final Set<String> SINGLETON_ALT = Collections.singleton("alt");
2525
static CLDRConfig testInfo = CLDRConfig.getInstance();
2626

2727
public static void main(String[] args) {
@@ -32,7 +32,6 @@ public void testValues() {
3232
Factory cldrFactory = testInfo.getCldrFactory();
3333
HashMap<String, String> altPaths = new HashMap<>();
3434
Multimap<String, R4<String, String, String, String>> altStarred = TreeMultimap.create();
35-
PathStarrer pathStarrer = new PathStarrer().setSubstitutionPattern("*");
3635
final Set<String> available = new LinkedHashSet<>();
3736
available.add("root");
3837
available.add("en");
@@ -58,10 +57,9 @@ public void testValues() {
5857
if (altValue != null) {
5958
altPaths.put(xpath, locale);
6059
logln(locale + "\t" + xpath);
61-
String starredPath =
62-
pathStarrer.setSkippingAttributes(
63-
parts.cloneAsThawed(), SINGLETON_ALT);
64-
String attrs = pathStarrer.getAttributesString("|");
60+
final List<String> attributes = new ArrayList<>();
61+
String starredPath = starSkippingAlt(parts.cloneAsThawed(), attributes);
62+
String attrs = Joiner.on("|").join(attributes);
6563
final XPathParts noAlt = parts.cloneAsThawed().removeAttribute(i, "alt");
6664
String plainPath = noAlt.toString();
6765
altStarred.put(
@@ -81,9 +79,22 @@ public void testValues() {
8179
}
8280
}
8381

82+
private String starSkippingAlt(XPathParts parts, List<String> attributes) {
83+
attributes.clear();
84+
for (int i = 0; i < parts.size(); ++i) {
85+
for (String key : parts.getAttributeKeys(i)) {
86+
if (!"alt".equals(key)) {
87+
attributes.add(parts.getAttributeValue(i, key));
88+
parts.setAttribute(i, key, PathStarrer.SIMPLE_STAR_PATTERN);
89+
}
90+
}
91+
}
92+
return parts.toString();
93+
}
94+
8495
public void testAlt() {
85-
Output<String> pathWhereFound = new Output<String>();
86-
Output<String> localeWhereFound = new Output<String>();
96+
Output<String> pathWhereFound = new Output<>();
97+
Output<String> localeWhereFound = new Output<>();
8798

8899
CLDRFile testCldrFile = CLDRConfig.getInstance().getCLDRFile("fr_CA", true);
89100
String plain = "//ldml/localeDisplayNames/languages/language[@type=\"af\"]";

tools/cldr-code/src/test/java/org/unicode/cldr/unittest/TestExampleGenerator.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,8 +1935,6 @@ public void TestForMissing() {
19351935
new HashSet<>(); // assume whether there is an example is independent of locale, to
19361936
// speed up the test.
19371937
final String separator = "•";
1938-
PathStarrer ps = new PathStarrer();
1939-
ps.setSubstitutionPattern("*");
19401938

19411939
// Setup for calling phase.getShowRowAction
19421940
DummyPathValueInfo dummyPathValueInfo = null;
@@ -2007,9 +2005,10 @@ public VoterInfo getVoterInfo() {
20072005
if (level.isAtLeast(Level.COMPREHENSIVE)) {
20082006
continue;
20092007
}
2010-
String starred = ps.set(xpath);
2011-
String attrs = ps.getAttributesString(separator);
2012-
2008+
String starred = PathStarrer.getWithPattern(xpath, PathStarrer.SIMPLE_STAR_PATTERN);
2009+
String attrs =
2010+
Joiner.on(separator)
2011+
.join(XPathParts.getFrozenInstance(xpath).getAttributeValues());
20132012
PathHeader ph = phf.fromPath(xpath);
20142013
if (CHECK_ROW_ACTION) {
20152014
dummyPathValueInfo.setXpath(xpath);
@@ -2036,12 +2035,12 @@ public VoterInfo getVoterInfo() {
20362035
continue;
20372036
}
20382037

2039-
samplesForWithout.put(key, sampleAttrAndValue(ps, separator, value));
2038+
samplesForWithout.put(key, sampleAttrAndValue(attrs, value));
20402039
sampleUrlForWithout.put(key, ph.getUrl(BaseUrl.PRODUCTION, localeId));
20412040
countWithoutExamples.add(key, 1);
20422041
} else {
20432042
if (!samplesForWith.containsKey(key)) {
2044-
samplesForWith.put(key, sampleAttrAndValue(ps, separator, value));
2043+
samplesForWith.put(key, sampleAttrAndValue(attrs, value));
20452044
}
20462045
countWithExamples.add(key, 1);
20472046
}
@@ -2352,8 +2351,8 @@ private String getResult(String starredPath, String attr) {
23522351
return result;
23532352
}
23542353

2355-
public String sampleAttrAndValue(PathStarrer ps, final String separator, String value) {
2356-
return ps.getAttributesString(separator) + "➔«" + value + "»";
2354+
private String sampleAttrAndValue(String attrs, String value) {
2355+
return attrs + "➔«" + value + "»";
23572356
}
23582357

23592358
public void testRationals() {

tools/cldr-code/src/test/java/org/unicode/cldr/unittest/TestPathHeader.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,8 +1002,6 @@ public void TestContainment() {
10021002
}
10031003

10041004
public void TestZ() {
1005-
PathStarrer pathStarrer = new PathStarrer().setSubstitutionPattern("%A");
1006-
10071005
Set<PathHeader> sorted = new TreeSet<>();
10081006
Map<String, String> missing = new TreeMap<>();
10091007
Map<String, String> skipped = new TreeMap<>();
@@ -1014,12 +1012,14 @@ public void TestZ() {
10141012
PathHeader pathHeader = pathHeaderFactory.fromPath(path);
10151013
String value = english.getStringValue(path);
10161014
if (pathHeader == null) {
1017-
final String starred = pathStarrer.set(path);
1015+
final String starred =
1016+
PathStarrer.getWithPattern(path, PathStarrer.PERCENT_A_PATTERN);
10181017
missing.put(starred, value + "\t" + path);
10191018
continue;
10201019
}
10211020
if (pathHeader.getSection().equalsIgnoreCase("skip")) {
1022-
final String starred = pathStarrer.set(path);
1021+
final String starred =
1022+
PathStarrer.getWithPattern(path, PathStarrer.PERCENT_A_PATTERN);
10231023
skipped.put(starred, value + "\t" + path);
10241024
continue;
10251025
}
@@ -1330,7 +1330,6 @@ public void TestCompletenessNonLdmlDtd() {
13301330

13311331
private class PathChecker {
13321332
PathHeader.Factory phf = pathHeaderFactory;
1333-
PathStarrer starrer = new PathStarrer().setSubstitutionPattern("%A");
13341333

13351334
Set<String> badHeaders = new TreeSet<>();
13361335
Map<PathHeader, PathHeader> goodHeaders = new HashMap<>();
@@ -1397,7 +1396,7 @@ public void checkSubpath(String path) {
13971396
} catch (Exception e) {
13981397
message = ": Exception in path header: " + e.getMessage();
13991398
}
1400-
String star = starrer.set(path);
1399+
String star = PathStarrer.getWithPattern(path, PathStarrer.PERCENT_A_PATTERN);
14011400
if (badHeaders.add(star)) {
14021401
errln(star + message + ", " + ph);
14031402
System.out.println(

0 commit comments

Comments
 (0)