summaryrefslogtreecommitdiff
path: root/clformat/src/main/java/bjc/utils/ioutils/format/CLString.java
blob: 6d7750307430e7e6317c7fbff8ea9a49edfab4aa (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package bjc.utils.ioutils.format;

import java.io.*;
import java.util.*;

import bjc.esodata.*;
import bjc.utils.ioutils.ReportWriter;
import bjc.utils.ioutils.format.directives.*;

/**
 * Represents a compiled format string.
 *
 * @author Ben Culkin
 */
public class CLString {
	private List<Edict> edicts;

	/**
	 * Create a new compiled format string.
	 *
	 * @param edts
	 *             The compiled directives that make up the format.
	 */
	public CLString(List<Edict> edts) {
		edicts = edts;
	}

	/**
	 * Execute this string with the given parameters.
	 *
	 * @param parms
	 *              The format parameters for the string.
	 *
	 * @return The result from executing the format string.
	 *
	 * @throws IOException
	 *                     If something I/O related has gone wrong.
	 */
	public String format(Object... parms) throws IOException {
		StringWriter sw = new StringWriter();
		try (ReportWriter rw = new ReportWriter(sw)) {
			return format(rw, parms);
		}
	}

	/**
	 * Execute this string with the given parameters.
	 *
	 * @param rw
	 *             The writer to write the string to.
	 *
	 * @param itms
	 *             The format parameters to use.
	 *
	 * @return The result of executing the string.
	 *
	 * @throws IOException
	 *                     If something I/O related goes wrong.
	 */
	public String format(ReportWriter rw, Tape<Object> itms) throws IOException {
		FormatContext formCTX = new FormatContext(rw, itms);

		return format(formCTX);
	}

	/**
	 * Execute this string with the given parameters.
	 *
	 * @param rw
	 *              The writer to write the string to.
	 *
	 * @param parms
	 *              The format parameters to use.
	 *
	 * @return The result of executing the string.
	 *
	 * @throws IOException
	 *                     If something I/O related goes wrong.
	 */
	public String format(ReportWriter rw, Object... parms) throws IOException {
		Tape<Object> itms = new SingleTape<>(parms);

		FormatContext formCTX = new FormatContext(rw, itms);

		return format(formCTX);
	}

	/**
	 * Execute a format string in a given context.
	 *
	 * @param formCTX
	 *                The context to use for formatting.
	 * 
	 * @return The result of executing the format string.
	 *
	 * @throws IOException
	 *                     If something I/O related goes wrong.
	 */
	public String format(FormatContext formCTX) throws IOException {
		try {
			for (Edict edt : edicts) edt.format(formCTX);
		} catch (DirectiveEscape eex) {
			// General escape exception, so stop formatting.
		}

		return formCTX.writer.toString();
	}

	/**
	 * Is this format string empty? (does it have 0 edicts?)
	 *
	 * @return If this format string is empty.
	 */
	public boolean isEmpty() {
		if (edicts.size() == 0) return true;
		else                    return false;
	}

	@Override
	public String toString() {
		return String.format("CLString [edicts=%s]", edicts);
	}
}