Skip to content

Commit

Permalink
- Added rule for CreatedDate
Browse files Browse the repository at this point in the history
- Use ServiceLoader to discover rules for Rfc5545RuleManager.
  • Loading branch information
danielgrigore committed Jan 23, 2017
1 parent dc62257 commit 13d25a1
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 53 deletions.
33 changes: 16 additions & 17 deletions src/main/java/net/fortuna/ical4j/model/Calendar.java
Expand Up @@ -31,21 +31,26 @@
*/
package net.fortuna.ical4j.model;

import net.fortuna.ical4j.model.component.CalendarComponent;
import net.fortuna.ical4j.model.property.*;
import net.fortuna.ical4j.model.rfc5545.RuleManager;
import net.fortuna.ical4j.util.Strings;
import net.fortuna.ical4j.validate.*;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import net.fortuna.ical4j.model.component.CalendarComponent;
import net.fortuna.ical4j.model.property.CalScale;
import net.fortuna.ical4j.model.property.Method;
import net.fortuna.ical4j.model.property.ProdId;
import net.fortuna.ical4j.model.property.Version;
import net.fortuna.ical4j.model.rfc5545.RuleManager;
import net.fortuna.ical4j.util.Strings;
import net.fortuna.ical4j.validate.AbstractCalendarValidatorFactory;
import net.fortuna.ical4j.validate.ValidationException;
import net.fortuna.ical4j.validate.Validator;

/**
* $Id$ [Apr 5, 2004]
Expand Down Expand Up @@ -382,18 +387,12 @@ public void conformToRfc5545() throws IllegalAccessException, IllegalArgumentExc

private static void conformPropertiesToRfc5545(List<Property> properties) {
for (Property property : properties) {
Set<Rfc5545PropertyRule<Property>> rulesToApply = RuleManager.getSupportedRulesFor(property);
for (Rfc5545PropertyRule<Property> rule : rulesToApply) {
rule.applyTo(property);
}
RuleManager.applyTo(property);
}
}

private static void conformComponentToRfc5545(Component component){
Set<Rfc5545ComponentRule<Component>> rulesToApply = RuleManager.getSupportedRulesFor(component);
for(Rfc5545ComponentRule<Component> rule : rulesToApply){
rule.applyTo(component);
}
RuleManager.applyTo(component);
}

private static enum CountableProperties{
Expand Down
@@ -0,0 +1,34 @@
package net.fortuna.ical4j.model.rfc5545;

import java.text.ParseException;

import net.fortuna.ical4j.model.Rfc5545PropertyRule;
import net.fortuna.ical4j.model.property.Created;

/**
*
* @author daniel grigore
*
*/
public class CreatedPropertyRule implements Rfc5545PropertyRule<Created> {

private static final String UTC_MARKER = "Z";

@Override
public void applyTo(Created created) {
if (created.isUtc() || created.getTimeZone() != null) {
return;
}
try {
created.setValue(created.getValue() + UTC_MARKER);
} catch (ParseException e) {
// Let the value as it is
}
}

@Override
public Class<Created> getSupportedType() {
return Created.class;
}

}
44 changes: 21 additions & 23 deletions src/main/java/net/fortuna/ical4j/model/rfc5545/RuleManager.java
Expand Up @@ -8,6 +8,7 @@
package net.fortuna.ical4j.model.rfc5545;

import java.util.LinkedHashSet;
import java.util.ServiceLoader;
import java.util.Set;

