summaryrefslogtreecommitdiff
path: root/src/formats/osxml/OsxmlAttributeLocator.cpp
blob: e37446ac7de7007f9332f358c3bd272d8af9cf81 (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
/*
    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/Location.hpp>
#include <core/common/CharReader.hpp>
#include <core/common/Utils.hpp>

#include "OsxmlAttributeLocator.hpp"

namespace ousia {

/**
 * Enum used internally in the statemachine of the xml argument parser.
 */
enum class XmlAttributeState {
	IN_TAG_NAME,
	SEARCH_ATTR,
	IN_ATTR_NAME,
	HAS_ATTR_NAME,
	HAS_ATTR_EQUALS,
	IN_ATTR_DATA
};

std::map<std::string, SourceLocation> OsxmlAttributeLocator::locate(
    CharReader &reader, size_t offs)
{
	std::map<std::string, SourceLocation> res;

	// Fork the reader, we don't want to mess up the XML parsing process, do we?
	CharReaderFork readerFork = reader.fork();

	// Move the read cursor to the start location, abort if this does not work
	if (offs != readerFork.seek(offs)) {
		return res;
	}

	// Now all we need to do is to implement one half of an XML parser. As this
	// is inherently complicated we'll totaly fail at it. Don't care. All we
	// want to get is those darn offsets for pretty error messages... (and we
	// can assume the XML is valid as it was already read by expat)
	XmlAttributeState state = XmlAttributeState::IN_TAG_NAME;
	char c;
	std::stringstream attrName;
	while (readerFork.read(c)) {
		// Abort at the end of the tag
		if (c == '>' && state != XmlAttributeState::IN_ATTR_DATA) {
			return res;
		}

		// One state machine to rule them all, one state machine to find them,
		// One state machine to bring them all and in the darkness bind them
		// (the byte offsets)
		switch (state) {
			case XmlAttributeState::IN_TAG_NAME:
				if (Utils::isWhitespace(c)) {
					res.emplace("$tag",
					            SourceLocation{reader.getSourceId(), offs + 1,
					                           readerFork.getOffset() - 1});
					state = XmlAttributeState::SEARCH_ATTR;
				}
				break;
			case XmlAttributeState::SEARCH_ATTR:
				if (!Utils::isWhitespace(c)) {
					state = XmlAttributeState::IN_ATTR_NAME;
					attrName << c;
				}
				break;
			case XmlAttributeState::IN_ATTR_NAME:
				if (Utils::isWhitespace(c)) {
					state = XmlAttributeState::HAS_ATTR_NAME;
				} else if (c == '=') {
					state = XmlAttributeState::HAS_ATTR_EQUALS;
				} else {
					attrName << c;
				}
				break;
			case XmlAttributeState::HAS_ATTR_NAME:
				if (!Utils::isWhitespace(c)) {
					if (c == '=') {
						state = XmlAttributeState::HAS_ATTR_EQUALS;
						break;
					}
					// Well, this is a strange XML file... We expected to
					// see a '=' here! Try to continue with the
					// "HAS_ATTR_EQUALS" state as this state will hopefully
					// inlcude some error recovery
				} else {
					// Skip whitespace here
					break;
				}
			// Fallthrough
			case XmlAttributeState::HAS_ATTR_EQUALS:
				if (!Utils::isWhitespace(c)) {
					if (c == '"') {
						// Here we are! We have found the beginning of an
						// attribute. Let's quickly lock the current offset away
						// in the result map
						res.emplace(attrName.str(),
						            SourceLocation{reader.getSourceId(),
						                           readerFork.getOffset()});
						state = XmlAttributeState::IN_ATTR_DATA;
					} else {
						// No, this XML file is not well formed. Assume we're in
						// an attribute name once again
						attrName.str(std::string{&c, 1});
						state = XmlAttributeState::IN_ATTR_NAME;
					}
				}
				break;
			case XmlAttributeState::IN_ATTR_DATA:
				if (c == '"') {
					// We're at the end of the attribute data, set the end
					// location
					auto it = res.find(attrName.str());
					if (it != res.end()) {
						it->second.setEnd(readerFork.getOffset() - 1);
					}

					// Reset the attribute name and restart the search
					attrName.str(std::string{});
					state = XmlAttributeState::SEARCH_ATTR;
				}
				break;
		}
	}
	return res;
}
}