package org.plenix.jsp.language;

import com.kav.xsl.XSLStylesheet;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import javax.servlet.http.*;

import org.plenix.jsp.Util;
import org.plenix.jsp.language.*;

public abstract class LanguageProcessorImpl
  implements LanguageProcessor
{
  // Members
  Vector names;

  String sourceExtension;
  String objectExtension;

  Hashtable parameters;
  Hashtable stylesheets;

  // Constructor
  public LanguageProcessorImpl()
  {
    names = new Vector();
    stylesheets = new Hashtable();
    parameters = new Hashtable();
  }

  // Builders
  public void addName(String newName)
  {
    int nameCount = names.size();

    for (int i = 0; i < nameCount; i++)
    {
      String name = (String) names.elementAt(i);

      if (name.equalsIgnoreCase(newName))
      {
        return;
      }
    }

    names.addElement(newName.toLowerCase());
  }

  public void setSourceExtension(String extension)
  {
    sourceExtension = extension;
  }

  public void setObjectExtension(String extension)
  {
    objectExtension = extension;
  }

  public void addStylesheet(String version, XSLStylesheet stylesheet)
  {
    stylesheets.put(version, stylesheet);
  }

  public void addParameter(String name, String value)
  {
    name = name.toLowerCase();
    parameters.put(name, Util.parseForVars(value, System.getProperties()));
  }

  // Accessors
  public String getName()
  {
    return (String) names.elementAt(0);
  }

  public String[] getNames()
  {
    String array[] = new String[names.size()];

    names.copyInto(array);

    return array;
  }

  public String getSourceExtension()
  {
    return sourceExtension;
  }

  public String getObjectExtension()
  {
    return objectExtension;
  }

  public XSLStylesheet getStylesheet(String version)
  {
    return (XSLStylesheet) stylesheets.get(version);
  }

  public Enumeration getParameterNames()
  {
    return parameters.keys();
  }

  public String getParameter(String name)
  {
    return (String) parameters.get(name.toLowerCase());
  }

  // Program instance services
  public abstract void init(String path) throws LanguageException;
  public abstract void compile(String baseName, String path) throws LanguageException;
  public abstract void unload(Object program) throws LanguageException;

  public Object load(String baseName, String path)
    throws LanguageException
  {
    throw new LanguageException("Load method has not been implemented");
  }

  public void execute(Object program, HttpServletRequest request, HttpServletResponse response)
    throws LanguageException
  {
    throw new LanguageException("Execute method has not been implemented");
  }
}
