summaryrefslogtreecommitdiff
path: root/algo1w4d1/ValidXML.h
blob: 081013087cc6a110d5b954c37f0140839a23a73d (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
#pragma once

#include "Stack.h"

class ValidXML {
public:
	/**
	 * @brief parse XML byte by byte (todo: utf-8 safe)
	 *
	 * XML is considered 'valid' when no hierarchy errors or invalid XML syntax
	 * is supplied (double tag opening/closing brackets). This means that partial
	 * or unfinished XML is considered valid. To check if XML is finished,
	 * uncomment the stack size check in the ValidXML::input_valid function.
	 *
	 * Each ValidXML instance parses one XML input, and cannot be reused.
	 */
	virtual void parse(char);
	virtual bool input_valid(); /** @brief return if input is valid (true when valid) */

public:
	ValidXML();
	virtual ~ValidXML();

private:
	bool _valid = true; /** @brief if parsed XML is still valid */
	Stack _tag_stack; /** @brief tag stack (used to validate tag hierarchy) */

	std::string _tag_name; /** @brief tag name (without attributes) */
	bool _tag_is_closing; /** @brief current tag is closing tag (starts with "</" instead of "<") */
	bool _parsing_tag; /** @brief parsing text or tag */
	unsigned _tag_counter; /** @brief characters since tag open character */

private:
	void tag_finished(); /** @brief handle finished tag */
};