/* * [ExportHolidaysToHTML.java] * * Summary: Export list of holidays to HTML for summary display. * * Copyright: (c) 2008-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2008-12-03 initial version */ package com.mindprod.holidays; import com.mindprod.common18.BigDate; import com.mindprod.common18.Build; import com.mindprod.common18.EIO; import com.mindprod.common18.Misc; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import static com.mindprod.entities.EntifyStrings.entifyHTML; import static java.lang.System.*; /** * Export list of holidays to HTML for summary display. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2008-12-03 initial version * @since 2008 */ public final class ExportHolidaysToHTML { /** * Default year to display a list of holidays for. */ private static final int thisYear = Misc.thisYear(); /** * where to send the exported HTML description of the holidays */ private static final File EXPORT_TO_FILE = new File( Build.MINDPROD_WEBROOT + "/jgloss/include/holidays.htmlfrag" ); private static final String DO_NOT_EDIT = "\n"; /** * A delegate object that implements the HolInfo abstract class. That lets us execute code for a number of different * holidays given only the name of the class that computes them. We dynamically load the HollInfo classes with * Class.forName. */ private static HolInfo[] holidayDelegates; ; /** * export list of all holidays as html for this year * * @throws IOException if trouble writing file. */ private static void exportHolidays() throws IOException { /* holidays in order for this year. */ final ArrayList sortedHolidays = new ArrayList<>( holidayDelegates.length ); for ( HolInfo h : holidayDelegates ) { // extract just holidays celebrated this year. if ( h != null && h.when( thisYear, false ) != BigDate.NULL_ORDINAL ) { sortedHolidays.add( h ); } } Collections.sort( sortedHolidays, new HolidaySort( thisYear, false ) ); // count how many holidays in each month int[] holidaysInMonth = new int[ 12 ]; for ( HolInfo h : sortedHolidays ) { holidaysInMonth[ new BigDate( h.when( thisYear, false ) ).getMM() - 1 ]++; } int springSummerRows = 0; for ( int i = 0; i < 6; i++ ) { if ( holidaysInMonth[ i ] > springSummerRows ) { springSummerRows = holidaysInMonth[ i ]; } } int fallWinterRows = 0; for ( int i = 6; i < 12; i++ ) { if ( holidaysInMonth[ i ] > fallWinterRows ) { fallWinterRows = holidaysInMonth[ i ]; } } String[][] matrix = new String[ Math.max( springSummerRows, fallWinterRows ) ][ 12 ]; { int row = 0; int prevMonth = -1; for ( HolInfo h : sortedHolidays ) { int month = new BigDate( h.when( thisYear, false ) ).getMM(); if ( month != prevMonth ) { row = 0; prevMonth = month; } matrix[ row++ ][ month - 1 ] = h.getName(); } } final PrintWriter prw = EIO.getPrintWriter( EXPORT_TO_FILE, 4 * 1024, EIO.UTF8 ); // export SpringSummer header prw.print( DO_NOT_EDIT + "\n" + "\n" + "" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" ); // export springSummer data for ( int row = 0; row < springSummerRows; row++ ) { prw.println( "" ); for ( int month = 0; month < 6; month++ ) { if ( matrix[ row ][ month ] == null ) { prw.println( "" ); } else { prw.print( "" ); } } prw.println( "" ); } // export FallWinter header prw.println( "" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "" ); // export FallWinter data for ( int row = 0; row < fallWinterRows; row++ ) { prw.println( "" ); for ( int month = 6; month < 12; month++ ) { if ( matrix[ row ][ month ] == null ) { prw.println( "" ); } else { prw.print( "" ); } } prw.println( "" ); } // wrap up prw.println( "\n" + "" + "
holidays
JanuaryFebruaryMarchAprilMayJune
 " ); prw.print( entifyHTML( matrix[ row ][ month ] ) ); prw.println( "
JulyAugustSeptemberOctoberNovemberDecember
 " ); prw.print( entifyHTML( matrix[ row ][ month ] ) ); prw.println( "
" ); prw.close(); } /** * get list of holidays from properties file: com.mindprod.holidays.Holiday.properties which lives in the jar. class * names are case-sensitive. file looks like this:
Christmas=yes
GroundhogDay=no
...
where * yes=include no=ignore */ private static void findHolidays() { String[][] result = null; try { // look in jar, and on classpath for // com.mindprod.holidays.Holiday.properties final InputStream fis = ExportHolidaysToHTML.class .getResourceAsStream( "Holiday.properties" ); result = Misc.loadProperties( fis ); // we now key=value pairs } catch ( IOException oops ) { out.println( oops + " Problem accessing Holiday.properties file." ); System.exit( 1 ); } // in pairs className=yes/no int length = result[ 0 ].length; int j = 0; // keep just the holidays marked yes. // in the holidayDelegate array. holidayDelegates = new HolInfo[ length ]; for ( int i = 0; i < length; i++ ) { try { if ( Misc.parseBoolean( result[ 1 ][ i ], false ) ) { holidayDelegates[ j++ ] = ( HolInfo ) ( Class .forName( "com.mindprod.holidays." + result[ 0 ][ i ] ) .newInstance() ); } } catch ( Exception oops ) { out.println( oops + " Bug in Holiday.properties or class file for " + result[ 0 ][ i ] ); System.exit( 1 ); } } // end for } /** * Allow this Applet to run as as application as well. * * @param args command line arguments ignored. * * @throws IOException if cannot load or export all the holidays. */ public static void main( String args[] ) throws IOException { findHolidays(); exportHolidays(); } // end main }