Skip to content

Commit

Permalink
Support extending content handler
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Jun 18, 2022
1 parent 03da0cd commit 8816001
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
24 changes: 19 additions & 5 deletions src/main/java/net/fortuna/ical4j/data/CalendarBuilder.java
Expand Up @@ -115,6 +115,12 @@ public CalendarBuilder(CalendarParser parser, TimeZoneRegistry tzRegistry) {
this.contentHandler = new DefaultContentHandler(this, tzRegistry);
}

public CalendarBuilder(CalendarParser parser, TimeZoneRegistry tzRegistry, ContentHandler contentHandler) {
this.parser = parser;
this.tzRegistry = tzRegistry;
this.contentHandler = contentHandler;
}

/**
* @param parser a custom calendar parser
* @param tzRegistry a custom timezone registry
Expand All @@ -123,30 +129,38 @@ public CalendarBuilder(CalendarParser parser, TimeZoneRegistry tzRegistry) {
public CalendarBuilder(CalendarParser parser, PropertyFactoryRegistry propertyFactoryRegistry,
ParameterFactoryRegistry parameterFactoryRegistry, TimeZoneRegistry tzRegistry) {

this(parser, new ContentHandlerContext().withParameterFactorySupplier(parameterFactoryRegistry)
.withPropertyFactorySupplier(propertyFactoryRegistry), tzRegistry);
this.parser = parser;
this.tzRegistry = tzRegistry;
this.contentHandler = new DefaultContentHandler(this, tzRegistry,
new ContentHandlerContext().withParameterFactorySupplier(parameterFactoryRegistry)
.withPropertyFactorySupplier(propertyFactoryRegistry));
}

/**
* @param parser a custom calendar parser
* @param tzRegistry a custom timezone registry
* @deprecated use {@link CalendarBuilder#CalendarBuilder(CalendarParser, ContentHandlerContext, TimeZoneRegistry)}
* @deprecated use {@link CalendarBuilder#CalendarBuilder(CalendarParser,TimeZoneRegistry, ContentHandler)}
*/
@Deprecated
public CalendarBuilder(CalendarParser parser, Supplier<List<ParameterFactory<?>>> parameterFactorySupplier,
Supplier<List<PropertyFactory<?>>> propertyFactorySupplier,
Supplier<List<ComponentFactory<?>>> componentFactorySupplier,
TimeZoneRegistry tzRegistry) {

this(parser, new ContentHandlerContext().withParameterFactorySupplier(parameterFactorySupplier)
this.parser = parser;
this.tzRegistry = tzRegistry;
this.contentHandler = new DefaultContentHandler(this, tzRegistry,
new ContentHandlerContext().withParameterFactorySupplier(parameterFactorySupplier)
.withPropertyFactorySupplier(propertyFactorySupplier)
.withComponentFactorySupplier(componentFactorySupplier), tzRegistry);
.withComponentFactorySupplier(componentFactorySupplier));
}

/**
* @param parser a custom calendar parser
* @param tzRegistry a custom timezone registry
* @deprecated use {@link CalendarBuilder#CalendarBuilder(CalendarParser, TimeZoneRegistry, ContentHandler)}
*/
@Deprecated
public CalendarBuilder(CalendarParser parser, ContentHandlerContext contentHandlerContext,
TimeZoneRegistry tzRegistry) {

Expand Down
11 changes: 6 additions & 5 deletions src/main/java/net/fortuna/ical4j/data/DefaultContentHandler.java
Expand Up @@ -20,16 +20,16 @@ public class DefaultContentHandler implements ContentHandler {

private final Consumer<Calendar> consumer;

private PropertyBuilder propertyBuilder;
protected PropertyBuilder propertyBuilder;

/**
* The current component builders.
*/
private final LinkedList<ComponentBuilder<CalendarComponent>> components = new LinkedList<>();
protected final LinkedList<ComponentBuilder<CalendarComponent>> components = new LinkedList<>();

private List<Property> calendarProperties;
protected List<Property> calendarProperties;

private List<CalendarComponent> calendarComponents;
protected List<CalendarComponent> calendarComponents;

public DefaultContentHandler(Consumer<Calendar> consumer, TimeZoneRegistry tzRegistry) {
this(consumer, tzRegistry, new ContentHandlerContext());
Expand Down Expand Up @@ -127,7 +127,8 @@ public void endComponent(String name) {
@Override
public void startProperty(String name) {
if (!context.getIgnoredPropertyNames().contains(name.toUpperCase())) {
propertyBuilder = new PropertyBuilder(context.getPropertyFactorySupplier().get()).name(name).timeZoneRegistry(tzRegistry);
propertyBuilder = new PropertyBuilder(context.getPropertyFactorySupplier().get()).name(name)
.timeZoneRegistry(tzRegistry);
} else {
propertyBuilder = null;
}
Expand Down

0 comments on commit 8816001

Please sign in to comment.