]>
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 | ||
15 | #include <wx/wxprec.h> | |
16 | ||
17 | #include "wx/defs.h" | |
18 | #if wxUSE_HTML | |
19 | ||
20 | #ifdef __BORDLANDC__ | |
21 | #pragma hdrstop | |
22 | #endif | |
23 | ||
24 | #ifndef WXPRECOMP | |
25 | #include <wx/wx.h> | |
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 | ||
72 | #define HTML_MAX_BUFLEN 1024 | |
73 | ||
74 | void wxHtmlParser::DoParsing(int begin_pos, int end_pos) | |
75 | { | |
76 | char temp[HTML_BUFLEN], c; | |
77 | int i; | |
78 | int templen; | |
79 | ||
80 | templen = 0; | |
81 | i = begin_pos; | |
82 | ||
83 | while (i < end_pos) { | |
84 | c = m_Source[i]; | |
85 | ||
86 | // continue building word: | |
87 | if (c != '<') { | |
88 | temp[templen++] = c; | |
89 | if (templen == HTML_BUFLEN-1) { | |
90 | temp[templen] = 0; | |
91 | AddText(temp); | |
92 | templen = 0; | |
93 | } | |
94 | i++; | |
95 | } | |
96 | ||
97 | else if (c == '<') { | |
98 | wxHtmlTag tag(m_Source, i, end_pos, m_Cache); | |
99 | ||
100 | if (templen) { | |
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 | ||
111 | if (templen) { // last word of block :-( | |
112 | temp[templen] = 0; | |
113 | AddText(temp); | |
114 | } | |
115 | } | |
116 | ||
117 | ||
118 | ||
119 | void 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) | |
126 | inner = h -> HandleTag(tag); | |
127 | if (!inner) { | |
128 | if (tag.HasEnding()) | |
129 | DoParsing(tag.GetBeginPos(), tag.GetEndPos1()); | |
130 | } | |
131 | } | |
132 | ||
133 | ||
134 | ||
135 | void wxHtmlParser::AddTagHandler(wxHtmlTagHandler *handler) | |
136 | { | |
137 | wxString s(handler -> GetSupportedTags()); | |
138 | wxStringTokenizer tokenizer(s, ", "); | |
139 | ||
140 | #if (wxVERSION_NUMBER < 2100) | |
141 | while (tokenizer.HasMoreToken()) | |
142 | #else | |
143 | while (tokenizer.HasMoreTokens()) | |
144 | #endif | |
145 | m_HandlersHash.Put(tokenizer.NextToken(), handler); | |
146 | ||
147 | if (m_HandlersList.IndexOf(handler) == wxNOT_FOUND) | |
148 | m_HandlersList.Append(handler); | |
149 | ||
150 | handler -> SetParser(this); | |
151 | } | |
152 | ||
153 | ||
154 | ||
155 | wxHtmlParser::~wxHtmlParser() | |
156 | { | |
157 | m_HandlersHash.Clear(); | |
158 | m_HandlersList.DeleteContents(TRUE); | |
159 | m_HandlersList.Clear(); | |
160 | } | |
161 | ||
162 | ||
163 | ||
164 | //----------------------------------------------------------------------------- | |
165 | // wxHtmlTagHandler | |
166 | //----------------------------------------------------------------------------- | |
167 | ||
168 | IMPLEMENT_ABSTRACT_CLASS(wxHtmlTagHandler,wxObject) | |
223d09f6 | 169 | #endif |
5526e819 | 170 |