summaryrefslogtreecommitdiff
path: root/clformat/src/main/java/bjc/utils/ioutils/format/directives/LiteralDirective.java
blob: 3e4a5ef6c6f7e2a28eeec594a4a8db9f0a344b4c (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
package bjc.utils.ioutils.format.directives;

import java.io.*;

import bjc.utils.ioutils.format.*;

/**
 * Implements directives that create a literal string.
 *
 * @author student
 *
 */
public class LiteralDirective implements Directive {
	private String lit;

	/**
	 * Create a new literal directive.
	 *
	 * @param lit
	 *                  The string for the directive.
	 * @param directive
	 *                  The character for the directive.
	 */
	public LiteralDirective(String lit, char directive) {
		Character.toString(directive);
		this.lit = lit;
	}

	@Override
	public Edict compile(CompileContext compCTX) {
		CLValue nTimes = null;

		CLParameters params = compCTX.decr.parameters;

		if (params.length() >= 1) {
			params.mapIndices("count");

			nTimes = params.resolveKey("count");
		}

		return new LiteralEdict(lit, nTimes);
	}
}

/*
 * Compiled version of the directive.
 */
class LiteralEdict implements Edict {
	private String lit;
	private CLValue nTimes;

	public LiteralEdict(String lit, CLValue nTimes) {
		this.lit = lit;

		this.nTimes = nTimes;
	}

	@Override
	public void format(FormatContext formCTX) throws IOException {
		int num = 1;

		if (nTimes != null) {
			num = nTimes.asInt(formCTX.items, "occurance count", "literal", 1);
		}

		for (int i = 0; i < num; i++) formCTX.writer.write(lit);
	}
}