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.replace;
20  
21  import net.sourceforge.javadpkg.Context;
22  
23  
24  /**
25   * <p>
26   * A {@link Replacer} implementation.
27   * </p>
28   *
29   * @author Gerrit Hohl (gerrit-hohl@users.sourceforge.net)
30   * @version <b>1.0</b>, 02.05.2016 by Gerrit Hohl
31   */
32  public class ReplacerImpl implements Replacer {
33  	
34  	
35  	/**
36  	 * <p>
37  	 * Creates a replacer.
38  	 * </p>
39  	 */
40  	public ReplacerImpl() {
41  		super();
42  	}
43  	
44  	
45  	@Override
46  	public String replace(String line, Replacements replacements, Context context) throws ReplacementException {
47  		int lastIndex, index;
48  		StringBuilder sb = null;
49  		String name, value;
50  
51  
52  		if (line == null)
53  			throw new IllegalArgumentException("Argument line is null.");
54  		if (replacements == null)
55  			throw new IllegalArgumentException("Argument replacements is null.");
56  		if (context == null)
57  			throw new IllegalArgumentException("Argument context is null.");
58  		
59  		// --- Do we have an empty line? ---
60  		if (line.isEmpty())
61  			return line;
62  		
63  		/*
64  		 * --- Go from one variable to the next variable.
65  		 *     Add the text between them to the builder.
66  		 *     Try to replace the variables by their values.
67  		 */
68  		lastIndex = 0;
69  		index = 0;
70  		while (index != -1) {
71  			index = line.indexOf("${", index);
72  			if (index >= 0) {
73  				if (sb == null) {
74  					sb = new StringBuilder();
75  				}
76  				sb.append(line.substring(lastIndex, index));
77  				lastIndex = index + 2;
78  				index = line.indexOf("}", lastIndex);
79  				if (index == -1)
80  					throw new ReplacementException("Found the beginning of a variable |" + line.substring(lastIndex)
81  							+ "|, but no end in line |" + line + "|.");
82  				name = line.substring(lastIndex, index);
83  				lastIndex = index + 1;
84  				index = lastIndex;
85  				
86  				value = replacements.getValue(name);
87  				if (value == null) {
88  					context.addWarning(new ReplacementVariableUnknownWarning(name));
89  					sb.append("${");
90  					sb.append(name);
91  					sb.append('}');
92  				} else {
93  					sb.append(value);
94  				}
95  			} else if (sb != null) {
96  				sb.append(line.substring(lastIndex));
97  			}
98  		}
99  		// --- Did we find no variables? ---
100 		if (sb == null)
101 			return line;
102 		return sb.toString();
103 	}
104 	
105 	
106 }