import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
//import oracle.xml.parser.v2.*;
import org.xml.sax.*;
import org.apache.xerces.parsers.*;
import org.apache.xerces.*;
import org.apache.xerces.validators.schema.identity.*;
import org.apache.xerces.dom.*;
import java.net.*;

public class ValidateServlet extends HttpServlet {
  //private static final String CONTENT_TYPE = "text/xml";
 // private static final String DOC_TYPE;

    FileOutputStream fos = null;
    DataOutputStream dos = null;


  /**Initialize global variables*/
  public void init(ServletConfig config) throws ServletException {
    super.init(config);
    try{
    fos = new FileOutputStream("./logs/cvxserv.log");
    dos = new DataOutputStream(fos);
    }catch(IOException e){}

  }

  /**Process the HTTP Get request*/
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try{
    PrintWriter out = response.getWriter();
    DOMParser dp  = new DOMParser();
    //writeError("one");
    //dp.setValidationMode(false);
    //writeError("two");
    //dp.setSchemaValidationMode(true);
    //writeError("three");
    //dp.setPreserveWhitespace (true);
    //InputSource is = new InputSource(request.getInputStream());
    ParseTest pt = new ParseTest();
    String s =  pt.parseXML(request.getInputStream());
    //dp.setValidation(true);
    //dp.setValidationSchema(true);
    writeError("four" + s);
//    try{
//    dp.parse(createURL("c:/broadwing/xml/cv_enterprise.xml"));
  //    dp.parse(is);
  //  }catch(Exception e){writeError("exception is " + e.getMessage());}
    writeError("five");
    out.println("<html><body>");
    out.println("its working");
    out.println("</body></html>");
    }catch(Exception e){}
  }

  public URL createURL(String fileName)
   {
      URL url = null;
      try
      {
         url = new URL(fileName);
      }
      catch (MalformedURLException ex){
         File f = new File(fileName);
         try{
            String path = f.getAbsolutePath();
            // This is a bunch of weird code that is required to
            // make a valid URL on the Windows platform, due
            // to inconsistencies in what getAbsolutePath returns.
            String fs = System.getProperty("file.separator");
            if (fs.length() == 1){
               char sep = fs.charAt(0);
               if (sep != '/')
                  path = path.replace(sep, '/');
               if (path.charAt(0) != '/')
                  path = '/' + path;
            }
            path = "file://" + path;
            url = new URL(path);
         }
         /*catch (MalformedURLException e)
         {
            System.out.println("Cannot create url for: " + fileName);
            System.exit(0);
         }*/
         catch(Exception ee){System.out.println(ee.getMessage());}
      }
      return url;
   }

  public void writeError(String errmsg){
    try{
    dos.writeChars(errmsg);
    }catch(IOException e){}
  }

  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request,response);
  }

  /**Clean up resources*/
  public void destroy() {
  }
}
