]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmlparser.cpp
serbase.cpp added to the list of files
[wxWidgets.git] / src / html / htmlparser.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: htmlparser.cpp
3 // Purpose: wxHtmlParser class (generic parser)
4 // Author: Vaclav Slavik
5 // Copyright: (c) 1999 Vaclav Slavik
6 // Licence: wxWindows Licence
7 /////////////////////////////////////////////////////////////////////////////
8
9
10 #ifdef __GNUG__
11 #pragma implementation
12 #endif
13
14 #include <wx/wxprec.h>
15
16 #include "wx/defs.h"
17 #if wxUSE_HTML
18
19 #ifdef __BORDLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WXPRECOMP
24 #include <wx/wx.h>
25 #endif
26
27 #include <wx/tokenzr.h>
28 #include <wx/wfstream.h>
29 #include <wx/url.h>
30 #include <wx/html/htmldefs.h>
31 #include <wx/html/htmlparser.h>
32
33
34
35 //-----------------------------------------------------------------------------
36 // wxHtmlParser
37 //-----------------------------------------------------------------------------
38
39 IMPLEMENT_ABSTRACT_CLASS(wxHtmlParser,wxObject)
40
41
42 wxObject* wxHtmlParser::Parse(const wxString& source)
43 {
44 wxObject *result;
45
46 InitParser(source);
47 DoParsing();
48 result = GetProduct();
49 DoneParser();
50 return result;
51 }
52
53
54
55 void wxHtmlParser::InitParser(const wxString& source)
56 {
57 m_Source = source;
58 m_Cache = new wxHtmlTagsCache(m_Source);
59 }
60
61
62
63 void wxHtmlParser::DoneParser()
64 {
65 delete m_Cache;
66 m_Cache = NULL;
67 }
68
69
70
71 #define HTML_MAX_BUFLEN 1024
72
73 void wxHtmlParser::DoParsing(int begin_pos, int end_pos)
74 {
75 char temp[HTML_BUFLEN], c;
76 int i;
77 int templen;
78
79 templen = 0;
80 i = begin_pos;
81
82 while (i < end_pos) {
83 c = m_Source[i];
84
85 // continue building word:
86 if (c != '<') {
87 temp[templen++] = c;
88 if (templen == HTML_BUFLEN-1) {
89 temp[templen] = 0;
90 AddText(temp);
91 templen = 0;
92 }
93 i++;
94 }
95
96 else if (c == '<') {
97 wxHtmlTag tag(m_Source, i, end_pos, m_Cache);
98
99 if (templen) {
100 temp[templen] = 0;
101 AddText(temp);
102 templen = 0;
103 }
104 AddTag(tag);
105 if (tag.HasEnding()) i = tag.GetEndPos2();
106 else i = tag.GetBeginPos();
107 }
108 }
109
110 if (templen) { // last word of block :-(
111 temp[templen] = 0;
112 AddText(temp);
113 }
114 }
115
116
117
118 void wxHtmlParser::AddTag(const wxHtmlTag& tag)
119 {
120 wxHtmlTagHandler *h;
121 bool inner = FALSE;
122
123 h = (wxHtmlTagHandler*) m_HandlersHash.Get(tag.GetName());
124 if (h)
125 inner = h -> HandleTag(tag);
126 if (!inner) {
127 if (tag.HasEnding())
128 DoParsing(tag.GetBeginPos(), tag.GetEndPos1());
129 }
130 }
131
132
133
134 void wxHtmlParser::AddTagHandler(wxHtmlTagHandler *handler)
135 {
136 wxString s(handler -> GetSupportedTags());
137 wxStringTokenizer tokenizer(s, ", ");
138
139 #if (wxVERSION_NUMBER < 2100)
140 while (tokenizer.HasMoreToken())
141 #else
142 while (tokenizer.HasMoreTokens())
143 #endif
144 m_HandlersHash.Put(tokenizer.NextToken(), handler);
145
146 if (m_HandlersList.IndexOf(handler) == wxNOT_FOUND)
147 m_HandlersList.Append(handler);
148
149 handler -> SetParser(this);
150 }
151
152
153
154 wxHtmlParser::~wxHtmlParser()
155 {
156 m_HandlersHash.Clear();
157 m_HandlersList.DeleteContents(TRUE);
158 m_HandlersList.Clear();
159 }
160
161
162
163 //-----------------------------------------------------------------------------
164 // wxHtmlTagHandler
165 //-----------------------------------------------------------------------------
166
167 IMPLEMENT_ABSTRACT_CLASS(wxHtmlTagHandler,wxObject)
168
169 #endif