/*
* [ValidateEmbellishments.java]
*
* Summary: Validate the Embellishments to make sure they jibe with the file contents.
*
* Copyright: (c) 2011-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 2011-11-10 initial version
*/
package com.mindprod.htmlmacros;
import com.mindprod.commandline.CommandLine;
import com.mindprod.common18.EIO;
import com.mindprod.common18.Misc;
import com.mindprod.common18.ST;
import com.mindprod.filter.AllButFootDirectoriesFilter;
import com.mindprod.filter.ExtensionListFilter;
import com.mindprod.htmlmacros.macro.Global;
import com.mindprod.htmlmacros.support.ConfigurationForMindprod;
import com.mindprod.htmlmacros.support.Embellishment;
import com.mindprod.hunkio.HunkIO;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import static com.mindprod.htmlmacros.macro.Global.configuration;
import static java.lang.System.*;
/**
* Validate the Embellishments to make sure they jibe with the file contents.
*
* Not a macro. Command line utility.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2011-11-10 initial version
* @since 2011-11-10
*/
public class ValidateEmbellishments
{
/**
* true if want extra debugging output
*/
private static final boolean DEBUGGING = false;
/**
* Get fully qualified name of all the files to process.
*/
private static CommandLine gatherFiles()
{
// just HTML dirs, not the include dirs. Not the KJV dirs.
final List htmlDirs = configuration.getDirsContainingHTMLMacros();
final String[] args = new String[ htmlDirs.size() + 1 ];
args[ 0 ] = "-q";
int i = 1;
final String localWebrootDir = configuration.getLocalWebrootWithBackslashes();
for ( String aDir : htmlDirs )
{
args[ i++ ] = localWebrootDir + "\\" + aDir;
}
return new CommandLine( args, new AllButFootDirectoriesFilter(), new ExtensionListFilter( "html" ) );
}
public static void main( String[] args ) throws IOException
{
Global.installConfiguration( new ConfigurationForMindprod() );
final CommandLine filesToProcess = gatherFiles();
out.println( filesToProcess.size() + " files gathered for mindprod site embellishment validation." );
Embellishment.fireup();
Set withMarkers = Embellishment.getEmbellishmentsWithMarker();
int changes = 0;
final int chop = configuration.getLocalWebrootWithSlashes().length() + 1;
// loop through all files
for ( File fileToProcess : filesToProcess )
{
final String big = HunkIO.readEntireFile( fileToProcess, HunkIO.UTF8 );
if ( big == null )
{
err.println( "Problems reading file " + EIO.getCanOrAbsPath( fileToProcess ) );
}
else
{
String letters = Embellishment.getEmbellishmentLettersForFile( fileToProcess );
if ( DEBUGGING && letters.length() > 0 )
{
err.println( "---" + fileToProcess.toString().substring( chop ) + ", " + letters );
}
final String originalLetters = letters;
boolean changed = false;
// loop through all embellishments to process contents of one file
for ( Embellishment e : withMarkers )
{
final String marker = e.getMarker();
final String altMarker = e.getAltMarker();
if ( marker != null )
{
if ( big.contains( marker ) || altMarker != null && big.contains( altMarker ) )
{
// marker was present
if ( !e.isFileEmbellishedWithThisLetter( fileToProcess ) )
{
letters += e.getLetter();
changed = true;
}
}
else
{
// was no marker present
if ( e.isFileEmbellishedWithThisLetter( fileToProcess ) )
{
int place = letters.indexOf( e.getLetter() );
assert place >= 0 : "bug: can't find marker for existing embellishment letter";
// excess embellishment
letters = letters.substring( 0, place ) + letters.substring( place + 1 );
changed = true;
}
}
}
// if no marker we cannot comment on whether embellishment should be there or not.
} // end inner for
if ( changed )
{
// we don't actually change the letters, just suggest the changes.
// output replacement change, not aligned so it will be recognisable.
// # allows us to leave history in embellishments file.
letters = ST.sort( letters );
err.println( fileToProcess.toString().substring( chop ) + ", " +
letters + " # was " + originalLetters );
changes++;
}
}
} // end outer for
out.println( changes + " Embellishment changes suggested" );
Misc.trackLastThread();
System.exit( changes > 0 ? 1 : 0 );
} // end main
} // end class