make/langtools/tools/propertiesparser/parser/MessageInfo.java
author herrick
Thu, 28 Mar 2019 13:49:38 -0400
branchJDK-8200758-branch
changeset 57292 7a683c461b80
parent 49158 f62d1d1c2d9c
permissions -rw-r--r--
Merge
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
49158
f62d1d1c2d9c 8194296: Check copyright of files in make/langtools/tools
jjg
parents: 47216
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
f62d1d1c2d9c 8194296: Check copyright of files in make/langtools/tools
jjg
parents: 47216
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
f62d1d1c2d9c 8194296: Check copyright of files in make/langtools/tools
jjg
parents: 47216
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
28334
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
 */
34752
9c262a013456 8145342: Some copyright notices are inconsistently and ill formatted
vasya
parents: 28334
diff changeset
    25
28334
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    26
package propertiesparser.parser;
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.MessageType.CompoundType;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    29
import propertiesparser.parser.MessageType.OrType;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    30
import propertiesparser.parser.MessageType.SimpleType;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    31
import propertiesparser.parser.MessageType.UnionType;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    32
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    33
import java.util.ArrayList;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    34
import java.util.List;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    35
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    36
/**
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    37
 * An object to represent the comment that may precede the property
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    38
 * specification in a Message.
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    39
 * 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
    40
 * 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
    41
 */
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    42
public final class MessageInfo {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    43
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    44
    /** The fields of the Info object. */
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    45
    List<MessageType> types = new ArrayList<>();
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    46
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    47
    MessageInfo(String text) throws IllegalArgumentException {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    48
        if (text != null) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    49
            if (!text.startsWith("# "))
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    50
                throw new IllegalArgumentException();
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    51
            String[] segs = text.substring(2).split(", ");
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    52
            types = new ArrayList<>();
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    53
            for (String seg : segs) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    54
                types.add(parseType(seg));
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
    }
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    58
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    59
    public List<MessageType> getTypes() {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    60
        return types;
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
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    63
    boolean isEmpty() {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    64
        return types.isEmpty();
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
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    67
    @Override
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    68
    public String toString() {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    69
        return types.toString();
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
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
     * 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
    74
     * individually and form an 'or' type.
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    75
     */
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    76
    MessageType parseType(String text) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    77
        int commentStart = text.indexOf("(");
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    78
        if (commentStart != -1) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    79
            //remove optional comment
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    80
            text = text.substring(0, commentStart);
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
        text = text.substring(text.indexOf(": ") + 2);
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    83
        String[] alternatives = text.split(" " + OrType.OR_NAME + " ");
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    84
        MessageType[] types = new MessageType[alternatives.length];
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    85
        for (int i = 0 ; i < alternatives.length ; i++) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    86
            types[i] = parseAlternative(alternatives[i].trim());
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
        return types.length > 1 ?
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    89
                new OrType(types) : types[0];
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
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    92
    /**
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    93
     * 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
    94
     * union types and custom types.
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
    MessageType parseAlternative(String text) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    97
        //try with custom types
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    98
        if (text.charAt(0) == '\'') {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    99
            int end = text.indexOf('\'', 1);
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   100
            return new MessageType.CustomType(text.substring(1, end));
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   101
        }
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   102
        //try with simple types
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   103
        for (SimpleType st : SimpleType.values()) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   104
            if (text.equals(st.kindName())) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   105
                return st;
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
        //try with compound types
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   109
        for (CompoundType.Kind ck : CompoundType.Kind.values()) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   110
            if (text.startsWith(ck.kindName)) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   111
                MessageType elemtype = parseAlternative(text.substring(ck.kindName.length() + 1).trim());
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   112
                return new CompoundType(ck, elemtype);
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   113
            }
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   114
        }
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   115
        //try with union types
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   116
        for (UnionType.Kind uk : UnionType.Kind.values()) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   117
            if (text.startsWith(uk.kindName)) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   118
                return new UnionType(uk);
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
        }
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   121
        //no match - report a warning
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   122
        System.err.println("WARNING - unrecognized type: " + text);
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   123
        return SimpleType.UNKNOWN;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   124
    }
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   125
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   126
    /** 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
   127
    static final MessageInfo dummyInfo = new MessageInfo(null);
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
   128
}