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.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   * <p>
43   * A {@link CopyrightBuilder} implementation.
44   * </p>
45   *
46   * @author Gerrit Hohl (gerrit-hohl@users.sourceforge.net)
47   * @version <b>1.0</b>, 04.05.2016 by Gerrit Hohl
48   */
49  public class CopyrightBuilderImpl implements CopyrightBuilder, CopyrightConstants {
50  	
51  	
52  	/** The builder for the fields. */
53  	private FieldBuilder fieldBuilder;
54  	
55  	
56  	/**
57  	 * <p>
58  	 * Creates a builder.
59  	 * </p>
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  		// --- Build the fields ---
81  		fields = this.buildFields(copyright);
82  
83  		// --- Build the copyright ---
84  		this.buildCopyright(fields, target, context);
85  	}
86  	
87  	
88  	/**
89  	 * <p>
90  	 * Builds the copyright into the fields.
91  	 * </p>
92  	 *
93  	 * @param copyright
94  	 *            The copyright.
95  	 * @return The fields.
96  	 * @throws BuildException
97  	 *             If an error occurs during the building.
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 				// TODO Add warning as comments are not part of the specification.
134 				this.addField(fields, FIELD_COMMENT, entry.getValue().getComment(), false);
135 			}
136 		}
137 
138 		return fields;
139 	}
140 
141 
142 	/**
143 	 * <p>
144 	 * Adds a field to the list of fields if a value is available.
145 	 * </p>
146 	 *
147 	 * @param fields
148 	 *            The fields.
149 	 * @param name
150 	 *            The name.
151 	 * @param value
152 	 *            The value (optional).
153 	 * @param formatValue
154 	 *            The flag if the value should be formatted.
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 	 * <p>
170 	 * Adds a field to the list of fields if a value is available.
171 	 * </p>
172 	 *
173 	 * @param fields
174 	 *            The fields.
175 	 * @param name
176 	 *            The name.
177 	 * @param values
178 	 *            The values (optional).
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 	 * <p>
201 	 * Adds a field to the list of fields if a license is available.
202 	 * </p>
203 	 *
204 	 * @param fields
205 	 *            The fields.
206 	 * @param name
207 	 *            The name.
208 	 * @param license
209 	 *            The license (optional).
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 	 * <p>
230 	 * Builds the copyright from the fields into the target.
231 	 * </p>
232 	 *
233 	 * @param fields
234 	 *            The fields.
235 	 * @param target
236 	 *            The target.
237 	 * @param context
238 	 *            The context.
239 	 * @throws IOException
240 	 *             If an I/O error occurs.
241 	 * @throws BuildException
242 	 *             If an error occurs during the building.
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 }