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  
24  import org.apache.maven.plugin.logging.Log;
25  
26  import net.sourceforge.javadpkg.Context;
27  import net.sourceforge.javadpkg.Copyright;
28  import net.sourceforge.javadpkg.CopyrightLicense;
29  import net.sourceforge.javadpkg.CopyrightParser;
30  import net.sourceforge.javadpkg.FilesCopyright;
31  import net.sourceforge.javadpkg.ParseException;
32  import net.sourceforge.javadpkg.impl.CopyrightImpl;
33  import net.sourceforge.javadpkg.impl.CopyrightLicenseImpl;
34  import net.sourceforge.javadpkg.impl.CopyrightParserImpl;
35  import net.sourceforge.javadpkg.impl.FilesCopyrightImpl;
36  import net.sourceforge.javadpkg.io.DataSource;
37  import net.sourceforge.javadpkg.io.impl.DataFileSource;
38  import net.sourceforge.javadpkg.plugin.CopyrightConfigurationParser;
39  import net.sourceforge.javadpkg.plugin.cfg.CopyrightConfiguration;
40  import net.sourceforge.javadpkg.plugin.cfg.CopyrightFilesConfiguration;
41  import net.sourceforge.javadpkg.plugin.cfg.CopyrightLicenseConfiguration;
42  
43  
44  /**
45   * <p>
46   * A {@link CopyrightConfigurationParser} implementation.
47   * </p>
48   *
49   * @author Gerrit Hohl (gerrit-hohl@users.sourceforge.net)
50   * @version <b>1.0</b>, 09.05.2016 by Gerrit Hohl
51   */
52  public class CopyrightConfigurationParserImpl implements CopyrightConfigurationParser {
53  	
54  	
55  	/** The parser for the copyright. */
56  	private CopyrightParser copyrightParser;
57  	
58  	
59  	// TODO Check configuration(s).
60  	
61  	
62  	/**
63  	 * <p>
64  	 * Creates a parser.
65  	 * </p>
66  	 */
67  	public CopyrightConfigurationParserImpl() {
68  		super();
69  		
70  		this.copyrightParser = new CopyrightParserImpl();
71  	}
72  
73  
74  	@Override
75  	public Copyright parseCopyrightConfiguration(Log log, CopyrightConfiguration config, Context context)
76  			throws IOException, ParseException {
77  		
78  		Copyright copyright;
79  		File file;
80  
81  
82  		if (config == null)
83  			throw new IllegalArgumentException("Argument config is null.");
84  		if (context == null)
85  			throw new IllegalArgumentException("Argument context is null.");
86  		
87  		file = config.getFile();
88  		if (file != null) {
89  			copyright = this.parseFile(log, file, context);
90  		} else {
91  			copyright = this.parseConfiguration(log, config, context);
92  		}
93  
94  		return copyright;
95  	}
96  
97  
98  	/**
99  	 * <p>
100 	 * Parses the copyright from the specified file.
101 	 * </p>
102 	 *
103 	 * @param log
104 	 *            The log.
105 	 * @param file
106 	 *            The file.
107 	 * @param context
108 	 *            The context.
109 	 * @return The copyright.
110 	 * @throws IOException
111 	 *             If an I/O error occurs.
112 	 * @throws ParseException
113 	 *             If an error occurs during the parsing.
114 	 */
115 	private Copyright parseFile(Log log, File file, Context context) throws IOException, ParseException {
116 		Copyright copyright;
117 
118 
119 		if (log.isInfoEnabled()) {
120 			log.info("Read copyright from file |" + file.getAbsolutePath() + "|.");
121 		}
122 		try {
123 			try (DataSource source = new DataFileSource(file)) {
124 				copyright = this.copyrightParser.parseCopyright(source, context);
125 			}
126 		} catch (IOException e) {
127 			throw new IOException("Couldn't parse the copyright file |" + file.getAbsolutePath() + "|: " + e.getMessage(), e);
128 		} catch (ParseException e) {
129 			throw new IOException("Couldn't parse the copyright file |" + file.getAbsolutePath() + "|: " + e.getMessage(), e);
130 		}
131 		return copyright;
132 	}
133 	
134 	
135 	/**
136 	 * <p>
137 	 * Parses the copyright from the specified configuration.
138 	 * </p>
139 	 *
140 	 * @param log
141 	 *            The log.
142 	 * @param config
143 	 *            The configuration.
144 	 * @param context
145 	 *            The context.
146 	 * @return The copyright.
147 	 * @throws ParseException
148 	 *             If an error occurs during the parsing.
149 	 */
150 	private Copyright parseConfiguration(Log log, CopyrightConfiguration config, Context context) throws ParseException {
151 		CopyrightImpl copyright;
152 		FilesCopyright filesCopyright;
153 		CopyrightLicense license;
154 
155 
156 		if (log.isInfoEnabled()) {
157 			log.info("Read copyright from configuration.");
158 		}
159 		copyright = new CopyrightImpl();
160 		copyright.setUpstreamName(config.getUpstreamName());
161 		copyright.setUpstreamContact(config.getUpstreamContact());
162 		copyright.setSource(config.getSource());
163 		copyright.setDisclaimer(config.getDisclaimer());
164 		copyright.setComment(config.getComment());
165 		copyright.setLicense(this.parseLicenseConfiguration(config.getLicense(), context));
166 		copyright.setCopyright(config.getCopyright());
167 		for (CopyrightFilesConfiguration cfg : config.getFiles()) {
168 			filesCopyright = this.parseFilesConfiguration(cfg, context);
169 			copyright.addFilesCopyright(filesCopyright);
170 		}
171 		for (CopyrightLicenseConfiguration cfg : config.getLicenses()) {
172 			license = this.parseLicenseConfiguration(cfg, context);
173 			copyright.addLicense(license);
174 		}
175 		return copyright;
176 	}
177 
178 
179 	/**
180 	 * <p>
181 	 * Parses the license from the specified configuration.
182 	 * </p>
183 	 *
184 	 * @param config
185 	 *            The configuration.
186 	 * @param context
187 	 *            The context.
188 	 * @return The copyright.
189 	 * @throws ParseException
190 	 *             If an error occurs during the parsing.
191 	 */
192 	private CopyrightLicense parseLicenseConfiguration(CopyrightLicenseConfiguration config, Context context)
193 			throws ParseException {
194 		
195 		CopyrightLicenseImpl license;
196 
197 
198 		if (config == null)
199 			return null;
200 		
201 		license = new CopyrightLicenseImpl();
202 		license.setName(config.getName());
203 		license.setText(config.getText());
204 
205 		return license;
206 	}
207 
208 
209 	/**
210 	 * <p>
211 	 * Parses the copyright for certain files from the specified configuration.
212 	 * </p>
213 	 *
214 	 * @param config
215 	 *            The configuration.
216 	 * @param context
217 	 *            The context.
218 	 * @return The copyright.
219 	 * @throws ParseException
220 	 *             If an error occurs during the parsing.
221 	 */
222 	private FilesCopyright parseFilesConfiguration(CopyrightFilesConfiguration config, Context context) throws ParseException {
223 		FilesCopyrightImpl files;
224 		
225 		
226 		if (config == null)
227 			return null;
228 		
229 		files = new FilesCopyrightImpl();
230 		files.setFiles(config.getFiles());
231 		files.setCopyright(config.getCopyright());
232 		files.setLicense(this.parseLicenseConfiguration(config.getLicense(), context));
233 		files.setComment(config.getComment());
234 		
235 		return files;
236 	}
237 
238 
239 }