1 package org.unicode.cldr.test; 2 3 import java.util.Arrays; 4 import java.util.Collection; 5 import java.util.Iterator; 6 import java.util.LinkedHashSet; 7 import java.util.List; 8 import java.util.Map; 9 import java.util.Set; 10 import java.util.TreeSet; 11 12 import org.unicode.cldr.util.CLDRFile; 13 import org.unicode.cldr.util.CLDRPaths; 14 import org.unicode.cldr.util.Differ; 15 import org.unicode.cldr.util.Factory; 16 import org.unicode.cldr.util.StandardCodes; 17 import org.unicode.cldr.util.XMLFileReader; 18 19 import com.ibm.icu.text.Transform; 20 21 public class TestMetadata { main(String[] args)22 public static void main(String[] args) { 23 Factory cldrFactory = Factory.make(CLDRPaths.MAIN_DIRECTORY, ".*"); 24 CLDRFile metadata = cldrFactory.make("supplementalMetadata", false); 25 // Set allKeys = new TreeSet(); 26 // CollectionUtilities.addAll(metadata.iterator(), allKeys); 27 // System.out.println("Keys: " + allKeys); 28 // attribute order 29 30 // Set<String> elements = new TreeSet<String>(); 31 // Set<String> attributes = new TreeSet<String>(); 32 // Set<LinkedHashSet<String>> elementOrderingLists = new LinkedHashSet<LinkedHashSet<String>>(); 33 // 34 // getElementsAndAttributes(CldrUtility.MAIN_DIRECTORY + "root.xml", elements, attributes, elementOrderingLists); 35 // Set<String> suppElements = new TreeSet<String>(); 36 // Set<String> suppAttributes = new TreeSet<String>(); 37 // Set<LinkedHashSet<String>> suppElementOrderingLists = new LinkedHashSet<LinkedHashSet<String>>(); 38 // getElementsAndAttributes(CldrUtility.COMMON_DIRECTORY + "supplemental/characters.xml", suppElements, 39 // suppAttributes, suppElementOrderingLists); 40 // 41 // Set<String> allElements = new TreeSet<String>(); 42 // allElements.addAll(elements); 43 // allElements.addAll(suppElements); 44 // Set<String> allAttributes = new TreeSet<String>(); 45 // allAttributes.addAll(attributes); 46 // allAttributes.addAll(suppAttributes); 47 // 48 // List<String> attributeOrder = Arrays.asList(metadata.getStringValue("//supplementalData/metadata/attributeOrder") 49 // .split("\\s+")); 50 // List<String> programAttributeOrder = CLDRFile.getAttributeOrder(); 51 // 52 // Set<String> allAttributeOrder = new TreeSet<String>(); 53 // allAttributeOrder.addAll(attributeOrder); 54 // allAttributeOrder.addAll(programAttributeOrder); 55 // allAttributeOrder.remove("_q"); 56 // if (showSetDifferences("dtd attributes", allAttributes, "attributeOrder+programAttributeOrder", 57 // allAttributeOrder)) { 58 // System.out.println("ERROR: differences in sets!"); 59 // } 60 // 61 // if (!attributeOrder.equals(programAttributeOrder)) { 62 // System.out.println("ElementOrderDifference: "); 63 // System.out.println(showDifference(programAttributeOrder, attributeOrder, ", ")); 64 // System.out.println("metadata: " + attributeOrder); 65 // System.out.println("program: " + programAttributeOrder); 66 // System.out.println("ERROR: differences in sets!"); 67 // } 68 // 69 // List<String> elementOrder = Arrays.asList(metadata.getStringValue("//supplementalData/metadata/elementOrder").split( 70 // "\\s+")); 71 // List<String> programElementOrder = (List<String>) CLDRFile.getElementOrder(); 72 // 73 // sublistCheck(elementOrderingLists, programElementOrder); 74 // sublistCheck(suppElementOrderingLists, programElementOrder); 75 // 76 // Set<String> allElementOrder = new TreeSet<String>(); 77 // allElementOrder.addAll(elementOrder); 78 // allElementOrder.addAll(programElementOrder); 79 // if (showSetDifferences("dtd elements", allElements, "elementOrder+programElementOrder", allElementOrder)) { 80 // System.out.println("ERROR: differences in sets!"); 81 // } 82 // 83 // if (!elementOrder.equals(programElementOrder)) { 84 // System.out.println("ElementOrderDifference: "); 85 // System.out.println(showDifference(programElementOrder, elementOrder, ", ")); 86 // System.out.println("metadata: " + elementOrder); 87 // System.out.println("program: " + programElementOrder); 88 // System.out.println("ERROR: differences in sets!"); 89 // } 90 91 testZones(metadata); 92 System.out.println("Done"); 93 } 94 sublistCheck(Set<LinkedHashSet<String>> elementOrderingLists, List<String> elementOrder)95 private static void sublistCheck(Set<LinkedHashSet<String>> elementOrderingLists, List<String> elementOrder) { 96 for (Iterator<LinkedHashSet<String>> it = elementOrderingLists.iterator(); it.hasNext();) { 97 LinkedHashSet<String> sublist = (LinkedHashSet<String>) it.next(); 98 // verify that the elements are in the list in the right order. 99 int lastPosition = -1; 100 for (Iterator<String> it2 = sublist.iterator(); it2.hasNext();) { 101 String item = it2.next(); 102 int position = elementOrder.indexOf(item); 103 if (position <= lastPosition) { 104 System.out.println("ERROR: elements out of order for: " + item + " in " + sublist); 105 return; 106 } 107 } 108 System.out.println("Elements in order for: " + sublist); 109 } 110 } 111 showSetDifferences(String name1, Collection<String> set1, String name2, Collection<String> set2)112 private static boolean showSetDifferences(String name1, Collection<String> set1, String name2, Collection<String> set2) { 113 boolean hasDifference = false; 114 TreeSet<String> temp = new TreeSet<String>(); 115 temp.addAll(set1); 116 temp.removeAll(set2); 117 if (temp.size() != 0) { 118 System.out.println(name1 + " minus " + name2 + ":\t" + temp); 119 hasDifference |= true; 120 } 121 temp.clear(); 122 temp.addAll(set2); 123 temp.removeAll(set1); 124 if (temp.size() != 0) { 125 System.out.println(name2 + " minus " + name1 + ":\t" + temp); 126 hasDifference |= true; 127 } 128 return hasDifference; 129 } 130 getElementsAndAttributes(String fileWithDTD, Collection<String> elements, Collection<String> attributes, Collection<LinkedHashSet<String>> elementOrderingLists)131 private static void getElementsAndAttributes(String fileWithDTD, Collection<String> elements, Collection<String> attributes, 132 Collection<LinkedHashSet<String>> elementOrderingLists) { 133 XMLFileReader xfr = new XMLFileReader().setHandler(new MyHandler(elements, attributes, elementOrderingLists)); 134 xfr.read(fileWithDTD, -1, true); 135 } 136 137 private static class ToString<T> implements Transform<T, String> { 138 @Override transform(T source)139 public String transform(T source) { 140 return source == null ? "<null>" : source.toString(); 141 } 142 } 143 showDifference(Iterable<T> a, Iterable<T> b, String separator)144 public static <T> String showDifference(Iterable<T> a, Iterable<T> b, String separator) { 145 return showDifference(a, b, separator, new ToString<T>(), new ToString<T>()); 146 } 147 showDifference(Iterable<T> a, Iterable<T> b, String separator, Transform<T, String> aDisplay, Transform<T, String> bDisplay)148 public static <T> String showDifference(Iterable<T> a, Iterable<T> b, String separator, 149 Transform<T, String> aDisplay, 150 Transform<T, String> bDisplay) { 151 Differ<T> differ = new Differ<T>(300, 10); 152 StringBuilder out = new StringBuilder(); 153 Iterator<T> ai = a.iterator(); 154 Iterator<T> bi = b.iterator(); 155 boolean first = true; 156 while (true) { 157 boolean done = true; 158 if (ai.hasNext()) { 159 differ.addA(ai.next()); 160 done = false; 161 } 162 if (bi.hasNext()) { 163 differ.addB(bi.next()); 164 done = false; 165 } 166 differ.checkMatch(done); 167 168 if (differ.getACount() != 0 || differ.getBCount() != 0) { 169 if (first) 170 first = false; 171 else 172 out.append(separator); 173 out.append("..."); 174 out.append(separator).append(aDisplay.transform(differ.getA(-1))); 175 176 if (differ.getACount() != 0) { 177 for (int i = 0; i < differ.getACount(); ++i) { 178 out.append(separator).append('-'); 179 out.append(aDisplay.transform(differ.getA(i))); 180 } 181 } 182 if (differ.getBCount() != 0) { 183 for (int i = 0; i < differ.getBCount(); ++i) { 184 out.append(separator).append('+'); 185 out.append(bDisplay.transform(differ.getB(i))); 186 } 187 } 188 out.append(separator).append(aDisplay.transform(differ.getA(differ.getACount()))); 189 } 190 if (done) break; 191 } 192 return out.toString(); 193 } 194 testZones(CLDRFile metadata)195 private static void testZones(CLDRFile metadata) { 196 String zoneList = null; 197 for (Iterator<String> it = metadata.iterator(); it.hasNext();) { 198 String key = it.next(); 199 if (key.indexOf("\"$tzid\"") >= 0) { 200 zoneList = metadata.getStringValue(key); 201 break; 202 } 203 } 204 205 String[] zones = zoneList.split("\\s+"); 206 Set<String> metaZoneSet = new TreeSet<String>(); 207 metaZoneSet.addAll(Arrays.asList(zones)); 208 209 StandardCodes sc = StandardCodes.make(); 210 Map<String, List<String>> new_oldZones = sc.getZoneData(); 211 Set<String> stdZoneSet = new TreeSet<String>(); 212 stdZoneSet.addAll(new_oldZones.keySet()); 213 214 if (metaZoneSet.equals(stdZoneSet)) { 215 System.out.println("Zone Set is up-to-date"); 216 } else { 217 Set<String> diff = new TreeSet<String>(); 218 diff.addAll(metaZoneSet); 219 diff.removeAll(stdZoneSet); 220 System.out.println("Meta Zones - Std Zones: " + diff); 221 diff.clear(); 222 diff.addAll(stdZoneSet); 223 diff.removeAll(metaZoneSet); 224 System.out.println("Std Zones - Meta Zones: " + diff); 225 226 System.out.println("Meta Zones: " + metaZoneSet); 227 System.out.println("Std Zones: " + stdZoneSet); 228 229 } 230 } 231 232 static class MyHandler extends XMLFileReader.SimpleHandler { 233 Collection<String> elements; 234 Collection<String> attributes; 235 Collection<LinkedHashSet<String>> elementOrderingLists; 236 MyHandler(Collection<String> elements, Collection<String> attributes, Collection<LinkedHashSet<String>> elementOrderingLists)237 public MyHandler(Collection<String> elements, Collection<String> attributes, Collection<LinkedHashSet<String>> elementOrderingLists) { 238 this.elements = elements; 239 this.attributes = attributes; 240 this.elementOrderingLists = elementOrderingLists; 241 } 242 handleAttributeDecl(String eName, String aName, String type, String mode, String value)243 public void handleAttributeDecl(String eName, String aName, String type, String mode, String value) { 244 attributes.add(aName); 245 // System.out.println( 246 // "eName: " + eName 247 // + ",\t aName: " + aName 248 // + ",\t type: " + type 249 // + ",\t mode: " + mode 250 // + ",\t value: " + value 251 // ); 252 } 253 handleElementDecl(String name, String model)254 public void handleElementDecl(String name, String model) { 255 elements.add(name); 256 LinkedHashSet<String> ordering = new LinkedHashSet<String>(Arrays.asList(model.split("[^-_a-zA-Z0-9]+"))); 257 ordering.remove(""); 258 ordering.remove("PCDATA"); 259 ordering.remove("EMPTY"); 260 if (ordering.size() > 1) { 261 if (elementOrderingLists.add(ordering)) { 262 // System.out.println(model + " =>\t" + ordering); 263 } 264 } 265 // System.out.println( 266 // "name: " + name 267 // + ",\t model: " + model); 268 } 269 270 // public void handlePathValue(String path, String value) { 271 // System.out.println( 272 // "path: " + path 273 // + ",\t value: " + value); 274 // } 275 // 276 // public void handleComment(String path, String comment) { 277 // System.out.println( 278 // "path: " + path 279 // + ",\t comment: " + comment); 280 // } 281 282 } 283 }