1 /* 2 ********************************************************************** 3 * Copyright (c) 2002-2004, International Business Machines 4 * Corporation and others. All Rights Reserved. 5 ********************************************************************** 6 * Author: Mark Davis 7 ********************************************************************** 8 */ 9 package org.unicode.cldr.util; 10 11 import java.util.HashMap; 12 import java.util.Map; 13 import java.util.regex.Matcher; 14 15 import org.unicode.cldr.test.CheckCLDR; 16 17 import com.ibm.icu.text.Transliterator; 18 19 /** 20 * @deprecated 21 * TODO: what is supposed to replace PrettyPath? If no replacement is planned, make it no longer deprecated? 22 */ 23 @Deprecated 24 public class PrettyPath { 25 private Transliterator prettyPathZoneTransform; 26 { 27 prettyPathZoneTransform = CheckCLDR.getTransliteratorFromFile("prettyPathZone", "prettyPathZone.txt"); 28 Transliterator.registerInstance(prettyPathZoneTransform); 29 } 30 private Transliterator prettyPathTransform = CheckCLDR.getTransliteratorFromFile("ID", "prettyPath.txt"); 31 32 private Map<String, String> prettyPath_path = new HashMap<>(); 33 private Map<String, String> path_prettyPath_sortable = new HashMap<>(); 34 private boolean showErrors; 35 36 /** 37 * Gets sortable form of the pretty path, and caches the mapping for faster later mapping; see the two argument 38 * form. 39 * 40 * @param path 41 * @return pretty path 42 */ getPrettyPath(String path)43 public String getPrettyPath(String path) { 44 return getPrettyPath(path, true); 45 } 46 47 /** 48 * Gets the pretty path, and caches the mapping for faster later mapping. If you use the sortable form, then later 49 * you will want to call getOutputForm. 50 * 51 * @param path 52 * @param sortable 53 * true if you want the sortable form 54 * @return pretty path 55 */ getPrettyPath(String path, boolean sortable)56 public String getPrettyPath(String path, boolean sortable) { 57 String prettyString = path_prettyPath_sortable.get(path); 58 if (path_prettyPath_sortable.get(path) == null) { 59 prettyString = prettyPathTransform.transliterate(path); 60 // some internal errors, shown here for debugging for now. 61 // later make exceptions. 62 if (prettyString.indexOf("%%") >= 0) { 63 if (showErrors) System.out.println("Warning:\tIncomplete translit:\t" + prettyString + "\t " + path); 64 65 } else if (CldrUtility.countInstances(prettyString, "|") != 2) { 66 if (showErrors) System.out.println("Warning:\tpath length != 3: " + prettyString); 67 } 68 // add to caches 69 path_prettyPath_sortable.put(path, prettyString); 70 // String prettyNonSortable = sortingGorpRemoval.reset(prettyString).replaceAll(""); 71 // if (prettyNonSortable.equals(prettyString)) { 72 // path_prettyPath.put(path, prettyString); 73 // } else { 74 // path_prettyPath.put(path, prettyNonSortable); 75 // addBackmap(prettyNonSortable, path, prettyPath_path); 76 // } 77 addBackmap(prettyString, path, prettyPath_path); 78 } 79 if (!sortable) return getOutputForm(prettyString); 80 return prettyString; 81 } 82 addBackmap(String prettyString, String path, Map<String, String> prettyPath_path_map)83 private void addBackmap(String prettyString, String path, Map<String, String> prettyPath_path_map) { 84 String old = prettyPath_path_map.get(prettyString); 85 if (old != null) { 86 if (showErrors) System.out.println("Warning:\tFailed bijection, " + prettyString); 87 if (showErrors) System.out.println("Warning:\tPath1: " + path); 88 if (showErrors) System.out.println("Warning:\tPath2: " + old); 89 } else { 90 prettyPath_path_map.put(prettyString, path); // bijection 91 } 92 } 93 94 /** 95 * Get original path. ONLY works if getPrettyPath was called with the original! 96 * 97 * @param prettyPath 98 * @return 99 */ getOriginal(String prettyPath)100 public String getOriginal(String prettyPath) { 101 return prettyPath_path.get(prettyPath); 102 } 103 104 /** 105 * Return the pretty path with the sorting gorp removed. This is the form that should be displayed to the user. 106 * 107 * @param prettyPath 108 * @return cleaned pretty path 109 */ getOutputForm(String prettyPath)110 public String getOutputForm(String prettyPath) { 111 try { 112 return sortingGorpRemoval.reset(prettyPath).replaceAll(""); 113 } catch (Exception e) { 114 return prettyPath; 115 } 116 } 117 118 private static Matcher sortingGorpRemoval = PatternCache.get("(?<=(^|[|]))([0-9]+-)?").matcher(""); 119 isShowErrors()120 public boolean isShowErrors() { 121 return showErrors; 122 } 123 setShowErrors(boolean showErrors)124 public PrettyPath setShowErrors(boolean showErrors) { 125 this.showErrors = showErrors; 126 return this; 127 } 128 }