summaryrefslogtreecommitdiff
path: root/src/main/java/ReplPair.java
blob: 474f7c9356cf56adbf34ffafca8f72816d92d881 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * String pairs for replacements.
 *
 * @author Ben Culkin
 */
public class ReplPair {
	/**
	 * The string to look for.
	 */
	public String find;
	/**
	 * The string to replace it with.
	 */
	public String replace;

	/**
	 * Create a new blank replacement pair.
	 */
	public ReplPair() {

	}

	/**
	 * Create a new replacement pair.
	 *
	 * @param f
	 * 	The string to find.
	 * @param r
	 * 	The string to replace.
	 */
	public ReplPair(String f, String r) {
		find = f;
		replace = r;
	}

	/**
	 * Read a list of replacement pairs from an input source.
	 *
	 * @param scn
	 * 	The source to read the replacements from.
	 */
	public static List<ReplPair> readList(Scanner scn) {
		return ReplPair.readList(new ArrayList<>(), scn);
	}

	/**
	 * Read a list of replacement pairs from an input source, adding them to
	 * an existing list.
	 *
	 * @param detals
	 * 	The list to add the replacements to.
	 * @param scn
	 * 	The source to read the replacements from.
	 */
	public static List<ReplPair> readList(List<ReplPair> detals, Scanner scn) {
		while (scn.hasNextLine()) {
			String name = scn.nextLine().trim();
			if (name.equals("")) continue;
			if (name.startsWith("#")) continue;

			String body;
			do {
				body = scn.nextLine().trim();
			} while (body.startsWith("#"));

			detals.add(new ReplPair(name, body));
		}

		return detals;
	}
}