summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/storage/StorageObject.java
blob: 5840e33a7fe890e5cbcb07e621af6158135b4258 (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package com.earth2me.essentials.storage;

import java.io.PrintWriter;
import java.io.Reader;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.TypeDescription;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;


public class StorageObject
{
	protected Class<? extends StorageObject> clazz;

	protected StorageObject()
	{
	}
	private static Map<Class, Constructor> constructors = new HashMap<Class, Constructor>();

	public static <T extends StorageObject> T load(Class<? extends T> clazz, Reader reader)
	{
		Constructor constructor;
		if (constructors.containsKey(clazz))
		{
			constructor = constructors.get(clazz);
		}
		else
		{
			constructor = prepareConstructor(clazz);
			constructors.put(clazz, constructor);
		}

		final Yaml yaml = new Yaml(constructor);
		T ret = (T)yaml.load(reader);
		if (ret == null)
		{
			try
			{
				ret = (T)clazz.newInstance();
			}
			catch (InstantiationException ex)
			{
				Logger.getLogger(StorageObject.class.getName()).log(Level.SEVERE, null, ex);
			}
			catch (IllegalAccessException ex)
			{
				Logger.getLogger(StorageObject.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
		ret.clazz = clazz;
		return ret;
	}

	private static Constructor prepareConstructor(final Class<?> clazz)
	{
		final Constructor constructor = new Constructor(clazz);
		final Set<Class> classes = new HashSet<Class>();

		prepareConstructor(constructor, classes, clazz);
		return constructor;
	}

	private static void prepareConstructor(final Constructor constructor, final Set<Class> classes, final Class clazz)
	{
		classes.add(clazz);
		final TypeDescription description = new TypeDescription(clazz);
		for (Field field : clazz.getDeclaredFields())
		{
			final ListType listType = field.getAnnotation(ListType.class);
			if (listType != null)
			{
				description.putListPropertyType(field.getName(), listType.value());
				if (StorageObject.class.isAssignableFrom(listType.value())
					&& !classes.contains(listType.value()))
				{
					prepareConstructor(constructor, classes, listType.value());
				}
			}
			final MapType mapType = field.getAnnotation(MapType.class);
			if (mapType != null)
			{
				description.putMapPropertyType(field.getName(), String.class, mapType.value());
				if (StorageObject.class.isAssignableFrom(mapType.value())
					&& !classes.contains(mapType.value()))
				{
					prepareConstructor(constructor, classes, mapType.value());
				}
			}
			if (StorageObject.class.isAssignableFrom(field.getType())
				&& !classes.contains(field.getType()))
			{
				prepareConstructor(constructor, classes, field.getType());
			}
		}
		constructor.addTypeDescription(description);
	}
	private transient Yaml yaml;

	public void save(final PrintWriter writer)
	{
		final DumperOptions ops = new DumperOptions();
		yaml = new Yaml(ops);
		try
		{
			writeToFile(this, writer, 0, clazz);
		}
		catch (IllegalArgumentException ex)
		{
			Logger.getLogger(StorageObject.class.getName()).log(Level.SEVERE, null, ex);
		}
		catch (IllegalAccessException ex)
		{
			Logger.getLogger(StorageObject.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	private void writeToFile(final Object object, final PrintWriter writer, final int depth, final Class clazz) throws IllegalArgumentException, IllegalAccessException
	{
		for (Field field : clazz.getDeclaredFields())
		{
			final int modifier = field.getModifiers();
			if (Modifier.isPrivate(modifier) && !Modifier.isTransient(depth) && !Modifier.isStatic(depth))
			{
				field.setAccessible(true);
				final boolean commentPresent = field.isAnnotationPresent(Comment.class);
				final String name = field.getName();
				if (commentPresent)
				{
					final Comment comments = field.getAnnotation(Comment.class);
					for (String comment : comments.value())
					{
						final String trimmed = comment.trim();
						if (trimmed.isEmpty())
						{
							continue;
						}
						writeIndention(writer, depth);
						writer.print("# ");
						writer.print(trimmed);
						writer.println();
					}
				}

				final Object data = field.get(object);
				if (data == null && !commentPresent)
				{
					continue;
				}
				writeIndention(writer, depth);
				if (data == null && commentPresent)
				{
					writer.print('#');
				}
				writer.print(name);
				writer.print(": ");
				if (data == null && commentPresent)
				{
					writer.println();
					continue;
				}
				if (data instanceof StorageObject)
				{
					writer.println();
					writeToFile(data, writer, depth + 1, data.getClass());
				}
				else if (data instanceof Map)
				{
					writer.println();
					for (Entry<String, Object> entry : ((Map<String, Object>)data).entrySet())
					{
						final Object value = entry.getValue();
						if (value != null)
						{
							writeIndention(writer, depth + 1);
							writer.print(entry.getKey());
							writer.print(": ");
							if (value instanceof StorageObject)
							{
								writer.println();
								writeToFile(value, writer, depth + 2, value.getClass());
							}
							else if (value instanceof String || value instanceof Boolean || value instanceof Number)
							{
								yaml.dumpAll(Collections.singletonList(value).iterator(), writer);
							}
							else
							{
								throw new UnsupportedOperationException();
							}

						}
					}
				}
				else if (data instanceof Collection)
				{
					writer.println();
					for (Object entry : (Collection<Object>)data)
					{
						if (entry != null)
						{
							writeIndention(writer, depth + 1);
							writer.print("- ");
							if (entry instanceof String || entry instanceof Boolean || entry instanceof Number)
							{
								yaml.dumpAll(Collections.singletonList(entry).iterator(), writer);
							}
							else
							{
								throw new UnsupportedOperationException();
							}
						}
					}
				}
				else if (data instanceof String || data instanceof Boolean || data instanceof Number)
				{
					yaml.dumpAll(Collections.singletonList(data).iterator(), writer);
				}
				else
				{
					throw new UnsupportedOperationException();
				}
			}
		}
	}

	private void writeIndention(final PrintWriter writer, final int depth)
	{
		for (int i = 0; i < depth; i++)
		{
			writer.print("  ");
		}
	}
}