Imports System.IO

Public Class xhtml


    Public Shared Sub CrearXHTML(FicheroPlantilla As Byte(), FicheroDestino As String, Bloques() As BloquesXHTML)
        Dim tr As System.IO.TextReader = New IO.StreamReader(New MemoryStream(FicheroPlantilla))
        CrearXHTML(tr, FicheroDestino, Bloques)
        tr.Close()
    End Sub
    Public Shared Sub CrearXHTML(FicheroPlantilla As String, FicheroDestino As String, Bloques() As BloquesXHTML)
        Dim tr As System.IO.TextReader = System.IO.File.OpenText(FicheroPlantilla)
        CrearXHTML(tr, FicheroDestino, Bloques)
        tr.Close()
    End Sub
    Public Shared Sub CrearXHTML(tr As System.IO.TextReader, FicheroDestino As String, Bloques() As BloquesXHTML)
        Try
            'Dim clsReader As System.IO.TextReader = System.IO.File.OpenText(FicheroPlantilla)
            Dim clsWriter As System.IO.TextWriter = System.IO.File.CreateText(FicheroDestino)
            Dim blqs As New Hashtable

            Dim sLinea As String = tr.ReadLine() & vbCrLf
            ' CABECERA
            Dim sBloqueCabecera As String = ""
            While Not sLinea.Contains("<!--TSL4:#")
                sBloqueCabecera &= sLinea
                sLinea = tr.ReadLine() & vbCrLf
            End While
            clsWriter.Write(sBloqueCabecera)
            Do
                Dim sNombreBloque As String = sLinea.Trim.Substring(10, sLinea.Trim.Length - 14)
                Dim sBloque As String = ""
                Do
                    sBloque &= sLinea
                    sLinea = tr.ReadLine() & vbCrLf
                Loop Until sLinea.Contains("<!--TSL4:FIN-#")
                sBloque &= sLinea
                blqs.Add(sNombreBloque, sBloque)
                sLinea = tr.ReadLine() & vbCrLf
            Loop Until Not sLinea.Contains("<!--TSL4:#")
            Dim sBloquePie As String = sLinea
            sBloquePie &= tr.ReadToEnd
            For i = 0 To Bloques.Count - 1
                Dim BloqueReemplazado As BloquesXHTML = Bloques(i)
                Dim Bloque As String = blqs(BloqueReemplazado.NombreBloque)
                For j = 0 To BloqueReemplazado.Parametros.Length - 1
                    Dim p = BloqueReemplazado.Parametros(j)
                    If p.Codigo = "" Then p.Codigo = "$" & (j + 1).ToString.PadLeft(3, "0")
                    Bloque = Bloque.Replace(p.Codigo, p.Valor)
                Next
                For Np = 1 To 300
                    Bloque = Bloque.Replace("$" & Np.ToString.PadLeft(3, "0"), "    ")
                Next
                clsWriter.Write(Bloque)
            Next
            clsWriter.Write(sBloquePie)

            clsWriter.Close()
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Sub
End Class
Public Class BloquesXHTML
    Public NombreBloque As String
    Public Parametros() As ParametroXHTML
End Class
Public Class ParametroXHTML
    Public Codigo As String
    Public Valor As String
End Class