langtools/make/tools/propertiesparser/PropertiesParser.java
author lana
Fri, 11 Sep 2015 10:26:27 -0700
changeset 32459 c52f6fe8ecb6
parent 28334 1633de6070ae
child 37850 24e70308f4ea
permissions -rw-r--r--
Added tag jdk9-b81 for changeset 13c2ca2ea10a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28334
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
     1
/*
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
     4
 *
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    10
 *
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    15
 * accompanied this code).
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    16
 *
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    20
 *
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    23
 * questions.
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    24
 */
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    25
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    26
package propertiesparser;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    27
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    28
import propertiesparser.parser.MessageFile;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    29
import propertiesparser.gen.ClassGenerator;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    30
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    31
import java.io.BufferedWriter;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    32
import java.io.File;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    33
import java.io.FileInputStream;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    34
import java.io.FileNotFoundException;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    35
import java.io.FileOutputStream;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    36
import java.io.IOException;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    37
import java.io.OutputStreamWriter;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    38
import java.io.Writer;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    39
import java.lang.RuntimeException;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    40
import java.lang.Throwable;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    41
import java.text.MessageFormat;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    42
import java.util.ArrayList;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    43
import java.util.Collections;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    44
import java.util.HashMap;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    45
import java.util.Iterator;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    46
import java.util.List;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    47
import java.util.Map;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    48
import java.util.Properties;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    49
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    50
/** Translates a .properties file into a .java file containing an enum-like Java class
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    51
 *  which defines static factory methods for all resource keys in a given resource file. <P>
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    52
 *
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    53
 *  Usage: java PropertiesParser -compile [path to .properties file] [output folder where .java file will be written]
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    54
 *
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    55
 * @author mcimadamore
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    56
 */
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    57
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    58
public class PropertiesParser {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    59
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    60
    public Logger logger;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    61
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    62
    public PropertiesParser(Logger logger) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    63
        this.logger = logger;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    64
    }
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    65
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    66
    public static void main(String[] args) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    67
        PropertiesParser pp = new PropertiesParser(msg -> System.out.println(msg));
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    68
        boolean ok = pp.run(args);
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    69
        if ( !ok ) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    70
            System.exit(1);
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    71
        }
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    72
    }
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    73
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    74
    public static interface Logger {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    75
        void info(String msg);
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    76
    }
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    77
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    78
    public void info(String msg) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    79
        logger.info(msg);
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    80
    }
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    81
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    82
    public boolean run(String[] args) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    83
        Map<String, String> optionsMap = parseOptions(args);
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    84
        if (optionsMap.isEmpty()) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    85
            usage();
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    86
            return false;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    87
        }
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    88
        try {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    89
            optionsMap.forEach((propfile, outfile) -> compilePropertyFile(propfile, outfile));
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    90
            return true;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    91
        } catch (RuntimeException ex) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    92
            ex.printStackTrace();
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    93
            return false;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    94
        }
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    95
    }
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    96
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    97
    private void compilePropertyFile(String propertyPath, String outPath) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    98
        try {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    99
            File propertyFile = new File(propertyPath);
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   100
            String prefix = propertyFile.getName().split("\\.")[0];
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   101
            MessageFile messageFile = new MessageFile(propertyFile, prefix);
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   102
            new ClassGenerator().generateFactory(messageFile, new File(outPath));
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   103
        } catch (Throwable ex) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   104
            throw new RuntimeException(ex);
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   105
        }
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   106
    }
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   107
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   108
    private Map<String, String> parseOptions(String args[]) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   109
        Map<String, String> optionsMap = new HashMap<>(args.length);
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   110
        for ( int i = 0; i < args.length ; i++ ) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   111
            if ( "-compile".equals(args[i]) && i+2 < args.length ) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   112
                optionsMap.put(args[++i], args[++i]);
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   113
            } else {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   114
                return new HashMap<>();
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   115
            }
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   116
        }
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   117
        return optionsMap;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   118
    }
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   119
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   120
    private void usage() {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   121
        info("usage:");
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   122
        info("    java PropertiesParser {-compile path_to_properties_file path_to_java_output_dir}");
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   123
        info("");
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   124
        info("Example:");
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   125
        info("    java PropertiesParser -compile resources/test.properties resources");
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   126
    }
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   127
}