Imports System.Text.RegularExpressions
''' <summary>
''' 変換(SQLServerコード→HTML)
''' </summary>
''' <remarks></remarks>
Public Class SqlServerCodeConverterToHtml
Inherits CodeConverterToHtml
#Region "定数定義"
''' <summary>
''' 背景色
''' </summary>
''' <remarks></remarks>
Private Const BACK_COLOR As String = "#FFFFE0"
''' <summary>
''' 背景色(タイトル)
''' </summary>
''' <remarks></remarks>
Private Const BACK_COLOR_TITLE As String = "#66CCCC"
''' <summary>
''' 前景色
''' </summary>
''' <remarks></remarks>
Private Const FORE_COLOR As String = "#000000"
''' <summary>
''' 前景色(コメント)
''' </summary>
''' <remarks></remarks>
Private Const FORE_COLOR_COMMENT As String = "#008040"
''' <summary>
''' 前景色(文字列)
''' </summary>
''' <remarks></remarks>
Private Const FORE_COLOR_STRING As String = "#FF0000"
''' <summary>
''' 前景色(予約語)
''' </summary>
''' <remarks></remarks>
Private Const FORE_COLOR_RESERVED_WORD As String = "#0000FF"
#End Region
#Region "変数定義"
''' <summary>
''' 正規表現
''' </summary>
''' <remarks></remarks>
Private m_RegEx As Regex = Nothing
''' <summary>
''' 予約語一覧
''' </summary>
''' <remarks></remarks>
Private m_ReservedWords As New List(Of String)
#End Region
#Region "コンストラクタ"
''' <summary>
''' コンストラクタ
''' </summary>
''' <remarks></remarks>
Public Sub New()
End Sub
#End Region
#Region "変換対象の文字列を検索する正規表現を取得する。"
''' <summary>
''' 変換対象の文字列を検索する正規表現を取得する。
''' </summary>
''' <returns>正規表現</returns>
''' <remarks></remarks>
Protected Overrides Function GetRegEx() As Regex
If Not (Me.m_RegEx Is Nothing) Then
Return Me.m_RegEx
End If
Dim strPatternAll As String = Nothing
'正規表現パターン
'※コメント判別
'(先頭開始、0個以上の「 」、1個以上の「--」、0個以上の任意文字)
Dim strPattern1 As String = "^( )*(--)+.*"
'※コメント判別
'(先頭開始、0個以上の空白、1個以上の「--」、0個以上の任意文字)
Dim strPattern2 As String = "^\s*(--)+.*"
'※文字列判別
'(「'」開始、0個以上の任意文字、最も手前の「'」終了)
Dim strPattern3 As String = "'.*?'"
'※予約語判別
'(単語単位で予約語の何れかと同じ)
Dim wkStr As String = String.Join("|", Me.GetReservedWords())
Dim strPattern4 As String = "\b(" & wkStr & ")\b"
strPatternAll = _
strPattern1 & "|" & strPattern2 & "|" & _
strPattern3 & "|" & strPattern4
Me.m_RegEx = New Regex(strPatternAll, RegexOptions.IgnoreCase)
Return Me.m_RegEx
End Function
#End Region
#Region "正規表現の検索結果を編集する。"
''' <summary>
''' 正規表現の検索結果を編集する。
''' </summary>
''' <param name="match">検索結果</param>
''' <returns>編集結果</returns>
''' <remarks></remarks>
Protected Overrides Function MatchEvaluator(ByVal match As Match) As String
If match.Value.Replace(" ", "").StartsWith("--") OrElse _
match.Value.Trim().StartsWith("--") Then
'コメントの場合
Dim wk1 As Integer = match.Value.IndexOf("--")
If wk1 = 0 Then
Return String.Format( _
"<font color='{0}'>{1}</font>", _
FORE_COLOR_COMMENT, _
match.Value.Substring(wk1))
End If
Return match.Value.Substring(0, wk1) & _
String.Format( _
"<font color='{0}'>{1}</font>", _
FORE_COLOR_COMMENT, _
match.Value.Substring(wk1))
End If
If match.Value.StartsWith("'") AndAlso _
match.Value.EndsWith("'") Then
'文字列の場合
Return String.Format( _
"<font color='{0}'>{1}</font>", _
FORE_COLOR_STRING, _
match.Value)
End If
'予約語の場合
Return String.Format( _
"<font color='{0}'>{1}</font>", _
FORE_COLOR_RESERVED_WORD, _
match.Value)
End Function
#End Region
#Region "予約語一覧を取得する。"
''' <summary>
''' 予約語一覧を取得する。
''' </summary>
''' <returns>予約語一覧</returns>
''' <remarks></remarks>
Public Overrides Function GetReservedWords() As String()
If Me.m_ReservedWords.Count > 0 Then
Return Me.m_ReservedWords.ToArray()
End If
With Me.m_ReservedWords
.AddRange(New String() {"ADD", "ALL", "ALTER", "AND", "ANY"})
.AddRange(New String() {"AS", "ASC", "AUTHORIZATION", "BACKUP", "BEGIN"})
.AddRange(New String() {"BETWEEN", "BREAK", "BROWSE", "BULK", "BY"})
.AddRange(New String() {"CASCADE", "CASE", "CHECK", "CHECKPOINT", "CLOSE"})
.AddRange(New String() {"CLUSTERED", "COALESCE", "COLLATE", "COLUMN", "COMMIT"})
.AddRange(New String() {"COMPUTE", "CONSTRAINT", "CONTAINS", "CONTAINSTABLE", "CONTINUE"})
.AddRange(New String() {"CONVERT", "CREATE", "CROSS", "CURRENT", "CURRENT_DATE"})
.AddRange(New String() {"CURRENT_TIME", "CURRENT_TIMESTAMP", "CURRENT_USER", "CURSOR", "DATABASE"})
.AddRange(New String() {"DBCC", "DEALLOCATE", "DECLARE", "DEFAULT", "DELETE"})
.AddRange(New String() {"DENY", "DESC", "DISK", "DISTINCT", "DISTRIBUTED"})
.AddRange(New String() {"DOUBLE", "DROP", "DUMP", "ELSE", "END"})
.AddRange(New String() {"ERRLVL", "ESCAPE", "EXCEPT", "EXEC", "EXECUTE"})
.AddRange(New String() {"EXISTS", "EXIT", "EXTERNAL", "FETCH", "FILE"})
.AddRange(New String() {"FILLFACTOR", "FOR", "FOREIGN", "FREETEXT", "FREETEXTTABLE"})
.AddRange(New String() {"FROM", "FULL", "FUNCTION", "GOTO", "GRANT"})
.AddRange(New String() {"GROUP", "HAVING", "HOLDLOCK", "IDENTITY", "IDENTITY_INSERT"})
.AddRange(New String() {"IDENTITYCOL", "IF", "IN", "INDEX", "INNER"})
.AddRange(New String() {"INSERT", "INTERSECT", "INTO", "IS", "JOIN"})
.AddRange(New String() {"KEY", "KILL", "LEFT", "LIKE", "LINENO"})
.AddRange(New String() {"LOAD", "MERGE", "NATIONAL", "NOCHECK", "NONCLUSTERED"})
.AddRange(New String() {"NOT", "NULL", "NULLIF", "OF", "OFF"})
.AddRange(New String() {"OFFSETS", "ON", "OPEN", "OPENDATASOURCE", "OPENQUERY"})
.AddRange(New String() {"OPENROWSET", "OPENXML", "OPTION", "OR", "ORDER"})
.AddRange(New String() {"OUTER", "OVER", "PERCENT", "PIVOT", "PLAN"})
.AddRange(New String() {"PRECISION", "PRIMARY", "PRINT", "PROC", "PROCEDURE"})
.AddRange(New String() {"PUBLIC", "RAISERROR", "READ", "READTEXT", "RECONFIGURE"})
.AddRange(New String() {"REFERENCES", "REPLICATION", "RESTORE", "RESTRICT", "RETURN"})
.AddRange(New String() {"REVERT", "REVOKE", "RIGHT", "ROLLBACK", "ROWCOUNT"})
.AddRange(New String() {"ROWGUIDCOL", "RULE", "SAVE", "SCHEMA", "SECURITYAUDIT"})
.AddRange(New String() {"SELECT", "SEMANTICKEYPHRASETABLE", "SEMANTICSIMILARITYDETAILSTABLE"})
.AddRange(New String() {"SEMANTICSIMILARITYTABLE", "SESSION_USER"})
.AddRange(New String() {"SET", "SETUSER", "SHUTDOWN", "SOME"})
.AddRange(New String() {"STATISTICS", "SYSTEM_USER", "TABLE"})
.AddRange(New String() {"TABLESAMPLE", "TEXTSIZE", "THEN"})
.AddRange(New String() {"TO", "TOP", "TRAN"})
.AddRange(New String() {"TRANSACTION", "TRIGGER", "TRUNCATE"})
.AddRange(New String() {"TRY_CONVERT", "TSEQUAL", "UNION"})
.AddRange(New String() {"UNIQUE", "UNPIVOT", "UPDATE"})
.AddRange(New String() {"UPDATETEXT", "USE", "USER"})
.AddRange(New String() {"VALUES", "VARYING", "VIEW"})
.AddRange(New String() {"WAITFOR", "WHEN", "WHERE"})
.AddRange(New String() {"WHILE", "WITH", "WITHINGROUP", "WRITETEXT"})
End With
Return Me.m_ReservedWords.ToArray()
End Function
#End Region
#Region "タイトルを取得"
''' <summary>
''' タイトルを取得
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Overrides Function GetDefaultTitle() As String
Return "SQLServer"
End Function
#End Region
#Region "背景色(タイトル)を取得"
''' <summary>
''' 背景色(タイトル)を取得
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Overrides Function GetTitleBackColor() As String
Return BACK_COLOR_TITLE
End Function
#End Region
#Region "背景色を取得"
''' <summary>
''' 背景色を取得
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Overrides Function GetBackColor() As String
Return BACK_COLOR
End Function
#End Region
#Region "前景色を取得"
''' <summary>
''' 前景色を取得
''' </summary>
''' <returns>前景色</returns>
''' <remarks></remarks>
Public Overrides Function GetForeColor() As String
Return FORE_COLOR
End Function
#End Region
End Class | |
0 件のコメント:
コメントを投稿