summaryrefslogtreecommitdiff
path: root/lib/xml_base.cpp
blob: e26f73c25d380db72067140dce6d544e056e3f61 (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
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html       *
// * Copyright (C) Zenju (zenju AT gmx DOT de) - All Rights Reserved        *
// **************************************************************************

#include "xml_base.h"
#include <zen/file_handling.h>
#include <zen/file_io.h>

using namespace zen;


//loadXmlDocument vs loadStream:
//1. better error reporting
//2. quick exit if (potentially large) input file is not an XML
void xmlAccess::loadXmlDocument(const Zstring& filename, XmlDoc& doc) //throw FfsXmlError
{
    std::string stream;
    try
    {
        {
            //quick test whether input is an XML: avoid loading large binary files up front!
            const std::string xmlBegin = "<?xml version=";
            std::vector<char> buffer(xmlBegin.size() + sizeof(zen::BYTE_ORDER_MARK_UTF8));

            FileInput inputFile(filename); //throw FileError
            const size_t bytesRead = inputFile.read(&buffer[0], buffer.size()); //throw FileError

            const std::string fileBegin(&buffer[0], bytesRead);

            if (!startsWith(fileBegin, xmlBegin) &&
                !startsWith(fileBegin, zen::BYTE_ORDER_MARK_UTF8 + xmlBegin)) //respect BOM!
                throw FfsXmlError(replaceCpy(_("File %x does not contain a valid configuration."), L"%x", fmtFileName(filename)));
        }

        const zen::UInt64 fs = zen::getFilesize(filename); //throw FileError
        stream.resize(to<size_t>(fs));

        FileInput inputFile(filename); //throw FileError
        const size_t bytesRead = inputFile.read(&stream[0], stream.size()); //throw FileError
        if (bytesRead < to<size_t>(fs))
            throw FfsXmlError(replaceCpy(_("Cannot read file %x."), L"%x", fmtFileName(filename)));
    }
    catch (const FileError& error)
    {
        throw FfsXmlError(error.toString());
    }

    try
    {
        zen::parse(stream, doc); //throw XmlParsingError
    }
    catch (const XmlParsingError& e)
    {
        throw FfsXmlError(
            replaceCpy(replaceCpy(replaceCpy(_("Error parsing file %x, row %y, column %z."),
                                             L"%x", fmtFileName(filename)),
                                  L"%y", numberTo<std::wstring>(e.row + 1)),
                       L"%z", numberTo<std::wstring>(e.col + 1)));
    }
}


const std::wstring xmlAccess::getErrorMessageFormatted(const XmlIn& in)
{
    std::wstring msg;

    const auto& failedElements = in.getErrorsAs<std::wstring>();
    if (!failedElements.empty())
    {
        msg = _("Cannot read the following XML elements:") + L"\n";
        std::for_each(failedElements.begin(), failedElements.end(),
        [&](const std::wstring& elem) { msg += L"\n" + elem; });
    }

    return msg;
}


void xmlAccess::saveXmlDocument(const zen::XmlDoc& doc, const Zstring& filename) //throw FfsXmlError
{
    std::string stream = serialize(doc); //throw ()

    bool saveNecessary = true;
    try
    {
        if (zen::getFilesize(filename) == stream.size()) //throw FileError
            try
            {
                if (zen::loadStream(filename) == stream) //throw XmlFileError
                    saveNecessary = false;
            }
            catch (const zen::XmlFileError&) {}
    }
    catch (FileError&) {}

    if (saveNecessary)
        try
        {
            FileOutput outputFile(filename, FileOutput::ACC_OVERWRITE); //throw FileError
            outputFile.write(stream.c_str(), stream.length());          //
        }
        catch (const FileError& error) //more detailed error messages than with wxWidgets
        {
            throw FfsXmlError(error.toString());
        }
}
bgstack15