import net.fortuna.ical4j.model.Component;
Expand All @@ -32,37 +33,34 @@ public class RuleManager {
private static final Set<Rfc5545ComponentRule<? extends Component>> COMPONENT_RULES = new LinkedHashSet<Rfc5545ComponentRule<? extends Component>>();

static {
register(new VAlarmRule());

register(new DatePropertyRule());

register(new DateListPropertyRule());

register(new VEventRule());

register(new TzIdRule());

register(new DTStampRule());

register(new AttendeePropertyRule());
for (Rfc5545PropertyRule<?> rule : ServiceLoader.load(Rfc5545PropertyRule.class)) {
if (rule.getSupportedType() == null) {
throw new NullPointerException();
}
PROPERTY_RULES.add(rule);
}
for (Rfc5545ComponentRule<?> rule : ServiceLoader.load(Rfc5545ComponentRule.class)) {
if (rule.getSupportedType() == null) {
throw new NullPointerException();
}
COMPONENT_RULES.add(rule);
}
}

private static void register(Rfc5545PropertyRule<? extends Property> rule) {
if (rule.getSupportedType() == null) {
throw new NullPointerException();
public static void applyTo(Property element) {
for (Rfc5545PropertyRule<Property> rule : getSupportedRulesFor(element)) {
rule.applyTo(element);
}
PROPERTY_RULES.add(rule);
}

private static void register(Rfc5545ComponentRule<? extends Component> rule) {
if (rule.getSupportedType() == null) {
throw new NullPointerException();
public static void applyTo(Component element) {
for (Rfc5545ComponentRule<Component> rule : getSupportedRulesFor(element)) {
rule.applyTo(element);
}
COMPONENT_RULES.add(rule);
}

@SuppressWarnings("unchecked")
public static Set<Rfc5545PropertyRule<Property>> getSupportedRulesFor(Property element) {
private static Set<Rfc5545PropertyRule<Property>> getSupportedRulesFor(Property element) {
if (element == null) {
throw new NullPointerException();
}
Expand All @@ -76,7 +74,7 @@ public static Set<Rfc5545PropertyRule<Property>> getSupportedRulesFor(Property e
}

@SuppressWarnings("unchecked")
public static Set<Rfc5545ComponentRule<Component>> getSupportedRulesFor(Component element) {
private static Set<Rfc5545ComponentRule<Component>> getSupportedRulesFor(Component element) {
if (element == null) {
throw new NullPointerException();
}
Expand Down
@@ -0,0 +1,2 @@
net.fortuna.ical4j.model.rfc5545.VAlarmRule
net.fortuna.ical4j.model.rfc5545.VEventRule
@@ -0,0 +1,6 @@
net.fortuna.ical4j.model.rfc5545.CreatedPropertyRule
net.fortuna.ical4j.model.rfc5545.DatePropertyRule
net.fortuna.ical4j.model.rfc5545.DateListPropertyRule
net.fortuna.ical4j.model.rfc5545.TzIdRule
net.fortuna.ical4j.model.rfc5545.DTStampRule
net.fortuna.ical4j.model.rfc5545.AttendeePropertyRule
Expand Up @@ -11,49 +11,42 @@

public class AttendeePropertyRuleTest {

private AttendeePropertyRule rule = new AttendeePropertyRule();

@Test
public void shouldCorrectlyRemoveApostrophes() throws URISyntaxException {
Attendee attendee = new Attendee("mailto:'mobile-media-applications@1und1.de'");
attendee.getParameters().add(new Cn("Mobile Media"));
this.rule.applyTo(attendee);
RuleManager.applyTo(attendee);
assertEquals("mailto:mobile-media-applications@1und1.de", attendee.getValue());
}

@Test
public void shouldLeaveAttendeeAsItIs() throws URISyntaxException {
Attendee attendee = new Attendee("mailto:mobile-media-applications@1und1.de");
attendee.getParameters().add(new Cn("Mobile Media"));
this.rule.applyTo(attendee);
RuleManager.applyTo(attendee);
assertEquals("mailto:mobile-media-applications@1und1.de", attendee.getValue());
}

@Test
public void shouldNotThrowExceptionIfAttendeeIsNull() throws URISyntaxException {
this.rule.applyTo(null);
}

@Test
public void shouldNotThrowExceptionIfAttendeeIsEmpty() throws URISyntaxException {
this.rule.applyTo(new Attendee());
RuleManager.applyTo(new Attendee());
}

@Test
public void shouldNotThrowExceptionIfOneApostrophe() throws URISyntaxException {
this.rule.applyTo(new Attendee("mailto:'"));
RuleManager.applyTo(new Attendee("mailto:'"));
}

@Test
public void shouldNotThrowExceptionIfTwoApostrophes() throws URISyntaxException {
this.rule.applyTo(new Attendee("mailto:''"));
RuleManager.applyTo(new Attendee("mailto:''"));
}

@Test
public void shouldNotDoAnythingIfAnotherScheem() throws URISyntaxException {
String value = "http://something";
Attendee attende = new Attendee(value);
this.rule.applyTo(attende);
RuleManager.applyTo(attende);
assertEquals(value, attende.getValue());
}

Expand Down
@@ -0,0 +1,19 @@
package net.fortuna.ical4j.model.rfc5545;

import static org.junit.Assert.assertEquals;

import java.text.ParseException;

import org.junit.Test;

import net.fortuna.ical4j.model.property.Created;

public class CreatedPropertyRuleTest {

@Test
public void shouldSetUtcToBrokenCreatedDate() throws ParseException {
Created created = new Created("20161026T130842");
RuleManager.applyTo(created);
assertEquals("20161026T130842Z", created.getValue());
}
}

0 comments on commit 13d25a1

Please sign in to comment.