Imports System.Xml
Imports System.IO

Public Class ParserPatents
    Private _path As String
    Private _patents As New List(Of Patent)

    Public Sub New()

    End Sub
#Region "Parse Results of Search"
    Public Function ReadPatentXMLFile(path As String) As List(Of Patent)
        Using reader As XmlReader = XmlReader.Create(path)
            While reader.Read()
                If reader.IsStartElement() Then
                    If reader.Name = "PatentHitListRecord" Then
                        Dim patent As Patent = CreatePatent(reader.ReadSubtree())
                        _patents.Add(patent)
                    End If
                End If
            End While
        End Using

        Return _patents
    End Function

    Private Function CreatePatent(r As XmlReader)
        Dim patent As New Patent()

        While r.Read()
            If r.IsStartElement() Then
                If r.NodeType = XmlNodeType.Element Then
                    Select Case r.Name
                        Case "leading-registered-number"
                            patent.LeadingRegisteredNumber = r.ReadInnerXml()
                        Case "registered-number"
                            patent.RegisteredNumber = r.ReadInnerXml()
                        Case "type"
                            patent.Type = r.ReadInnerXml()
                        Case "invention-title"
                            patent.Title = r.ReadInnerXml()
                        Case "edition"
                            patent.Edition = r.ReadInnerXml()
                        Case "classification"
                            patent.Classification = r.ReadInnerXml()
                        Case "legalstatus"
                            patent.Status = r.ReadInnerXml()
                        Case "applicationDate"
                            patent.ApplicationDate = r.ReadInnerXml()
                        Case "publicationDate"
                            patent.PublicationDate = r.ReadInnerXml()
                        Case "applicant"
                            patent.ApplicantsList.Add(r.ReadInnerXml())
                        Case "inventor"
                            patent.InventorsList.Add(r.ReadInnerXml())
                    End Select

                End If
            End If
        End While

        Return patent
    End Function

#End Region

#Region "Parse Results of GetRegisterInfo"
    Public Sub ReadPatentRegisterInfoXMLFile(path As String, ByRef p As Patent)
        Using reader As XmlReader = XmlReader.Create(path)
            While reader.Read()
                If reader.IsStartElement() Then
                    If reader.Name = "dpma-patent-document" Then
                        ReadPatentDocument(reader.ReadSubtree(), p)
                    End If
                End If
            End While
        End Using
    End Sub

    Public Sub ReadPatentDocument(r As XmlReader, ByRef p As Patent)
        While r.Read()
            If r.IsStartElement() Then
                If r.NodeType = XmlNodeType.Element Then
                    Select Case r.Name
                        Case "abstract"
                            p.Abstract = r.ReadInnerXml()
                        Case "event-data"
                            ReadEvent(r.ReadSubtree(), p)
                        Case "publication-reference"
                            r.ReadToFollowing("document-id")
                            ReadPublicationDocument(r.ReadSubtree(), p)
                        Case "application-reference"
                            r.ReadToFollowing("document-id")
                            ReadApplicationDocument(r.ReadSubtree(), p)
                    End Select
                End If
            End If
        End While
    End Sub

    Public Sub ReadPublicationDocument(r As XmlReader, ByRef p As Patent)
        Dim pDoc As New PatentDocument

        While r.Read()
            If r.IsStartElement() Then
                If r.NodeType = XmlNodeType.Element Then
                    Select Case r.Name
                        Case "country"
                            pDoc.Country = r.ReadInnerXml()
                        Case "doc-number"
                            pDoc.DocNumber = r.ReadInnerXml()
                        Case "kind"
                            pDoc.Kind = r.ReadInnerXml()
                    End Select
                End If
            End If
        End While

        p.PublicationDocuments.Add(pDoc)
    End Sub

    Public Sub ReadApplicationDocument(r As XmlReader, ByRef p As Patent)
        Dim pDoc As New PatentDocument

        While r.Read()
            If r.IsStartElement() Then
                If r.NodeType = XmlNodeType.Element Then
                    Select Case r.Name
                        Case "country"
                            pDoc.Country = r.ReadInnerXml()
                        Case "doc-number"
                            pDoc.DocNumber = r.ReadInnerXml()
                        Case "date"
                            pDoc.DocDate = r.ReadInnerXml()
                    End Select
                End If
            End If
        End While

        p.ApplicationDocuments.Add(pDoc)
    End Sub

    Public Sub ReadEvent(r As XmlReader, ByRef p As Patent)
        Dim pEvent As New PatentEvent

        While r.Read()
            If r.IsStartElement() Then
                If r.NodeType = XmlNodeType.Element Then
                    Select Case r.Name
                        Case "type-of-procedure"
                            pEvent.Type = r.ReadInnerXml()
                        Case "procedural-status"
                            pEvent.Status = r.ReadInnerXml()
                        Case "date-of-procedural-status"
                            pEvent.StatusDate = r.ReadInnerXml()
                        Case "date-of-capture"
                            pEvent.CaptureDate = r.ReadInnerXml()
                        Case "publication-info"
                            ReadPublicationInfo(r.ReadSubtree(), pEvent)

                    End Select
                End If
            End If
        End While

        p.Events.Add(pEvent)
    End Sub

    Public Sub ReadPublicationInfo(r As XmlReader, ByRef pEvent As PatentEvent)
        Dim pub As New PatentPublication

        While r.Read()
            If r.IsStartElement() Then
                If r.NodeType = XmlNodeType.Element Then
                    Select Case r.Name
                        Case "issue-number"
                            pub.IssueNumber = r.ReadInnerXml()
                        Case "year"
                            pub.Year = r.ReadInnerXml()
                        Case "publication-date"
                            pub.PublicationDate = r.ReadInnerXml()
                        Case "publication-type"
                            pub.PublicationType = r.ReadInnerXml()
                        Case "part"
                            pub.Part = r.ReadInnerXml()
                    End Select
                End If
            End If
        End While

        pEvent.PublicationInfo = pub
    End Sub
#End Region
End Class
