package com.mindprod.compactor;
import com.mindprod.filter.AllDirectoriesFilter;
import com.mindprod.filter.CommandLine;
import com.mindprod.filter.ExtensionListFilter;
import com.mindprod.hunkio.HunkIO;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import static java.lang.System.*;
import java.util.regex.Pattern;
/**
* Compacts HTML my removing excess white space.
*
* @author Roedy Green
* @version 2.7 2008-07-27 remove all space just before > in a tag. < space will convert to < space.
*/
public class Compactor
{
/**
* undisplayed copyright notice
*/
@SuppressWarnings( { "UnusedDeclaration" } )
public static final String EMBEDDED_COPYRIGHT =
"copyright (c) 1999-2008 Roedy Green, Canadian Mind Products, http://mindprod.com";
/**
* date this version was released.
*/
@SuppressWarnings( { "UnusedDeclaration" } )
private static final String RELEASE_DATE = "2008-07-27";
/**
* embedded version string.
*/
@SuppressWarnings( { "UnusedDeclaration" } )
public static final String VERSION_STRING = "2.7";
/**
* compact and tidy one file.
*
* @param fileBeingProcessed File to compact and tidy.
* @param quiet true if want progress messages suppressed
* @throws IOException Suppress IntelliJ Code Analyse that wants to make this private.
* @noinspection WeakerAccess,SameParameterValue,StringEquality
*/
public static void compactFile( File fileBeingProcessed,
boolean quiet ) throws IOException
{
if ( !quiet )
{
out.print( " compacting " + fileBeingProcessed.getName() + " " );
}
if ( !( fileBeingProcessed.getName().endsWith( ".html" )
|| fileBeingProcessed
.getName().endsWith( ".htm" ) ) )
{
err.println( "Cannot compact: "
+ fileBeingProcessed.getName()
+ "not .html file" );
return;
}
String big = HunkIO.readEntireFile( fileBeingProcessed );
String result = compactString( big, fileBeingProcessed.getPath() );
if ( result == big )
{
if ( !quiet )
{
out.println( "-" );
}
return;
}
if ( !quiet )
{
out.println( "*" );
}
File tempfile =
HunkIO.createTempFile( "temp", ".tmp", fileBeingProcessed );
FileWriter emit = new FileWriter( tempfile );
emit.write( result );
emit.close();
fileBeingProcessed.delete();
tempfile.renameTo( fileBeingProcessed );
}
/**
* Remove excess whitespace from HTML represented by string.
*
* @param big the String to compact.
* @param where used in error messages to indicate where the error occurred, usually the name of the file being
* compacted.
* @return the compacted String, big itself if nothing changed.
*/
public static String compactString( final String big, final String where )
{
return HTMLState.compactString( big, where, true,
Pattern.compile( "\\s*macro\\s"),
Pattern.compile( "#"),
Pattern.compile( "\\s*generated\\s" ),
Pattern.compile( "\\s*/generated\\s" )
);
}
/**
* compacts HTML files.
*
* @param args names of files to process, dirs, files, -s, *.*, no wildcards.
*/
public static void main( String[] args )
{
out.println( "Gathering files to process..." );
CommandLine wantedFiles = new CommandLine( args,
1000,
new AllDirectoriesFilter(),
new ExtensionListFilter(
"html" ) );
for ( File file : wantedFiles )
{
try
{
compactFile( file, false);
}
catch ( FileNotFoundException e )
{
err.println( "Error: "
+ file.getAbsolutePath()
+ " not found." );
}
catch ( Exception e )
{
err.println( e.getMessage()
+ " in file "
+ file.getAbsolutePath() );
err.println();
e.printStackTrace();
}
}
}
}