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.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package propertiesparser.parser;
27 
28 import java.util.regex.Pattern;
29 
30 /**
31  * A line of text within the message file.
32  * The lines form a doubly linked list for simple navigation.
33  */
34 public class MessageLine {
35 
36     static final Pattern emptyOrCommentPattern = Pattern.compile("( *#.*)?");
37     static final Pattern typePattern = Pattern.compile("[-\\\\'A-Z\\.a-z ]+( \\([-A-Za-z 0-9]+\\))?");
38     static final Pattern infoPattern = Pattern.compile(String.format("# ([0-9]+: %s, )*[0-9]+: %s",
39             typePattern.pattern(), typePattern.pattern()));
40 
41     public String text;
42     MessageLine prev;
43     MessageLine next;
44 
MessageLine(String text)45     MessageLine(String text) {
46         this.text = text;
47     }
48 
isEmptyOrComment()49     public boolean isEmptyOrComment() {
50         return emptyOrCommentPattern.matcher(text).matches();
51     }
52 
isInfo()53     public boolean isInfo() {
54         return infoPattern.matcher(text).matches();
55     }
56 
hasContinuation()57     boolean hasContinuation() {
58         return (next != null) && text.endsWith("\\");
59     }
60 
append(String text)61     MessageLine append(String text) {
62         MessageLine l = new MessageLine(text);
63         append(l);
64         return l;
65     }
66 
append(MessageLine l)67     void append(MessageLine l) {
68         assert l.prev == null && l.next == null;
69         l.prev = this;
70         l.next = next;
71         if (next != null) {
72             next.prev = l;
73         }
74         next = l;
75     }
76 }
77