/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 *
 * The contents of this file are subject to the Netscape Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/NPL/
 *
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 *
 * The Original Code is mozilla.org code.
 *
 * The Initial Developer of the Original Code is Netscape
 * Communications Corporation.  Portions created by Netscape are
 * Copyright (C) 1998 Netscape Communications Corporation. All
 * Rights Reserved.
 *
 * Contributor(s):
 * Craig point Dunn at ConceptDevelopment.NET
 */
/*
 * DO NOT EDIT THIS DOCUMENT MANUALLY !!!
 * THIS FILE IS AUTOMATICALLY GENERATED BY THE TOOLS UNDER
 *    AutoDetect/tools/
 */

using System;
using System.Net;
using org.mozilla.intl.chardet;

namespace org.mozilla.intl.chardet {
//import java.io.* ;
//import java.net.* ;
//import java.util.* ;
//import org.mozilla.intl.chardet.* ;

public class HtmlCharsetDetector {

    public static bool found = false ;

    public static void Main(String [] argv) { //throws Exception {

    if (argv.Length != 1 && argv.Length != 2) {

      Console.Out.WriteLine(
            "Usage: HtmlCharsetDetector <url> [<languageHint>]");

      Console.Out.WriteLine("");
      Console.Out.WriteLine("Where <url> is http://...");
      Console.Out.WriteLine("For optional <languageHint>. Use following...");
      Console.Out.WriteLine("        1 => Japanese");
      Console.Out.WriteLine("        2 => Chinese");
      Console.Out.WriteLine("        3 => Simplified Chinese");
      Console.Out.WriteLine("        4 => Traditional Chinese");
      Console.Out.WriteLine("        5 => Korean");
      Console.Out.WriteLine("        6 => Dont know (default)");

      return ;
    }

    // Initalize the nsDetector() ;
    int lang = (argv.Length == 2)? Convert.ToInt32 (argv[1])
                     : nsPSMDetector.ALL ;
    nsDetector det = new nsDetector(lang) ;

    // Set an observer...
    // The Notify() will be called when a matching charset is found.
    Notifier not = new Notifier();
    det.Init(not);

    Uri url = new Uri(argv[0]);
    HttpWebRequest req = (HttpWebRequest) WebRequest.Create(url.AbsoluteUri);

    try {
       System.Net.HttpWebResponse imp = (HttpWebResponse) req.GetResponse();
    } catch(System.Net.WebException we) { // remote url not found, 404; other error
        Console.Out.WriteLine("Web Request Error " + we.Message);
    }
    byte[] buf = new byte[1024] ;    int len;    bool done = false ;    bool isAscii = true ;

    while( (len=imp.GetResponseStream().Read(buf,0,buf.Length)) != -1) {
       // Check if the stream is only ascii.
       if (isAscii)
           isAscii = det.isAscii(buf,len);
       // DoIt if non-ascii and not done yet.
       if (!isAscii && !done)
            done = det.DoIt(buf,len, false);
    }
    det.DataEnd();


    if (isAscii) {
       Console.Out.WriteLine("CHARSET = ASCII");
       found = true ;
    }

    if (!found) {
       String [] prob = det.getProbableCharsets() ;
       for(int i=0; i<prob.Length; i++) {
        Console.Out.WriteLine("Probable Charset = " + prob[i]);
       }
    }
  }
}



// C# doesn't support anonymous methods...
 public class Notifier : nsICharsetDetectionObserver  {
    public void Notify(String charset) {
        HtmlCharsetDetector.found = true ;
        Console.Out.WriteLine("CHARSET = " + charset);
    }
 }

} // namespace