1 /*
2  * Copyright (C) 2004-2011, Unicode, Inc., Google, Inc., and others.
3  * For terms of use, see http://www.unicode.org/terms_of_use.html
4  */
5 
6 package org.unicode.cldr.tool.resolver;
7 
8 import java.util.HashSet;
9 import java.util.Set;
10 
11 import org.unicode.cldr.util.CLDRFile;
12 
13 /**
14  * Utility methods for the CLDR Resolver tool
15  *
16  * @author ryanmentley@google.com (Ryan Mentley)
17  */
18 public class ResolverUtils {
19     /**
20      * Output level from 0-5. 0 is nothing, 1 is errors, 2-3 is pretty sane, 5
21      * will flood your terminal.
22      */
23     static int verbosity = 2;
24 
25     /**
26      * This is a static class and should never be instantiated
27      */
28     private ResolverUtils() {
29     }
30 
31     /**
32      * Get the set of paths with non-null values from a CLDR file (including all
33      * extra paths).
34      *
35      * @param file the CLDRFile from which to extract paths
36      * @return a Set containing all the paths returned by
37      *         {@link CLDRFile#iterator()}, plus those from
38      *         {@link CLDRFile#getExtraPaths(java.util.Collection)}
39      */
40     public static Set<String> getAllPaths(CLDRFile file) {
41         String locale = file.getLocaleID();
42         Set<String> paths = new HashSet<>();
43         for (String path : file) {
44             paths.add(path);
45         }
46         for (String path : file.getExtraPaths()) {
47             if (file.getStringValue(path) != null) {
48                 paths.add(path);
49             } else {
50                 debugPrintln(path + " is null in " + locale + ".", 3);
51             }
52         }
53         return paths;
54     }
55 
56     /**
57      * Debugging method used to make null and empty strings more obvious in
58      * printouts
59      *
60      * @param str the string
61      * @return "[null]" if str==null, "[empty]" if str is the empty string, str
62      *         otherwise
63      */
64     public static String strRep(String str) {
65         if (str == null) {
66             return "[null]";
67         } else if (str.isEmpty()) {
68             return "[empty]";
69         } else {
70             return str;
71         }
72     }
73 
74     /**
75      * Debugging method to print things based on verbosity.
76      *
77      * @param str The string to print
78      * @param msgVerbosity The minimum verbosity level at which to print this message
79      */
80     static void debugPrint(String str, int msgVerbosity) {
81         if (verbosity >= msgVerbosity) {
82             System.out.print(str);
83         }
84     }
85 
86     /**
87      * Debugging method to print things based on verbosity.
88      *
89      * @param str The string to print
90      * @param msgVerbosity The minimum verbosity level at which to print this message
91      */
92     static void debugPrintln(String str, int msgVerbosity) {
93         debugPrint(str + "\n", msgVerbosity);
94     }
95 }
96