]>
Commit | Line | Data |
---|---|---|
5526e819 | 1 | ///////////////////////////////////////////////////////////////////////////// |
69941f05 | 2 | // Name: htmlpars.cpp |
5526e819 VS |
3 | // Purpose: wxHtmlParser class (generic parser) |
4 | // Author: Vaclav Slavik | |
69941f05 | 5 | // RCS-ID: $Id$ |
5526e819 VS |
6 | // Copyright: (c) 1999 Vaclav Slavik |
7 | // Licence: wxWindows Licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation | |
13 | #endif | |
14 | ||
3096bd2f | 15 | #include "wx/wxprec.h" |
5526e819 VS |
16 | |
17 | #include "wx/defs.h" | |
f6bcfd97 | 18 | #if wxUSE_HTML && wxUSE_STREAMS |
5526e819 VS |
19 | |
20 | #ifdef __BORDLANDC__ | |
21 | #pragma hdrstop | |
22 | #endif | |
23 | ||
24 | #ifndef WXPRECOMP | |
3096bd2f | 25 | #include "wx/wx.h" |
5526e819 VS |
26 | #endif |
27 | ||
69941f05 VS |
28 | #include "wx/tokenzr.h" |
29 | #include "wx/wfstream.h" | |
30 | #include "wx/url.h" | |
31 | #include "wx/html/htmldefs.h" | |
32 | #include "wx/html/htmlpars.h" | |
5526e819 VS |
33 | |
34 | ||
35 | ||
36 | //----------------------------------------------------------------------------- | |
37 | // wxHtmlParser | |
38 | //----------------------------------------------------------------------------- | |
39 | ||
40 | IMPLEMENT_ABSTRACT_CLASS(wxHtmlParser,wxObject) | |
41 | ||
42 | ||
43 | wxObject* wxHtmlParser::Parse(const wxString& source) | |
44 | { | |
45 | wxObject *result; | |
46 | ||
47 | InitParser(source); | |
48 | DoParsing(); | |
49 | result = GetProduct(); | |
50 | DoneParser(); | |
51 | return result; | |
52 | } | |
53 | ||
54 | ||
55 | ||
56 | void wxHtmlParser::InitParser(const wxString& source) | |
57 | { | |
58 | m_Source = source; | |
59 | m_Cache = new wxHtmlTagsCache(m_Source); | |
60 | } | |
61 | ||
62 | ||
63 | ||
64 | void wxHtmlParser::DoneParser() | |
65 | { | |
66 | delete m_Cache; | |
67 | m_Cache = NULL; | |
68 | } | |
69 | ||
70 | ||
71 | ||
5526e819 VS |
72 | |
73 | void wxHtmlParser::DoParsing(int begin_pos, int end_pos) | |
74 | { | |
99397a94 VS |
75 | if (end_pos <= begin_pos) return; |
76 | ||
19bcc604 VS |
77 | char c; |
78 | char *temp = new char[end_pos - begin_pos + 1]; | |
5526e819 VS |
79 | int i; |
80 | int templen; | |
81 | ||
82 | templen = 0; | |
83 | i = begin_pos; | |
84 | ||
4f9297b0 VS |
85 | while (i < end_pos) |
86 | { | |
01dba85a | 87 | c = m_Source[(unsigned int) i]; |
5526e819 VS |
88 | |
89 | // continue building word: | |
4f9297b0 VS |
90 | if (c != '<') |
91 | { | |
5526e819 | 92 | temp[templen++] = c; |
5526e819 VS |
93 | i++; |
94 | } | |
95 | ||
4f9297b0 VS |
96 | else if (c == '<') |
97 | { | |
5526e819 VS |
98 | wxHtmlTag tag(m_Source, i, end_pos, m_Cache); |
99 | ||
4f9297b0 VS |
100 | if (templen) |
101 | { | |
5526e819 VS |
102 | temp[templen] = 0; |
103 | AddText(temp); | |
104 | templen = 0; | |
105 | } | |
106 | AddTag(tag); | |
107 | if (tag.HasEnding()) i = tag.GetEndPos2(); | |
108 | else i = tag.GetBeginPos(); | |
109 | } | |
110 | } | |
111 | ||
4f9297b0 VS |
112 | if (templen) |
113 | { // last word of block :-( | |
5526e819 VS |
114 | temp[templen] = 0; |
115 | AddText(temp); | |
116 | } | |
19bcc604 | 117 | delete[] temp; |
5526e819 VS |
118 | } |
119 | ||
120 | ||
121 | ||
122 | void wxHtmlParser::AddTag(const wxHtmlTag& tag) | |
123 | { | |
124 | wxHtmlTagHandler *h; | |
125 | bool inner = FALSE; | |
126 | ||
127 | h = (wxHtmlTagHandler*) m_HandlersHash.Get(tag.GetName()); | |
128 | if (h) | |
4f9297b0 VS |
129 | inner = h->HandleTag(tag); |
130 | if (!inner) | |
131 | { | |
5526e819 VS |
132 | if (tag.HasEnding()) |
133 | DoParsing(tag.GetBeginPos(), tag.GetEndPos1()); | |
134 | } | |
135 | } | |
136 | ||
137 | ||
138 | ||
139 | void wxHtmlParser::AddTagHandler(wxHtmlTagHandler *handler) | |
140 | { | |
4f9297b0 | 141 | wxString s(handler->GetSupportedTags()); |
5526e819 VS |
142 | wxStringTokenizer tokenizer(s, ", "); |
143 | ||
5526e819 | 144 | while (tokenizer.HasMoreTokens()) |
5526e819 VS |
145 | m_HandlersHash.Put(tokenizer.NextToken(), handler); |
146 | ||
147 | if (m_HandlersList.IndexOf(handler) == wxNOT_FOUND) | |
148 | m_HandlersList.Append(handler); | |
149 | ||
4f9297b0 | 150 | handler->SetParser(this); |
5526e819 VS |
151 | } |
152 | ||
153 | ||
154 | ||
a7a4d01b VS |
155 | void wxHtmlParser::PushTagHandler(wxHtmlTagHandler *handler, wxString tags) |
156 | { | |
157 | wxStringTokenizer tokenizer(tags, ", "); | |
158 | wxString key; | |
159 | ||
4f9297b0 VS |
160 | if (m_HandlersStack == NULL) |
161 | { | |
a7a4d01b | 162 | m_HandlersStack = new wxList; |
4f9297b0 | 163 | m_HandlersStack->DeleteContents(TRUE); |
a7a4d01b VS |
164 | } |
165 | ||
4f9297b0 | 166 | m_HandlersStack->Insert(new wxHashTable(m_HandlersHash)); |
a7a4d01b | 167 | |
4f9297b0 VS |
168 | while (tokenizer.HasMoreTokens()) |
169 | { | |
a7a4d01b VS |
170 | key = tokenizer.NextToken(); |
171 | m_HandlersHash.Delete(key); | |
172 | m_HandlersHash.Put(key, handler); | |
173 | } | |
174 | } | |
175 | ||
176 | ||
177 | ||
178 | void wxHtmlParser::PopTagHandler() | |
179 | { | |
180 | wxNode *first; | |
181 | ||
f3c82859 | 182 | if (m_HandlersStack == NULL || |
4f9297b0 | 183 | (first = m_HandlersStack->GetFirst()) == NULL) |
f3c82859 VS |
184 | { |
185 | wxLogWarning(_("Warning: attempt to remove HTML tag handler from empty stack.")); | |
186 | return; | |
187 | } | |
4f9297b0 VS |
188 | m_HandlersHash = *((wxHashTable*) first->GetData()); |
189 | m_HandlersStack->DeleteNode(first); | |
a7a4d01b VS |
190 | } |
191 | ||
192 | ||
193 | ||
5526e819 VS |
194 | wxHtmlParser::~wxHtmlParser() |
195 | { | |
a7a4d01b | 196 | if (m_HandlersStack) delete m_HandlersStack; |
5526e819 VS |
197 | m_HandlersHash.Clear(); |
198 | m_HandlersList.DeleteContents(TRUE); | |
199 | m_HandlersList.Clear(); | |
200 | } | |
201 | ||
202 | ||
203 | ||
204 | //----------------------------------------------------------------------------- | |
205 | // wxHtmlTagHandler | |
206 | //----------------------------------------------------------------------------- | |
207 | ||
208 | IMPLEMENT_ABSTRACT_CLASS(wxHtmlTagHandler,wxObject) | |
223d09f6 | 209 | #endif |
5526e819 | 210 |