make/langtools/tools/propertiesparser/parser/Message.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 java.util.ArrayList;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    29
import java.util.List;
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
/**
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    32
 * A message within the message file.
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    33
 * A message is a series of lines containing a "name=value" property,
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    34
 * optionally preceded by a comment describing the use of placeholders
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    35
 * such as {0}, {1}, etc within the property value.
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
public final class Message {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    38
    final MessageLine firstLine;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    39
    private MessageInfo messageInfo;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    40
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    41
    Message(MessageLine l) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    42
        firstLine = l;
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
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    45
    /**
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    46
     * Get the Info object for this message. It may be empty if there
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    47
     * if no comment preceding the property specification.
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    48
     */
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    49
    public MessageInfo getMessageInfo() {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    50
        if (messageInfo == null) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    51
            MessageLine l = firstLine.prev;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    52
            if (l != null && l.isInfo())
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    53
                messageInfo = new MessageInfo(l.text);
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    54
            else
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    55
                messageInfo = MessageInfo.dummyInfo;
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
        return messageInfo;
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
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
     * Get all the lines pertaining to this message.
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
    public List<MessageLine> getLines(boolean includeAllPrecedingComments) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    64
        List<MessageLine> lines = new ArrayList<>();
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    65
        MessageLine l = firstLine;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    66
        if (includeAllPrecedingComments) {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    67
            // scan back to find end of prev message
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    68
            while (l.prev != null && l.prev.isEmptyOrComment())
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    69
                l = l.prev;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    70
            // skip leading blank lines
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    71
            while (l.text.isEmpty())
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    72
                l = l.next;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    73
        } else {
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    74
            if (l.prev != null && l.prev.isInfo())
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    75
                l = l.prev;
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
        // include any preceding lines
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    79
        for ( ; l != firstLine; l = l.next)
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    80
            lines.add(l);
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
        // include message lines
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    83
        for (l = firstLine; l != null && l.hasContinuation(); l = l.next)
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    84
            lines.add(l);
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    85
        lines.add(l);
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    86
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    87
        // include trailing blank line if present
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    88
        l = l.next;
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    89
        if (l != null && l.text.isEmpty())
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff changeset
    90
            lines.add(l);
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
        return lines;
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
}