Message.java revision 2776:aa568700edd1
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 */ 23package propertiesparser.parser; 24 25import java.util.ArrayList; 26import java.util.List; 27 28/** 29 * A message within the message file. 30 * A message is a series of lines containing a "name=value" property, 31 * optionally preceded by a comment describing the use of placeholders 32 * such as {0}, {1}, etc within the property value. 33 */ 34public final class Message { 35 final MessageLine firstLine; 36 private MessageInfo messageInfo; 37 38 Message(MessageLine l) { 39 firstLine = l; 40 } 41 42 /** 43 * Get the Info object for this message. It may be empty if there 44 * if no comment preceding the property specification. 45 */ 46 public MessageInfo getMessageInfo() { 47 if (messageInfo == null) { 48 MessageLine l = firstLine.prev; 49 if (l != null && l.isInfo()) 50 messageInfo = new MessageInfo(l.text); 51 else 52 messageInfo = MessageInfo.dummyInfo; 53 } 54 return messageInfo; 55 } 56 57 /** 58 * Get all the lines pertaining to this message. 59 */ 60 public List<MessageLine> getLines(boolean includeAllPrecedingComments) { 61 List<MessageLine> lines = new ArrayList<>(); 62 MessageLine l = firstLine; 63 if (includeAllPrecedingComments) { 64 // scan back to find end of prev message 65 while (l.prev != null && l.prev.isEmptyOrComment()) 66 l = l.prev; 67 // skip leading blank lines 68 while (l.text.isEmpty()) 69 l = l.next; 70 } else { 71 if (l.prev != null && l.prev.isInfo()) 72 l = l.prev; 73 } 74 75 // include any preceding lines 76 for ( ; l != firstLine; l = l.next) 77 lines.add(l); 78 79 // include message lines 80 for (l = firstLine; l != null && l.hasContinuation(); l = l.next) 81 lines.add(l); 82 lines.add(l); 83 84 // include trailing blank line if present 85 l = l.next; 86 if (l != null && l.text.isEmpty()) 87 lines.add(l); 88 89 return lines; 90 } 91} 92