Imports System.Xml

Public Class ArinSetting

    Private File As String
    Private vDocu As XmlDocument = Nothing
    Private nsmgr As XmlNamespaceManager
    Public Root As XmlElement

    Public ReadOnly Property Docu() As XmlDocument
        Get
            Return Me.vDocu
        End Get
    End Property

    Public Sub New(ByVal Filename As String)
        Me.File = Filename
        If Exist() Then '//파일이 존재하면 도큐먼트반환
            Me.vDocu = New XmlDocument
            nsmgr = New XmlNamespaceManager(New Xml.NameTable)
            nsmgr.AddNamespace("x", "http://tindevil.com")
            Try
                Me.vDocu.Load(Filename)
                Root = vDocu.DocumentElement
            Catch ex As Exception
                Me.vDocu = Nothing
                Me.Root = Nothing
            End Try
            Autoflush = False
        Else
            'Dim m As String = "지정된 설정파일(" + Filename + ") 이 없으므로 새로 생성합니다"
            'MsgBox(m, MsgBoxStyle.Information, "확인")
            CreateFile()
        End If
    End Sub

    ''' <summary>
    ''' '파일명이 없는경우 현재 실행폴더에 Setting.xml 이 작성됩니다"
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New()
        Me.New(My.Application.Info.DirectoryPath & "\Setting.xml")
    End Sub

    Public Sub New(ByVal docu As Xml.XmlDocument)
        Me.vDocu = docu
        Me.File = My.Application.Info.DirectoryPath & "\temp.xml"
        nsmgr = New XmlNamespaceManager(New Xml.NameTable)
        nsmgr.AddNamespace("x", "http://tindevil.com")
        Root = vDocu.DocumentElement
        Autoflush = False
    End Sub

    ''' <summary>
    ''' 파일존재여부
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Exist() As Boolean
        If Not vDocu Is Nothing Then Return True
        Return System.IO.File.Exists(Me.File)
    End Function

    Public Sub CreateFile()
        '//주어진파일명으로 기본파일을 생성한다.
        Dim NewXml As New System.Text.StringBuilder
        NewXml.AppendLine("<?xml version='1.0' encoding='KSC5601'?>")
        NewXml.AppendLine("<tindevil xmlns='http://tindevil.com'> ")
        NewXml.AppendLine("</tindevil>")
        If System.IO.File.Exists(Me.File) Then System.IO.File.Delete(Me.File)
        My.Computer.FileSystem.WriteAllText(Me.File, NewXml.ToString.Replace("'", Chr(&H22)), False)
        Me.vDocu = New XmlDocument
        Me.vDocu.Load(Me.File)
        nsmgr = New XmlNamespaceManager(New Xml.NameTable)
        nsmgr.AddNamespace("x", "http://tindevil.com")
        Root = vDocu.DocumentElement
    End Sub

    Public Function Read(ByVal appkey As String, ByVal subkey As String, Optional ByVal defaltvalue As String = "", Optional ByVal Nullvalue As String = "") As String '//변수초기화
        '//파일이없을경우 빈값을 반환합니다.
        If Exist() = False Then Return ""
        Dim L As XmlElement = Me.Root.SelectSingleNode(appkey, nsmgr) '//appkey를 먼저 조회한다.
        If L Is Nothing Then Return ""
        Dim C As XmlElement = L.SelectSingleNode(subkey, nsmgr)
        If C Is Nothing Then Return ""
        Return C.InnerText
    End Function

    Public Function Get_NameSpace() As XmlNamespaceManager
        Return Me.nsmgr
    End Function
    Public Function NS() As XmlNamespaceManager
        Return Me.nsmgr
    End Function

    Public Function CreateElement(ByVal name As String) As XmlElement
        Return Me.Docu.CreateElement(name, Me.NS.DefaultNamespace)
    End Function
    Public Function GetNode(ByVal appkey As String) As XmlElement
        '//파일이없을경우 빈값을 반환합니다.
        If Docu Is Nothing Then Return Nothing
        Return Me.Root.SelectSingleNode(appkey, nsmgr) '//appkey를 먼저 조회한다.
    End Function
    Public Function GetNodes(ByVal appkey As String) As Xml.XmlNodeList
        '//파일이없을경우 빈값을 반환합니다.
        If Docu Is Nothing Then Return Nothing
        Dim L As XmlNodeList = Me.Root.SelectNodes(appkey, nsmgr) '//appkey를 먼저 조회한다.
        Return L
    End Function

    Private _autoflush As Boolean = True
    Property Autoflush As Boolean
        Get
            Return _autoflush
        End Get
        Set(value As Boolean)
            _autoflush = value
        End Set
    End Property

    Public WriteOnly Property [Set](subkey As String) As String
        Set(value As String)
            Data(subkey) = value
        End Set
    End Property


    Public Property Data(ByVal appkey As String, ByVal subkey As String, Optional ByVal defaltvalue As Object = "") As String
        Get
            '//파일이없을경우 빈값을 반환합니다.
            If Exist() = False Then Return defaltvalue
            Dim L As XmlElement = Me.Root.SelectSingleNode(appkey, nsmgr) '//appkey를 먼저 조회한다.
            If L Is Nothing Then Return defaltvalue
            Dim C As XmlElement = L.SelectSingleNode(subkey, nsmgr)
            If C Is Nothing Then Return defaltvalue
            Return C.InnerText
        End Get
        Set(ByVal value As String)
            '//파일이없을경우 빈값을 반환합니다.
            If Exist() = False Then Return
            Dim L As XmlElement = Me.Root.SelectSingleNode(appkey, nsmgr) '//appkey를 먼저 조회한다.
            If L Is Nothing Then '//만들어야한다.
                L = Me.Docu.CreateElement(appkey)
                Me.Root.AppendChild(L)
            End If
            Dim C As XmlElement = L.SelectSingleNode(subkey, nsmgr)
            If C Is Nothing Then '//만들어야한다.
                C = Me.Docu.CreateElement(subkey)
                C.InnerText = value
                L.AppendChild(C)
            Else
                C.InnerText = value
            End If
            If Autoflush Then Me.Docu.Save(Me.File)
            'MsgBox(value)
        End Set
    End Property

    Public Property Data(ByVal subkey As String) As String
        Get
            Return Data("userdata", subkey)
        End Get
        Set(ByVal value As String)
            Data("userdata", subkey) = value
        End Set
    End Property

    Public Sub Save()
        Me.Docu.Save(Me.File)
    End Sub


End Class


