summaryrefslogtreecommitdiffstats
path: root/fbreader/src/network/opds/OPDSLink_GenericXMLParser.cpp
blob: ef671167f916e1b78ce423a926ba46fb05f477f2 (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
/*
 * Copyright (C) 2008-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 <ZLXMLNamespace.h>

#include "OPDSLink_GenericXMLParser.h"
#include "URLRewritingRule.h"
#include "OPDSLink_AdvancedSearch.h"

static const std::string TAG_ENTRY = "entry";
static const std::string FBREADER_ADVANCED_SEARCH = "advancedSearch";
static const std::string FBREADER_AUTHENTICATION = "authentication";
static const std::string FBREADER_REWRITING_RULE = "urlRewritingRule";
static const std::string FBREADER_RELATION_ALIAS = "relationAlias";
static const std::string FBREADER_EXTRA = "extra";

OPDSLink::GenericXMLParser::GenericXMLParser(shared_ptr<OPDSFeedReader> feedReader) :
	OPDSXMLParser(feedReader) {
}

OPDSLink::GenericFeedReader &OPDSLink::GenericXMLParser::getFeedReader() const {
	return static_cast<OPDSLink::GenericFeedReader&>(*myFeedReader);
}

void OPDSLink::GenericXMLParser::startElementHandler(const char *tag, const char **attributes) {
	switch (myState) {
		case FEED:
			if (testTag(ZLXMLNamespace::Atom, TAG_ENTRY, tag)) {
				getFeedReader().clear();
			}
			break;
		case F_ENTRY:
			if (testTag(ZLXMLNamespace::FBReaderCatalogMetadata, FBREADER_ADVANCED_SEARCH, tag)) {
				const char *style = attributeValue(attributes, "style");
				const char *author = attributeValue(attributes, "author");
				const char *titleOrSeries = attributeValue(attributes, "titleOrSeries");
				const char *tag = attributeValue(attributes, "tag");
				const char *annotation = attributeValue(attributes, "annotation");
				if (style != 0 && author != 0 && titleOrSeries != 0 && tag != 0 && annotation != 0) {
					getFeedReader().setAdvancedSearch(new OPDSLink::AdvancedSearch(style, titleOrSeries, author, tag, annotation));
				}
				return;
			} else if (testTag(ZLXMLNamespace::FBReaderCatalogMetadata, FBREADER_AUTHENTICATION, tag)) {
				const char *type = attributeValue(attributes, "type");
				if (type != 0) {
					getFeedReader().setAuthenticationType(type);
				}
				return;
			} else if (testTag(ZLXMLNamespace::FBReaderCatalogMetadata, FBREADER_RELATION_ALIAS, tag)) {
				const char *name = attributeValue(attributes, "name");
				const char *type = attributeValue(attributes, "type");
				const char *alias = attributeValue(attributes, "alias");
				if (name != 0 && alias != 0) {
					getFeedReader().addRelationAlias(OPDSLink::RelationAlias(alias, (type != 0) ? type : std::string()), name);
				}
			} else if (testTag(ZLXMLNamespace::FBReaderCatalogMetadata, FBREADER_REWRITING_RULE, tag)) {

				getFeedReader().addUrlRewritingRule(new URLRewritingRule(getAttributesMap(attributes)));

//				const char *type  = attributeValue(attributes, "type");
//				const char *apply = attributeValue(attributes, "apply");
//				const char *name  = attributeValue(attributes, "name");
//				const char *value = attributeValue(attributes, "value");

//				//TODO add rewrite type of 'rewriting rules'
//				URLRewritingRule::RuleApply ruleApply = URLRewritingRule::ALWAYS;
//				if (apply != 0) {
//					const std::string applyStr = apply;
//					if (applyStr == "external") {
//						ruleApply = URLRewritingRule::EXTERNAL;
//					} else if (applyStr == "internal") {
//						ruleApply = URLRewritingRule::INTERNAL;
//					} else if (applyStr != "always") {
//						type = 0;
//					}
//				}

//				if (type != 0 && name != 0 && value != 0) {
//					std::string typeStr = type;
//					if (typeStr == "addUrlParameter") {
//						getFeedReader().addUrlRewritingRule(new URLRewritingRule(URLRewritingRule::ADD_URL_PARAMETER, ruleApply, name, value));
//					}
//				}

				return;
			}
			break;
		default:
			break;
	}
	OPDSXMLParser::startElementHandler(tag, attributes);
}