]>
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 | ||
85 | while (i < end_pos) { | |
01dba85a | 86 | c = m_Source[(unsigned int) i]; |
5526e819 VS |
87 | |
88 | // continue building word: | |
89 | if (c != '<') { | |
90 | temp[templen++] = c; | |
5526e819 VS |
91 | i++; |
92 | } | |
93 | ||
94 | else if (c == '<') { | |
95 | wxHtmlTag tag(m_Source, i, end_pos, m_Cache); | |
96 | ||
97 | if (templen) { | |
98 | temp[templen] = 0; | |
99 | AddText(temp); | |
100 | templen = 0; | |
101 | } | |
102 | AddTag(tag); | |
103 | if (tag.HasEnding()) i = tag.GetEndPos2(); | |
104 | else i = tag.GetBeginPos(); | |
105 | } | |
106 | } | |
107 | ||
108 | if (templen) { // last word of block :-( | |
109 | temp[templen] = 0; | |
110 | AddText(temp); | |
111 | } | |
19bcc604 | 112 | delete[] temp; |
5526e819 VS |
113 | } |
114 | ||
115 | ||
116 | ||
117 | void wxHtmlParser::AddTag(const wxHtmlTag& tag) | |
118 | { | |
119 | wxHtmlTagHandler *h; | |
120 | bool inner = FALSE; | |
121 | ||
122 | h = (wxHtmlTagHandler*) m_HandlersHash.Get(tag.GetName()); | |
123 | if (h) | |
124 | inner = h -> HandleTag(tag); | |
125 | if (!inner) { | |
126 | if (tag.HasEnding()) | |
127 | DoParsing(tag.GetBeginPos(), tag.GetEndPos1()); | |
128 | } | |
129 | } | |
130 | ||
131 | ||
132 | ||
133 | void wxHtmlParser::AddTagHandler(wxHtmlTagHandler *handler) | |
134 | { | |
135 | wxString s(handler -> GetSupportedTags()); | |
136 | wxStringTokenizer tokenizer(s, ", "); | |
137 | ||
5526e819 | 138 | while (tokenizer.HasMoreTokens()) |
5526e819 VS |
139 | m_HandlersHash.Put(tokenizer.NextToken(), handler); |
140 | ||
141 | if (m_HandlersList.IndexOf(handler) == wxNOT_FOUND) | |
142 | m_HandlersList.Append(handler); | |
143 | ||
144 | handler -> SetParser(this); | |
145 | } | |
146 | ||
147 | ||
148 | ||
a7a4d01b VS |
149 | void wxHtmlParser::PushTagHandler(wxHtmlTagHandler *handler, wxString tags) |
150 | { | |
151 | wxStringTokenizer tokenizer(tags, ", "); | |
152 | wxString key; | |
153 | ||
154 | if (m_HandlersStack == NULL) { | |
155 | m_HandlersStack = new wxList; | |
156 | m_HandlersStack -> DeleteContents(TRUE); | |
157 | } | |
158 | ||
159 | m_HandlersStack -> Insert(new wxHashTable(m_HandlersHash)); | |
160 | ||
161 | while (tokenizer.HasMoreTokens()) { | |
162 | key = tokenizer.NextToken(); | |
163 | m_HandlersHash.Delete(key); | |
164 | m_HandlersHash.Put(key, handler); | |
165 | } | |
166 | } | |
167 | ||
168 | ||
169 | ||
170 | void wxHtmlParser::PopTagHandler() | |
171 | { | |
172 | wxNode *first; | |
173 | ||
f3c82859 VS |
174 | if (m_HandlersStack == NULL || |
175 | (first = m_HandlersStack -> GetFirst()) == NULL) | |
176 | { | |
177 | wxLogWarning(_("Warning: attempt to remove HTML tag handler from empty stack.")); | |
178 | return; | |
179 | } | |
a7a4d01b VS |
180 | m_HandlersHash = *((wxHashTable*) first -> GetData()); |
181 | m_HandlersStack -> DeleteNode(first); | |
182 | } | |
183 | ||
184 | ||
185 | ||
5526e819 VS |
186 | wxHtmlParser::~wxHtmlParser() |
187 | { | |
a7a4d01b | 188 | if (m_HandlersStack) delete m_HandlersStack; |
5526e819 VS |
189 | m_HandlersHash.Clear(); |
190 | m_HandlersList.DeleteContents(TRUE); | |
191 | m_HandlersList.Clear(); | |
192 | } | |
193 | ||
194 | ||
195 | ||
196 | //----------------------------------------------------------------------------- | |
197 | // wxHtmlTagHandler | |
198 | //----------------------------------------------------------------------------- | |
199 | ||
200 | IMPLEMENT_ABSTRACT_CLASS(wxHtmlTagHandler,wxObject) | |
223d09f6 | 201 | #endif |
5526e819 | 202 |