~ubuntu-branches/ubuntu/vivid/fbreader/vivid-proposed

« back to all changes in this revision

Viewing changes to fbreader/src/formats/fb2/FB2TagManager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Eugene V. Lyubimkin
  • Date: 2008-06-17 23:01:53 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20080617230153-a16c45tq69j8geru
Tags: 0.8.17-11
* debian/control:
  - Enhanced all ZLibrary descriptions, not the main one.
  - Done some renaming to use canonical names of libraries and toolkits in
    decriptions, as suggested by developers-reference:
    'qt' -> 'Qt', 'gtk' -> 'GTK+', 'zlibrary' -> 'ZLibrary'.
  - Bump 'Depends' on quilt to (>= 0.24).
* debian/rules:
  - Included quilt makefile instead of quilt makefile instead
    of copy&paste'ing it.
* debian/fbreader.links:
  - Added this file to relay on dh_link's work instead of using 'ln -sf'
    directly in the debian/rules.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2008 Geometer Plus <contact@geometerplus.com>
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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
 
17
 * 02110-1301, USA.
 
18
 */
 
19
 
 
20
#include <vector>
 
21
 
 
22
#include <ZLXMLReader.h>
 
23
#include <ZLibrary.h>
 
24
#include <ZLStringUtil.h>
 
25
 
 
26
#include "FB2TagManager.h"
 
27
 
 
28
class FB2TagInfoReader : public ZLXMLReader {
 
29
 
 
30
public:
 
31
        FB2TagInfoReader(std::map<std::string,std::vector<std::string> > &tagMap);
 
32
 
 
33
        void startElementHandler(const char *tag, const char **attributes);
 
34
        void endElementHandler(const char *tag);
 
35
 
 
36
private:
 
37
        std::map<std::string,std::vector<std::string> > &myTagMap;
 
38
 
 
39
        std::string myCategoryName;
 
40
        std::string mySubCategoryName;
 
41
        std::vector<std::string> myGenreIds;
 
42
        std::string myLanguage;
 
43
};
 
44
 
 
45
FB2TagInfoReader::FB2TagInfoReader(std::map<std::string,std::vector<std::string> > &tagMap) : myTagMap(tagMap) {
 
46
        myLanguage = ZLibrary::Language();
 
47
        if (myLanguage != "ru") {
 
48
                myLanguage = "en";
 
49
        }
 
50
}
 
51
 
 
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";
 
57
 
 
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");
 
61
                if (id != 0) {
 
62
                        myGenreIds.push_back(id);
 
63
                }
 
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");
 
68
                        if (name != 0) {
 
69
                                myCategoryName = name;
 
70
                                ZLStringUtil::stripWhiteSpaces(myCategoryName);
 
71
                        }
 
72
                }
 
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");
 
77
                        if (name != 0) {
 
78
                                mySubCategoryName = name;
 
79
                                ZLStringUtil::stripWhiteSpaces(mySubCategoryName);
 
80
                        }
 
81
                }
 
82
        }
 
83
}
 
84
 
 
85
void FB2TagInfoReader::endElementHandler(const char *tag) {
 
86
        if (GENRE_TAG == tag) {
 
87
                myCategoryName.erase();
 
88
                mySubCategoryName.erase();
 
89
                myGenreIds.clear();
 
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);
 
95
                        }
 
96
                }
 
97
                mySubCategoryName.erase();
 
98
                myGenreIds.clear();
 
99
        }
 
100
}
 
101
 
 
102
FB2TagManager *FB2TagManager::ourInstance = 0;
 
103
 
 
104
const FB2TagManager &FB2TagManager::instance() {
 
105
        if (ourInstance == 0) {
 
106
                ourInstance = new FB2TagManager();
 
107
        }
 
108
        return *ourInstance;
 
109
}
 
110
 
 
111
FB2TagManager::FB2TagManager() {
 
112
        FB2TagInfoReader(myTagMap).readDocument(
 
113
                ZLibrary::ApplicationDirectory() + ZLibrary::FileNameDelimiter +
 
114
                "formats" + ZLibrary::FileNameDelimiter + "fb2" +
 
115
                ZLibrary::FileNameDelimiter + "fb2genres.xml"
 
116
        );
 
117
}
 
118
 
 
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;
 
123
}