View Javadoc
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.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   * <p>
46   * A {@link ChangeLogBuilder} implementation.
47   * </p>
48   *
49   * @author Gerrit Hohl (gerrit-hohl@users.sourceforge.net)
50   * @version <b>1.0</b>, 04.05.2016 by Gerrit Hohl
51   */
52  public class ChangeLogBuilderImpl implements ChangeLogBuilder, ChangeLogConstants {
53  	
54  	
55  	/** The newline. */
56  	private static final String			NEWLINE	= "\n";
57  	
58  	
59  	/** The builder for the package name. */
60  	private PackageNameBuilder			packageNameBuilder;
61  	/** The builder for the version. */
62  	private PackageVersionBuilder		packageVersionBuilder;
63  	/** The builder for the urgency. */
64  	private ChangeLogUrgencyBuilder		changeLogUrgencyBuilder;
65  	/** The builder for the maintainer. */
66  	private PackageMaintainerBuilder	packageMaintainerBuilder;
67  
68  
69  	/**
70  	 * <p>
71  	 * Creates a builder.
72  	 * </p>
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 					// --- Package name ---
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 					// --- Version ---
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 					// --- Distributions ---
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 					// --- Urgency ---
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 					// --- Details ---
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 					// --- Maintainer ---
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 					// --- Timestamp ---
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 	 * <p>
185 	 * Builds a detail into the writer.
186 	 * </p>
187 	 *
188 	 * @param out
189 	 *            The writer.
190 	 * @param detail
191 	 *            The detail.
192 	 * @throws IOException
193 	 *             If an I/O error occurs.
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 		// TODO Implement method.
225 		throw new BuildException("Not yet implemented");
226 	}
227 
228 
229 }