summaryrefslogtreecommitdiffstats
path: root/scilab/modules/slint/src/cpp/SLintXmlResult.cpp
blob: 2b59ae268eec06651acc51f62127d0049b88403a (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 *  Copyright (C) 2015 - Scilab Enterprises - Calixte DENIZET
 *
 * Copyright (C) 2012 - 2016 - Scilab Enterprises
 *
 * This file is hereby licensed under the terms of the GNU GPL v2.0,
 * pursuant to article 5.3.4 of the CeCILL v.2.1.
 * This file was originally licensed under the terms of the CeCILL v2.1,
 * and continues to be available under such terms.
 * For more information, see the COPYING file which you should have received
 * along with this program.
 *
 */

#include <string>

#include "output/SLintXmlResult.hxx"
#include "checkers/SLintChecker.hxx"
#include "FileException.hxx"
#include "SLint.hxx"
#include "SciFile.hxx"
#include "UTF8.hxx"

extern "C"
{
#include "sci_malloc.h"
#include "charEncoding.h"
#include "localization.h"
}

namespace slint
{

SLintXmlResult::SLintXmlResult(const std::wstring & _path) : current(nullptr), path(_path)
{
    const std::wstring fullpath = SLint::getFullPath(path);
    out = new std::ofstream(scilab::UTF8::toUTF8(fullpath), std::ios::out);
    if (!out->is_open())
    {
        delete out;
        out = nullptr;
        throw FileException(fullpath, L"Can\'t open it.");
    }
    else
    {
        (*out) << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
            << "<SLintResult>\n";
    }
}

SLintXmlResult::~SLintXmlResult()
{
    if (out)
    {
        out->close();
        delete out;
    }
}

void SLintXmlResult::finalize()
{
    if (current.get())
    {
        (*out) << "  </File>\n";
    }

    (*out) << "</SLintResult>\n";
    out->close();
    delete out;
    out = nullptr;
}

const std::string SLintXmlResult::getStr(const std::wstring & str)
{
    return scilab::UTF8::toUTF8(replaceByEntities(str));
}

void SLintXmlResult::handleMessage(SLintContext & context, const Location & loc, const SLintChecker & checker, const unsigned sub, const std::wstring & msg)
{
    if (context.getSciFile().get() != current.get())
    {
        if (current.get())
        {
            (*out) << "  </File>\n";
        }
        current = context.getSciFile();
        print(current);
    }
    print(loc, checker, sub, msg);
}

void SLintXmlResult::print(const SciFilePtr & file)
{
    (*out) << "  <File name=\"" << getStr(file->getFilename()) << "\">\n";
}

void SLintXmlResult::print(const Location & loc, const SLintChecker & checker, const unsigned sub, const std::wstring & msg)
{
    (*out) << "    <Result>\n";
    print(loc);
    print(checker, sub);
    print(msg);
    (*out) << "    </Result>\n";
}

void SLintXmlResult::print(const Location & loc)
{
    (*out) << "      <Location first_line=\"" << loc.first_line
        << "\" first_column=\"" << loc.first_column
        << "\" last_line=\"" << loc.last_line
        << "\" last_column=\"" << loc.last_column
        << "\"/>\n";
}

void SLintXmlResult::print(const SLintChecker & checker, const unsigned sub)
{
    (*out) << "      <Checker name=\"" << checker.getName()
        << "\" id=\"" << getStr(checker.getId(sub))
        << "\"/>\n";
}

void SLintXmlResult::print(const std::wstring & msg)
{
    (*out) << "      <Message text=\"" << getStr(msg)
        << "\"/>\n";
}

std::wstring SLintXmlResult::replaceByEntities(const std::wstring & seq)
{
    std::vector<wchar_t> buf;
    for (auto c : seq)
    {
        if (c == L'<')
        {
            pushEntity(buf, L"&lt;", 4);
        }
        else if (c == L'>')
        {
            pushEntity(buf, L"&gt;", 4);
        }
        else if (c == L'\'')
        {
            pushEntity(buf, L"&apos;", 6);
        }
        else if (c == L'\"')
        {
            pushEntity(buf, L"&quot;", 6);
        }
        else if (c == L'&')
        {
            pushEntity(buf, L"&amp;", 5);
        }
        else
        {
            buf.push_back(c);
        }
    }

    return std::wstring(buf.begin(), buf.end());
}

} // namespace slint