/*
 * @(#)StripFileEntities.java
 *
 * Summary: Strips HTML entities such as &quot; from a string, replacing them by their Unicode 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.
 *  2.7 2009-11-14 - generate a table for the HTML cheat sheet of quote-like entities.
 *  2.8 2009-12-22 - export table on HTML 5 entities. Now import csv file rather than embed entity facts.
 */
package com.mindprod.entities;

import com.mindprod.hunkio.HunkIO;

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

/**
 * Strips HTML entities such as &quot; from a string, replacing them by their Unicode equivalents.
 * <p/>
 * @noinspection WeakerAccess
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 2.8 2009-12-22 - export table on HTML 5 entities. Now import csv file rather than embed entity facts.
 * @see #InsertEntities
 * @see #InsertFileEntities
 * @see #StripEntities
 * @see #StripFileEntities
 * @since 2005
 */
public final class StripFileEntities extends StripEntities
    {
    // -------------------------- STATIC METHODS --------------------------

    /**
     * process one file, replacing HTML entities with their character equivalents stripping HTML tags, and collapsing
     * multiple white spaces to a single white space.
     *
     * @param filename name of file to process. Output replaces current contents.
     *
     * @throws IOException if cannot read/write the file.
     */
    static void stripFile( 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 );
        // strip out all the entities and HTML tags, nbsp -> space.
        big = flattenHTML( 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 stripFile

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

    /**
     * Strips HTML entities from HTML files converting them to their character equivalents.
     *
     * @param args names of files to process
     */
    public static void main( String[] args )
        {
        for ( String fileToProcess : args )
            {
            try
                {
                stripFile( fileToProcess );
                }
            catch ( IOException e )
                {
                System.out.println( e.getMessage() );
                }
            }
        }// end main
    }
