gui
[lbook_fbreader.git] / fbreader / src / formats / html / HtmlReader.h
blob7ff08db327f8d36805228b4148ecf2a7cadd1312
1 /*
2 * Copyright (C) 2004-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
17 * 02110-1301, USA.
20 #ifndef __HTMLREADER_H__
21 #define __HTMLREADER_H__
23 #include <string>
24 #include <vector>
26 #include <ZLEncodingConverter.h>
27 #include "../EncodedTextReader.h"
29 class ZLInputStream;
31 class HtmlReader : public EncodedTextReader {
33 public:
34 struct HtmlAttribute {
35 std::string Name;
36 std::string Value;
37 bool HasValue;
39 HtmlAttribute(const std::string &name);
40 ~HtmlAttribute();
41 void setValue(const std::string &value);
44 struct HtmlTag {
45 std::string Name;
46 size_t Offset;
47 bool Start;
48 std::vector<HtmlAttribute> Attributes;
50 HtmlTag();
51 ~HtmlTag();
52 void addAttribute(const std::string &name);
53 void setLastAttributeValue(const std::string &value);
55 private:
56 HtmlTag(const HtmlTag&);
57 const HtmlTag &operator= (const HtmlTag&);
60 private:
61 static void setTag(HtmlTag &tag, const std::string &fullName);
63 public:
64 virtual void readDocument(ZLInputStream &stream);
66 protected:
67 HtmlReader(const std::string &encoding);
68 virtual ~HtmlReader();
70 protected:
71 virtual void startDocumentHandler() = 0;
72 virtual void endDocumentHandler() = 0;
74 virtual bool tagHandler(const HtmlTag &tag) = 0;
75 virtual bool characterDataHandler(const char *text, int len, bool convert) = 0;
77 private:
78 void appendString(std::string &to, std::string &from);
81 inline HtmlReader::HtmlAttribute::HtmlAttribute(const std::string &name) : Name(name), HasValue(false) {}
82 inline HtmlReader::HtmlAttribute::~HtmlAttribute() {}
83 inline void HtmlReader::HtmlAttribute::setValue(const std::string &value) { Value = value; HasValue = true; }
85 inline HtmlReader::HtmlTag::HtmlTag() : Start(true) {}
86 inline HtmlReader::HtmlTag::~HtmlTag() {}
87 inline void HtmlReader::HtmlTag::addAttribute(const std::string &name) { Attributes.push_back(HtmlAttribute(name)); }
88 inline void HtmlReader::HtmlTag::setLastAttributeValue(const std::string &value) { if (!Attributes.empty()) Attributes.back().setValue(value); }
90 #endif /* __HTMLREADER_H__ */