-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
FatturaBase.cs
129 lines (114 loc) · 4.26 KB
/
FatturaBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;
using System.IO;
using System.Security.Cryptography;
using System.Xml;
using FatturaElettronica.Defaults;
using FatturaElettronica.Extensions;
using FatturaElettronica.Ordinaria;
using BaseClassSerializable = FatturaElettronica.Core.BaseClassSerializable;
namespace FatturaElettronica
{
public abstract class FatturaBase : BaseClassSerializable
{
public override void ReadXml(XmlReader r)
{
r.MoveToContent();
base.ReadXml(r);
}
public override void WriteXml(XmlWriter w)
{
w.WriteStartElement(RootElement.Prefix, GetLocalName(), GetNameSpace());
w.WriteAttributeString("versione", GetFormatoTrasmissione());
if (!string.IsNullOrEmpty(SistemaEmittente))
{
w.WriteAttributeString("SistemaEmittente", SistemaEmittente);
}
foreach (var a in RootElement.ExtraAttributes)
{
w.WriteAttributeString(a.Prefix, a.LocalName, a.ns, a.value);
}
base.WriteXml(w);
w.WriteEndElement();
}
public static FatturaBase CreateInstanceFromXml(Stream stream, bool validateSignature = false)
{
try
{
return CreateInstanceFromPlainXml(stream);
}
catch (XmlException)
{
stream.Position = 0;
return CreateInstanceFromXmlSigned(stream, validateSignature);
}
}
private static FatturaBase CreateInstanceFromPlainXml(Stream stream)
{
FatturaBase ret;
using (var r = XmlReader.Create(stream,
new()
{
IgnoreWhitespace = true, IgnoreComments = true, IgnoreProcessingInstructions = true
}))
{
while (r.Read() && !r.LocalName.Contains("Fattura"))
{
}
var att = r.GetAttribute("versione");
if (att == FormatoTrasmissione.Semplificata)
ret = Semplificata.FatturaSemplificata.CreateInstance(Instance.Semplificata);
else
ret = FatturaOrdinaria.CreateInstance(
att == FormatoTrasmissione.PubblicaAmministrazione
? Instance.PubblicaAmministrazione
: Instance.Privati);
stream.Position = 0;
}
using (var r = XmlReader.Create(stream,
new()
{
IgnoreWhitespace = true, IgnoreComments = true, IgnoreProcessingInstructions = true
}))
{
ret.ReadXml(r);
}
return ret;
}
private static FatturaBase CreateInstanceFromXmlSigned(Stream stream,
bool validateSignature = true)
{
try
{
using var parsed = SignedFileExtensions.ParseSignature(stream, validateSignature);
var newStream = new MemoryStream();
parsed.WriteTo(newStream);
newStream.Position = 0;
return CreateInstanceFromXml(newStream);
}
catch (CryptographicException)
{
stream.Position = 0;
return CreateInstanceFromXmlSignedBase64(stream, validateSignature);
}
}
private static FatturaBase CreateInstanceFromXmlSignedBase64(Stream stream,
bool validateSignature = true)
{
byte[] converted;
using (var reader = new StreamReader(stream))
{
converted = Convert.FromBase64String(reader.ReadToEnd());
}
return CreateInstanceFromXmlSigned(new MemoryStream(converted), validateSignature);
}
protected override void ReadAndHandleXmlStartElement(XmlReader r)
{
SistemaEmittente = r.GetAttribute("SistemaEmittente");
base.ReadAndHandleXmlStartElement(r);
}
public string SistemaEmittente { get; set; }
public abstract string GetFormatoTrasmissione();
protected abstract string GetLocalName();
protected abstract string GetNameSpace();
}
}