/*
 * @(#)InsertFileEntities.java
 *
 * Summary: Inserts HTML entities such as &quot; into a file replacing the single character equivalents.
 *
 * Copyright: (c) 2005-2010 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.5+
 *
 * Created with: IntelliJ IDEA IDE.
 *
 * Version History:
 *  2.6 2009-04-05 - StripEntities now leaves a space behind when it removes a <br><p><td> etc tag.
 */
package com.mindprod.entities;

import com.mindprod.hunkio.HunkIO;

import java.io.File;
import java.io.IOException;

/**
 * Inserts HTML entities such as &quot; into a file replacing the single character equivalents.
 * <p/>
 * @noinspection WeakerAccess
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 2.6 2009-04-05 - StripEntities now leaves a space behind when it removes a <br><p><td> etc tag.
 * @see #InsertEntities
 * @see #InsertFileEntities
 * @see #StripEntities
 * @see #StripFileEntities
 * @since 2005
 */
public final class InsertFileEntities extends InsertEntities
    {
    // -------------------------- STATIC METHODS --------------------------

    /**
     * process one file, Inserting HTML entities such as &quot; Warning. Not designed to handle a web page that already
     * contains some entities. See amper utility.
     *
     * @param filename name of file to process. Output replaces current contents.
     *
     * @throws IOException if trouble reading or writing filei.
     * @noinspection WeakerAccess
     */
    static void insertEntitiesInFile( String filename ) throws IOException
        {
        // create a temp file in the same directory as filename
        File file = new File( filename );
        if ( !file.exists() )
            {
            throw new IOException( "missing file " + filename );
            }
        File tempfile = HunkIO.createTempFile( "temp", ".tmp", file );

        String big = HunkIO.readEntireFile( file );
        // Insert the entities
        big = insertHTMLEntities( big );
        HunkIO.writeEntireFile( tempfile, big );
        // successfully created output in same directory as input,
        // Now make it replace the input file.
        file.delete();
        if ( file.exists() )
            {
            throw new IOException( "Could not modify input file " + filename );
            }
        tempfile.renameTo( file );
        // don't delete tempfile, it has been renamed to a real file
        }// end insertEntitiesInFile

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

    /**
     * Inserts HTML entities such as &quot; into a files mentioned on the command line.
     *
     * @param args names of files to process
     */
    public static void main( String[] args )
        {
        for ( String fileToProcess : args )
            {
            try
                {
                insertEntitiesInFile( fileToProcess );
                }
            catch ( IOException e )
                {
                System.out.println( e.getMessage() );
                }
            }
        }// end main
    }
