/* * [Updated.java] * * Summary: Inserts date some file was last modified in form yyyy-mm-dd. * * Copyright: (c) 2006-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 2006-01-13 written to replaced #FLASTMOD that does not work with ../ filenames. */ package com.mindprod.htmlmacros.macro; import com.mindprod.common18.BigDate; import java.io.File; import java.util.Date; import java.util.TimeZone; import static com.mindprod.htmlmacros.macro.Global.configuration; import static java.lang.System.*; /** * Inserts date some file was last modified in form yyyy-mm-dd. *

* This may not give the result you want. * It inserts the last modified date of the file right now. * It may soon be updated, or untouched. This is particularly germane * if the file is the file being processed. It may soon be modified with different macro expansions. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2006-01-13 written to replaced #FLASTMOD that does not work with ../ filenames. * @since 2006-01-13 */ public final class Updated extends Macro { /** * how to use the macro */ private static final String USAGE = "\nUpdated macro needs [uPath] [plain/decorated]"; /** * Updated macro expansion, used my MenuItem * * @param decorated true if want CSS updated style with prefixes word updated * @param fileToQuery you last modification date you want want expanded. File is relative to webRoot * * @return macro expansion */ public static String expand( boolean decorated, String fileToQuery ) { final File f = new File( configuration.getLocalWebrootWithBackslashes(), fileToQuery ); // when file updated final String when; if ( !f.exists() ) { when = "unknown"; } else { long timestamp = f.lastModified(); if ( timestamp == 0 ) { // file does not exist when = "unknown"; } else { BigDate updated = new BigDate( new Date( timestamp ), TimeZone.getDefault() ); when = updated.toString(); } } // decorate with surrounding class? if ( decorated ) { // css style will insert text "updated", in silver return "" + when + ""; } else { // just text in tarnished gold. return "" + when + ""; } } /** * Generate date from or Default is decorated to add prefix word decorated and put is updated css style. or filename * is relative to webRoot, not relative to current document. * * @param parms LOCAL_WEBROOT_WITH_BACKSLASHES-relative filename, not relative to file being processed. * If you were including it would be confusing, relative to including or included * document. * @param quiet true if want output suppressed. * @param verbose @return expanded macro text */ public String expandMacro( String[] parms, final boolean quiet, final boolean verbose ) { if ( !quiet ) { out.print( "U" ); } if ( !( 1 <= parms.length && parms.length <= 2 ) ) { throw new IllegalArgumentException( USAGE ); } // relative to webRoot, not fileBeingProcessed // default is decorated. return expand( parms.length == 1 || parms[ 1 ].equalsIgnoreCase( "decorated" ), parms[ 0 ] ); } }