File: LocaleString.java

package info (click to toggle)
gprofng-gui 1%3A2.2-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 7,128 kB
  • sloc: java: 95,861; sh: 4,036; makefile: 683; perl: 345
file content (310 lines) | stat: -rw-r--r-- 11,007 bytes parent folder | download | duplicates (2)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* Copyright (C) 2022-2025 Free Software Foundation

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses>.  */

// stdin:	source files
// stdout:	properties file
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.MessageFormat;
import java.text.ParsePosition;
import java.util.Arrays;
import java.util.Vector;

public final class LocaleString {

    private final static MessageFormat mf1 = new MessageFormat("({0}){1}"); // NOI18N
    private final static MessageFormat mf2 = new MessageFormat("{0}){1}"); // NOI18N
    private final static MessageFormat mf3 = new MessageFormat("\''{0}\'',{1}"); // NOI18N
    private final static MessageFormat mf4 = new MessageFormat("\"{0}\",{1}"); // NOI18N
    private final static ParsePosition parsePos = new ParsePosition(0);
    private final static String key = "AnLocale.getString"; // NOI18N
    private final static int key_len = key.length();
    private final static String key1 = "getLocaleStr"; // NOI18N
    private final static int key1_len = key1.length();

    public static void main(String[] args) {
        int index, index1;
        Elem strs;

        // Open stdin as BufferedReader
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String line = null;
        Vector<Elem> values = new Vector<Elem>();
        Vector<Elem> mnem_values = new Vector<Elem>();

        try {
            for (;;) {
                if ((line == null) && ((line = in.readLine()) == null)) {
                    break;
                }

                // Search keyword, the Locale get string method
                index = line.indexOf(key);
                index1 = line.indexOf(key1);
                if ((line.indexOf("getLocaleStr(String") > 0) || // NOI18N
                        (line.indexOf("AnLocale.getString(str") > 0) || // NOI18N
                        (line.indexOf("getLocaleStr(char") > 0) || // NOI18N
                        (line.indexOf("AnLocale.getString(c") > 0)) {   // NOI18N
                    line = null;
                    continue;
                }

                if ((index == -1) && (index1 == -1)) {
                    line = null;
                    continue;
                }
                // Parse for the string argument
                for (;;) {
                    if (index != -1) {
                        strs = parse(line.substring(index + key_len).trim());
                    } else {
                        strs = parse(line.substring(index1 + key1_len).trim());
                    }
                    if (strs != null) {
                        if (strs.isMnemonic()) {
                            mnem_values.addElement(strs);
                        } else {
                            values.addElement(strs);
                        }
                        line = strs.getLeftStr();
                        break;
                    }

                    line += in.readLine();
                }
            }
        } catch (Exception e) {
            System.err.println(e);
            System.exit(1);
        }

        // Sort & Output result properties
        values = sort(values);
        mnem_values = sort(mnem_values);

        Elem elem;
        for (index = 0; index < values.size(); index++) {
            elem = values.get(index);
            if (elem.getKey() != null) {
                System.out.println(setEscape(elem.getValue()) + "[" + elem.getKey() + "]" + "=" + // NOI18N
                        (elem.getValue().startsWith(" ") ? "\\" : "") + elem.getValue()); // NOI18N
            } else {
                System.out.println(setEscape(elem.getValue()) + "=" + // NOI18N
                        (elem.getValue().startsWith(" ") ? "\\" : "") + elem.getValue()); // NOI18N
            }
        }

        if (mnem_values.size() > 0) {
            System.out.println("\n# MNEMONICS"); // NOI18N
            System.out.println("# =================================================================================="); // NOI18N
            System.out.println("#"); // NOI18N

            for (index = 0; index < mnem_values.size(); index++) {
                elem = mnem_values.get(index);
                System.out.println(elem.getKey() + "=" + // NOI18N
                        (elem.getValue().startsWith(" ") ? "\\" : "") + elem.getValue()); // NOI18N
            }
        }

    }
    private final static Object syncFormat = new Object();

