Skip to content

Commit

Permalink
Added support for suppressed invalid properties when parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Jan 5, 2023
1 parent 9426228 commit 8e58da3
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/main/java/net/fortuna/ical4j/data/ContentHandlerContext.java
Expand Up @@ -21,12 +21,15 @@ public class ContentHandlerContext {

private List<String> ignoredPropertyNames = Collections.emptyList();

private boolean supressInvalidProperties;

public ContentHandlerContext withParameterFactorySupplier(Supplier<List<ParameterFactory<?>>> parameterFactorySupplier) {
ContentHandlerContext context = new ContentHandlerContext();
context.parameterFactorySupplier = parameterFactorySupplier;
context.propertyFactorySupplier = this.propertyFactorySupplier;
context.componentFactorySupplier = this.componentFactorySupplier;
context.ignoredPropertyNames = this.ignoredPropertyNames;
context.supressInvalidProperties = this.supressInvalidProperties;
return context;
}

Expand All @@ -36,6 +39,7 @@ public ContentHandlerContext withPropertyFactorySupplier(Supplier<List<PropertyF
context.propertyFactorySupplier = propertyFactorySupplier;
context.componentFactorySupplier = this.componentFactorySupplier;
context.ignoredPropertyNames = this.ignoredPropertyNames;
context.supressInvalidProperties = this.supressInvalidProperties;
return context;
}

Expand All @@ -45,6 +49,7 @@ public ContentHandlerContext withComponentFactorySupplier(Supplier<List<Componen
context.propertyFactorySupplier = this.propertyFactorySupplier;
context.componentFactorySupplier = componentFactorySupplier;
context.ignoredPropertyNames = this.ignoredPropertyNames;
context.supressInvalidProperties = this.supressInvalidProperties;
return context;
}

Expand All @@ -54,6 +59,17 @@ public ContentHandlerContext withIgnoredPropertyNames(List<String> ignoredProper
context.propertyFactorySupplier = this.propertyFactorySupplier;
context.componentFactorySupplier = this.componentFactorySupplier;
context.ignoredPropertyNames = ignoredPropertyNames;
context.supressInvalidProperties = this.supressInvalidProperties;
return context;
}

public ContentHandlerContext withSupressInvalidProperties(boolean supressInvalidProperties) {
ContentHandlerContext context = new ContentHandlerContext();
context.parameterFactorySupplier = this.parameterFactorySupplier;
context.propertyFactorySupplier = this.propertyFactorySupplier;
context.componentFactorySupplier = this.componentFactorySupplier;
context.ignoredPropertyNames = this.ignoredPropertyNames;
context.supressInvalidProperties = supressInvalidProperties;
return context;
}

Expand All @@ -72,4 +88,8 @@ public Supplier<List<ComponentFactory<?>>> getComponentFactorySupplier() {
public List<String> getIgnoredPropertyNames() {
return ignoredPropertyNames;
}

public boolean isSupressInvalidProperties() {
return supressInvalidProperties;
}
}
13 changes: 11 additions & 2 deletions src/main/java/net/fortuna/ical4j/data/DefaultContentHandler.java
Expand Up @@ -156,7 +156,17 @@ public void propertyValue(String value) {
public void endProperty(String name) throws URISyntaxException, ParseException, IOException {
if (!context.getIgnoredPropertyNames().contains(name.toUpperCase())) {
assertProperty(propertyBuilder);
Property property = propertyBuilder.build();
Property property;
try {
property = propertyBuilder.build();
} catch (URISyntaxException | ParseException | IOException e) {
if (context.isSupressInvalidProperties()) {
LOG.warn("Suppressing invalid property", e);
return;
} else {
throw e;
}
}

if (propertyHasTzId) {
propertiesWithTzId.add(property);
Expand All @@ -168,7 +178,6 @@ public void endProperty(String name) throws URISyntaxException, ParseException,
} else if (calendar != null) {
calendar.getProperties().add(property);
}
property = null;
}
}

Expand Down
Expand Up @@ -69,4 +69,37 @@ class DefaultContentHandlerTest extends Specification {
}
}
}

def 'test supressed invalid properties'() {
given: 'a calendar reference'
def result

and: 'a content handler instance with supressed invalid properties'
DefaultContentHandler contentHandler = [{result = it} as Consumer<Calendar>,
TimeZoneRegistryFactory.instance.createRegistry(),
new ContentHandlerContext().withSupressInvalidProperties(true)]

when: 'a calendar is parsed'
contentHandler.startCalendar()
contentHandler.startComponent('vevent')
contentHandler.startProperty('dtstart')
contentHandler.parameter('value', 'DATE')
contentHandler.propertyValue('20181212')
contentHandler.endProperty('dtstart')
contentHandler.startProperty('dtend')
contentHandler.parameter('value', 'DATE')
contentHandler.propertyValue('20181213T120000Z')
contentHandler.endProperty('dtend')
contentHandler.endComponent('vevent')
contentHandler.endCalendar()

then: 'the resulting calendar doesn\'t include suppressed properties'
result == new ContentBuilder().with {
calendar {
vevent {
dtstart '20181212', parameters: parameters { value 'DATE' }
}
}
}
}
}

0 comments on commit 8e58da3

Please sign in to comment.