/
PropertyBuilder.java
109 lines (89 loc) · 3.48 KB
/
PropertyBuilder.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
package net.fortuna.ical4j.model;
import net.fortuna.ical4j.model.property.DateListProperty;
import net.fortuna.ical4j.model.property.DateProperty;
import net.fortuna.ical4j.model.property.XProperty;
import org.apache.commons.codec.DecoderException;
import java.util.ArrayList;
import java.util.List;
/**
* Provides a configurable builder for creating {@link Property} instances from {@link String} values.
*
* You can specify an arbitrary list of supported property factories, and a list of property names to ignore.
*/
public class PropertyBuilder extends AbstractContentBuilder {
private final List<PropertyFactory<?>> factories;
private String name;
private String value;
private final List<Parameter> parameters = new ArrayList<>();
private TimeZoneRegistry timeZoneRegistry;
public PropertyBuilder() {
this(new ArrayList<>());
}
public PropertyBuilder(List<PropertyFactory<? extends Property>> factories) {
this.factories = factories;
}
/**
* Set the list of property factories supporting this builder instance.
* @param factories a list of property factories
* @return the builder instance
* @deprecated preference the constructor option for specifying factories
*/
@Deprecated
public PropertyBuilder factories(List<PropertyFactory<?>> factories) {
this.factories.clear();
this.factories.addAll(factories);
return this;
}
public PropertyBuilder name(String name) {
// property names are case-insensitive, but convert to upper case to simplify further processing
this.name = name.toUpperCase();
return this;
}
public PropertyBuilder value(String value) {
// remove any trailing whitespace
this.value = value.trim();
return this;
}
public PropertyBuilder parameter(Parameter parameter) {
parameters.add(parameter);
return this;
}
public PropertyBuilder timeZoneRegistry(TimeZoneRegistry timeZoneRegistry) {
this.timeZoneRegistry = timeZoneRegistry;
return this;
}
public Property build() {
Property property = null;
String decodedValue;
try {
decodedValue = PropertyCodec.INSTANCE.decode(value);
} catch (DecoderException e) {
decodedValue = value;
}
for (PropertyFactory<?> factory : factories) {
if (factory.supports(name)) {
property = factory.createProperty(new ParameterList(parameters), value);
if (property instanceof DateProperty) {
((DateProperty<?>) property).setTimeZoneRegistry(timeZoneRegistry);
property.setValue(value);
} else if (property instanceof DateListProperty) {
((DateListProperty<?>) property).setTimeZoneRegistry(timeZoneRegistry);
property.setValue(value);
}
}
}
if (property == null) {
if (isExperimentalName(name)) {
property = new XProperty(name, new ParameterList(parameters), value);
} else if (allowIllegalNames()) {
property = new XProperty(name, new ParameterList(parameters), value);
} else {
throw new IllegalArgumentException("Illegal property [" + name + "]");
}
}
if (property instanceof Encodable) {
property.setValue(decodedValue);
}
return property;
}
}