aboutsummaryrefslogtreecommitdiffstats
path: root/tools/java/org/unicode/cldr/util/PrettyPath.java
blob: 5ba950f119acc205da3be0aa7e01bd10b6cfbeac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 **********************************************************************
 * Copyright (c) 2002-2004, International Business Machines
 * Corporation and others.  All Rights Reserved.
 **********************************************************************
 * Author: Mark Davis
 **********************************************************************
 */
package org.unicode.cldr.util;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;

import org.unicode.cldr.test.CheckCLDR;

import com.ibm.icu.text.Transliterator;

/**
 * @deprecated
 */
public class PrettyPath {
    private Transliterator prettyPathZoneTransform;
    {
        prettyPathZoneTransform = CheckCLDR.getTransliteratorFromFile("prettyPathZone", "prettyPathZone.txt");
        Transliterator.registerInstance(prettyPathZoneTransform);
    }
    private Transliterator prettyPathTransform = CheckCLDR.getTransliteratorFromFile("ID", "prettyPath.txt");

    private Map<String, String> prettyPath_path = new HashMap<String, String>();
    private Map<String, String> path_prettyPath_sortable = new HashMap<String, String>();
    private boolean showErrors;

    /**
     * Gets sortable form of the pretty path, and caches the mapping for faster later mapping; see the two argument
     * form.
     *
     * @param path
     * @return pretty path
     */
    public String getPrettyPath(String path) {
        return getPrettyPath(path, true);
    }

    /**
     * Gets the pretty path, and caches the mapping for faster later mapping. If you use the sortable form, then later
     * you will want to call getOutputForm.
     *
     * @param path
     * @param sortable
     *            true if you want the sortable form
     * @return pretty path
     */
    public String getPrettyPath(String path, boolean sortable) {
        String prettyString = (String) path_prettyPath_sortable.get(path);
        if (path_prettyPath_sortable.get(path) == null) {
            prettyString = prettyPathTransform.transliterate(path);
            // some internal errors, shown here for debugging for now.
            // later make exceptions.
            if (prettyString.indexOf("%%") >= 0) {
                if (showErrors) System.out.println("Warning:\tIncomplete translit:\t" + prettyString + "\t " + path);

            } else if (CldrUtility.countInstances(prettyString, "|") != 2) {
                if (showErrors) System.out.println("Warning:\tpath length != 3: " + prettyString);
            }
            // add to caches
            path_prettyPath_sortable.put(path, prettyString);
            // String prettyNonSortable = sortingGorpRemoval.reset(prettyString).replaceAll("");
            // if (prettyNonSortable.equals(prettyString)) {
            // path_prettyPath.put(path, prettyString);
            // } else {
            // path_prettyPath.put(path, prettyNonSortable);
            // addBackmap(prettyNonSortable, path, prettyPath_path);
            // }
            addBackmap(prettyString, path, prettyPath_path);
        }
        if (!sortable) return getOutputForm(prettyString);
        return prettyString;
    }

    private void addBackmap(String prettyString, String path, Map<String, String> prettyPath_path_map) {
        String old = (String) prettyPath_path_map.get(prettyString);
        if (old != null) {
            if (showErrors) System.out.println("Warning:\tFailed bijection, " + prettyString);
            if (showErrors) System.out.println("Warning:\tPath1: " + path);
            if (showErrors) System.out.println("Warning:\tPath2: " + old);
        } else {
            prettyPath_path_map.put(prettyString, path); // bijection
        }
    }

    /**
     * Get original path. ONLY works if getPrettyPath was called with the original!
     *
     * @param prettyPath
     * @return
     */
    public String getOriginal(String prettyPath) {
        return (String) prettyPath_path.get(prettyPath);
    }

    /**
     * Return the pretty path with the sorting gorp removed. This is the form that should be displayed to the user.
     *
     * @param prettyPath
     * @return cleaned pretty path
     */
    public String getOutputForm(String prettyPath) {
        try {
            return sortingGorpRemoval.reset(prettyPath).replaceAll("");
        } catch (Exception e) {
            return prettyPath;
        }
    }

    private static Matcher sortingGorpRemoval = PatternCache.get("(?<=(^|[|]))([0-9]+-)?").matcher("");

    public boolean isShowErrors() {
        return showErrors;
    }

    public PrettyPath setShowErrors(boolean showErrors) {
        this.showErrors = showErrors;
        return this;
    }
}