summaryrefslogtreecommitdiffstats
path: root/fbreader/src/network/litres/LitResAuthorsParser.cpp
blob: 453ffb5723fe9ed98b10e1bbc72fc8a5a15d917d (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * Copyright (C) 2004-2012 Geometer Plus <[email protected]>
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include <cstdlib>

#include <ZLUnicodeUtil.h>

#include "../NetworkLink.h"
#include "LitResGenre.h"
#include "LitResUtil.h"

#include "LitResAuthorsParser.h"

static const std::string TAG_CATALOG = "catalit-persons";
static const std::string TAG_SUBJECT = "subject";
static const std::string TAG_TITLE = "title";
static const std::string TAG_MAIN = "main";
static const std::string TAG_FIRST_NAME = "first-name";
static const std::string TAG_MIDDLE_NAME = "middle-name";
static const std::string TAG_LAST_NAME = "last-name";

std::string LitResAuthorsParser::stringAttributeValue(const char **attributes, const char *name) {
	if (attributes == 0) {
		return std::string();
	}
	const char *value = attributeValue(attributes, name);
	return value != 0 ? value : std::string();
}

LitResAuthorsParser::LitResAuthorsParser(AuthorsList &authors) : myAuthors(authors) {
	myState = START;
}


void LitResAuthorsParser::startElementHandler(const char *tag, const char **attributes) {
	processState(tag, false, attributes);
	myState = getNextState(tag, false);
	myBuffer.clear();
}

void LitResAuthorsParser::endElementHandler(const char *tag) {
	processState(tag, true, 0);
	myState = getNextState(tag, true);
	myBuffer.clear();
}

void LitResAuthorsParser::characterDataHandler(const char *data, std::size_t len) {
	myBuffer.append(data, len);
}

void LitResAuthorsParser::processState(const std::string &tag, bool closed, const char **attributes) {
	switch(myState) {
	case START:
			break;
	case CATALOG:
		if (!closed && TAG_SUBJECT == tag) {
			myAuthorId = stringAttributeValue(attributes, "id");
		}
		break;
	case SUBJECT:
		if (closed && TAG_SUBJECT == tag) {
			LitresAuthorData litresAuthor;
			litresAuthor.Id = myAuthorId;
			litresAuthor.DisplayName = myAuthorDisplayName;
			litresAuthor.FirstName = myAuthorFirstName;
			litresAuthor.MiddleName = myAuthorMiddleName;
			litresAuthor.LastName = myAuthorLastName;
			litresAuthor.SortKey = ZLUnicodeUtil::toLower(myAuthorLastName);

			myAuthors.push_back(litresAuthor);
			myAuthorId.clear();
			myAuthorDisplayName.clear();
			myAuthorFirstName.clear();
			myAuthorMiddleName.clear();
			myAuthorLastName.clear();
		}
		break;
	case TITLE:
		break;
	case MAIN:
		if (closed && TAG_MAIN == tag) {
			ZLUnicodeUtil::utf8Trim(myBuffer);
			myAuthorDisplayName = myBuffer;
		}
		break;
	case FIRST_NAME:
		if (closed && TAG_FIRST_NAME == tag) {
			ZLUnicodeUtil::utf8Trim(myBuffer);
			myAuthorFirstName = myBuffer;
		}
		break;
	case MIDDLE_NAME:
		if (closed && TAG_MIDDLE_NAME == tag) {
			ZLUnicodeUtil::utf8Trim(myBuffer);
			myAuthorMiddleName = myBuffer;
		}
		break;
	case LAST_NAME:
		if (closed && TAG_LAST_NAME == tag) {
			ZLUnicodeUtil::utf8Trim(myBuffer);
			myAuthorLastName = myBuffer;
		}
		break;
	}
}

LitResAuthorsParser::State LitResAuthorsParser::getNextState(const std::string &tag, bool closed) {
	switch(myState) {
	case START:
		if (!closed && TAG_CATALOG == tag) {
			return CATALOG;
		}
		break;
	case CATALOG:
		if (!closed) {
			if (TAG_SUBJECT == tag) {
				return SUBJECT;
			}
		} else {
			if (TAG_CATALOG == tag) {
				return START;
			}
		}
		break;
	case SUBJECT:
		if (!closed) {
			if (TAG_TITLE == tag) {
				return TITLE;
			}
		} else {
			if (TAG_SUBJECT == tag) {
				return CATALOG;
			}
		}
		break;
	case TITLE:
		if (!closed) {
			if (TAG_MAIN == tag) {
				return MAIN;
			}
		} else {
			if (TAG_TITLE == tag) {
				return FIRST_NAME;
			}
		}
		break;
	case MAIN:
		if (closed && TAG_MAIN == tag) {
				return TITLE;
		}
		break;
	case FIRST_NAME:
		if (closed && TAG_FIRST_NAME == tag) {
			return MIDDLE_NAME;
		}
		break;
	case MIDDLE_NAME:
		if (closed && TAG_MIDDLE_NAME == tag) {
			return LAST_NAME;
		}
		break;
	case LAST_NAME:
		if (closed && TAG_LAST_NAME == tag) {
			return SUBJECT;
		}
		break;
	}
	return myState;
}