summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java
blob: 87a749ee76d891f4b576051941bd16e24f2de59d (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package bjc.utils.components;

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

import java.io.InputStream;

import bjc.utils.ioutils.RuleBasedConfigReader;

/**
 * Read a component description from a file.
 *
 * The file format is based entirely off of pragma statements, and should have
 * at least one of each of the following statements
 * <ul>
 * <li>pragma name &lt;component-name&rt;</li>
 * <li>pragma author &lt;component-version&rt;</li>
 * <li>pragma description &lt;component-description&rt;</li>
 * <li>pragma version &lt;component-version&rt;</li>
 * </ul>
 * 
 * @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)));
	}

	private static final class ComponentDescriptionState {
		/* Tentative name of this component. */
		private String name;

		/* Tentative description of this component. */
		private String description;

		/* Tentative author of this component. */
		private String author;

		/* Tentative version of this component. */
		private int version;

		/**
		 * Set the author of this component.
		 *
		 * @param author
		 *               The author of this component.
		 */
		public void setAuthor(final String author) {
			this.author = author;
		}

		/**
		 * Set the description of this component.
		 *
		 * @param description
		 *                    The description of this component.
		 */
		public void setDescription(final String description) {
			this.description = description;
		}

		/**
		 * Set the name of this component.
		 *
		 * @param name
		 *             The name of this component.
		 */
		public void setName(final String name) {
			this.name = name;
		}

		/**
		 * Set the version of this component.
		 *
		 * @param version
		 *                The version of this component.
		 */
		public void setVersion(final int version) {
			this.version = version;
		}

		/**
		 * Convert this state into the description it represents.
		 *
		 * @return The description represented by this state.
		 */
		public ComponentDescription toDescription() {
			return new ComponentDescription(name, author, description, version);
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;

			result = prime * result + (author == null ? 0 : author.hashCode());
			result = prime * result + (description == null ? 0 : description.hashCode());
			result = prime * result + (name == null ? 0 : name.hashCode());
			result = prime * result + version;

			return result;
		}

		@Override
		public boolean equals(final Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;

			final ComponentDescriptionState other = (ComponentDescriptionState) obj;

			if (author == null) {
				if (other.author != null)
					return false;
			} else if (!author.equals(other.author))
				return false;

			if (description == null) {
				if (other.description != null)
					return false;
			} else if (!description.equals(other.description))
				return false;

			if (name == null) {
				if (other.name != null)
					return false;
			} else if (!name.equals(other.name))
				return false;

			if (version != other.version)
				return false;

			return true;
		}

		/*
		 * (non-Javadoc)
		 *
		 * @see java.lang.Object#toString()
		 */
		@Override
		public String toString() {
			final StringBuilder builder = new StringBuilder();
			builder.append("ComponentDescriptionState [");

			if (name != null) {
				builder.append("name=");
				builder.append(name);
				builder.append(", ");
			}

			if (description != null) {
				builder.append("description=");
				builder.append(description);
				builder.append(", ");
			}

			if (author != null) {
				builder.append("author=");
				builder.append(author);
				builder.append(", ");
			}

			builder.append("version=");
			builder.append(version);
			builder.append("]");

			return builder.toString();
		}
	}
}