/*******************************************************************************
* Copyright (c)                                                                *
*           2005   Tristan Allwood,                                            *
*           2005   Daniel Burke,                                               *
*           2005   Marc Hull,                                                  *
*           2005   Ekaterina Itskova,                                          *
*           2005   Steve Zymler                                                *
*                                                                              *
* Permission is hereby granted, free of charge, to any person obtaining a      *
* copy of this software and associated documentation files (the                *
* "Software"), to deal in the Software without restriction, including          *
* without limitation the rights to use, copy, modify, merge, publish,          *
* distribute, sublicense, and/or sell copies of the Software, and to           *
* permit persons to whom the Software is furnished to do so, subject to        *
* the following conditions:                                                    *
*                                                                              *
* The above copyright notice and this permission notice shall be included      *
* in all copies or substantial portions of the Software.                       *
*                                                                              *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS      *
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF                   *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.       *
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY         *
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,         *
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE            *
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                       *
*******************************************************************************/
/*
 * Created on 26-Nov-2004 by toa02
 *
 */
package uk.ac.ic.doc.kevlar.core.programs.fileUtils;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;

import uk.ac.ic.doc.kevlar.api.programs.arguments.IArgumentInstance;
import uk.ac.ic.doc.kevlar.core.types.textType.IFilenameType;
import uk.ac.ic.doc.kevlar.core.types.textType.TextTypeImpl;
import uk.ac.ic.doc.kevlar.publicInterfaces.AbstractProgram;
import uk.ac.ic.doc.kevlar.publicInterfaces.ICloseEventListener;
import uk.ac.ic.doc.kevlar.publicInterfaces.IInputPipe;
import uk.ac.ic.doc.kevlar.publicInterfaces.IOutputPipe;
import uk.ac.ic.doc.kevlar.publicInterfaces.IPipeEnd;

/**
 * @author toa02
 * 
 */
public class GetFileContents extends AbstractProgram {

    /**
     * @see uk.ac.ic.doc.kevlar.publicInterfaces.AbstractProgram#run(uk.ac.ic.doc.kevlar.api.programs.arguments.IArgumentInstance,
     *      null, null)
     */
    public void run(IArgumentInstance args, Map<String, IInputPipe> inputs,
            Map<String, IOutputPipe> outputs) {

        final IInputPipe in = inputs.get("Files-In");

        final IOutputPipe out = outputs.get("Text-Out");

        final IOutputPipe out2 = outputs.get("Files-Out");

        final IOutputPipe err = outputs.get("Error");
        
        final String NEWLINE = System.getProperty("line.separator");

        while (true) {
            try {
                IFilenameType ift = (IFilenameType) in.read();

                FileInputStream fis ;
                try {
                    fis = new FileInputStream(ift.getFile());
                } catch ( FileNotFoundException fnfe ) {
                    if( err != null ) {
                        err.write(ift);
                    }
                    continue;
                }

                BufferedReader bis = new BufferedReader(new InputStreamReader(
                        fis));

                StringBuilder sb = new StringBuilder();
                String s;

                // sb.append(ift.getText()).append(NEWLINE);

                while (null != (s = bis.readLine())) {
                    sb.append(s).append(NEWLINE);
                }

                sb.append(NEWLINE);

                try {
                    if( out != null ) {
                        out.write(new TextTypeImpl(sb.toString()));
                    }
                } catch (IllegalStateException ise) {
                    /* ignore it */
                }

                try {
                    if( out2 != null ) {
                        out2.write(ift);
                    }
                } catch (IllegalStateException ise) {
                    /* ignore it */
                }

            } catch (InterruptedException ie) {
                exit(0);
            } catch (IllegalStateException ise) {
                exit(0);
            } catch (IOException e) {
                continue;
            }
        }
    }

}
