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  
25  import net.sourceforge.javadpkg.BuildException;
26  import net.sourceforge.javadpkg.GlobalConstants;
27  import net.sourceforge.javadpkg.Script;
28  import net.sourceforge.javadpkg.ScriptBuilder;
29  import net.sourceforge.javadpkg.io.DataTarget;
30  
31  
32  /**
33   * <p>
34   * A {@link ScriptBuilder} implementation.
35   * </p>
36   *
37   * @author Gerrit Hohl (gerrit-hohl@users.sourceforge.net)
38   * @version <b>1.0</b>, 02.05.2016 by Gerrit Hohl
39   */
40  public class ScriptBuilderImpl implements ScriptBuilder, GlobalConstants {
41  
42  
43  	/**
44  	 * <p>
45  	 * Creates a builder.
46  	 * </p>
47  	 */
48  	public ScriptBuilderImpl() {
49  		super();
50  	}
51  
52  
53  	@Override
54  	public void buildScript(DataTarget target, Script script) throws IOException, BuildException {
55  		if (target == null)
56  			throw new IllegalArgumentException("Argument target is null.");
57  		if (script == null)
58  			throw new IllegalArgumentException("Argument script is null.");
59  			
60  		try {
61  			try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(target.getOutputStream(), UTF_8_CHARSET))) {
62  				writer.write(script.getText());
63  			}
64  		} catch (IOException e) {
65  			throw new IOException("Couldn't build script |" + target.getName() + "|: " + e.getMessage(), e);
66  		}
67  	}
68  
69  
70  }