-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
PropertyParser.java
148 lines (131 loc) · 5.65 KB
/
PropertyParser.java
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
package net.aufdemrand.denizencore.objects.properties;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.tags.Attribute;
import net.aufdemrand.denizencore.utilities.debugging.dB;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
public class PropertyParser {
public static abstract class PropertyGetter {
public abstract Property get(dObject obj);
}
static Map<Class<? extends dObject>, Map<String, List<PropertyGetter>>> propgetters = new HashMap<Class<? extends dObject>, Map<String, List<PropertyGetter>>>();
public static void registerProperty(PropertyGetter getter, Class<? extends dObject> object) {
registerProperty(getter, object, (String) null);
}
public static void insert(Map<String, List<PropertyGetter>> gettyByTag, String attrl, PropertyGetter getter) {
List<PropertyGetter> getters = gettyByTag.get(attrl);
if (getters == null) {
getters = new ArrayList<PropertyGetter>();
gettyByTag.put(attrl, getters);
}
getters.add(getter);
}
public static void registerProperty(PropertyGetter getter, Class<? extends dObject> object, String... attrLow) {
Map<String, List<PropertyGetter>> gettyByTag = propgetters.get(object);
if (gettyByTag == null) {
gettyByTag = new HashMap<String, List<PropertyGetter>>();
propgetters.put(object, gettyByTag);
}
for (String attrl : attrLow) {
insert(gettyByTag, attrl, getter);
}
insert(gettyByTag, "_all_", getter);
}
public static void registerProperty(final Class property, Class<? extends dObject> object) {
try {
final Method getf = property.getMethod("getFrom", dObject.class);
getf.setAccessible(true);
PropertyGetter getter = new PropertyGetter() {
@Override
public Property get(dObject obj) {
try {
Object o = getf.invoke(null, obj);
if (o instanceof Property) {
return (Property) o;
}
}
catch (IllegalAccessException e) {
dB.echoError(e);
}
catch (InvocationTargetException e) {
dB.echoError(e);
}
return null;
}
};
registerProperty(getter, object);
}
catch (NoSuchMethodException e) {
dB.echoError("Unable to register property '" + property.getSimpleName() + "'!");
}
}
public static String getPropertiesString(dObject object) {
StringBuilder prop_string = new StringBuilder();
// Iterate through each property associated with the dObject type, invoke 'describes'
// and if 'true', add property string from the property to the prop_string.
for (Property property : getProperties(object)) {
String description = property.getPropertyString();
if (description != null) {
prop_string.append(property.getPropertyId()).append('=')
.append(description.replace(';', (char) 0x2011)).append(';');
}
}
// Return the list of properties
if (prop_string.length() > 0) // Remove final semicolon
{
return "[" + prop_string.substring(0, prop_string.length() - 1) + "]";
}
else {
return "";
}
}
public static List<Property> empty = new ArrayList<Property>();
public static List<Property> getProperties(dObject object, String attribLow) {
Map<String, List<PropertyGetter>> gettyByTag = propgetters.get(object.getClass());
if (gettyByTag != null) {
List<Property> returnMe = empty;
List<PropertyGetter> getty = gettyByTag.get(attribLow);
if (getty != null) {
returnMe = new ArrayList<Property>(getty.size());
for (PropertyGetter property : getty) {
Property propGot = property.get(object);
if (propGot != null) {
returnMe.add(propGot);
}
}
}
getty = gettyByTag.get(null);
if (getty != null) {
if (returnMe == empty) {
returnMe = new ArrayList<Property>(getty.size());
}
for (PropertyGetter property : getty) {
Property propGot = property.get(object);
if (propGot != null) {
returnMe.add(propGot);
}
}
}
return returnMe;
}
return empty;
}
public static List<Property> getProperties(dObject object) {
Map<String, List<PropertyGetter>> gettyByTag = propgetters.get(object.getClass());
if (gettyByTag != null) {
List<PropertyGetter> getty = gettyByTag.get("_all_");
if (getty != null) {
List<Property> props = new ArrayList<Property>();
for (PropertyGetter property : getty) {
Property propGot = property.get(object);
if (propGot != null) {
props.add(propGot);
}
}
return props;
}
}
return empty;
}
}