メインクラス

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

Form1.vb


Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows フォーム デザイナで生成されたコード "

Public Sub New()
MyBase.New()

' この呼び出しは Windows フォーム デザイナで必要です。
InitializeComponent()

' InitializeComponent() 呼び出しの後に初期化を追加します。

End Sub

' Form は、コンポーネント一覧に後処理を実行するために dispose をオーバーライドします。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

' Windows フォーム デザイナで必要です。
Private components As System.ComponentModel.IContainer

' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。
'Windows フォーム デザイナを使って変更してください。
' コード エディタを使って変更しないでください。
Friend WithEvents cboLangType As System.Windows.Forms.ComboBox
Friend WithEvents label3 As System.Windows.Forms.Label
Friend WithEvents btnExit As System.Windows.Forms.Button
Friend WithEvents btnOK As System.Windows.Forms.Button
Friend WithEvents txtOutput As System.Windows.Forms.TextBox
Friend WithEvents txtInput As System.Windows.Forms.TextBox
Friend WithEvents label2 As System.Windows.Forms.Label
Friend WithEvents label1 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.cboLangType = New System.Windows.Forms.ComboBox
Me.label3 = New System.Windows.Forms.Label
Me.btnExit = New System.Windows.Forms.Button
Me.btnOK = New System.Windows.Forms.Button
Me.txtOutput = New System.Windows.Forms.TextBox
Me.txtInput = New System.Windows.Forms.TextBox
Me.label2 = New System.Windows.Forms.Label
Me.label1 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'cboLangType
'
Me.cboLangType.Items.AddRange(New Object() {"cs7", "vc6", "vc7", "vj6", "vj7", "java", "js", "bcb", "vb6", "vb7", "del", "psq", "tsq"})
Me.cboLangType.Location = New System.Drawing.Point(113, 7)
Me.cboLangType.Name = "cboLangType"
Me.cboLangType.Size = New System.Drawing.Size(48, 20)
Me.cboLangType.TabIndex = 9
'
'label3
'
Me.label3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.label3.Location = New System.Drawing.Point(9, 7)
Me.label3.Name = "label3"
Me.label3.Size = New System.Drawing.Size(96, 19)
Me.label3.TabIndex = 8
Me.label3.Text = "言語タイプ"
Me.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'btnExit
'
Me.btnExit.Location = New System.Drawing.Point(313, 87)
Me.btnExit.Name = "btnExit"
Me.btnExit.Size = New System.Drawing.Size(72, 32)
Me.btnExit.TabIndex = 15
Me.btnExit.Text = "終了"
'
'btnOK
'
Me.btnOK.Location = New System.Drawing.Point(233, 87)
Me.btnOK.Name = "btnOK"
Me.btnOK.Size = New System.Drawing.Size(72, 32)
Me.btnOK.TabIndex = 14
Me.btnOK.Text = "実行"
'
'txtOutput
'
Me.txtOutput.Location = New System.Drawing.Point(113, 55)
Me.txtOutput.Name = "txtOutput"
Me.txtOutput.Size = New System.Drawing.Size(272, 19)
Me.txtOutput.TabIndex = 13
Me.txtOutput.Text = ""
'
'txtInput
'
Me.txtInput.Location = New System.Drawing.Point(113, 31)
Me.txtInput.Name = "txtInput"
Me.txtInput.Size = New System.Drawing.Size(272, 19)
Me.txtInput.TabIndex = 11
Me.txtInput.Text = ""
'
'label2
'
Me.label2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.label2.Location = New System.Drawing.Point(9, 55)
Me.label2.Name = "label2"
Me.label2.Size = New System.Drawing.Size(96, 19)
Me.label2.TabIndex = 12
Me.label2.Text = "出力ファイル名"
Me.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'label1
'
Me.label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.label1.Location = New System.Drawing.Point(9, 31)
Me.label1.Name = "label1"
Me.label1.Size = New System.Drawing.Size(96, 19)
Me.label1.TabIndex = 10
Me.label1.Text = "入力ファイル名"
Me.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
Me.ClientSize = New System.Drawing.Size(394, 127)
Me.Controls.Add(Me.cboLangType)
Me.Controls.Add(Me.label3)
Me.Controls.Add(Me.btnExit)
Me.Controls.Add(Me.btnOK)
Me.Controls.Add(Me.txtOutput)
Me.Controls.Add(Me.txtInput)
Me.Controls.Add(Me.label2)
Me.Controls.Add(Me.label1)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "Form1"
Me.Text = "SourceToHTML"
Me.ResumeLayout(False)

End Sub

