/*
 * @(#)FixPADGen.java
 *
 * Summary: fixes syntax errors in HTML in current directory created by PADGen.
 *
 * Copyright: (c) 2009-2017 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          see http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.6+
 *
 * Created with: IntelliJ IDEA IDE.
 *
 * Version History:
 *  1.2 2009-01-02
 */
package com.mindprod.stomp;

import com.mindprod.filter.ExtensionListFilter;
import com.mindprod.hunkio.HunkIO;

import java.io.File;
import java.io.IOException;
import static java.lang.System.err;

/**
 * fixes syntax errors in HTML in current directory created by PADGen.
 *
 * Presumes F:\Program Files\PADGen\PADGen.tml has been replaced with a corrected template.
 * See http://mindprod.com/jgloss/padgen.html
 *
 * @author Roedy Green, Canadian Mind Products
 * @since 2009
 * @version 1.2 2009-01-02
 */
public class FixPADGen
    {
    // ------------------------------ CONSTANTS ------------------------------

    /**
     * encoding of the generated html file.
     */
    private static final String ENCODING = "UTF-8";

    // -------------------------- STATIC METHODS --------------------------

    /**
     * fix syntax bugs in Pad.html files, created by PadGen,
     * Presumes F:\Program Files\PADGen\PADGen.tml has been replaced with a corrected template.
     * See http://mindprod.com/jgloss/padgen.html
     *
     * @param padHTMLFile HTML pad summary file
     */
    static void tidyPadHtml( final File padHTMLFile )
        {
        try
            {
            String text = HunkIO.readEntireFile( padHTMLFile, ENCODING );
            boolean changed = false;
            if ( text.startsWith( "<!doctype html public" ) )
                {
                // vslick tidy has wrecked dtd.  Just fix that
                text = "<!DOCTYPE HTML PUBLIC" + text.substring( "<!doctype html public".length() );
                changed = true;
                }

            int place;
            // remove dummy references.
            while ( ( place = text.indexOf( "<a class=\"offsite\" href=\"\"></a><br>" ) ) >= 0 )
                {
                text = text.substring( 0, place ) +
                       text.substring( place + "<a class=\"offsite\" href=\"\"></a><br>".length() );
                changed = true;
                }
            if ( changed )
                {
                HunkIO.writeEntireFile( padHTMLFile, text, ENCODING );
                }
            }
        catch ( IOException e )
            {
            err.println( "Unable to access " + padHTMLFile.toString() );
            }
        }

    // --------------------------- main() method ---------------------------

    /**
     * Tidy all PADGen HTML files in the current directory.
     *
     * @param args ignored
     */
    public static void main( String[] args )
        {
        final File thisDir = new File( "." );
        final String[] filenames = thisDir.list( new ExtensionListFilter( "html" ) );
        for ( String filename : filenames )
            {
            tidyPadHtml( new File( thisDir, filename ) );
            }
        }
    }