package com.koberg.makecontent;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Enumeration;
import java.net.URL;

import org.xml.sax.*;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;


public class MasterLogin extends HttpServlet {

   ServletContext CONTEXT;
   private static Templates templates = null;
   private static TransformerFactory tFactory = null;
   
   public void init(ServletConfig config) throws ServletException {
   
      super.init(config);
      
      CONTEXT = getServletConfig().getServletContext();
      
      System.setProperty("javax.xml.transform.TransformerFactory",
			   "org.apache.xalan.xsltc.trax.TransformerFactoryImpl");
            
      System.setProperty("javax.xml.parsers.SAXParserFactory", 
            "org.apache.xerces.jaxp.SAXParserImpl");
      // initial parameters are brought in from the /WEB-INF/web.xml
      // <context-param site_chooser="/WEB-INF/tool/site-chooser.xsl"/>
      String value = (String) CONTEXT.getInitParameter("site_chooser");   
      String xsl = ""; 
      try {
         xsl = CONTEXT.getResource(value).toString(); 
System.out.println("xsl: " + xsl);
      } catch (Exception  e) {
         System.out.println("");
         System.out.println("********************************************");
	      System.out.println("can't get XSL system ID string, " + e.getMessage());
	      e.printStackTrace(System.err);
         System.out.println("********************************************");
         System.out.println("");
	   }
	   Source xslSource = null;
      
      try {
System.out.println("in the try");

	      if (xsl != null && xsl.length()> 0) xslSource = new StreamSource(xsl);
         
	      if (xslSource != null) {      
System.out.println("xslSource is " + xslSource);
            tFactory = TransformerFactory.newInstance();
System.out.println("tFactory is " + tFactory);
		      templates = tFactory.newTemplates(xslSource);           
System.out.println("template is " + templates);
	      } else {
System.out.println("No Stylesheet!");
	      }
	   } catch (TransformerConfigurationException e) {
         System.out.println("");
         System.out.println("********************************************");
	      System.out.println("can't make templates, " + e.getMessage());
	      e.printStackTrace(System.err);
         System.out.println("********************************************");
         System.out.println("");
	   } catch (Exception e) {
         System.out.println("");
         System.out.println("********************************************");
	      e.printStackTrace(System.out);    
         System.out.println("********************************************");
         System.out.println("");
	   }      
   }


   public void doPost (HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {

      res.setContentType("text/html");
	   PrintWriter out = res.getWriter();
	   Source xmlSource = null;   
      
      String email      = req.getParameter("j_username");
      String password   = req.getParameter("j_password");
      
      Hashtable style_params = new Hashtable();

      style_params.put("user-name", email);
      style_params.put("user-password", password);
       // initial parameters are brought in from the /WEB-INF/web.xml
      // <context-param xml_config_sites_list="/WEB-INF/tool/config-sites-list.xml"/>
      String xml = CONTEXT.getResource(CONTEXT.getInitParameter("xml_config_sites_list")).toString();
      
      try {
	      if (xml != null && xml.length()> 0) {
		      xmlSource = new StreamSource(new URL(xml).openStream());
	      } else {
		      out.write("No XML Input Document!");
	    }
	    long start = System.currentTimeMillis();
	    Transformer transformer = null;
System.out.println("in doPost, templates is " + templates);
	    transformer = templates.newTransformer();
       
       Enumeration params_keys = style_params.keys();
       Enumeration params_values = style_params.elements();
       while (params_keys.hasMoreElements()) {
         String param = (String) params_keys.nextElement();
         String value = (String) params_values.nextElement();
         transformer.setParameter(param, value);
       }
        
	    // Perform the transformation.
	    transformer.transform(xmlSource, new StreamResult(out));
	    long stop = System.currentTimeMillis();
out.println("<!-- transformed in "+ (stop-start)+"ms -->");
	   } catch (Exception e) {
	      e.printStackTrace(out);    
	   }
	   out.close();
   } // end of doPost
}


