Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CXsltChecksum.cs « XslTransformApi « Xslt « tests « System.Private.Xml « src - github.com/mono/corefx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 68a1f9fa082279c103d0176dd9b7c459a03939dc (plain)
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Xunit;
using Xunit.Abstractions;
using System;
using System.Globalization;
using System.IO;
using System.Xml;

public class CXsltChecksum
{
    private bool _fTrace;			// Flag for turning on trace in CXsltCache
    private string _strXml;			// XML from the cache

    private ITestOutputHelper _output;

    // --------------------------------------------------------------------------------------------------
    //    Constructor
    // --------------------------------------------------------------------------------------------------
    public CXsltChecksum(bool fTrace, ITestOutputHelper output)
    {
        _fTrace = fTrace;
        _output = output;
    }

    // --------------------------------------------------------------------------------------------------
    //    Properties
    // --------------------------------------------------------------------------------------------------
    public string Xml
    {
        get
        {
            return _strXml;
        }
    }

    public double Calc(XmlReader xr)
    {
        Decimal dResult = 0;	// Numerical value of the checksum
        int i = 0;				// Generic counter
        int iLength = 0;		// Length of the data
        CXmlCache xc;			// Cached output from XslTransform

        _strXml = "";

        // Load the data into the cache
        xc = new CXmlCache();
        xc.ExpandAttributeValues = true;
        xc.Trace = _fTrace;

        xc.Load(xr);
        _strXml = xc.Xml;

        // If there is data to write and omit-xml-declaration is "no", then write the XmlDecl to the stream

        if (_strXml.Length > 0)
            _strXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + _strXml;

        // Calculate the checksum
        iLength = _strXml.Length;

        for (i = 0; i < iLength; i++)
        {
            dResult += Math.Round((Decimal)(_strXml[i] / (i + 1.0)), 10);
            //_output.WriteLine("#{0}({1})", _strXml[i].ToByte(), dResult);
        }
        return Convert.ToDouble(dResult, NumberFormatInfo.InvariantInfo);
    }

    public double Calc(string strFileName)
    {
        Decimal dResult = 0;		// Numerical value of the checksum
        int i = 0;					// Generic counter
        int cBytesRead = 1;			// # of bytes read at one time
        int cTotalRead = 0;			// Total # of bytes read so far
        Decimal dEndBuffer = 0;		// Buffer to remove from the end (This is necessary because
        // 	notepad adds CR/LF onto the end of every file

        Char[] rgBuffer = new Char[4096];
        _strXml = "";

        try
        {
            using (StreamReader fs = new StreamReader(new FileStream(strFileName, FileMode.Open, FileAccess.Read)))
            {
                cBytesRead = fs.Read(rgBuffer, 0, 4096);

                while (cBytesRead > 0)
                {
                    // Keep XML property up to date
                    _strXml = String.Concat(_strXml, new String(rgBuffer, 0, cBytesRead));

                    // Calculate the checksum
                    for (i = 0; i < cBytesRead; i++)
                    {
                        dResult += Math.Round((Decimal)(rgBuffer[i] / (cTotalRead + i + 1.0)), 10);
                        //_output.WriteLine("#{0}({1}) -- {2}", rgBuffer[i], dResult, rgBuffer[i].ToChar());
                    }
                    cTotalRead += cBytesRead;
                    dEndBuffer = 0;

                    // Keep reading (in case file is bigger than 4K)
                    cBytesRead = fs.Read(rgBuffer, 0, 4096);
                }
            }
        }
        catch (Exception ex)
        {
            _output.WriteLine(ex.Message);
            return 0;
        }
        return Convert.ToDouble(dResult - dEndBuffer, NumberFormatInfo.InvariantInfo);
    }
}