diff --git a/src/main/java/org/mtransit/parser/DefaultAgencyTools.java b/src/main/java/org/mtransit/parser/DefaultAgencyTools.java index a6fd85c..dae22a2 100644 --- a/src/main/java/org/mtransit/parser/DefaultAgencyTools.java +++ b/src/main/java/org/mtransit/parser/DefaultAgencyTools.java @@ -44,7 +44,7 @@ import org.mtransit.parser.mt.data.MTripIds; import org.mtransit.parser.mt.data.MVerify; -import java.text.SimpleDateFormat; +import java.text.DateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; @@ -127,7 +127,7 @@ public class DefaultAgencyTools implements GAgencyTools { TOMORROW = false; } - private static final SimpleDateFormat DATE_FORMAT = GFieldTypes.makeDateFormat(); + private static final DateFormat DATE_FORMAT = GFieldTypes.makeDateFormat(); @SuppressWarnings({"unused", "WeakerAccess"}) public int getTodayDateInt() { @@ -142,6 +142,7 @@ public static void main(@NotNull String[] args) { @Nullable private HashSet serviceIdInts; + @SuppressWarnings("WeakerAccess") public void start(@NotNull String[] args) { if (args.length < 3) { throw new MTLog.Fatal("Invalid number(%d) of arguments! (%s)", args.length, Arrays.asList(args)); @@ -1223,7 +1224,7 @@ public int getThreadPoolSize() { @Override public Pair getTimes(@NotNull GStopTime gStopTime, @NotNull List tripStopTimes, - @NotNull SimpleDateFormat timeFormat) { + @NotNull DateFormat timeFormat) { if (!gStopTime.hasArrivalTime() || !gStopTime.hasDepartureTime()) { return extractTimes(gStopTime, tripStopTimes, timeFormat); } else { @@ -1236,7 +1237,7 @@ public Pair getTimes(@NotNull GStopTime gStopTime, @NotNull private static Pair extractTimes(GStopTime gStopTime, @NotNull List tripStopTimes, - SimpleDateFormat timeFormat) { + DateFormat timeFormat) { try { Pair timesInMs = extractTimeInMs(gStopTime, tripStopTimes); long arrivalTimeInMs = timesInMs.first; @@ -1650,7 +1651,7 @@ && diffLowerThan(DATE_FORMAT, c, p.getTodayStringInt(), initialTodayStringInt, M return todayServiceIds; } - static void parseCalendars(@NotNull List gCalendars, @Nullable List gCalendarDates, SimpleDateFormat DATE_FORMAT, Calendar c, Period p, boolean lookBackward) { + static void parseCalendars(@NotNull List gCalendars, @Nullable List gCalendarDates, DateFormat DATE_FORMAT, Calendar c, Period p, boolean lookBackward) { findCalendarsTodayPeriod(gCalendars, gCalendarDates, p, lookBackward); if (p.getStartDate() == null || p.getEndDate() == null) { MTLog.log("[parse-calendars] > NO schedule available for %s in calendars. (start:%s|end:%s)", p.getTodayStringInt(), p.getStartDate(), p.getEndDate()); @@ -1910,7 +1911,7 @@ private static Period getEntirePeriodMinMaxDate(List gCalendars, List return p; } - public static int incDateDays(@NotNull SimpleDateFormat dateFormat, + public static int incDateDays(@NotNull DateFormat dateFormat, @NotNull Calendar calendar, @Nullable Integer dateInt, int numberOfDays) { @@ -1923,7 +1924,8 @@ public static int incDateDays(@NotNull SimpleDateFormat dateFormat, } } - public static boolean diffLowerThan(@NotNull SimpleDateFormat dateFormat, + @SuppressWarnings("WeakerAccess") + public static boolean diffLowerThan(@NotNull DateFormat dateFormat, @NotNull Calendar calendar, @Nullable Integer startDateInt, @Nullable Integer endDateInt, @@ -1937,7 +1939,7 @@ public static boolean diffLowerThan(@NotNull SimpleDateFormat dateFormat, @SuppressWarnings("WeakerAccess") public static long diffInMs( - @NotNull SimpleDateFormat dateFormat, + @NotNull DateFormat dateFormat, @NotNull Calendar calendar, @Nullable Integer startDateInt, @Nullable Integer endDateInt diff --git a/src/main/java/org/mtransit/parser/gtfs/GAgencyTools.java b/src/main/java/org/mtransit/parser/gtfs/GAgencyTools.java index 767fd0a..138fe69 100644 --- a/src/main/java/org/mtransit/parser/gtfs/GAgencyTools.java +++ b/src/main/java/org/mtransit/parser/gtfs/GAgencyTools.java @@ -13,11 +13,11 @@ import org.mtransit.parser.gtfs.data.GStopTime; import org.mtransit.parser.gtfs.data.GTrip; import org.mtransit.parser.mt.data.MAgency; +import org.mtransit.parser.mt.data.MDirection; import org.mtransit.parser.mt.data.MDirectionCardinalType; import org.mtransit.parser.mt.data.MRoute; -import org.mtransit.parser.mt.data.MDirection; -import java.text.SimpleDateFormat; +import java.text.DateFormat; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -319,7 +319,7 @@ public interface GAgencyTools { @NotNull Pair getTimes(@NotNull GStopTime gStopTime, @NotNull List tripStopTimes, - @NotNull SimpleDateFormat timeFormat); + @NotNull DateFormat timeFormat); // FREQUENCY int getStartTime(@NotNull GFrequency gFrequency); diff --git a/src/main/java/org/mtransit/parser/gtfs/data/GCalendar.kt b/src/main/java/org/mtransit/parser/gtfs/data/GCalendar.kt index b0db94d..9a85573 100644 --- a/src/main/java/org/mtransit/parser/gtfs/data/GCalendar.kt +++ b/src/main/java/org/mtransit/parser/gtfs/data/GCalendar.kt @@ -2,7 +2,6 @@ package org.mtransit.parser.gtfs.data import androidx.annotation.Discouraged import org.mtransit.parser.MTLog -import org.mtransit.parser.db.SQLUtils.escapeId import java.util.Calendar // https://developers.google.com/transit/gtfs/reference#calendar_fields @@ -74,12 +73,6 @@ data class GCalendar( private val _serviceId: String get() = GIDs.getString(serviceIdInt) - val escapedServiceId: String - get() = _serviceId.escapeId() - - val escapedServiceIdInt: Int - get() = escapedServiceId.toGIDInt() - val dates: List by lazy { initAllDates( serviceIdInt, diff --git a/src/main/java/org/mtransit/parser/gtfs/data/GFieldTypes.kt b/src/main/java/org/mtransit/parser/gtfs/data/GFieldTypes.kt index cc0868c..cfab865 100644 --- a/src/main/java/org/mtransit/parser/gtfs/data/GFieldTypes.kt +++ b/src/main/java/org/mtransit/parser/gtfs/data/GFieldTypes.kt @@ -1,6 +1,7 @@ package org.mtransit.parser.gtfs.data import org.mtransit.commons.Constants.SPACE_ +import java.text.DateFormat import java.text.SimpleDateFormat import java.util.Calendar import java.util.Date @@ -16,82 +17,48 @@ object GFieldTypes { const val DATE_FORMAT_PATTERN = "yyyyMMdd" - @Deprecated(message = "NOT thread-safe", replaceWith = ReplaceWith("GFieldTypes.makeTimeFormat()")) - @JvmField - val TIME_FORMAT = makeTimeFormat() - - @Deprecated(message = "NOT thread-safe", replaceWith = ReplaceWith("GFieldTypes.makeDateFormat()")) - @JvmField - val DATE_FORMAT = makeDateFormat() - - @Deprecated(message = "NOT thread-safe", replaceWith = ReplaceWith("GFieldTypes.makeDateAndTimeFormat()")) - @JvmField - val DATE_TIME_FORMAT = makeDateAndTimeFormat() - @JvmStatic - fun makeTimeFormat(): SimpleDateFormat { - return SimpleDateFormat(TIME_FORMAT_PATTERN, Locale.ENGLISH) - } + fun makeTimeFormat(): DateFormat = + SimpleDateFormat(TIME_FORMAT_PATTERN, Locale.ENGLISH) @JvmStatic - fun makeDateFormat(): SimpleDateFormat { - return SimpleDateFormat(DATE_FORMAT_PATTERN, Locale.ENGLISH) - } + fun makeDateFormat(): DateFormat = + SimpleDateFormat(DATE_FORMAT_PATTERN, Locale.ENGLISH) @JvmStatic - fun makeDateAndTimeFormat(): SimpleDateFormat { - return SimpleDateFormat(DATE_FORMAT_PATTERN + SPACE_ + TIME_FORMAT_PATTERN, Locale.ENGLISH) - } + fun makeDateAndTimeFormat(): DateFormat = + SimpleDateFormat(DATE_FORMAT_PATTERN + SPACE_ + TIME_FORMAT_PATTERN, Locale.ENGLISH) - @Suppress("DEPRECATION") - @JvmOverloads @JvmStatic - fun toDate(dateFormat: SimpleDateFormat = DATE_FORMAT, gDateInt: Int): Date = toDate(dateFormat, gDateInt.toString()) + fun toDate(dateFormat: DateFormat, gDateInt: Int): Date = toDate(dateFormat, gDateInt.toString()) - @Suppress("DEPRECATION") - @JvmOverloads @JvmStatic - fun toDate(dateFormat: SimpleDateFormat = DATE_FORMAT, gDateString: String): Date { - return dateFormat.parse(gDateString) - } + fun toDate(dateFormat: DateFormat, gDateString: String): Date = + dateFormat.parse(gDateString) - @Suppress("DEPRECATION") - @JvmOverloads @JvmStatic - fun fromDate(dateFormat: SimpleDateFormat = DATE_FORMAT, calendar: Calendar): String = fromDate(dateFormat, calendar.time) + fun fromDate(dateFormat: DateFormat, calendar: Calendar): String = fromDate(dateFormat, calendar.time) - @Suppress("DEPRECATION") - @JvmOverloads @JvmStatic - fun fromDate(dateFormat: SimpleDateFormat = DATE_FORMAT, gDateString: Date): String { - return dateFormat.format(gDateString) - } + fun fromDate(dateFormat: DateFormat, gDateString: Date): String = + dateFormat.format(gDateString) - @Suppress("DEPRECATION") - @JvmOverloads @JvmStatic - fun fromDateToInt(dateFormat: SimpleDateFormat = DATE_FORMAT, gDateString: Date) = fromDate(dateFormat, gDateString).toInt() + fun fromDateToInt(dateFormat: DateFormat, gDateString: Date) = fromDate(dateFormat, gDateString).toInt() fun cleanTime(gTimeString: String) = gTimeString.padStart(6, '0') // "%06d".format(Locale.ENGLISH, gTimeString) NOT working?? - @Suppress("DEPRECATION") - @JvmOverloads @JvmStatic - fun toTimeStamp(dateTimeFormat: SimpleDateFormat = DATE_TIME_FORMAT, gDateInt: Int, gTimeInt: Int) = + fun toTimeStamp(dateTimeFormat: DateFormat, gDateInt: Int, gTimeInt: Int) = toDate(dateTimeFormat, gDateInt, gTimeInt).time - @Suppress("DEPRECATION") - @JvmOverloads @JvmStatic - fun toDate(dateTimeFormat: SimpleDateFormat = DATE_TIME_FORMAT, gDateInt: Int, gTimeInt: Int) = + fun toDate(dateTimeFormat: DateFormat, gDateInt: Int, gTimeInt: Int) = toDate(dateTimeFormat, gDateInt.toString(), gTimeInt.toString()) - @Suppress("DEPRECATION") - @JvmOverloads @JvmStatic - fun toDate(dateTimeFormat: SimpleDateFormat = DATE_TIME_FORMAT, gDateString: String, gTimeString: String): Date { - return dateTimeFormat.parse(gDateString + SPACE_ + cleanTime(gTimeString)) - } + fun toDate(dateTimeFormat: DateFormat, gDateString: String, gTimeString: String): Date = + dateTimeFormat.parse(gDateString + SPACE_ + cleanTime(gTimeString)) fun Int.isBefore(date: Int?): Boolean { date ?: return false diff --git a/src/main/java/org/mtransit/parser/mt/GenerateMObjectsTask.java b/src/main/java/org/mtransit/parser/mt/GenerateMObjectsTask.java index 91edf65..7eb510c 100644 --- a/src/main/java/org/mtransit/parser/mt/GenerateMObjectsTask.java +++ b/src/main/java/org/mtransit/parser/mt/GenerateMObjectsTask.java @@ -35,7 +35,7 @@ import org.mtransit.parser.mt.data.MStop; import org.mtransit.parser.mt.data.MTrip; -import java.text.SimpleDateFormat; +import java.text.DateFormat; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -192,7 +192,7 @@ private MSpec doCall() { long lastTimestamp = -1L; if (!mServiceDatesList.isEmpty()) { MServiceDate firstServiceDate = mServiceDatesList.get(0); - final int maxCalendarDate = GFieldTypes.fromDateToInt(new Date()) + 10 * 10_000; // max 10 years IN THE FUTURE + final int maxCalendarDate = GFieldTypes.fromDateToInt(DATE_FORMAT, new Date()) + 10 * 10_000; // max 10 years IN THE FUTURE mServiceDatesList.removeIf(serviceDate -> serviceDate.getCalendarDate() > maxCalendarDate ); @@ -249,7 +249,6 @@ private MSpec doCall() { && (lastDeparture < -1 || lastDeparture < lastFrequency.getEndTime())) { lastDeparture = lastFrequency.getEndTime(); } - final SimpleDateFormat DATE_TIME_FORMAT = GFieldTypes.makeDateAndTimeFormat(); DATE_TIME_FORMAT.setTimeZone(TimeZone.getTimeZone(mAgenciesList.get(0).getTimezone())); try { firstTimestamp = GFieldTypes.toTimeStamp(DATE_TIME_FORMAT, firstCalendarDate, firstDeparture); @@ -727,7 +726,9 @@ private HashMap parseGTripStops(HashMap mSchedu return splitDirectionStopTimesHeadSign; } - private final SimpleDateFormat TIME_FORMAT = GFieldTypes.makeTimeFormat(); + private final DateFormat TIME_FORMAT = GFieldTypes.makeTimeFormat(); + private final DateFormat DATE_TIME_FORMAT = GFieldTypes.makeDateAndTimeFormat(); + private final DateFormat DATE_FORMAT = GFieldTypes.makeDateFormat(); private String parseGStopTimes(HashMap mSchedules, long mDirectionId, diff --git a/src/main/java/org/mtransit/parser/mt/MGenerator.java b/src/main/java/org/mtransit/parser/mt/MGenerator.java index 8d0dfe3..db2c077 100644 --- a/src/main/java/org/mtransit/parser/mt/MGenerator.java +++ b/src/main/java/org/mtransit/parser/mt/MGenerator.java @@ -2,7 +2,6 @@ import static org.mtransit.commons.Constants.EMPTY; -import org.apache.commons.io.IOUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.mtransit.commons.Cleaner; @@ -21,11 +20,12 @@ import org.mtransit.parser.db.DumpDbUtils; import org.mtransit.parser.db.SQLUtils; import org.mtransit.parser.gtfs.GAgencyTools; -import org.mtransit.parser.gtfs.GReader; import org.mtransit.parser.gtfs.data.GFieldTypes; import org.mtransit.parser.gtfs.data.GIDs; import org.mtransit.parser.gtfs.data.GSpec; import org.mtransit.parser.mt.data.MAgency; +import org.mtransit.parser.mt.data.MDirection; +import org.mtransit.parser.mt.data.MDirectionStop; import org.mtransit.parser.mt.data.MFrequency; import org.mtransit.parser.mt.data.MRoute; import org.mtransit.parser.mt.data.MSchedule; @@ -34,8 +34,6 @@ import org.mtransit.parser.mt.data.MServiceIds; import org.mtransit.parser.mt.data.MSpec; import org.mtransit.parser.mt.data.MStop; -import org.mtransit.parser.mt.data.MDirection; -import org.mtransit.parser.mt.data.MDirectionStop; import org.mtransit.parser.mt.data.MString; import org.mtransit.parser.mt.data.MStrings; import org.mtransit.parser.mt.data.MTrip; @@ -52,6 +50,7 @@ import java.nio.file.attribute.FileTime; import java.sql.Connection; import java.sql.Statement; +import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; @@ -325,7 +324,7 @@ public static void dumpFiles(@NotNull GAgencyTools gAgencyTools, mSpec.getFirstTimestampInSeconds(), mSpec.getLastTimestampInSeconds(), inputUrl, false ); - dumpStoreListing(rawDirF, fileBase, minMaxDates.first, minMaxDates.second); + MStoreListingGenerator.dumpStoreReleaseNotes(rawDirF, fileBase, minMaxDates.first, minMaxDates.second); bumpDBVersion(rawDirF, gtfsDir); } MTLog.log("Writing files (%s)... DONE in %s.", @@ -976,7 +975,7 @@ private static void dumpFrequencyRoutes(@NotNull GAgencyTools gAgencyTools, private static final String GTFS_RDS_VALUES_XML = "gtfs_rts_values.xml"; // do not change to avoid breaking compat w/ old modules - private static final SimpleDateFormat DATE_FORMAT = GFieldTypes.makeDateFormat(); + private static final DateFormat DATE_FORMAT = GFieldTypes.makeDateFormat(); private static final Pattern RDS_DB_VERSION_REGEX = Pattern.compile( "(()(\\d+)())", // do not change to avoid breaking compat w/ old modules @@ -1220,7 +1219,7 @@ private static void dumpValues(File dumpDirF, String fileBase, MSpec mSpec, Doub @NotNull private static String getCommentedDateTime(int timestampInSec, @NotNull MSpec mSpec) { try { - final SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss z", Locale.ENGLISH); + final DateFormat dateTimeFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss z", Locale.ENGLISH); try { dateTimeFormat.setTimeZone(TimeZone.getTimeZone(mSpec.getFirstAgency().getTimezone())); } catch (Exception e) { @@ -1255,74 +1254,6 @@ private static String getRESOURCES_STRING(@NotNull String resName, @Nullable Obj return String.format(RESOURCE_STRING_AND_NAME_VALUE, resName, resValue); } - private static final String PLAY = "play"; - private static final String RELEASE_NOTES = "release-notes"; - private static final String EN_US = "en-US"; - private static final String FR_FR = "fr-FR"; - private static final String DEFAULT_TXT = "default.txt"; - - private static final Pattern SCHEDULE = Pattern.compile("(Schedule from ([A-Za-z]+ [0-9]{1,2}, [0-9]{4}) to ([A-Za-z]+ [0-9]{1,2}, [0-9]{4})(\\.)?)", - Pattern.CASE_INSENSITIVE); - private static final String SCHEDULE_FROM_TO = "Schedule from %1$s to %2$s."; - private static final String SCHEDULE_KEEP_FROM_TO = "Schedule from $2 to %2$s."; - - private static final Pattern SCHEDULE_FR = Pattern.compile("(Horaires du ([0-9]{1,2} \\w+ [0-9]{4}) au ([0-9]{1,2} \\w+ [0-9]{4})(\\.)?)", - Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CHARACTER_CLASS); - @SuppressWarnings("SpellCheckingInspection") - private static final String SCHEDULE_FROM_TO_FR = "Horaires du %1$s au %2$s."; - @SuppressWarnings("SpellCheckingInspection") - private static final String SCHEDULE_KEEP_FROM_TO_FR = "Horaires du $2 au %2$s."; - - private static void dumpStoreListing(File dumpDirF, String fileBase, Integer minDate, Integer maxDate) { - SimpleDateFormat SCHEDULE_DATE = new SimpleDateFormat("MMMMM d, yyyy", Locale.ENGLISH); - SimpleDateFormat SCHEDULE_DATE_FR = new SimpleDateFormat("d MMMMM yyyy", Locale.FRENCH); - File file; - File dumpDirRootF = dumpDirF.getParentFile().getParentFile(); - File dumpDirPlayF = new File(dumpDirRootF, PLAY); - File dumpDirReleaseNotesF = new File(dumpDirPlayF, RELEASE_NOTES); - File dumpDirReleaseNotesEnUsF = new File(dumpDirReleaseNotesF, EN_US); - file = new File(dumpDirReleaseNotesEnUsF, DEFAULT_TXT); - boolean isNext = "next_".equalsIgnoreCase(fileBase); - if (file.exists()) { - MTLog.log("Generated store listing file: '%s'.", file); - try { - String content = IOUtils.toString(Files.newInputStream(file.toPath()), GReader.UTF_8); - content = SCHEDULE.matcher(content).replaceAll( - String.format( - isNext ? SCHEDULE_KEEP_FROM_TO : SCHEDULE_FROM_TO, // - SCHEDULE_DATE.format(GFieldTypes.toDate(DATE_FORMAT, minDate)), - SCHEDULE_DATE.format(GFieldTypes.toDate(DATE_FORMAT, maxDate)) - ) - ); - IOUtils.write(content, Files.newOutputStream(file.toPath()), GReader.UTF_8); - } catch (Exception ioe) { - throw new MTLog.Fatal(ioe, "Error while writing store listing files!"); - } - } else { - MTLog.log("Do not generate store listing file: %s.", file); - } - File dumpDirReleaseNotesFrFrF = new File(dumpDirReleaseNotesF, FR_FR); - file = new File(dumpDirReleaseNotesFrFrF, DEFAULT_TXT); - if (file.exists()) { - MTLog.log("Generated store listing file: %s.", file); - try { - String content = IOUtils.toString(Files.newInputStream(file.toPath()), GReader.UTF_8); - content = SCHEDULE_FR.matcher(content).replaceAll( - String.format( - isNext ? SCHEDULE_KEEP_FROM_TO_FR : SCHEDULE_FROM_TO_FR, // - SCHEDULE_DATE_FR.format(GFieldTypes.toDate(DATE_FORMAT, minDate)), - SCHEDULE_DATE_FR.format(GFieldTypes.toDate(DATE_FORMAT, maxDate)) - ) - ); - IOUtils.write(content, Files.newOutputStream(file.toPath()), GReader.UTF_8); - } catch (Exception ioe) { - throw new MTLog.Fatal(ioe, "Error while writing store listing files!"); - } - } else { - MTLog.log("Do not generate store listing file: %s.", file); - } - } - public static boolean checkDataFilesExists(@NotNull String fileBase) { try { final String rootDir = "../app-android/"; diff --git a/src/main/java/org/mtransit/parser/mt/MStoreListingGenerator.kt b/src/main/java/org/mtransit/parser/mt/MStoreListingGenerator.kt new file mode 100644 index 0000000..a136447 --- /dev/null +++ b/src/main/java/org/mtransit/parser/mt/MStoreListingGenerator.kt @@ -0,0 +1,115 @@ +package org.mtransit.parser.mt + +import org.mtransit.parser.MTLog +import org.mtransit.parser.MTLog.Fatal +import org.mtransit.parser.gtfs.data.GFieldTypes +import java.io.File +import java.text.DateFormat +import java.text.SimpleDateFormat +import java.util.Locale +import java.util.regex.Pattern + +object MStoreListingGenerator { + + private const val PLAY = "play" + private const val LISTINGS = "listings" + private const val RELEASE_NOTES = "release-notes" + private const val DEFAULT_TXT = "default.txt" + + private val DATE_FORMAT = GFieldTypes.makeDateFormat() + + // region en-US (default) + + private const val EN_US = "en-US" + + @Suppress("SpellCheckingInspection") + private val DATE_FORMAT_EN: DateFormat get() = SimpleDateFormat("MMMMM d, yyyy", Locale.ENGLISH) // NOT thread-safe + + private val SCHEDULE_EN = Pattern.compile( + "(Schedule from ([A-Za-z]+ [0-9]{1,2}, [0-9]{4}) to ([A-Za-z]+ [0-9]{1,2}, [0-9]{4})(\\.)?)", + Pattern.CASE_INSENSITIVE + ).toRegex() + + private const val SCHEDULE_EN_FROM_TO = $$"Schedule from %1$s to %2$s." + + private const val SCHEDULE_EN_KEEP_FROM_TO = $$"Schedule from $2 to %2$s." + + // endregion + + // region fr-FR + + private const val FR_FR = "fr-FR" + + @Suppress("SpellCheckingInspection") + private val DATE_FORMAT_FR: DateFormat get() = SimpleDateFormat("d MMMMM yyyy", Locale.FRENCH) // NOT thread-safe + + private val SCHEDULE_FR = Pattern.compile( + "(Horaires du ([0-9]{1,2} \\w+ [0-9]{4}) au ([0-9]{1,2} \\w+ [0-9]{4})(\\.)?)", + Pattern.CASE_INSENSITIVE or Pattern.UNICODE_CHARACTER_CLASS + ).toRegex() + + @Suppress("SpellCheckingInspection") + private const val SCHEDULE_FROM_TO_FR = $$"Horaires du %1$s au %2$s." + + @Suppress("SpellCheckingInspection") + private const val SCHEDULE_KEEP_FROM_TO_FR = $$"Horaires du $2 au %2$s." + + // endregion + + @JvmStatic + fun dumpStoreReleaseNotes( + dumpDirF: File, + fileBase: String, + minDate: Int, + maxDate: Int, + ) { + val (dirListingF, dirReleaseNotesF) = File(dumpDirF.parentFile.parentFile, PLAY).let { dirPlayF -> + File(dirPlayF, LISTINGS) to File(dirPlayF, RELEASE_NOTES) + } + val isNext = "next_".equals(fileBase, ignoreCase = true) + dumpStoreReleaseNote( + dirListingF, dirReleaseNotesF, isNext, minDate, maxDate, + EN_US, DATE_FORMAT_EN, SCHEDULE_EN, SCHEDULE_EN_FROM_TO, SCHEDULE_EN_KEEP_FROM_TO + ) + dumpStoreReleaseNote( + dirListingF, dirReleaseNotesF, isNext, minDate, maxDate, + FR_FR, DATE_FORMAT_FR, SCHEDULE_FR, SCHEDULE_FROM_TO_FR, SCHEDULE_KEEP_FROM_TO_FR + ) + } + + private fun dumpStoreReleaseNote( + dirListingF: File, + dirReleaseNotesF: File, + isNext: Boolean, + minDate: Int, + maxDate: Int, + lang: String, + dateFormat: DateFormat, + regex: Regex, + formatFromTo: String, + formatKeepFromTo: String, + ) { + val dirListingLangF = File(dirListingF, lang) + if (!dirListingLangF.exists()) { + MTLog.log("Do not generate '$lang' store release notes file (listing '$dirListingLangF' does not exist).") + return + } + val dirReleaseNotesLangF = File(dirReleaseNotesF, lang) + if (dirReleaseNotesLangF.mkdirs()) MTLog.log("Created missing parent directory: '%s'.", dirReleaseNotesLangF) + val dumpFileReleaseNotesLang = File(dirReleaseNotesLangF, DEFAULT_TXT) + try { + val minDateS = dateFormat.format(GFieldTypes.toDate(DATE_FORMAT, minDate)) + val maxDateS = dateFormat.format(GFieldTypes.toDate(DATE_FORMAT, maxDate)) + val content = if (dumpFileReleaseNotesLang.exists()) { + MTLog.log("Update store release notes file: $dumpFileReleaseNotesLang.") + dumpFileReleaseNotesLang.readText().replace(regex, (if (isNext) formatKeepFromTo else formatFromTo).format(minDateS, maxDateS)) + } else { + MTLog.log("Generate new store release notes file: $dumpFileReleaseNotesLang.") + formatFromTo.format(minDateS, maxDateS) + } + dumpFileReleaseNotesLang.writeText(content) + } catch (ioe: Exception) { + throw Fatal(ioe, "Error while writing new store release notes files!") + } + } +} diff --git a/src/main/java/org/mtransit/parser/mt/data/MSpec.kt b/src/main/java/org/mtransit/parser/mt/data/MSpec.kt index 9984c97..90d7456 100644 --- a/src/main/java/org/mtransit/parser/mt/data/MSpec.kt +++ b/src/main/java/org/mtransit/parser/mt/data/MSpec.kt @@ -3,8 +3,6 @@ package org.mtransit.parser.mt.data import org.mtransit.commons.toIntTimestampSec import org.mtransit.parser.MTLog import org.mtransit.parser.db.DBUtils -import org.mtransit.parser.gtfs.data.GFieldTypes -import java.text.SimpleDateFormat import java.util.TreeMap import java.util.concurrent.TimeUnit @@ -82,10 +80,4 @@ data class MSpec( val lastTimestampInSeconds: Int get() = TimeUnit.MILLISECONDS.toSeconds(lastTimestamp).toIntTimestampSec() - - companion object { - @JvmStatic - val newTimeFormatInstance: SimpleDateFormat - get() = GFieldTypes.makeTimeFormat() - } } \ No newline at end of file diff --git a/src/test/java/org/mtransit/parser/DefaultAgencyToolsTest.java b/src/test/java/org/mtransit/parser/DefaultAgencyToolsTest.java index bfce4b6..b0dc727 100644 --- a/src/test/java/org/mtransit/parser/DefaultAgencyToolsTest.java +++ b/src/test/java/org/mtransit/parser/DefaultAgencyToolsTest.java @@ -5,6 +5,7 @@ import org.mtransit.parser.gtfs.data.GCalendarDate; import org.mtransit.parser.gtfs.data.GCalendarDatesExceptionType; +import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Calendar; @@ -23,7 +24,7 @@ @SuppressWarnings("ConstantConditions") public class DefaultAgencyToolsTest { - private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH); + private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH); private static final Calendar c = Calendar.getInstance(); @Test