Jump to content

Wikipedia:CSVLoader/Source

From Wikipedia, the free encyclopedia

This page is not frequently updated. Full source code available at Google Code.

'CSV Loader
'Ganesh Krishnamurthy, May 2008
'
'This program is free software; you can redistribute it and/or modify
'it under the terms of the GNU General Public License as published by
'the Free Software Foundation; either version 2 of the License, or
'(at your option) any later version.

'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details.

'You should have received a copy of the GNU General Public License
'along with this program; if not, write to the Free Software
'Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

Public Class CSVLoader

    Implements WikiFunctions.Plugin.IAWBPlugin

    ' AWB objects:
    Private Shared AWBForm As WikiFunctions.Plugin.IAutoWikiBrowser
    Private Shared AWBList As WikiFunctions.Controls.Lists.ListMaker

    ' Menu item:
    Private Const conOurName As String = "CSV Loader"
    Private Const conOurWikiName As String = conOurName & " Plugin"
    Private WithEvents OurMenuItem As New ToolStripMenuItem(conOurWikiName)

    'Settings Class and Settings Form
    Friend Shared Settings As New CSVSettings()
    Private SettingsForm As New SettingsForm()

    ' User input and state:
    Private HeaderArray() As String
    Private InputArray(0, 0) As String
    Private TotalRows As Long
    Private LastItem As Long

    Public Sub Initialise(ByVal MainForm As WikiFunctions.Plugin.IAutoWikiBrowser) _
    Implements WikiFunctions.Plugin.IAWBPlugin.Initialise
        ' Add our menu item:
        With OurMenuItem
            .CheckOnClick = True
            .ToolTipText = "Enable the " & conOurWikiName
        End With

        MainForm.PluginsToolStripMenuItem.DropDownItems.Add(OurMenuItem)
        AWBForm = MainForm
        AWBList = AWBForm.ListMaker
        LastItem = 0
    End Sub

    Public Sub LoadSettings(ByVal Prefs() As Object) Implements WikiFunctions.Plugin.IAWBPlugin.LoadSettings
        Dim P As PrefsKeyPair
        For Each O As Object In Prefs
            P = O
            Select Case P.Name.ToLower().Trim()
                Case "textmode"
                    Settings.TextMode = P.Setting.ToString()
                Case "inputtext"
                    Settings.InputText = P.Setting.ToString()
                Case "columnheaders"
                    Settings.ColumnHeaders = P.Setting.ToString()
                Case "skip"
                    Settings.Skip = P.Setting
                Case "separator"
                    Settings.Separator = P.Setting.ToString()
            End Select
        Next
    End Sub

    Public ReadOnly Property Name() As String Implements WikiFunctions.Plugin.IAWBPlugin.Name
        Get
            Return conOurName
        End Get
    End Property

    Public ReadOnly Property WikiName() As String Implements WikiFunctions.Plugin.IAWBPlugin.WikiName
        Get
            Return conOurWikiName
        End Get
    End Property

    Public Sub Nudge(ByRef Cancel As Boolean) Implements WikiFunctions.Plugin.IAWBPlugin.Nudge
        Cancel = False
    End Sub

    Public Sub Nudged(ByVal Nudges As Integer) Implements WikiFunctions.Plugin.IAWBPlugin.Nudged

    End Sub

    Public Function ProcessArticle(ByVal sender As WikiFunctions.Plugin.IAutoWikiBrowser, _
    ByVal eventargs As WikiFunctions.Plugin.ProcessArticleEventArgs) As String _
    Implements WikiFunctions.Plugin.IAWBPlugin.ProcessArticle
        Dim ArticleText As String = "", ArticleTitle As String = ""
        Dim ArticleIndex As Long = -1, nCtr As Long

        'If menu item is not checked, then return
        If (Not PluginEnabled) Then
            ProcessArticle = eventargs.ArticleText
            Exit Function
        End If

        ArticleTitle = eventargs.ArticleTitle
        Select Case Settings.TextMode
            Case "Append"
                ArticleText = eventargs.ArticleText + Settings.InputText
            Case "Prepend"
                ArticleText = Settings.InputText + eventargs.ArticleText
            Case "Replace"
                ArticleText = Settings.InputText
        End Select

        For nCtr = LastItem To TotalRows - 1
            If InputArray(nCtr, 0) = ArticleTitle Then
                LastItem = nCtr
                ArticleIndex = nCtr
                Exit For
            End If
        Next

        If ArticleIndex >= 0 Then
            For nCtr = 0 To HeaderArray.Length - 1
                ArticleText = Replace(ArticleText, HeaderArray(nCtr), InputArray(ArticleIndex, nCtr))
            Next
            ProcessArticle = ArticleText
            eventargs.Skip = (eventargs.ArticleText = ArticleText) And Settings.Skip
        Else
            ProcessArticle = eventargs.ArticleText
        End If
    End Function

    Public Sub Reset() Implements WikiFunctions.Plugin.IAWBPlugin.Reset
        SettingsForm = New SettingsForm()
        Settings = New CSVSettings()
    End Sub

    Public Function SaveSettings() As Object() Implements WikiFunctions.Plugin.IAWBPlugin.SaveSettings
        Dim Prefs(4) As Object
        Prefs(0) = New PrefsKeyPair("TextMode", Settings.TextMode)
        Prefs(1) = New PrefsKeyPair("InputText", Settings.InputText)
        Prefs(2) = New PrefsKeyPair("ColumnHeaders", Settings.ColumnHeaders)
        Prefs(3) = New PrefsKeyPair("Skip", Settings.Skip)
        Prefs(4) = New PrefsKeyPair("Separator", Settings.Separator)
        Return Prefs
    End Function

    ' Event handlers:
    Private Sub OurMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles OurMenuItem.Click
        Dim InputString() As String
        Dim nCtr As Long, mCtr As Long
        Dim nRet As Long

        If PluginEnabled Then
            TotalRows = AWBList.Count
            If TotalRows > 0 Then
                SettingsForm = New SettingsForm()
                nRet = SettingsForm.ShowDialog()
                If Settings.ColumnHeaders.Length > 0 Then
                    HeaderArray = Split(Settings.ColumnHeaders, Settings.Separator)
                    ReDim InputArray(TotalRows - 1, HeaderArray.Length - 1)
                    nCtr = 0
                    Do While nCtr < TotalRows
                        InputString = Split(AWBList(nCtr).Name, Settings.Separator)
                        For mCtr = 0 To InputString.Length - 1
                            InputArray(nCtr, mCtr) = InputString(mCtr)
                        Next
                        AWBList.RemoveAt(nCtr)
                        AWBList.Insert(nCtr, InputString(0))
                        nCtr = nCtr + 1
                    Loop
                Else
                    MsgBox("Column Headers are required.")
                    PluginEnabled = False
                End If
            Else
                MsgBox("No articles selected.")
                PluginEnabled = False
            End If
        End If
    End Sub

    Property PluginEnabled() As Boolean
        Get
            Return OurMenuItem.Checked
        End Get
        Set(ByVal Value As Boolean)
            OurMenuItem.Checked = Value
        End Set
    End Property

    <Serializable()> Friend Class CSVSettings
        Public TextMode As String = "Append"
        Public InputText As String = ""
        Public ColumnHeaders As String = ""
        Public Skip As Boolean = True
        Public Separator As String = ","
    End Class
End Class