jdk

Форк
0
/
PropertiesParser.java 
118 строк · 3.9 Кб
1
/*
2
 * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
 *
5
 * This code is free software; you can redistribute it and/or modify it
6
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.  Oracle designates this
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
10
 *
11
 * This code is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14
 * version 2 for more details (a copy is included in the LICENSE file that
15
 * accompanied this code).
16
 *
17
 * You should have received a copy of the GNU General Public License version
18
 * 2 along with this work; if not, write to the Free Software Foundation,
19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
 *
21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
 * or visit www.oracle.com if you need additional information or have any
23
 * questions.
24
 */
25

26
package propertiesparser;
27

28
import propertiesparser.parser.MessageFile;
29
import propertiesparser.gen.ClassGenerator;
30

31
import java.io.File;
32
import java.io.PrintStream;
33
import java.util.TreeMap;
34
import java.util.Map;
35

36
/** Translates a .properties file into a .java file containing an enum-like Java class
37
 *  which defines static factory methods for all resource keys in a given resource file. <P>
38
 *
39
 *  Usage: java PropertiesParser -compile [path to .properties file] [output folder where .java file will be written]
40
 *
41
 * @author mcimadamore
42
 */
43

44
public class PropertiesParser {
45

46
    public Logger logger;
47

48
    public PropertiesParser(Logger logger) {
49
        this.logger = logger;
50
    }
51

52
    public static void main(String[] args) {
53
        boolean ok = run(args, System.out);
54
        if ( !ok ) {
55
            System.exit(1);
56
        }
57
    }
58

59
    public static boolean run(String[] args, PrintStream out) {
60
        PropertiesParser pp = new PropertiesParser(out::println);
61
        return pp.run(args);
62
    }
63

64
    public static interface Logger {
65
        void info(String msg);
66
    }
67

68
    public void info(String msg) {
69
        logger.info(msg);
70
    }
71

72
    public boolean run(String[] args) {
73
        Map<String, String> optionsMap = parseOptions(args);
74
        if (optionsMap.isEmpty()) {
75
            usage();
76
            return false;
77
        }
78
        try {
79
            optionsMap.forEach(this::compilePropertyFile);
80
            return true;
81
        } catch (RuntimeException ex) {
82
            ex.printStackTrace();
83
            return false;
84
        }
85
    }
86

87
    private void compilePropertyFile(String propertyPath, String outPath) {
88
        try {
89
            File propertyFile = new File(propertyPath);
90
            String prefix = propertyFile.getName().split("\\.")[0];
91
            MessageFile messageFile = new MessageFile(propertyFile, prefix);
92
            new ClassGenerator().generateFactory(messageFile, new File(outPath));
93
        } catch (Throwable ex) {
94
            throw new RuntimeException(ex);
95
        }
96
    }
97

98
    private Map<String, String> parseOptions(String args[]) {
99
        /* Use TreeMap to guarantee stable forEach iteration */
100
        Map<String, String> optionsMap = new TreeMap<>();
101
        for ( int i = 0; i < args.length ; i++ ) {
102
            if ( "-compile".equals(args[i]) && i+2 < args.length ) {
103
                optionsMap.put(args[++i], args[++i]);
104
            } else {
105
                return new TreeMap<>();
106
            }
107
        }
108
        return optionsMap;
109
    }
110

111
    private void usage() {
112
        info("usage:");
113
        info("    java PropertiesParser {-compile path_to_properties_file path_to_java_output_dir}");
114
        info("");
115
        info("Example:");
116
        info("    java PropertiesParser -compile resources/test.properties resources");
117
    }
118
}
119

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.