-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2567 from bardsoftware/tkt-2548-upgrade-ical4j
Upgrade ical4j library
- Loading branch information
Showing
11 changed files
with
247 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
biz.ganttproject.impex.ical/src/main/java/biz/ganttproject/impex/ical/IcsImport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright 2013-2024 BarD Software s.r.o., Dmitry Barashev. | ||
* | ||
* This file is part of GanttProject, an opensource project management tool. | ||
* | ||
* GanttProject is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* GanttProject is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with GanttProject. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package biz.ganttproject.impex.ical; | ||
|
||
import biz.ganttproject.LoggerApi; | ||
import biz.ganttproject.core.calendar.CalendarEvent; | ||
import biz.ganttproject.core.calendar.GPCalendarCalc; | ||
import biz.ganttproject.core.time.TimeDuration; | ||
import biz.ganttproject.core.time.impl.GPTimeUnitStack; | ||
import com.google.common.collect.Lists; | ||
import net.fortuna.ical4j.data.CalendarBuilder; | ||
import net.fortuna.ical4j.data.ParserException; | ||
import net.fortuna.ical4j.data.UnfoldingReader; | ||
import net.fortuna.ical4j.model.Calendar; | ||
import net.fortuna.ical4j.model.Property; | ||
import net.fortuna.ical4j.model.component.CalendarComponent; | ||
import net.fortuna.ical4j.model.component.VEvent; | ||
import net.fortuna.ical4j.model.property.DateProperty; | ||
import net.fortuna.ical4j.model.property.RRule; | ||
import net.fortuna.ical4j.model.property.Summary; | ||
import net.fortuna.ical4j.transform.recurrence.Frequency; | ||
import net.fortuna.ical4j.util.CompatibilityHints; | ||
import net.sourceforge.ganttproject.GPLogger; | ||
import org.w3c.util.DateParser; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.time.*; | ||
import java.time.temporal.Temporal; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
class IcsImport { | ||
static List<CalendarEvent> readEvents(InputStream input) throws ParserException, IOException { | ||
CompatibilityHints.setHintEnabled(CompatibilityHints.KEY_RELAXED_PARSING, true); | ||
CalendarBuilder builder = new CalendarBuilder(); | ||
List<CalendarEvent> gpEvents = Lists.newArrayList(); | ||
Calendar c = builder.build(new UnfoldingReader(new InputStreamReader(input))); | ||
for (CalendarComponent comp : c.getComponents()) { | ||
if (comp instanceof VEvent) { | ||
VEvent event = (VEvent) comp; | ||
|
||
if (event.getDateTimeStart().isEmpty()) { | ||
LOGGER.debug("No start date found, ignoring. Event={}", new Object[] {event}, Collections.emptyMap()); | ||
continue; | ||
} | ||
var eventStartDate = event.getDateTimeStart().get().getDate(); | ||
if (event.getDateTimeEnd().isEmpty()) { | ||
LOGGER.debug("No end date found, using start date instead. Event={}", new Object[] {event}, Collections.emptyMap()); | ||
} | ||
var eventEndDate = event.getDateTimeEnd().map(DateProperty::getDate).orElse(eventStartDate); | ||
TimeDuration oneDay = GPTimeUnitStack.createLength(GPTimeUnitStack.DAY, 1); | ||
if (eventEndDate != null) { | ||
java.util.Date startDate = GPTimeUnitStack.DAY.adjustLeft(getDate(eventStartDate)); | ||
java.util.Date endDate = GPTimeUnitStack.DAY.adjustLeft(getDate(eventEndDate)); | ||
var recursYearly = new AtomicBoolean(false); | ||
event.getProperty(Property.RRULE).ifPresent(it -> { | ||
if (it instanceof RRule<?> recurrenceRule) { | ||
var frequency = recurrenceRule.getRecur().getFrequency(); | ||
var interval = recurrenceRule.getRecur().getInterval(); | ||
recursYearly.set(frequency == Frequency.YEARLY && interval == 1); | ||
} | ||
}); | ||
while (startDate.compareTo(endDate) < 0) { | ||
var summary = event.getSummary().map(Summary::getValue).orElse(""); | ||
|
||
gpEvents.add(CalendarEvent.newEvent( | ||
startDate, recursYearly.get(), CalendarEvent.Type.HOLIDAY, | ||
summary.trim(), | ||
null)); | ||
startDate = GPCalendarCalc.PLAIN.shiftDate(startDate, oneDay); | ||
} | ||
} | ||
} | ||
} | ||
return gpEvents; | ||
|
||
} | ||
|
||
private static java.util.Date getDate(Temporal t) { | ||
if (t instanceof LocalDate localDate) { | ||
return DateParser.toJavaDate(localDate); | ||
} | ||
if (t instanceof Instant instant) { | ||
return java.util.Date.from(instant); | ||
} | ||
if (t instanceof LocalDateTime localDateTime) { | ||
return DateParser.toJavaDate(localDateTime.toLocalDate()); | ||
} | ||
if (t instanceof ZonedDateTime zonedDateTime) { | ||
return DateParser.toJavaDate(zonedDateTime.toLocalDate()); | ||
} | ||
if (t instanceof OffsetDateTime offsetDateTime) { | ||
return DateParser.toJavaDate(offsetDateTime.toLocalDate()); | ||
} | ||
throw new IllegalArgumentException("Unsupported temporal type: " + t.getClass()); | ||
} | ||
|
||
private static final LoggerApi LOGGER = GPLogger.create("Import.Ics"); | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
...anttproject.impex.ical/src/test/kotlin/biz/ganttproject/impex/ical/IcsFileImporterTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2024 BarD Software s.r.o., Dmitry Barashev. | ||
* | ||
* This file is part of GanttProject, an opensource project management tool. | ||
* | ||
* GanttProject is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* GanttProject is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with GanttProject. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package biz.ganttproject.impex.ical | ||
|
||
import biz.ganttproject.core.calendar.CalendarEvent | ||
import biz.ganttproject.core.time.CalendarFactory | ||
import biz.ganttproject.core.time.CalendarFactory.LocaleApi | ||
import biz.ganttproject.core.time.CalendarFactory.setLocaleApi | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Assertions.assertFalse | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.w3c.util.DateParser | ||
import java.text.DateFormat | ||
import java.util.Locale | ||
|
||
class IcsFileImporterTest { | ||
@BeforeEach | ||
fun setUp() { | ||
object : CalendarFactory() { | ||
init { | ||
setLocaleApi(object : LocaleApi { | ||
override fun getLocale(): Locale { | ||
return Locale.US | ||
} | ||
|
||
override fun getShortDateFormat(): DateFormat { | ||
return DateFormat.getDateInstance(DateFormat.SHORT, Locale.US) | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
@Test | ||
fun `basic smoke test`() { | ||
val events = IcsImport.readEvents(IcsFileImporterTest::class.java.getResourceAsStream("/test.ics")!!) | ||
assertFalse(events.isEmpty()) | ||
assertEquals(DateParser.parse("2023-12-07"), events[0].date) | ||
assertEquals("Chanukah: 1 Candle", events[0].title) | ||
assertEquals(CalendarEvent.Type.HOLIDAY ,events[0].type) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
BEGIN:VCALENDAR | ||
VERSION:2.0 | ||
PRODID:-//hebcal.com/NONSGML Hebcal Calendar v15.0.6//EN | ||
CALSCALE:GREGORIAN | ||
METHOD:PUBLISH | ||
X-LOTUS-CHARSET:UTF-8 | ||
X-PUBLISHED-TTL:PT14D | ||
X-WR-CALNAME:Jewish Holidays ✡️ | ||
X-WR-CALDESC:Major Jewish holidays for the Diaspora from Hebcal.com | ||
X-APPLE-CALENDAR-COLOR:#800002 | ||
BEGIN:VEVENT | ||
DTSTAMP:20240501T202041Z | ||
CATEGORIES:Holiday | ||
SUMMARY: Chanukah: 1 Candle | ||
DTSTART;VALUE=DATE:20231207 | ||
DTEND;VALUE=DATE:20231208 | ||
UID:hebcal-20231207-1c6fa26d | ||
TRANSP:TRANSPARENT | ||
X-MICROSOFT-CDO-BUSYSTATUS:FREE | ||
X-MICROSOFT-CDO-ALLDAYEVENT:TRUE | ||
CLASS:PUBLIC | ||
DESCRIPTION:Hanukkah\, the Jewish festival of rededication. Also known as | ||
the Festival of Lights\, the eight-day festival is observed by lighting th | ||
e candles of a hanukkiah (menorah)\n\nhttps://hebcal.com/h/chanukah-2023?u | ||
c=ical-jewish-holidays-v2 | ||
END:VEVENT | ||
END:VCALENDAR |
Oops, something went wrong.