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.util.ArrayList;
22  import java.util.List;
23  
24  import net.sourceforge.javadpkg.Context;
25  import net.sourceforge.javadpkg.Script;
26  import net.sourceforge.javadpkg.ScriptVariableReplacer;
27  import net.sourceforge.javadpkg.replace.ReplacementException;
28  import net.sourceforge.javadpkg.replace.Replacements;
29  import net.sourceforge.javadpkg.replace.Replacer;
30  import net.sourceforge.javadpkg.replace.ReplacerImpl;
31  
32  
33  /**
34   * <p>
35   * A {@link ScriptVariableReplacer} implementation.
36   * </p>
37   *
38   * @author Gerrit Hohl (gerrit-hohl@users.sourceforge.net)
39   * @version <b>1.0</b>, 02.05.2016 by Gerrit Hohl
40   */
41  public class ScriptVariableReplacerImpl implements ScriptVariableReplacer {
42  	
43  	
44  	/** The replacer. */
45  	private Replacer replacer;
46  	
47  	
48  	/**
49  	 * <p>
50  	 * Creates a replacer.
51  	 * </p>
52  	 */
53  	public ScriptVariableReplacerImpl() {
54  		super();
55  		
56  		this.replacer = new ReplacerImpl();
57  	}
58  	
59  	
60  	@Override
61  	public Script replaceScriptVariables(Script script, Replacements replacements, Context context)
62  			throws ReplacementException {
63  		
64  		Script result;
65  		List<String> lines;
66  		String replaced;
67  		
68  		
69  		if (script == null)
70  			throw new IllegalArgumentException("Argument script is null.");
71  		if (replacements == null)
72  			throw new IllegalArgumentException("Argument replacements is null.");
73  		if (context == null)
74  			throw new IllegalArgumentException("Argument context is null.");
75  		
76  		lines = new ArrayList<>();
77  		for (String line : script.getLines()) {
78  			replaced = this.replacer.replace(line, replacements, context);
79  			lines.add(replaced);
80  		}
81  		result = new ScriptImpl(lines);
82  		return result;
83  	}
84  	
85  	
86  }