1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sourceforge.javadpkg.impl;
20
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Map.Entry;
25
26 import net.sourceforge.javadpkg.BuildException;
27 import net.sourceforge.javadpkg.Context;
28 import net.sourceforge.javadpkg.Copyright;
29 import net.sourceforge.javadpkg.CopyrightBuilder;
30 import net.sourceforge.javadpkg.CopyrightConstants;
31 import net.sourceforge.javadpkg.CopyrightLicense;
32 import net.sourceforge.javadpkg.FilesCopyright;
33 import net.sourceforge.javadpkg.field.Field;
34 import net.sourceforge.javadpkg.field.FieldBuilder;
35 import net.sourceforge.javadpkg.field.impl.EmptyField;
36 import net.sourceforge.javadpkg.field.impl.FieldBuilderImpl;
37 import net.sourceforge.javadpkg.field.impl.FieldImpl;
38 import net.sourceforge.javadpkg.io.DataTarget;
39
40
41
42
43
44
45
46
47
48
49 public class CopyrightBuilderImpl implements CopyrightBuilder, CopyrightConstants {
50
51
52
53 private FieldBuilder fieldBuilder;
54
55
56
57
58
59
60
61 public CopyrightBuilderImpl() {
62 super();
63
64 this.fieldBuilder = new FieldBuilderImpl();
65 }
66
67
68 @Override
69 public void buildCopyright(Copyright copyright, DataTarget target, Context context) throws IOException, BuildException {
70 List<Field> fields;
71
72
73 if (copyright == null)
74 throw new IllegalArgumentException("Argument copyright is null.");
75 if (target == null)
76 throw new IllegalArgumentException("Argument target is null.");
77 if (context == null)
78 throw new IllegalArgumentException("Argument context is null.");
79
80
81 fields = this.buildFields(copyright);
82
83
84 this.buildCopyright(fields, target, context);
85 }
86
87
88
89
90
91
92
93
94
95
96
97
98
99 private List<Field> buildFields(Copyright copyright) throws BuildException {
100 List<Field> fields;
101 String format;
102
103
104 format = copyright.getFormat();
105 if (format == null) {
106 format = FORMAT_V1_0;
107 } else if (!FORMAT_V1_0.equals(format))
108 throw new BuildException("The format of the copyright is |" + format + "|, but only the format |" + FORMAT_V1_0
109 + "| is supported by the builder.");
110
111 fields = new ArrayList<>();
112 this.addField(fields, FIELD_FORMAT, format, false);
113 this.addField(fields, FIELD_UPSTREAM_NAME, copyright.getUpstreamName(), false);
114 this.addField(fields, FIELD_UPSTREAM_CONTACT, copyright.getUpstreamContact(), false);
115 this.addField(fields, FIELD_SOURCE, copyright.getSource(), false);
116 this.addField(fields, FIELD_DISCLAIMER, copyright.getDisclaimer(), false);
117 this.addField(fields, FIELD_COMMENT, copyright.getComment(), false);
118 this.addField(fields, FIELD_LICENSE, copyright.getLicense());
119 this.addField(fields, FIELD_COPYRIGHT, copyright.getCopyright(), false);
120
121 for (FilesCopyright files : copyright.getFilesCopyrights()) {
122 fields.add(EmptyField.EMPTY_FIELD);
123 this.addField(fields, FIELD_FILES, files.getFiles());
124 this.addField(fields, FIELD_COPYRIGHT, files.getCopyright(), false);
125 this.addField(fields, FIELD_LICENSE, files.getLicense());
126 this.addField(fields, FIELD_COMMENT, files.getComment(), false);
127 }
128
129 for (Entry<String, CopyrightLicense> entry : copyright.getLicenses().entrySet()) {
130 fields.add(EmptyField.EMPTY_FIELD);
131 this.addField(fields, FIELD_LICENSE, entry.getValue());
132 if (entry.getValue().getComment() != null) {
133
134 this.addField(fields, FIELD_COMMENT, entry.getValue().getComment(), false);
135 }
136 }
137
138 return fields;
139 }
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156 private void addField(List<Field> fields, String name, String value, boolean formatValue) {
157 Field field;
158
159
160 if (value == null)
161 return;
162
163 field = new FieldImpl(name, value, formatValue);
164 fields.add(field);
165 }
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180 private void addField(List<Field> fields, String name, List<String> values) {
181 StringBuilder sb;
182
183
184 if ((values == null) || values.isEmpty())
185 return;
186
187 sb = new StringBuilder();
188 for (String value : values) {
189 if (sb.length() > 0) {
190 sb.append('\n');
191 }
192 sb.append(value);
193 }
194
195 this.addField(fields, name, sb.toString(), false);
196 }
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211 private void addField(List<Field> fields, String name, CopyrightLicense license) {
212 StringBuilder sb;
213
214
215 if (license == null)
216 return;
217
218 sb = new StringBuilder();
219 sb.append(license.getName());
220 if (license.getText() != null) {
221 sb.append('\n');
222 sb.append(license.getText());
223 }
224 this.addField(fields, name, sb.toString(), true);
225 }
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244 private void buildCopyright(List<Field> fields, DataTarget target, Context context) throws IOException, BuildException {
245 try {
246 this.fieldBuilder.buildFields(fields, target, context);
247 } catch (IOException e) {
248 throw new IOException("Couldn't build copyright |" + target.getName() + "|: " + e.getMessage());
249 } catch (BuildException e) {
250 throw new BuildException("Couldn't build copyright |" + target.getName() + "|: " + e.getMessage());
251 }
252 }
253
254
255 }