]> git.saurik.com Git - wxWidgets.git/blame - src/html/htmlpars.cpp
fingers crossed..
[wxWidgets.git] / src / html / htmlpars.cpp
CommitLineData
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
40IMPLEMENT_ABSTRACT_CLASS(wxHtmlParser,wxObject)
41
42
43wxObject* 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
5526e819
VS
54void wxHtmlParser::InitParser(const wxString& source)
55{
1309ba6c 56 SetSource(source);
5526e819 57}
1309ba6c 58
5526e819
VS
59void wxHtmlParser::DoneParser()
60{
61 delete m_Cache;
62 m_Cache = NULL;
63}
64
1309ba6c
VS
65void wxHtmlParser::SetSource(const wxString& src)
66{
67 m_Source = src;
68 delete m_Cache;
69 m_Cache = new wxHtmlTagsCache(m_Source);
70}
5526e819
VS
71
72void wxHtmlParser::DoParsing(int begin_pos, int end_pos)
73{
99397a94
VS
74 if (end_pos <= begin_pos) return;
75
19bcc604
VS
76 char c;
77 char *temp = new char[end_pos - begin_pos + 1];
5526e819
VS
78 int i;
79 int templen;
80
81 templen = 0;
82 i = begin_pos;
83
4f9297b0
VS
84 while (i < end_pos)
85 {
01dba85a 86 c = m_Source[(unsigned int) i];
5526e819
VS
87
88 // continue building word:
4f9297b0
VS
89 if (c != '<')
90 {
5526e819 91 temp[templen++] = c;
5526e819
VS
92 i++;
93 }
94
4f9297b0
VS
95 else if (c == '<')
96 {
5526e819
VS
97 wxHtmlTag tag(m_Source, i, end_pos, m_Cache);
98
4f9297b0
VS
99 if (templen)
100 {
5526e819
VS
101 temp[templen] = 0;
102 AddText(temp);
103 templen = 0;
104 }
105 AddTag(tag);
106 if (tag.HasEnding()) i = tag.GetEndPos2();
107 else i = tag.GetBeginPos();
108 }
109 }
110
4f9297b0
VS
111 if (templen)
112 { // last word of block :-(
5526e819
VS
113 temp[templen] = 0;
114 AddText(temp);
115 }
19bcc604 116 delete[] temp;
5526e819
VS
117}
118
5526e819
VS
119void wxHtmlParser::AddTag(const wxHtmlTag& tag)
120{
121 wxHtmlTagHandler *h;
122 bool inner = FALSE;
123
124 h = (wxHtmlTagHandler*) m_HandlersHash.Get(tag.GetName());
125 if (h)
4f9297b0
VS
126 inner = h->HandleTag(tag);
127 if (!inner)
128 {
5526e819
VS
129 if (tag.HasEnding())
130 DoParsing(tag.GetBeginPos(), tag.GetEndPos1());
131 }
132}
133
5526e819
VS
134void wxHtmlParser::AddTagHandler(wxHtmlTagHandler *handler)
135{
4f9297b0 136 wxString s(handler->GetSupportedTags());
5526e819
VS
137 wxStringTokenizer tokenizer(s, ", ");
138
5526e819 139 while (tokenizer.HasMoreTokens())
5526e819
VS
140 m_HandlersHash.Put(tokenizer.NextToken(), handler);
141
142 if (m_HandlersList.IndexOf(handler) == wxNOT_FOUND)
143 m_HandlersList.Append(handler);
144
4f9297b0 145 handler->SetParser(this);
5526e819
VS
146}
147
a7a4d01b
VS
148void wxHtmlParser::PushTagHandler(wxHtmlTagHandler *handler, wxString tags)
149{
150 wxStringTokenizer tokenizer(tags, ", ");
151 wxString key;
152
4f9297b0
VS
153 if (m_HandlersStack == NULL)
154 {
a7a4d01b 155 m_HandlersStack = new wxList;
4f9297b0 156 m_HandlersStack->DeleteContents(TRUE);
a7a4d01b
VS
157 }
158
4f9297b0 159 m_HandlersStack->Insert(new wxHashTable(m_HandlersHash));
a7a4d01b 160
4f9297b0
VS
161 while (tokenizer.HasMoreTokens())
162 {
a7a4d01b
VS
163 key = tokenizer.NextToken();
164 m_HandlersHash.Delete(key);
165 m_HandlersHash.Put(key, handler);
166 }
167}
168
a7a4d01b
VS
169void wxHtmlParser::PopTagHandler()
170{
171 wxNode *first;
172
f3c82859 173 if (m_HandlersStack == NULL ||
4f9297b0 174 (first = m_HandlersStack->GetFirst()) == NULL)
f3c82859
VS
175 {
176 wxLogWarning(_("Warning: attempt to remove HTML tag handler from empty stack."));
177 return;
178 }
4f9297b0
VS
179 m_HandlersHash = *((wxHashTable*) first->GetData());
180 m_HandlersStack->DeleteNode(first);
a7a4d01b
VS
181}
182
5526e819
VS
183wxHtmlParser::~wxHtmlParser()
184{
a7a4d01b 185 if (m_HandlersStack) delete m_HandlersStack;
5526e819
VS
186 m_HandlersHash.Clear();
187 m_HandlersList.DeleteContents(TRUE);
188 m_HandlersList.Clear();
189}
190
5526e819
VS
191//-----------------------------------------------------------------------------
192// wxHtmlTagHandler
193//-----------------------------------------------------------------------------
194
195IMPLEMENT_ABSTRACT_CLASS(wxHtmlTagHandler,wxObject)
223d09f6 196#endif
5526e819 197