2
* Copyright (C) 2008 Geometer Plus <contact@geometerplus.com>
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22
#include <ZLXMLReader.h>
24
#include <ZLStringUtil.h>
26
#include "FB2TagManager.h"
28
class FB2TagInfoReader : public ZLXMLReader {
31
FB2TagInfoReader(std::map<std::string,std::vector<std::string> > &tagMap);
33
void startElementHandler(const char *tag, const char **attributes);
34
void endElementHandler(const char *tag);
37
std::map<std::string,std::vector<std::string> > &myTagMap;
39
std::string myCategoryName;
40
std::string mySubCategoryName;
41
std::vector<std::string> myGenreIds;
42
std::string myLanguage;
45
FB2TagInfoReader::FB2TagInfoReader(std::map<std::string,std::vector<std::string> > &tagMap) : myTagMap(tagMap) {
46
myLanguage = ZLibrary::Language();
47
if (myLanguage != "ru") {
52
static const std::string CATEGORY_NAME_TAG = "root-descr";
53
static const std::string SUBCATEGORY_NAME_TAG = "genre-descr";
54
static const std::string GENRE_TAG = "genre";
55
static const std::string SUBGENRE_TAG = "subgenre";
56
static const std::string SUBGENRE_ALT_TAG = "genre-alt";
58
void FB2TagInfoReader::startElementHandler(const char *tag, const char **attributes) {
59
if ((SUBGENRE_TAG == tag) || (SUBGENRE_ALT_TAG == tag)) {
60
const char *id = attributeValue(attributes, "value");
62
myGenreIds.push_back(id);
64
} else if (CATEGORY_NAME_TAG == tag) {
65
const char *lang = attributeValue(attributes, "lang");
66
if ((lang != 0) && (myLanguage == lang)) {
67
const char *name = attributeValue(attributes, "genre-title");
69
myCategoryName = name;
70
ZLStringUtil::stripWhiteSpaces(myCategoryName);
73
} else if (SUBCATEGORY_NAME_TAG == tag) {
74
const char *lang = attributeValue(attributes, "lang");
75
if ((lang != 0) && (myLanguage == lang)) {
76
const char *name = attributeValue(attributes, "title");
78
mySubCategoryName = name;
79
ZLStringUtil::stripWhiteSpaces(mySubCategoryName);
85
void FB2TagInfoReader::endElementHandler(const char *tag) {
86
if (GENRE_TAG == tag) {
87
myCategoryName.erase();
88
mySubCategoryName.erase();
90
} else if (SUBGENRE_TAG == tag) {
91
if (!myCategoryName.empty() && !mySubCategoryName.empty()) {
92
const std::string fullTagName = myCategoryName + '/' + mySubCategoryName;
93
for (std::vector<std::string>::const_iterator it = myGenreIds.begin(); it != myGenreIds.end(); ++it) {
94
myTagMap[*it].push_back(fullTagName);
97
mySubCategoryName.erase();
102
FB2TagManager *FB2TagManager::ourInstance = 0;
104
const FB2TagManager &FB2TagManager::instance() {
105
if (ourInstance == 0) {
106
ourInstance = new FB2TagManager();
111
FB2TagManager::FB2TagManager() {
112
FB2TagInfoReader(myTagMap).readDocument(
113
ZLibrary::ApplicationDirectory() + ZLibrary::FileNameDelimiter +
114
"formats" + ZLibrary::FileNameDelimiter + "fb2" +
115
ZLibrary::FileNameDelimiter + "fb2genres.xml"
119
const std::vector<std::string> &FB2TagManager::humanReadableTags(const std::string &id) const {
120
static const std::vector<std::string> EMPTY;
121
std::map<std::string,std::vector<std::string> >::const_iterator it = myTagMap.find(id);
122
return (it != myTagMap.end()) ? it->second : EMPTY;