    // Parser for getting the string argument
    private static Elem parse(String str) {
        MessageFormat mf;
        String ans, value, check;
        Object[] result;
        Elem elem;

        mf = mf1;
        ans = ""; // NOI18N

        for (;;) {
            parsePos.setIndex(0);
            result = mf.parse(str, parsePos);
            mf = mf2;
            if (result != null) {
                value = (String) result[0];
                str = (String) result[1];
                ans += value;
                check = value.trim();
                // If ends with ", then done
                if (check.endsWith("\"") && !check.endsWith("\\\"")) { // NOI18N
                    value = ans.trim();
                    parsePos.setIndex(0);
                    synchronized (syncFormat) { // begin critical section: format is not thread safe
                        result = mf3.parse(value, parsePos);
                        String key = null;
                        boolean is_mnemonic = false;
                        if (result != null) {
                            value = ((String) result[0]).trim();
                            key = ((String) result[1]).trim();
                            is_mnemonic = true;
                        } else {
                            result = mf4.parse(value, parsePos);
                            if (result != null) {
                                value = ((String) result[0]).trim();
                                key = ((String) result[1]).trim();
                            }
                        }
                        elem = new Elem(key, value, str, is_mnemonic);
                    } // end of critical section
                    return elem.removeQuotes();
                } else {
                    ans += ")"; // NOI18N
                }
            } else {
                return null;
            }
        }
    }

    // Sort the string list
    private static Vector<Elem> sort(final Vector<Elem> vec) {
        final int size;
        int index;

        // Convert results to Object[] & Sort
        size = vec.size();
        final Object[] strs = vec.toArray();
        Arrays.sort(strs);

        // Remove Repeated elements
        Elem elem = null;
        Vector<Elem> result = new Vector<Elem>();

        for (index = 0; index < size; index++) {
            if (((Elem) strs[index]).equals(elem)) {
                continue;
            }
            elem = (Elem) strs[index];
            // Remove the "" from the both ends
            result.addElement(new Elem(elem));
        }
        return result;
    }

    // Add escape char '\' for ' ' & ':'
    private static String setEscape(final String str) {
        String esc;
        final int size;
        int index;
        char c;

        esc = ""; // NOI18N
        size = str.length();
        for (index = 0; index < size; index++) {
            c = str.charAt(index);
            if ((c == ' ') || (c == ':') || (c == '=')) // NOI18N
            {
                esc += '\\'; // NOI18N
            }
            esc += c;
        }

        return esc;
    }

    /* Class which contains a parsed properties value */
    private static final class Elem implements Comparable<Elem> {

        private String key;   // optional bundle key
        private String value; // required properties value
        private String left;  // residual string
        private boolean is_mnemonic;  // element is mnemonic

        // Constructor
        public Elem(final String key, final String value, final String left, final boolean is_mnemonic) {
            this.key = key;
            this.value = value;
            this.left = left;
            this.is_mnemonic = is_mnemonic;
        }

        // Copy constructor
        public Elem(final Elem elem) {
            this.key = elem.key;
            this.value = elem.value;
            this.is_mnemonic = elem.is_mnemonic;
        }

        public String getLeftStr() {
            return left;
        }

        public String getKey() {
            return key;
        }

        public boolean isMnemonic() {
            return is_mnemonic;
        }

        public String getValue() {
            return value;
        }

        // Comparator implementation method
        @Override
        public int compareTo(final Elem obj) {
            final String obj_key = obj.key;
            final String obj_value = obj.value;
            final boolean obj_mnem = obj.is_mnemonic;

            if (key != null && obj_key != null) {
                if (is_mnemonic && obj_mnem) {
                    return key.compareTo(obj_key);
                } else {
                    return (value + key).compareTo(obj_value + obj_key);
                }
            } else {
                return value.compareTo(obj_value);
            }
        }

        public String toString() {
            return (key == null ? value : key) + "=" + value; // NOI18N
        }

        protected Elem removeQuotes() {
            if (key != null) {
                if (key.charAt(0) == '\"' || key.charAt(0) == '\'') { // NOI18N
                    key = key.substring(1, key.length() - 1);
                }
            }
            if (value != null) {
                if (value.charAt(0) == '\"' || value.charAt(0) == '\'') { // NOI18N
                    value = value.substring(1, value.length() - 1);
                }
            }
            return this;
        }

        public boolean equals(final Elem e) {
            if (e != null) {
                if (!this.is_mnemonic && !e.is_mnemonic) {
                    if (this.key != null && e.key != null) {
                        return this.compareTo(e) == 0;
                    }
                    if (this.key != null || e.key != null) {
                        return false;
                    }
                }
                return this.compareTo(e) == 0;
            }
            return false;
        }
    }
}