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.BufferedWriter;
22 import java.io.IOException;
23 import java.io.OutputStreamWriter;
24 import java.io.Writer;
25 import java.util.List;
26
27 import net.sourceforge.javadpkg.BuildException;
28 import net.sourceforge.javadpkg.ChangeLog;
29 import net.sourceforge.javadpkg.ChangeLogBuilder;
30 import net.sourceforge.javadpkg.ChangeLogConstants;
31 import net.sourceforge.javadpkg.ChangeLogUrgencyBuilder;
32 import net.sourceforge.javadpkg.ChangeLogVersionEntry;
33 import net.sourceforge.javadpkg.ChangeLogVersionEntryDetail;
34 import net.sourceforge.javadpkg.Context;
35 import net.sourceforge.javadpkg.control.PackageMaintainerBuilder;
36 import net.sourceforge.javadpkg.control.PackageNameBuilder;
37 import net.sourceforge.javadpkg.control.PackageVersionBuilder;
38 import net.sourceforge.javadpkg.control.impl.PackageMaintainerBuilderImpl;
39 import net.sourceforge.javadpkg.control.impl.PackageNameBuilderImpl;
40 import net.sourceforge.javadpkg.control.impl.PackageVersionBuilderImpl;
41 import net.sourceforge.javadpkg.io.DataTarget;
42
43
44
45
46
47
48
49
50
51
52 public class ChangeLogBuilderImpl implements ChangeLogBuilder, ChangeLogConstants {
53
54
55
56 private static final String NEWLINE = "\n";
57
58
59
60 private PackageNameBuilder packageNameBuilder;
61
62 private PackageVersionBuilder packageVersionBuilder;
63
64 private ChangeLogUrgencyBuilder changeLogUrgencyBuilder;
65
66 private PackageMaintainerBuilder packageMaintainerBuilder;
67
68
69
70
71
72
73
74 public ChangeLogBuilderImpl() {
75 super();
76
77 this.packageNameBuilder = new PackageNameBuilderImpl();
78 this.packageVersionBuilder = new PackageVersionBuilderImpl();
79 this.changeLogUrgencyBuilder = new ChangeLogUrgencyBuilderImpl();
80 this.packageMaintainerBuilder = new PackageMaintainerBuilderImpl();
81 }
82
83
84 @Override
85 public void buildChangeLog(ChangeLog changeLog, DataTarget target, Context context) throws IOException, BuildException {
86 boolean firstEntry = true;
87 String value, version;
88 List<String> values;
89 List<ChangeLogVersionEntryDetail> details;
90
91
92 if (changeLog == null)
93 throw new IllegalArgumentException("Argument changeLog is null.");
94 if (target == null)
95 throw new IllegalArgumentException("Argument target is null.");
96 if (context == null)
97 throw new IllegalArgumentException("Argument context is null.");
98
99 try {
100 try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(target.getOutputStream(), UTF_8_CHARSET))) {
101 for (ChangeLogVersionEntry entry : changeLog.getEntries()) {
102 if (firstEntry) {
103 firstEntry = false;
104 } else {
105 out.write(NEWLINE);
106 }
107
108
109 try {
110 value = this.packageNameBuilder.buildPackageName(entry.getPackage(), context);
111 } catch (BuildException e) {
112 throw new BuildException("Couldn't build change log |" + target.getName()
113 + "| because package name couldn't be build: " + e.getMessage(), e);
114 }
115 out.write(value);
116 out.write(" (");
117
118
119 try {
120 version = this.packageVersionBuilder.buildPackageVersion(entry.getVersion(), context);
121 } catch (BuildException e) {
122 throw new BuildException("Couldn't build change log |" + target.getName()
123 + "| because version couldn't be build: " + e.getMessage(), e);
124 }
125 out.write(version);
126 out.write(")");
127
128
129 values = entry.getDistributions();
130 if (values.isEmpty())
131 throw new IllegalArgumentException("Couldn't build change log |" + target.getName()
132 + "| because no distribution was defined for version |" + version + "|.");
133 for (String v : values) {
134 out.write(" ");
135 out.write(v);
136 }
137 out.write("; urgency=");
138
139
140 try {
141 value = this.changeLogUrgencyBuilder.buildChangeLogUrgency(entry.getUrgency(), context);
142 } catch (BuildException e) {
143 throw new BuildException("Couldn't build change log |" + target.getName()
144 + "| because urgency couldn't be build for version |" + version + "|: " + e.getMessage(), e);
145 }
146 out.write(value);
147 out.write(NEWLINE);
148
149
150 details = entry.getDetails();
151 if (details.isEmpty())
152 throw new BuildException("Couldn't build change log |" + target.getName()
153 + "| because no details are defined for version |" + version + "|.");
154 for (ChangeLogVersionEntryDetail detail : details) {
155 out.write(NEWLINE);
156 this.buildDetail(out, detail);
157 }
158 out.write(NEWLINE);
159
160
161 try {
162 value = this.packageMaintainerBuilder.buildPackageMaintainer(entry.getMaintainer(), context);
163 } catch (BuildException e) {
164 throw new BuildException("Couldn't build change log |" + target.getName()
165 + "| because maintainer couldn't be build for version |" + version + "|: " + e.getMessage(), e);
166 }
167 out.write(TERMINATION_PREFIX);
168 out.write(value);
169 out.write(MAINTAINER_TIMESTAMP_SEPARATOR);
170
171
172 value = TIMESTAMP_FORMAT.format(entry.getDate());
173 out.write(value);
174 out.write(NEWLINE);
175 }
176 }
177 } catch (IOException e) {
178 throw new IOException("Couldn't write change log |" + target.getName() + "|: " + e.getMessage(), e);
179 }
180 }
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195 private void buildDetail(Writer out, ChangeLogVersionEntryDetail detail) throws IOException {
196 String text;
197 String[] lines;
198 boolean firstLine = true;
199
200
201 text = detail.getText();
202 lines = text.split("\n", -1);
203 for (String line : lines) {
204 if (firstLine) {
205 out.write(DETAIL_START_PREFIX);
206 firstLine = false;
207 } else {
208 out.write(NEWLINE);
209 out.write(DETAIL_LINE_PREFIX);
210 }
211
212 if (line.isEmpty()) {
213 out.write(".");
214 } else {
215 out.write(line);
216 }
217 }
218 out.write(NEWLINE);
219 }
220
221
222 @Override
223 public void buildChangeLogHtml(ChangeLog changeLog, DataTarget target, Context context) throws IOException, BuildException {
224
225 throw new BuildException("Not yet implemented");
226 }
227
228
229 }