/*
 * 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 org.jdom.*;

import java.util.*;

/**
 * Term
 * 
 * A general implementation of Terms (Vars, Inds, Cterms) for translation into Prova syntax
 * 
 * @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 Term {

    static Term termControl(Element term)
    {		
        if(term.getName() == "var")
            return new Var(term);
        else if(term.getName() == "ind")
            return new Ind(term);
		else if(term.getName() == "cterm")			
            return new Cterm(term);
        return null;
    }
}


class Var extends Term
{
    private Element var;
    String content;
    private Namespace ns;

    public Var(Element _var)
    {
        var = _var;
        ns=var.getNamespace();
        content = var.getText();
		content = content.toUpperCase();
		Attribute type = var.getAttribute("type",ns);
		if (type != null) {			
			String sort = type.getValue();			
			if (sort.indexOf(".")>-1) {
				// Java type				
				content=sort+"."+content;
			} else {
				// DL Semantic Web type
				sort = sort.replaceFirst(":","_");
				content=content+":"+sort;
			}
			
		}
    }
	
    public String toString()
    {
        return content;
    }

}

class Ind extends Term
{
    private Element ind;
    String content;

    public Ind(Element _ind)
    {
        ind = _ind;
        content = ind.getText();
		Attribute type = ind.getAttribute("type");
		if (type != null) {
			String sort = type.getValue();
			if (sort.indexOf(".")>-1) {
				// Java type				
				if (sort.indexOf("String")>-1) {if (content.indexOf("\"")==-1) content = "\""+content+"\"";}
				else if (sort.indexOf("Integer")>-1) content = content;
				else content=sort+"("+content+")";
				
			} else {
				// DL Semantic Web type
				sort = sort.replaceFirst(":","_");
				content=sort+":"+content;
			}
			
		} else {
			if(!isNumber(content)) if (content.indexOf("\"")==-1) content = "\""+content+"\"";
		}
    }
 
    private String getResult()
    {
 
            return content;
                
    }

    public String toString()
    {
        return getResult();
    }
    

	private  boolean isNumber(String str)
	{
        final String filter = "0123456789."; 
        for (int i = 0; i < str.length(); i++) 
        {
            if (filter.indexOf(str.charAt(i)) == -1) 
            {
                return false;
            }
        }
        return true;
	}    
}

class Cterm extends Term
{
    private Element cterm;
    private String rel;

    private List termElements;
    private List termObjects;
    private Namespace ns;

    public Cterm(Element _cterm)
    {
        cterm = _cterm;
        ns=cterm.getNamespace();
        rel = cterm.getChild("_opc",ns).getChild("ctor",ns).getText();
        termObjects = new ArrayList();
        termElements = cterm.getChildren();
		if (termElements!=null)
        for(int n = 1; n < termElements.size(); n++)
        {
            Element term = (Element)termElements.get(n);
            termObjects.add(Term.termControl(term));
        }
    }

    public String toString()
    {
        StringBuffer sb = new StringBuffer(rel + "(");
        for(int n = 0 ; n < termObjects.size(); n++)
        {
            Term term = (Term)termObjects.get(n);
            sb.append(term.toString());
            if(n != termObjects.size() - 1)
            {
                sb.append(", ");
            }
        }
        sb.append(")");
        return sb.toString();
    }
}