1 /*
2 * dpkg - Debian Package library and the Debian Package Maven plugin
3 * (c) Copyright 2016 Gerrit Hohl
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 package net.sourceforge.javadpkg.field.impl;
20
21 import net.sourceforge.javadpkg.field.Field;
22
23 /**
24 * <p>
25 * An abstract {@link Field} implementation.
26 * </p>
27 *
28 * @author Gerrit Hohl (gerrit-hohl@users.sourceforge.net)
29 * @version <b>1.0</b>, 06.05.2016 by Gerrit Hohl
30 */
31 public abstract class AbstractField implements Field {
32
33
34 /** The flag if the field is a nameless field. */
35 private boolean nameless;
36 /** The flag if the field is an empty field. */
37 private boolean empty;
38
39
40 /**
41 * <p>
42 * Creates a field.
43 * </p>
44 *
45 * @param nameless
46 * The flag if the field is a nameless field.
47 * @param empty
48 * The flag if the field is an empty field.
49 */
50 public AbstractField(boolean nameless, boolean empty) {
51 super();
52
53 this.nameless = nameless;
54 this.empty = empty;
55 }
56
57
58 /**
59 * <p>
60 * Formats the specified value.
61 * </p>
62 * <p>
63 * All empty lines are filled with a ".".
64 * </p>
65 *
66 * @param value
67 * The value.
68 * @return The formatted value.
69 */
70 protected String formatValue(String value) {
71 StringBuilder sb;
72 String[] lines;
73
74
75 if (value == null)
76 return null;
77
78 sb = new StringBuilder();
79 lines = value.split("\n", -1);
80 for (String line : lines) {
81 if (sb.length() > 0) {
82 sb.append('\n');
83 }
84 if (line.isEmpty()) {
85 sb.append('.');
86 } else {
87 sb.append(line);
88 }
89 }
90 return sb.toString();
91 }
92
93
94 @Override
95 public boolean isNameless() {
96 return this.nameless;
97 }
98
99
100 @Override
101 public String getFormattedValue() {
102 StringBuilder sb;
103 String value;
104 String[] lines;
105
106
107 value = this.getValue();
108 if (value == null)
109 return null;
110
111 sb = new StringBuilder();
112 lines = value.split("\n", -1);
113 for (String line : lines) {
114 if (sb.length() > 0) {
115 sb.append('\n');
116 }
117 if (!".".equals(line)) {
118 sb.append(line);
119 }
120 }
121 return sb.toString();
122 }
123
124
125 @Override
126 public boolean isEmpty() {
127 return this.empty;
128 }
129
130
131 }