#End Region
'---------------------------------------------------------------------------------------------------
' 初期化
'---------------------------------------------------------------------------------------------------
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cboLangType.Text = ""
txtInput.Text = ""
txtOutput.Text = ""

'言語タイプを実行時引数から取り込む
If (Environment.GetCommandLineArgs().Length > 1) Then
cboLangType.Text = Environment.GetCommandLineArgs()(1)
End If

'入力ファイル名を実行時引数から取り込む
If (Environment.GetCommandLineArgs().Length > 2) Then
txtInput.Text = Environment.GetCommandLineArgs()(2)
End If

'出力ファイル名を実行時引数から取り込む
If (Environment.GetCommandLineArgs().Length > 3) Then
txtOutput.Text = Environment.GetCommandLineArgs()(3)
End If

'実行時引数が全てセットされていたら、すぐに実行する
If (Environment.GetCommandLineArgs().Length > 3) Then
btnOK.PerformClick()
End If
End Sub
'---------------------------------------------------------------------------------------------------
' 終了
'---------------------------------------------------------------------------------------------------
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
'---------------------------------------------------------------------------------------------------
' 実行ボタン クリック
'---------------------------------------------------------------------------------------------------
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
'実行時引数をチェック
If (Not isCorrectArg()) Then Return

'言語種別を取得
Dim langType As String = getLangType(cboLangType.Text)

'出力用クラスを初期化
Dim textWriter As Writer = New Writer(txtOutput.Text)

'変換用クラスを初期化
Dim conv As Convert = New Convert()

'テンプレートファイルを読んで、ヘッダ部を設定する
Dim header As Reader = New Reader(System.Windows.Forms.Application.StartupPath + "\Template\header.txt")
conv.charcopy(header, textWriter)
header = Nothing

'入力用クラスを初期化
Dim textReader As Reader = New Reader(txtInput.Text)

'HTML に変換する
conv.tohtml(textReader, textWriter, langType)

'入力用クラスを解放
textReader = Nothing

'テンプレートファイルを読んで、フッタ部を設定する
Dim footer As Reader = New Reader(System.Windows.Forms.Application.StartupPath + "\Template\footer.txt")
conv.charcopy(footer, textWriter)
footer = Nothing

'変換用クラスを解放
conv = Nothing

'出力用クラスを解放
textWriter.Close()
textWriter = Nothing

'終了
Me.Close()
End Sub
'---------------------------------------------------------------------------------------------------
' 実行時引数をチェック
'---------------------------------------------------------------------------------------------------
Private Function isCorrectArg() As Boolean
'必須チェック
If (cboLangType.Text = "") Then Return False
If (txtInput.Text = "") Then Return False
If (txtOutput.Text = "") Then Return False

'入力ファイルチェック
Dim textReader As System.IO.StreamReader
Try
textReader = New System.IO.StreamReader(txtInput.Text, System.Text.Encoding.GetEncoding("Shift_JIS"))
textReader.Close()
Catch ex As Exception
MessageBox.Show(ex.Message,"エラー",MessageBoxButtons.OK,MessageBoxIcon.Exclamation)
Return False
Finally
textReader = Nothing
End Try

'出力ファイルチェック
Dim textWriter As System.IO.StreamWriter
Try
textWriter = New System.IO.StreamWriter(txtOutput.Text, False, System.Text.Encoding.GetEncoding("Shift_JIS"))
textWriter.Close()
Catch ex As Exception
MessageBox.Show(ex.Message,"エラー",MessageBoxButtons.OK,MessageBoxIcon.Exclamation)
Return False
Finally
textWriter = Nothing
End Try

Return True
End Function
'---------------------------------------------------------------------------------------------------
' 言語種別を取得
'---------------------------------------------------------------------------------------------------
Private Function getLangType(ByVal aLangType As String) As String
Dim langType As String = aLangType.ToLower()

Select Case langType
Case "cs7": langType = "cs7" ' VC#.NET
Case "vc6": langType = "vc6" ' VC++
Case "vc7": langType = "vc7" ' VC++.NET
Case "vj6": langType = "java" ' VJ++
Case "vj7": langType = "java" ' VJ#.NET
Case "java": langType = "java" ' Java
Case "js": langType = "js" ' JavaScript
Case "bcb": langType = "bcb" ' C++Builder
Case "vb6": langType = "vb6" ' VB
Case "vb7": langType = "vb7" ' VB.NET
Case "del": langType = "del" ' Delphi
Case "psq": langType = "psq" ' PL/SQL
Case "tsq": langType = "tsq" ' T-SQL
Case Else: langType = "" ' other
End Select

Return langType
End Function
End Class