Skip to content

Commit

Permalink
Added support for ignored property names
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Jul 24, 2021
1 parent a84007e commit ab65a7f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
Expand Up @@ -127,7 +127,7 @@ public void endComponent(String name) {

@Override
public void startProperty(String name) {
propertyBuilder = new PropertyBuilder().factories(propertyFactorySupplier.get()).name(name);
propertyBuilder = new PropertyBuilder(propertyFactorySupplier.get()).name(name);
propertyHasTzId = false;
}

Expand Down
28 changes: 23 additions & 5 deletions src/main/java/net/fortuna/ical4j/model/PropertyBuilder.java
Expand Up @@ -6,22 +6,37 @@
import java.io.IOException;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
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 List<PropertyFactory<?>> factories = new ArrayList<>();
private final List<PropertyFactory<?>> factories;

private final List<String> ignoredNames;

private String name;

private String value;

private ParameterList parameters = new ParameterList();

public PropertyBuilder factories(List<PropertyFactory<?>> factories) {
this.factories.addAll(factories);
return this;
public PropertyBuilder() {
this(Collections.emptyList(), Collections.emptyList());
}

public PropertyBuilder(List<PropertyFactory<?>> factories) {
this(factories, Collections.emptyList());
}

public PropertyBuilder(List<PropertyFactory<?>> factories, List<String> ignoredNames) {
this.factories = factories;
this.ignoredNames = ignoredNames;
}

public PropertyBuilder name(String name) {
Expand All @@ -42,6 +57,9 @@ public PropertyBuilder parameter(Parameter parameter) {
}

public Property build() throws ParseException, IOException, URISyntaxException {
if (ignoredNames.contains(name)) {
throw new IllegalArgumentException("Unsupported property name: " + name);
}
Property property = null;
String decodedValue;
try {
Expand Down
@@ -1,17 +1,16 @@
package net.fortuna.ical4j.model

import net.fortuna.ical4j.model.property.Version
import spock.lang.Ignore
import spock.lang.Specification

class PropertyBuilderTest extends Specification {

def 'test build property'() {
given: 'a property builder instance'
PropertyBuilder builder = []
PropertyBuilder builder = [Arrays.asList(new Version.Factory())]

and: 'builder is initialised'
builder.factories(Arrays.asList(new Version.Factory())).name('version').value("2.0")
builder.name('version').value("2.0")

when: 'build method called'
Property p = builder.build()
Expand All @@ -20,13 +19,26 @@ class PropertyBuilderTest extends Specification {
p == Version.VERSION_2_0
}

@Ignore
def 'test build invalid property'() {
given: 'a property builder instance'
PropertyBuilder builder = []
PropertyBuilder builder = [Arrays.asList(new Version.Factory())]

and: 'builder is initialised'
builder.name('dtend').value('20150403')

when: 'build method called'
Property p = builder.build()

then: 'an exception is thrown'
thrown(IllegalArgumentException)
}

def 'test build ignored property'() {
given: 'a property builder instance'
PropertyBuilder builder = [[], ['DTEND']]

and: 'builder is initialised'
builder.factories(Arrays.asList(new Version.Factory())).name('dtend').value('20150403')
builder.name('DTEND').value('20150403')

when: 'build method called'
Property p = builder.build()
Expand Down

0 comments on commit ab65a7f

Please sign in to comment.