langtools/make/tools/propertiesparser/parser/MessageInfo.java
author vasya
Mon, 14 Dec 2015 20:18:19 +0100
changeset 34752 9c262a013456
parent 28334 1633de6070ae
permissions -rw-r--r--
8145342: Some copyright notices are inconsistently and ill formatted Summary: Adjusted formatting of copyright notices. Reviewed-by: darcy, alundblad
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.
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
     8
 *
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
     9
 * 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
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    12
 * 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
    13
 * accompanied this code).
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    14
 *
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    15
 * 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
    16
 * 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
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    18
 *
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    20
 * 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
    21
 * questions.
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    22
 */
34752
9c262a013456 8145342: Some copyright notices are inconsistently and ill formatted
vasya
parents: 28334
diff changeset
    23
28334
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    24
package propertiesparser.parser;
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
import propertiesparser.parser.MessageType.CompoundType;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    27
import propertiesparser.parser.MessageType.OrType;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    28
import propertiesparser.parser.MessageType.SimpleType;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    29
import propertiesparser.parser.MessageType.UnionType;
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.util.ArrayList;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    32
import java.util.List;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    33
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    34
/**
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    35
 * An object to represent the comment that may precede the property
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    36
 * specification in a Message.
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    37
 * The comment is modelled as a list of fields, where the fields correspond
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    38
 * to the placeholder values (e.g. {0}, {1}, etc) within the message value.
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    39
 */
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    40
public final class MessageInfo {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    41
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    42
    /** The fields of the Info object. */
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    43
    List<MessageType> types = new ArrayList<>();
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    44
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    45
    MessageInfo(String text) throws IllegalArgumentException {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    46
        if (text != null) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    47
            if (!text.startsWith("# "))
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    48
                throw new IllegalArgumentException();
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    49
            String[] segs = text.substring(2).split(", ");
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    50
            types = new ArrayList<>();
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    51
            for (String seg : segs) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    52
                types.add(parseType(seg));
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    53
            }
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
    }
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
    public List<MessageType> getTypes() {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    58
        return types;
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
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    61
    boolean isEmpty() {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    62
        return types.isEmpty();
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    63
    }
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
    @Override
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    66
    public String toString() {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    67
        return types.toString();
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    68
    }
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    69
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    70
    /**
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    71
     * Split the type comment into multiple alternatives (separated by 'or') - then parse each of them
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    72
     * individually and form an 'or' type.
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
    MessageType parseType(String text) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    75
        int commentStart = text.indexOf("(");
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    76
        if (commentStart != -1) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    77
            //remove optional comment
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    78
            text = text.substring(0, commentStart);
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    79
        }
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    80
        text = text.substring(text.indexOf(": ") + 2);
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    81
        String[] alternatives = text.split(" " + OrType.OR_NAME + " ");
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    82
        MessageType[] types = new MessageType[alternatives.length];
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    83
        for (int i = 0 ; i < alternatives.length ; i++) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    84
            types[i] = parseAlternative(alternatives[i].trim());
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    85
        }
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    86
        return types.length > 1 ?
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    87
                new OrType(types) : types[0];
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    88
    }
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    89
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    90
    /**
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    91
     * Parse a subset of the type comment; valid matches are simple types, compound types,
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    92
     * union types and custom types.
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    93
     */
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    94
    MessageType parseAlternative(String text) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    95
        //try with custom types
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    96
        if (text.charAt(0) == '\'') {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    97
            int end = text.indexOf('\'', 1);
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    98
            return new MessageType.CustomType(text.substring(1, end));
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    99
        }
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   100
        //try with simple types
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   101
        for (SimpleType st : SimpleType.values()) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   102
            if (text.equals(st.kindName())) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   103
                return st;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   104
            }
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
        //try with compound types
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   107
        for (CompoundType.Kind ck : CompoundType.Kind.values()) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   108
            if (text.startsWith(ck.kindName)) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   109
                MessageType elemtype = parseAlternative(text.substring(ck.kindName.length() + 1).trim());
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   110
                return new CompoundType(ck, elemtype);
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   111
            }
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   112
        }
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   113
        //try with union types
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   114
        for (UnionType.Kind uk : UnionType.Kind.values()) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   115
            if (text.startsWith(uk.kindName)) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   116
                return new UnionType(uk);
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   117
            }
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
        //no match - report a warning
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   120
        System.err.println("WARNING - unrecognized type: " + text);
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   121
        return SimpleType.UNKNOWN;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   122
    }
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   123
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   124
    /** Dummy message info to be used when no resource key comment is available. */
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   125
    static final MessageInfo dummyInfo = new MessageInfo(null);
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   126
}