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

import java.io.InputStream;

import bjc.utils.ioutils.RuleBasedConfigReader;

import static bjc.utils.ioutils.RuleBasedReaderPragmas.buildInteger;
import static bjc.utils.ioutils.RuleBasedReaderPragmas.buildStringCollapser;

/**
 * Read a component description from a file.
 *
 * @author ben
 */
public class ComponentDescriptionFileParser {
	/* The reader used to read in component descriptions */
	private static RuleBasedConfigReader<ComponentDescriptionState> reader;

	/* Initialize the reader and its pragmas. */
	static {
		/*
		 * This reader works entirely off of pragmas, so no need to
		 * handle rules.
		 */
		reader = new RuleBasedConfigReader<>((tokenizer, statePair) -> {
			/* Don't need to do anything on rule start. */
		}, (tokenizer, state) -> {
			/* Don't need to do anything on rule continuation. */
		}, (state) -> {
			/* Don't need to do anything on rule end. */
		});

		/* Setup reader pragmas. */
		setupReaderPragmas();
	}

	/**
	 * Parse a component description from a stream.
	 *
	 * @param inputSource
	 *        The stream to parse from.
	 *
	 * @return The description parsed from the stream.
	 */
	public static ComponentDescription fromStream(final InputStream inputSource) {
		if(inputSource == null) {
			throw new NullPointerException("Input source must not be null");
		}

		ComponentDescriptionState state = new ComponentDescriptionState();
		/*
		 * This is valid, because the thing that is returned is the same
		 * reference we passed in.
		 */
		reader.fromStream(inputSource, state);

		return state.toDescription();
	}

	/* Create all the pragmas the reader needs to function. */
	private static void setupReaderPragmas() {
		reader.addPragma("name", buildStringCollapser("name", (name, state) -> state.setName(name)));

		reader.addPragma("author", buildStringCollapser("author", (author, state) -> state.setAuthor(author)));

		reader.addPragma("description", buildStringCollapser("description",
				(description, state) -> state.setDescription(description)));

		reader.addPragma("version", buildInteger("version", (version, state) -> state.setVersion(version)));
	}
}