summaryrefslogtreecommitdiffstats
path: root/src/liborcus/gnumeric_detection_handler.cpp
blob: 1f0606e462240ba917618785c9184c2103bccb92 (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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include "gnumeric_detection_handler.hpp"
#include "gnumeric_namespace_types.hpp"
#include "gnumeric_token_constants.hpp"
#include "xml_context_base.hpp"
#include "detection_result.hpp"

namespace orcus {

namespace {

/**
 * Check the xml structure up to the first <gnm:Sheet> element. If all the
 * structure check passes up to that point, we call it "detected".
 */
class gnumeric_detection_context : public xml_context_base
{
public:
    gnumeric_detection_context(session_context& session_cxt, const tokens& tokens) :
        xml_context_base(session_cxt, tokens) {}

    virtual xml_context_base* create_child_context(xmlns_id_t, xml_token_t) { return nullptr; }
    virtual void characters(std::string_view, bool) {}
    virtual void end_child_context(xmlns_id_t, xml_token_t, xml_context_base*) {}

    virtual void start_element(xmlns_id_t ns, xml_token_t name, const::std::vector<xml_token_attr_t>& /*attrs*/)
    {
        xml_token_pair_t parent = push_stack(ns, name);
        if (ns == NS_gnumeric_gnm)
        {
            switch (name)
            {
                case XML_Workbook:
                    xml_element_expected(parent, XMLNS_UNKNOWN_ID, XML_UNKNOWN_TOKEN);
                break;
                case XML_Version:
                case XML_Attributes:
                case XML_Calculation:
                case XML_SheetNameIndex:
                case XML_Geometry:
                case XML_Sheets:
                    xml_element_expected(parent, NS_gnumeric_gnm, XML_Workbook);
                break;
                case XML_Attribute:
                    xml_element_expected(parent, NS_gnumeric_gnm, XML_Attributes);
                break;
                case XML_SheetName:
                    xml_element_expected(parent, NS_gnumeric_gnm, XML_SheetNameIndex);
                break;
                case XML_Sheet:
                    xml_element_expected(parent, NS_gnumeric_gnm, XML_Sheets);
                    throw detection_result(true);
                break;
                default:
                    ;
            }
        }
    }

    virtual bool end_element(xmlns_id_t ns, xml_token_t name)
    {
        return pop_stack(ns, name);
    }
};

}

gnumeric_detection_handler::gnumeric_detection_handler(session_context& session_cxt, const tokens& t) :
    xml_stream_handler(session_cxt, t, std::make_unique<gnumeric_detection_context>(session_cxt, t)) {}

gnumeric_detection_handler::~gnumeric_detection_handler() {}

void gnumeric_detection_handler::start_document() {}
void gnumeric_detection_handler::end_document() {}

}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */