/*
 * Copyright (C) 1999-2006 <a href="mailto:paschke@in.tum.de">Adrian Paschke</a>
 * 
 * The Regression Test is part of the Rule Based Service Level Agreement (RBSLA)
 * framework, available at 
 * <a href=" http://ibis.in.tum.de/staff/paschke/rbsla/index.htm">RBSLA</a>
 * 
 * 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 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
 */

package rbsla.rmltoprova;

import java.io.*;
import org.jdom.*;
import org.jdom.input.SAXBuilder;

import rbsla.rmltoprova.Analysis;

/**
 * Convertor
 * 
 * Translates RuleML 0.86 and RBSLA 0.01 files into Prova scripts
 * 
 * @author <A HREF="mailto:adrian.paschke@gmx.de">Adrian Paschke</A>
 * @version 0.1 <1 Feb 2005>
 * @since 0.1
 * @deprecated Since RuleML 0.87 and RBSLA 0.1 XSLT Stylesheets are provided for translation
 */
public class Convertor
{
    private Analysis sa=null;
    private SLADefinition ai=null;
    //the objects to open the input file
    private SAXBuilder builder;
    private Document readdoc;
    private Element rootElement;    
    
    //the input output file 
    private String inputFile;
    private String outputFile;   
    
    //the namespace
    private Namespace ns;
    
    /**
     * Constructor.
     * @param  inputFile the ruleml file / rbsla file to analysis
     * @param  outputFile  the prova output file
     */
    public Convertor(String _inputFile, String _outputFile)
    {
    	sa= null;
    	ai=null;
    	
    	inputFile = _inputFile;
    	outputFile = _outputFile;
    	
		builder = new SAXBuilder();
		try
		{
		    readdoc = builder.build(inputFile);
		    rootElement = readdoc.getRootElement();
		    ns = rootElement.getNamespace();
			
			if (rootElement.getName().equals("rulebase")) {				
				// plain RuleML 0.86 file
				sa = new Analysis(rootElement);
			} else 
			if (rootElement.getName().equals("rbsla")) { 
				// RBSLA 0.01 file
				try {
					ai = new SLADefinition(rootElement);
				} catch (Exception e) {}
				try {
					sa = new Analysis(rootElement.getChild("rulebase"));
				} catch (Exception e) {
					if (ai==null) e.printStackTrace();
				}
			} else {
				// not known
			}
			
			System.out.println("SLA Definitions");
		    if (ai!=null) System.out.println(ai);
			System.out.println("SLA Rules");
			if (sa!=null) System.out.println(sa);
		}
		catch(Exception  e)
		{
		   e.printStackTrace();
		}
		
    }
    
    public String getProva()
    {
    	StringBuffer result = new StringBuffer();
		if (ai!=null) result.append(ai.toString());
		if (sa!=null) result.append(sa.toString());
        return result.toString();
    }
    
    public boolean output()
    {
    	try
    	{
    		FileWriter fw = new FileWriter(outputFile,false);
    		BufferedWriter bw = new BufferedWriter(fw);
    		bw.write(getProva());
    		bw.close();
    		fw.close();
    		return true;
    	}
    	catch(IOException ioe)
    	{
    		ioe.printStackTrace();
    		return false;
    	}
    }

}