summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java
blob: bae9790e0e6886f78d9da1583b9b3794f79ef06e (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
package bjc.utils.components;

import java.io.InputStream;

import bjc.utils.funcutils.ListUtils;
import bjc.utils.parserutils.RuleBasedConfigReader;

/**
 * Read a component description from a file
 * 
 * @author ben
 *
 */
public class ComponentDescriptionFileParser {
	private static RuleBasedConfigReader<ComponentDescriptionState> reader;

	static {
		// This reader works entirely off of pragmas, so no need to handle
		// rules
		reader = new RuleBasedConfigReader<>((fst, par) -> {
		}, (fst, stat) -> {
		}, (stat) -> {
		});

		reader.addPragma("name", (fst, stat) -> {
			stat.setName(ListUtils.collapseTokens(fst.toList((s) -> s)));
		});

		reader.addPragma("author", (fst, stat) -> {
			stat.setAuthor(ListUtils.collapseTokens(fst.toList((s) -> s)));
		});

		reader.addPragma("description", (fst, stat) -> {
			stat.setDescription(
					ListUtils.collapseTokens(fst.toList((s) -> s)));
		});

		reader.addPragma("version", (fst, stat) -> {
			stat.setVersion(Integer.parseInt(fst.nextToken()));
		});
	}

	/**
	 * Parse a component description from a stream
	 * 
	 * @param is
	 *            The stream to parse from
	 * @return The description parsed from the stream
	 */
	public static ComponentDescription fromStream(InputStream is) {
		return reader.fromStream(is, new ComponentDescriptionState())
				.toDescription();
	}
}