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