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