summaryrefslogtreecommitdiff
path: root/src/formats/osxml/OsxmlParser.cpp
blob: 10cc77af4e31ab14d7eb8c3fc7ce4b34323c3900 (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
/*
    Ousía
    Copyright (C) 2014, 2015  Benjamin Paaßen, Andreas Stöckel

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <core/common/Variant.hpp>
#include <core/common/CharReader.hpp>
#include <core/parser/stack/Callbacks.hpp>
#include <core/parser/stack/GenericParserStates.hpp>
#include <core/parser/stack/Stack.hpp>
#include <core/parser/ParserContext.hpp>

#include "OsxmlEventParser.hpp"
#include "OsxmlParser.hpp"

namespace ousia {

using namespace parser_stack;

/**
 * Class containing the actual OsxmlParser implementation.
 */
class OsxmlParserImplementation : public OsxmlEvents, ParserCallbacks {
private:
	/**
	 * Actual xml parser -- converts the xml stream into a set of events.
	 */
	OsxmlEventParser parser;

	/**
	 * Pushdown automaton responsible for converting the xml events into an
	 * actual Node tree.
	 */
	Stack stack;

public:
	/**
	 * Constructor of the OsxmlParserImplementation class.
	 *
	 * @param reader is a reference to the CharReader instance from which the
	 * XML should be read.
	 * @param ctx is a reference to the ParserContext instance that should be
	 * used.
	 */
	OsxmlParserImplementation(CharReader &reader, ParserContext &ctx)
	    : parser(reader, *this, ctx.getLogger()),
	      stack(*this, ctx, GenericParserStates)
	{
	}

	/**
	 * Starts the actual parsing process.
	 */
	void parse() { parser.parse(); }

	void commandStart(const Variant &name,
	                  const Variant::mapType &args) override
	{
		stack.commandStart(name, args, true);
	}

	void annotationStart(const Variant &name,
	                     const Variant::mapType &args) override
	{
		stack.annotationStart(name, args, true);
	}

	void annotationEnd(const Variant &className,
	                   const Variant &elementName) override
	{
		stack.annotationEnd(className, elementName);
	}

	void rangeEnd() override { stack.rangeEnd(); }

	void data(const TokenizedData &data) override { stack.data(data); }

	TokenId registerToken(const std::string &token) override
	{
		return Tokens::Empty;
	}

	void unregisterToken(TokenId id) override
	{
		// Do nothing here
	}
};

/* Class OsxmlParser */

void OsxmlParser::doParse(CharReader &reader, ParserContext &ctx)
{
	OsxmlParserImplementation impl(reader, ctx);
	impl.parse();
}
}