Main.cs
上传用户:jingke1993
上传日期:2022-06-08
资源大小:140k
文件大小:7k
源码类别:

xml/soap/webservice

开发平台:

Visual C++

  1. using System;
  2. using System.Xml;
  3. using System.IO;
  4. using System.Net;
  5. using System.Text;
  6. using System.Windows.Forms;
  7. using System.Collections;
  8. namespace Sgml {
  9.     /// <summary>
  10.     /// This class provides a command line interface to the SgmlReader.
  11.     /// </summary>
  12.     public class CommandLine {
  13.         string proxy = null;
  14.         string output = null;
  15.         bool formatted = false;
  16.         bool noxmldecl = false;
  17.         Encoding encoding = null;
  18.         [STAThread]
  19.         static void Main(string[] args) {
  20.             try {
  21.                 CommandLine t = new CommandLine();
  22.                 t.Run(args);
  23.             } catch (Exception e) {
  24.                 Console.WriteLine("Error: " + e.Message);
  25.             }
  26.             return;
  27.         }
  28.         public void Run(string[] args) {
  29.             SgmlReader reader = new SgmlReader();
  30.             string inputUri = null;
  31.             for (int i = 0; i < args.Length; i++) {
  32.                 string arg = args[i];
  33.                 if (arg[0] == '-' || arg[0] == '/') {
  34.                     switch (arg.Substring(1)) {
  35.                         case "e":
  36.                             string errorlog = args[++i];
  37.                             if (errorlog.ToLower() == "$stderr") {
  38.                                 reader.ErrorLog = Console.Error;
  39.                             } 
  40.                             else {
  41.                                 reader.ErrorLogFile = errorlog;
  42.                             }
  43.                             break;
  44.                         case "html":
  45.                             reader.DocType = "HTML";
  46.                             break;
  47.                         case "dtd":
  48.                             reader.SystemLiteral = args[++i];
  49.                             break;
  50.                         case "proxy":
  51.                             proxy = args[++i];
  52.                             reader.WebProxy = proxy;
  53.                             break;
  54.                         case "encoding":
  55.                             encoding = Encoding.GetEncoding(args[++i]);
  56.                             break;
  57.                         case "f":
  58.                             formatted = true;
  59.                             reader.WhitespaceHandling = WhitespaceHandling.None;
  60.                             break;
  61.                         case "noxml":
  62.                             noxmldecl = true;
  63.                             break;
  64.                         case "doctype":
  65.                             reader.StripDocType = false;
  66.                             break;
  67.                         case "lower":
  68.                             reader.CaseFolding = CaseFolding.ToLower;
  69.                             break;
  70.                         case "upper":
  71.                             reader.CaseFolding = CaseFolding.ToUpper;
  72.                             break;
  73.                         default:
  74.                             Console.WriteLine("Usage: SgmlReader <options> [InputUri] [OutputFile]");
  75.                             Console.WriteLine("-e log         Optional log file name, name of '$STDERR' will write errors to stderr");
  76.                             Console.WriteLine("-f             Whether to pretty print the output.");
  77.                             Console.WriteLine("-html          Specify the built in HTML dtd");
  78.                             Console.WriteLine("-dtd url       Specify other SGML dtd to use");
  79.                             Console.WriteLine("-base          Add base tag to output HTML");
  80.                             Console.WriteLine("-noxml         Do not add XML declaration to the output");
  81.                             Console.WriteLine("-proxy svr:80  Proxy server to use for http requests");
  82.                             Console.WriteLine("-encoding name Specify an encoding for the output file (default UTF-8)");
  83.                             Console.WriteLine("-lower         Convert input tags to lower case");
  84.                             Console.WriteLine("-upper         Convert input tags to upper case");
  85.                             Console.WriteLine();
  86.                             Console.WriteLine("InputUri       The input file or http URL (default stdin).  ");
  87.                             Console.WriteLine("               Supports wildcards for local file names.");
  88.                             Console.WriteLine("OutputFile     Output file name (default stdout)");
  89.                             Console.WriteLine("               If input file contains wildcards then this just specifies the output file extension (default .xml)");
  90.                             return;
  91.                     }
  92.                 } 
  93.                 else {
  94.                     if (inputUri == null) {
  95.                         inputUri = arg;
  96.                         string ext = Path.GetExtension(arg).ToLower();
  97.                         if (ext == ".htm" || ext == ".html")
  98.                             reader.DocType = "HTML";
  99.                     }
  100.                     else if (output == null) output = arg;
  101.                 }
  102.             }
  103.             if (inputUri != null && !inputUri.StartsWith("http://") && inputUri.IndexOfAny(new char[] { '*', '?' }) >= 0) {
  104.                 // wild card processing of a directory of files.
  105.                 string path = Path.GetDirectoryName(inputUri);
  106.                 if (path == "") path = ".\";
  107.                 string ext = ".xml";
  108.                 if (output != null) 
  109.                     ext = Path.GetExtension(output);
  110.                 foreach (string uri in Directory.GetFiles(path, Path.GetFileName(inputUri))) {
  111.                     Console.WriteLine("Processing: " + uri);
  112.                     string file = Path.GetFileName(uri);
  113.                     output = Path.GetDirectoryName(uri) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(file) + ext;
  114.                     Process(reader, uri);
  115.                     reader.Close();
  116.                 }        
  117.                 return;
  118.             } 
  119.             Process(reader, inputUri);
  120.             reader.Close();
  121.            
  122.             return ;
  123.         }
  124.         void Process(SgmlReader reader, string uri) {   
  125.             if (uri == null) {
  126.                 reader.InputStream = Console.In;
  127.             } else {
  128.                 reader.Href = uri;
  129.             }
  130.             if (this.encoding == null) {
  131.                 this.encoding = reader.GetEncoding();
  132.             }
  133.             XmlTextWriter w = null;
  134.             if (output != null) {
  135.                 w = new XmlTextWriter(output, this.encoding);          
  136.             } 
  137.             else {
  138.                 w = new XmlTextWriter(Console.Out);
  139.             }
  140.             if (formatted) w.Formatting = Formatting.Indented;
  141.             if (!noxmldecl) {
  142.                 w.WriteStartDocument();
  143.             }
  144.             reader.Read();
  145.             while (!reader.EOF) {
  146.                 w.WriteNode(reader, true);
  147.             }
  148.             w.Flush();
  149.             w.Close();          
  150.         }
  151.     }    
  152. }