MessageLine.java revision 4202:2bd34895dda2
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.regex.Pattern;
27
28/**
29 * A line of text within the message file.
30 * The lines form a doubly linked list for simple navigation.
31 */
32public class MessageLine {
33
34    static final Pattern emptyOrCommentPattern = Pattern.compile("( *#.*)?");
35    static final Pattern typePattern = Pattern.compile("[-\\\\'A-Z\\.a-z ]+( \\([-A-Za-z 0-9]+\\))?");
36    static final Pattern infoPattern = Pattern.compile(String.format("# ([0-9]+: %s, )*[0-9]+: %s",
37            typePattern.pattern(), typePattern.pattern()));
38
39    public String text;
40    MessageLine prev;
41    MessageLine next;
42
43    MessageLine(String text) {
44        this.text = text;
45    }
46
47    public boolean isEmptyOrComment() {
48        return emptyOrCommentPattern.matcher(text).matches();
49    }
50
51    public boolean isInfo() {
52        return infoPattern.matcher(text).matches();
53    }
54
55    boolean hasContinuation() {
56        return (next != null) && text.endsWith("\\");
57    }
58
59    MessageLine append(String text) {
60        MessageLine l = new MessageLine(text);
61        append(l);
62        return l;
63    }
64
65    void append(MessageLine l) {
66        assert l.prev == null && l.next == null;
67        l.prev = this;
68        l.next = next;
69        if (next != null) {
70            next.prev = l;
71        }
72        next = l;
73    }
74}
75