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.plugin.impl;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.maven.plugin.logging.Log;
27  
28  import net.sourceforge.javadpkg.ChangeLog;
29  import net.sourceforge.javadpkg.ChangeLogConstants;
30  import net.sourceforge.javadpkg.ChangeLogParser;
31  import net.sourceforge.javadpkg.ChangeLogUrgencyParser;
32  import net.sourceforge.javadpkg.ChangeLogVersionEntry;
33  import net.sourceforge.javadpkg.ChangeLogVersionEntryDetail;
34  import net.sourceforge.javadpkg.Context;
35  import net.sourceforge.javadpkg.ParseException;
36  import net.sourceforge.javadpkg.control.PackageMaintainerParser;
37  import net.sourceforge.javadpkg.control.PackageNameParser;
38  import net.sourceforge.javadpkg.control.PackageVersionParser;
39  import net.sourceforge.javadpkg.control.impl.PackageMaintainerParserImpl;
40  import net.sourceforge.javadpkg.control.impl.PackageNameParserImpl;
41  import net.sourceforge.javadpkg.control.impl.PackageVersionParserImpl;
42  import net.sourceforge.javadpkg.impl.ChangeLogImpl;
43  import net.sourceforge.javadpkg.impl.ChangeLogParserImpl;
44  import net.sourceforge.javadpkg.impl.ChangeLogUrgencyParserImpl;
45  import net.sourceforge.javadpkg.impl.ChangeLogVersionEntryDetailImpl;
46  import net.sourceforge.javadpkg.impl.ChangeLogVersionEntryImpl;
47  import net.sourceforge.javadpkg.io.impl.DataFileSource;
48  import net.sourceforge.javadpkg.plugin.ChangeLogConfigurationParser;
49  import net.sourceforge.javadpkg.plugin.cfg.ChangeLogConfiguration;
50  import net.sourceforge.javadpkg.plugin.cfg.ChangeLogVersionEntryConfiguration;
51  
52  
53  /**
54   * <p>
55   * A {@link ChangeLogConfigurationParser} implementation.
56   * </p>
57   *
58   * @author Gerrit Hohl (gerrit-hohl@users.sourceforge.net)
59   * @version <b>1.0</b>, 09.05.2016 by Gerrit Hohl
60   */
61  public class ChangeLogConfigurationParserImpl implements ChangeLogConfigurationParser, ChangeLogConstants {
62  	
63  	
64  	/** The parser for the change log. */
65  	private ChangeLogParser			changeLogParser;
66  	/** The parser for the package name. */
67  	private PackageNameParser		packageNameParser;
68  	/** The parser for the package version. */
69  	private PackageVersionParser	packageVersionParser;
70  	/** The parser for the urgency. */
71  	private ChangeLogUrgencyParser	changeLogUrgencyParser;
72  	/** The parser for the package maintainer. */
73  	private PackageMaintainerParser	packageMaintainerParser;
74  	
75  	
76  	// TODO Check configuration(s).
77  
78  
79  	/**
80  	 * <p>
81  	 * Creates a parser.
82  	 * </p>
83  	 */
84  	public ChangeLogConfigurationParserImpl() {
85  		super();
86  		
87  		this.changeLogParser = new ChangeLogParserImpl();
88  		this.packageNameParser = new PackageNameParserImpl();
89  		this.packageVersionParser = new PackageVersionParserImpl();
90  		this.changeLogUrgencyParser = new ChangeLogUrgencyParserImpl();
91  		this.packageMaintainerParser = new PackageMaintainerParserImpl();
92  	}
93  	
94  	
95  	@Override
96  	public ChangeLog parseChangeLogConfiguration(Log log, ChangeLogConfiguration config, Context context)
97  			throws IOException, ParseException {
98  		
99  		ChangeLog changeLog;
100 		File file;
101 
102 
103 		if (config == null)
104 			throw new IllegalArgumentException("Argument config is null.");
105 		if (context == null)
106 			throw new IllegalArgumentException("Argument context is null.");
107 		
108 		file = config.getFile();
109 		if (file != null) {
110 			changeLog = this.parseFile(log, file, context);
111 		} else {
112 			changeLog = this.parseConfiguration(log, config, context);
113 		}
114 
115 		return changeLog;
116 	}
117 
118 
119 	/**
120 	 * <p>
121 	 * Parses the change log from the specified file.
122 	 * </p>
123 	 *
124 	 * @param log
125 	 *            The log.
126 	 * @param file
127 	 *            The file.
128 	 * @param context
129 	 *            The context.
130 	 * @return The change log.
131 	 * @throws IOException
132 	 *             If an I/O error occurs.
133 	 * @throws ParseException
134 	 *             If an error occurs during the parsing.
135 	 */
136 	private ChangeLog parseFile(Log log, File file, Context context) throws IOException, ParseException {
137 		ChangeLog changeLog;
138 		
139 		
140 		if (log.isInfoEnabled()) {
141 			log.info("Read change log from file |" + file.getAbsolutePath() + "|.");
142 		}
143 		try {
144 			try (DataFileSource source = new DataFileSource(file)) {
145 				changeLog = this.changeLogParser.parseChangeLog(source, context);
146 			}
147 		} catch (IOException e) {
148 			throw new IOException("Couldn't parse the change log file |" + file.getAbsolutePath() + "|: " + e.getMessage(), e);
149 		} catch (ParseException e) {
150 			throw new IOException("Couldn't parse the change log file |" + file.getAbsolutePath() + "|: " + e.getMessage(), e);
151 		}
152 		return changeLog;
153 	}
154 	
155 	
156 	/**
157 	 * <p>
158 	 * Parses the change log from the specified configuration.
159 	 * </p>
160 	 *
161 	 * @param log
162 	 *            The log.
163 	 * @param config
164 	 *            The configuration.
165 	 * @param context
166 	 *            The context.
167 	 * @return The change log.
168 	 * @throws ParseException
169 	 *             If an error occurs during the parsing.
170 	 */
171 	private ChangeLog parseConfiguration(Log log, ChangeLogConfiguration config, Context context) throws ParseException {
172 		ChangeLogImpl changeLog;
173 		ChangeLogVersionEntry entry;
174 		
175 		
176 		if (log.isInfoEnabled()) {
177 			log.info("Read change log from configuration.");
178 		}
179 		changeLog = new ChangeLogImpl();
180 		for (ChangeLogVersionEntryConfiguration cfg : config.getEntries()) {
181 			entry = this.parseVersionEntryConfiguration(cfg, context);
182 			changeLog.addEntry(entry);
183 		}
184 		return changeLog;
185 	}
186 
187 
188 	/**
189 	 * <p>
190 	 * Parses the version entry of a change log from the specified
191 	 * configuration.
192 	 * </p>
193 	 *
194 	 * @param config
195 	 *            The configuration.
196 	 * @param context
197 	 *            The context.
198 	 * @return The version entry.
199 	 * @throws ParseException
200 	 *             If an error occurs during the parsing.
201 	 */
202 	private ChangeLogVersionEntry parseVersionEntryConfiguration(ChangeLogVersionEntryConfiguration config, Context context)
203 			throws ParseException {
204 		
205 		ChangeLogVersionEntryImpl entry;
206 
207 
208 		entry = new ChangeLogVersionEntryImpl();
209 		entry.setPackageName(this.packageNameParser.parsePackageName(config.getName(), context));
210 		entry.setVersion(this.packageVersionParser.parsePackageVersion(config.getVersion(), context));
211 		entry.setDistributions(config.getDistributions());
212 		entry.setUrgency(this.changeLogUrgencyParser.parseChangeLogUrgency(config.getUrgency(), context));
213 		entry.setDetails(this.parseDetails(config.getDetails(), context));
214 		entry.setMaintainer(this.packageMaintainerParser.parsePackageMaintainer(config.getMaintainer(), context));
215 		try {
216 			entry.setDate(TIMESTAMP_FORMAT.parse(config.getDate()));
217 		} catch (java.text.ParseException e) {
218 			throw new ParseException("Couldn't parse timestamp |" + config.getDate() + "| of version entry: " + e.getMessage(),
219 					e);
220 		}
221 		return entry;
222 	}
223 
224 
225 	/**
226 	 * <p>
227 	 * Parses the details of a version entry of a change log from the specified
228 	 * configuration.
229 	 * </p>
230 	 *
231 	 * @param configs
232 	 *            The configurations.
233 	 * @param context
234 	 *            The context.
235 	 * @return The details.
236 	 * @throws ParseException
237 	 *             If an error occurs during the parsing.
238 	 */
239 	private List<ChangeLogVersionEntryDetail> parseDetails(List<String> configs, Context context) throws ParseException {
240 		List<ChangeLogVersionEntryDetail> details;
241 		ChangeLogVersionEntryDetail detail;
242 		
243 		
244 		details = new ArrayList<>(configs.size());
245 		for (String config : configs) {
246 			detail = new ChangeLogVersionEntryDetailImpl(config);
247 			details.add(detail);
248 		}
249 		return details;
250 	}
251 	
252 	
253 }