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

import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import bjc.utils.ioutils.SimpleProperties;

/**
 * Utility class for reading in the pattern for parsing format directives.
 *
 * @author bjculkin
 *
 */
public class CLPattern {
	private static String prefixParam;
	private static String formatMod;
	private static String directiveName;

	private static String prefixList;
	private static String formatDirective;

	private static Pattern pFormatDirective;

	static {
		SimpleProperties props = new SimpleProperties();

		try (InputStream is = CLFormatter.class.getResourceAsStream("/clformat.sprop")) {
			props.loadFrom(is, false);
		} catch (IOException ioex) {
			// WELP, we failed. Bail
			throw new RuntimeException("Couldn't load formats for formatter");
		}

		String seqPrefixParam = props.get("clFormatPrefixParam");

		prefixParam = String.format(props.get("clFormatPrefix"), seqPrefixParam);
		formatMod = props.get("clFormatModifier");
		directiveName = props.get("clFormatName");

		prefixList = String.format(props.get("delimSeparatedList"), prefixParam, ",");
		formatDirective = String.format(props.get("clFormatDirective"), prefixList,
				formatMod, directiveName);

		pFormatDirective = Pattern.compile(formatDirective);
	}

	/**
	 * Get a matcher for FORMAT directives.
	 *
	 * @param inp
	 *            The string to parse directives from.
	 * @return A matcher for format directives in the string.          
	 */
	public static Matcher getDirectiveMatcher(String inp) {
		return pFormatDirective.matcher(inp);
	}
}