まとめ 【メインクラス】

まとめ 【メインクラス】

これまで、コンソールアプリケーションとして、「SourceToHTML」を作成してきたが、
画面から実行できるよう変更する。

Form1.cs


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace SourceToHtml
{
//*******************************************************************************************************
// HTML変換処理 画面クラス
//*******************************************************************************************************
public class Form1 : System.Windows.Forms.Form
{

private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtInput;
private System.Windows.Forms.TextBox txtOutput;
private System.Windows.Forms.Button btnOK;
private System.Windows.Forms.Button btnExit;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.ComboBox cboLangType;
/// <summary>
/// 必要なデザイナ変数です。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows フォーム デザイナ サポートに必要です。
//
InitializeComponent();

//
// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
//
}

/// <summary>
/// 使用されているリソースに後処理を実行します。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows フォーム デザイナで生成されたコード
/// <summary>
/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
/// コード エディタで変更しないでください。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.txtInput = new System.Windows.Forms.TextBox();
this.txtOutput = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.btnOK = new System.Windows.Forms.Button();
this.btnExit = new System.Windows.Forms.Button();
this.label3 = new System.Windows.Forms.Label();
this.cboLangType = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// label1
//
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.label1.Location = new System.Drawing.Point(8, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(96, 19);
this.label1.TabIndex = 2;
this.label1.Text = "入力ファイル名";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// txtInput
//
this.txtInput.Location = new System.Drawing.Point(112, 32);
this.txtInput.Name = "txtInput";
this.txtInput.Size = new System.Drawing.Size(272, 19);
this.txtInput.TabIndex = 3;
this.txtInput.Text = "";
//
// txtOutput
//
this.txtOutput.Location = new System.Drawing.Point(112, 56);
this.txtOutput.Name = "txtOutput";
this.txtOutput.Size = new System.Drawing.Size(272, 19);
this.txtOutput.TabIndex = 5;
this.txtOutput.Text = "";
//
// label2
//
this.label2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.label2.Location = new System.Drawing.Point(8, 56);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(96, 19);
this.label2.TabIndex = 4;
this.label2.Text = "出力ファイル名";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// btnOK
//
this.btnOK.Location = new System.Drawing.Point(232, 88);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(72, 32);
this.btnOK.TabIndex = 6;
this.btnOK.Text = "実行";
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(312, 88);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(72, 32);
this.btnExit.TabIndex = 7;
this.btnExit.Text = "終了";
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// label3
//
this.label3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.label3.Location = new System.Drawing.Point(8, 8);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(96, 19);
this.label3.TabIndex = 0;
this.label3.Text = "言語タイプ";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// cboLangType
//
this.cboLangType.Items.AddRange(new object[] {
"cs7",
"vc6",
"vc7",
"vj6",
"vj7",
"java",
"js",
"bcb",
"vb6",
"vb7",
"del",
"psq",
"tsq"});
this.cboLangType.Location = new System.Drawing.Point(112, 8);
this.cboLangType.Name = "cboLangType";
this.cboLangType.Size = new System.Drawing.Size(48, 20);
this.cboLangType.TabIndex = 1;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.ClientSize = new System.Drawing.Size(394, 127);
this.Controls.Add(this.cboLangType);
this.Controls.Add(this.label3);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnOK);
this.Controls.Add(this.txtOutput);
this.Controls.Add(this.txtInput);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.Text = "SourceToHTML";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// アプリケーションのメイン エントリ ポイントです。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
//---------------------------------------------------------------------------------------------------
// 初期化
//---------------------------------------------------------------------------------------------------
private void Form1_Load(object sender, System.EventArgs e)
{
cboLangType.Text = "";
txtInput.Text = "";
txtOutput.Text = "";

//言語タイプを実行時引数から取り込む
if (Environment.GetCommandLineArgs().Length > 1)
cboLangType.Text = Environment.GetCommandLineArgs()[1];

//入力ファイル名を実行時引数から取り込む
if (Environment.GetCommandLineArgs().Length > 2)
txtInput.Text = Environment.GetCommandLineArgs()[2];

//出力ファイル名を実行時引数から取り込む
if (Environment.GetCommandLineArgs().Length > 3)
txtOutput.Text = Environment.GetCommandLineArgs()[3];

//実行時引数が全てセットされていたら、すぐに実行する
if (Environment.GetCommandLineArgs().Length > 3)
btnOK.PerformClick();
}
//---------------------------------------------------------------------------------------------------
// 終了
//---------------------------------------------------------------------------------------------------
private void btnExit_Click(object sender, System.EventArgs e)
{
this.Close();
}
//---------------------------------------------------------------------------------------------------
// 実行ボタン クリック
//---------------------------------------------------------------------------------------------------
private void btnOK_Click(object sender, System.EventArgs e)
{
//実行時引数をチェック
if (!isCorrectArg()) return;

//言語種別を取得
string langType = getLangType(cboLangType.Text);

//出力用クラスを初期化
Writer writer = new Writer(txtOutput.Text);

//変換用クラスを初期化
Convert conv = new Convert();

//テンプレートファイルを読んで、ヘッダ部を設定する
Reader header = new Reader(System.Windows.Forms.Application.StartupPath + "\\Template\\header.txt");
conv.charcopy(header, writer);
header = null;

//入力用クラスを初期化
Reader reader = new Reader(txtInput.Text);

//HTML に変換する
conv.tohtml(reader, writer, langType);

//入力用クラスを解放
reader = null;

//テンプレートファイルを読んで、フッタ部を設定する
Reader footer = new Reader(System.Windows.Forms.Application.StartupPath + "\\Template\\footer.txt");
conv.charcopy(footer, writer);
footer = null;

//変換用クラスを解放
conv = null;

//出力用クラスを解放
writer.Close();
writer = null;

this.Close();
}
//---------------------------------------------------------------------------------------------------
// 実行時引数をチェック
//---------------------------------------------------------------------------------------------------
private bool isCorrectArg()
{
//必須チェック
if (cboLangType.Text == "") return false;
if (txtInput.Text == "") return false;
if (txtOutput.Text == "") return false;


//入力ファイルチェック
System.IO.StreamReader textReader;
try
{
textReader = new System.IO.StreamReader(txtInput.Text, System.Text.Encoding.GetEncoding("Shift_JIS"));
textReader.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,"エラー",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
return false;
}
finally
{
textReader = null;
}

//出力ファイルチェック
System.IO.StreamWriter textWriter;
try
{
textWriter = new System.IO.StreamWriter(txtOutput.Text, false, System.Text.Encoding.GetEncoding("Shift_JIS"));
textWriter.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,"エラー",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
return false;
}
finally
{
textWriter = null;
}

return true;
}
//---------------------------------------------------------------------------------------------------
// 言語種別を取得
//---------------------------------------------------------------------------------------------------
private string getLangType(string aLangType)
{
string langType = aLangType.ToLower();

if (langType == "cs7") langType = "cs7"; // VC#.NET
else if (langType == "vc6") langType = "vc6"; // VC++
else if (langType == "vc7") langType = "vc7"; // VC++.NET
else if (langType == "vj6") langType = "java"; // VJ++
else if (langType == "vj7") langType = "java"; // VJ#.NET
else if (langType == "java") langType = "java"; // Java
else if (langType == "js") langType = "js"; // JavaScript
else if (langType == "bcb") langType = "bcb"; // C++Builder
else if (langType == "vb6") langType = "vb6"; // VB
else if (langType == "vb7") langType = "vb7"; // VB.NET
else if (langType == "del") langType = "del"; // Delphi
else if (langType == "psq") langType = "psq"; // PL/SQL
else if (langType == "tsq") langType = "tsq"; // T-SQL
else langType = ""; // other

return langType;
}
}
}