1/*
2 * Copyright (c) 2014, 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24package propertiesparser.parser;
25
26import java.util.ArrayList;
27import java.util.List;
28
29/**
30 * A message within the message file.
31 * A message is a series of lines containing a "name=value" property,
32 * optionally preceded by a comment describing the use of placeholders
33 * such as {0}, {1}, etc within the property value.
34 */
35public final class Message {
36    final MessageLine firstLine;
37    private MessageInfo messageInfo;
38
39    Message(MessageLine l) {
40        firstLine = l;
41    }
42
43    /**
44     * Get the Info object for this message. It may be empty if there
45     * if no comment preceding the property specification.
46     */
47    public MessageInfo getMessageInfo() {
48        if (messageInfo == null) {
49            MessageLine l = firstLine.prev;
50            if (l != null && l.isInfo())
51                messageInfo = new MessageInfo(l.text);
52            else
53                messageInfo = MessageInfo.dummyInfo;
54        }
55        return messageInfo;
56    }
57
58    /**
59     * Get all the lines pertaining to this message.
60     */
61    public List<MessageLine> getLines(boolean includeAllPrecedingComments) {
62        List<MessageLine> lines = new ArrayList<>();
63        MessageLine l = firstLine;
64        if (includeAllPrecedingComments) {
65            // scan back to find end of prev message
66            while (l.prev != null && l.prev.isEmptyOrComment())
67                l = l.prev;
68            // skip leading blank lines
69            while (l.text.isEmpty())
70                l = l.next;
71        } else {
72            if (l.prev != null && l.prev.isInfo())
73                l = l.prev;
74        }
75
76        // include any preceding lines
77        for ( ; l != firstLine; l = l.next)
78            lines.add(l);
79
80        // include message lines
81        for (l = firstLine; l != null && l.hasContinuation(); l = l.next)
82            lines.add(l);
83        lines.add(l);
84
85        // include trailing blank line if present
86        l = l.next;
87        if (l != null && l.text.isEmpty())
88            lines.add(l);
89
90        return lines;
91    }
92}
93