メインクラス

これまでC# で作成してきた「SourceToHTML」を、VJ# に焼きなおしてみます。

Form1.jsl


package SourceToHtml;

import System.*;
import System.Drawing.*;
import System.Collections.*;
import System.ComponentModel.*;
import System.Windows.Forms.*;
import System.Data.*;

/**
* Form1 の概要の説明です。
*/

public class Form1 extends System.Windows.Forms.Form
{
private System.Windows.Forms.ComboBox cboLangType;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button btnExit;
private System.Windows.Forms.Button btnOK;
private System.Windows.Forms.TextBox txtOutput;
private System.Windows.Forms.TextBox txtInput;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
/**
* 必要なデザイナ変数です。
*/

private System.ComponentModel.Container components = null;

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

//
// TODO: InitializeComponent 呼び出しの後にコンストラクタ コードを追加します。
//
}

/**
* 使用されているリソースに後処理を実行します。
*/

protected void Dispose(boolean disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
super.Dispose(disposing);
}

#region Windows フォーム デザイナで生成されたコード
/**
* デザイナのサポートに必要なメソッドです。コード エディタで
* このメソッドの内容を変更しないでください。
*/

private void InitializeComponent()
{
this.cboLangType = new System.Windows.Forms.ComboBox();
this.label3 = new System.Windows.Forms.Label();
this.btnExit = new System.Windows.Forms.Button();
this.btnOK = new System.Windows.Forms.Button();
this.txtOutput = new System.Windows.Forms.TextBox();
this.txtInput = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// cboLangType
//
this.cboLangType.get_Items().AddRange(new Object[]
{
"cs7",
"vc6",
"vc7",
"vj6",
"vj7",
"java",
"js",
"bcb",
"vb6",
"vb7",
"del",
"psq",
"tsq"}
);
this.cboLangType.set_Location(new System.Drawing.Point(113, 7));
this.cboLangType.set_Name("cboLangType");
this.cboLangType.set_Size(new System.Drawing.Size(48, 20));
this.cboLangType.set_TabIndex(9);
//
// label3
//
this.label3.set_BorderStyle(System.Windows.Forms.BorderStyle.FixedSingle);
this.label3.set_Location(new System.Drawing.Point(9, 7));
this.label3.set_Name("label3");
this.label3.set_Size(new System.Drawing.Size(96, 19));
this.label3.set_TabIndex(8);
this.label3.set_Text("言語タイプ");
this.label3.set_TextAlign(System.Drawing.ContentAlignment.MiddleCenter);
//
// btnExit
//
this.btnExit.set_Location(new System.Drawing.Point(313, 87));
this.btnExit.set_Name("btnExit");
this.btnExit.set_Size(new System.Drawing.Size(72, 32));
this.btnExit.set_TabIndex(15);
this.btnExit.set_Text("終了");
this.btnExit.add_Click( new System.EventHandler(this.btnExit_Click) );
//
// btnOK
//
this.btnOK.set_Location(new System.Drawing.Point(233, 87));
this.btnOK.set_Name("btnOK");
this.btnOK.set_Size(new System.Drawing.Size(72, 32));
this.btnOK.set_TabIndex(14);
this.btnOK.set_Text("実行");
this.btnOK.add_Click( new System.EventHandler(this.btnOK_Click) );
//
// txtOutput
//
this.txtOutput.set_Location(new System.Drawing.Point(113, 55));
this.txtOutput.set_Name("txtOutput");
this.txtOutput.set_Size(new System.Drawing.Size(272, 19));
this.txtOutput.set_TabIndex(13);
this.txtOutput.set_Text("");
//
// txtInput
//
this.txtInput.set_Location(new System.Drawing.Point(113, 31));
this.txtInput.set_Name("txtInput");
this.txtInput.set_Size(new System.Drawing.Size(272, 19));
this.txtInput.set_TabIndex(11);
this.txtInput.set_Text("");
//
// label2
//
this.label2.set_BorderStyle(System.Windows.Forms.BorderStyle.FixedSingle);
this.label2.set_Location(new System.Drawing.Point(9, 55));
this.label2.set_Name("label2");
this.label2.set_Size(new System.Drawing.Size(96, 19));
this.label2.set_TabIndex(12);
this.label2.set_Text("出力ファイル名");
this.label2.set_TextAlign(System.Drawing.ContentAlignment.MiddleCenter);
//
// label1
//
this.label1.set_BorderStyle(System.Windows.Forms.BorderStyle.FixedSingle);
this.label1.set_Location(new System.Drawing.Point(9, 31));
this.label1.set_Name("label1");
this.label1.set_Size(new System.Drawing.Size(96, 19));
this.label1.set_TabIndex(10);
this.label1.set_Text("入力ファイル名");
this.label1.set_TextAlign(System.Drawing.ContentAlignment.MiddleCenter);
//
// Form1
//
this.set_AutoScaleBaseSize(new System.Drawing.Size(5, 12));
this.set_ClientSize(new System.Drawing.Size(394, 127));
this.get_Controls().Add(this.cboLangType);
this.get_Controls().Add(this.label3);
this.get_Controls().Add(this.btnExit);
this.get_Controls().Add(this.btnOK);
this.get_Controls().Add(this.txtOutput);
this.get_Controls().Add(this.txtInput);
this.get_Controls().Add(this.label2);
this.get_Controls().Add(this.label1);
this.set_FormBorderStyle(System.Windows.Forms.FormBorderStyle.FixedSingle);
this.set_MaximizeBox(false);
this.set_MinimizeBox(false);
this.set_Name("Form1");
this.set_Text("SourceToHTML");
this.add_Load( new System.EventHandler(this.Form1_Load) );
this.ResumeLayout(false);

}
#endregion

/**
* アプリケーションのメイン エントリ ポイントです。
*/

/** @attribute System.STAThread() */
public static void main(String[] args)
{
Application.Run(new Form1());
}
//---------------------------------------------------------------------------------------------------
// 初期化
//---------------------------------------------------------------------------------------------------
private void Form1_Load (Object sender, System.EventArgs e)
{
cboLangType.set_Text("");
txtInput.set_Text("");
txtOutput.set_Text("");

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

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

//出力ファイル名を実行時引数から取り込む
if (Environment.GetCommandLineArgs().length > 3)
txtOutput.set_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.get_Text());

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

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

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

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

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

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

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

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

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

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


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

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

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

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

return langType;
}

}