/*
    PCS - A Framework For Java Web Applications
    Copyright (C) 2002 Patrick Carl, patrick.carl@web.de
 
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.
 
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 
    $Id: ControllerConfig.java,v 1.1 2003/01/08 02:33:10 pcs_org Exp $
 
 */

package de.spieleck.pcs.action.base;

import de.spieleck.pcs.Constants;
import de.spieleck.pcs.action.Action;
import de.spieleck.pcs.action.ActionException;
import de.spieleck.pcs.action.ActionResult;

import javax.xml.transform.stream.StreamSource;

import java.io.File;
import java.io.FileInputStream;
import java.util.Hashtable;

/**
 * This action loads XML data from a file and provides it via the 
 * ActionResult
 * @author  Patrick Carl
 */
public class ShowXmlAction implements Action{
    
    /** loads the XML file specified by the parameter "file"
     * @param parameters a Hashtable for information needed to perform the Action
     * @throws ActionException if an exception occurrs during processing
     * @return an instance of ActionResult as result of the Action
     *
     */
    public ActionResult perform(Hashtable parameters) throws ActionException {
        String path = (String) parameters.get(Constants.CONTEXT_REAL_PATH);
        String file = (String) parameters.get("file");
        // throw an Exception if no file parameter is given
        if(file == null || file.length() == 0)
            throw new ActionException("No parameter file given");
        else if(file.length() < 5){
            System.out.println("Very short file name given, adding .xml ");
            file = file + ".xml";
        }
        else if(!file.substring(file.length() - 4).equals(".xml"))
            file = file + ".xml";
        
        File f = new File(path + file);
        // throw an Exceptin if the file doesn't exist
        if(!f.exists())
            throw new ActionException("File does net exist: " + 
            f.getAbsolutePath());
        try{
            Hashtable h = new Hashtable();
            file = file.substring(0, file.length() - 3) + "xsl";
                
            return new ActionResult(new StreamSource(f),h, file);
        } catch(Exception e){
            e.printStackTrace();
            throw new ActionException(e.getMessage());
        }
    }
}