]> git.saurik.com Git - wxWidgets.git/blame - src/html/htmlfilt.cpp
Remove wxConfig object caching (m_config), which solves dangling pointer problem...
[wxWidgets.git] / src / html / htmlfilt.cpp
CommitLineData
5526e819 1/////////////////////////////////////////////////////////////////////////////
69941f05 2// Name: htmlfilt.cpp
5526e819
VS
3// Purpose: wxHtmlFilter - input filter for translating into HTML format
4// Author: Vaclav Slavik
69941f05 5// RCS-ID: $Id$
5526e819 6// Copyright: (c) 1999 Vaclav Slavik
65571936 7// Licence: wxWindows licence
5526e819
VS
8/////////////////////////////////////////////////////////////////////////////
9
4dcaf11a 10#include "wx/wxprec.h"
5526e819 11
314260fb 12#include "wx/defs.h"
f6bcfd97 13#if wxUSE_HTML && wxUSE_STREAMS
5526e819 14
2b5f62a0 15#ifdef __BORLANDC__
5526e819
VS
16#pragma hdrstop
17#endif
18
19#ifndef WXPRECOMP
04dbb646
VZ
20 #include "wx/log.h"
21 #include "wx/intl.h"
5526e819
VS
22#endif
23
2b5f62a0 24#include "wx/strconv.h"
69941f05 25#include "wx/html/htmlfilt.h"
4dcaf11a 26#include "wx/html/htmlwin.h"
5526e819 27
eb37e1d2 28// utility function: read a wxString from a wxInputStream
2b5f62a0 29static void ReadString(wxString& str, wxInputStream* s, wxMBConv& conv)
eb37e1d2
MB
30{
31 size_t streamSize = s->GetSize();
32
2b5f62a0 33 if (streamSize == ~(size_t)0)
eb37e1d2
MB
34 {
35 const size_t bufSize = 4095;
36 char buffer[bufSize+1];
37 size_t lastRead;
38
39 do
40 {
41 s->Read(buffer, bufSize);
42 lastRead = s->LastRead();
43 buffer[lastRead] = 0;
2b5f62a0 44 str.Append(wxString(buffer, conv));
eb37e1d2 45 }
2b5f62a0 46 while (lastRead == bufSize);
eb37e1d2
MB
47 }
48 else
49 {
50 char* src = new char[streamSize+1];
51 s->Read(src, streamSize);
52 src[streamSize] = 0;
2b5f62a0
VZ
53 str = wxString(src, conv);
54 delete[] src;
eb37e1d2
MB
55 }
56}
5526e819
VS
57
58/*
59
60There is code for several default filters:
61
62*/
63
64IMPLEMENT_ABSTRACT_CLASS(wxHtmlFilter, wxObject)
65
66//--------------------------------------------------------------------------------
67// wxHtmlFilterPlainText
68// filter for text/plain or uknown
69//--------------------------------------------------------------------------------
70
71IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterPlainText, wxHtmlFilter)
72
a4c97004 73bool wxHtmlFilterPlainText::CanRead(const wxFSFile& WXUNUSED(file)) const
5526e819 74{
d1da8872 75 return true;
5526e819
VS
76}
77
78
79
420ec58a 80wxString wxHtmlFilterPlainText::ReadFile(const wxFSFile& file) const
5526e819
VS
81{
82 wxInputStream *s = file.GetStream();
5526e819
VS
83 wxString doc, doc2;
84
85 if (s == NULL) return wxEmptyString;
2b5f62a0 86 ReadString(doc, s, wxConvISO8859_1);
5526e819 87
d1da8872
WS
88 doc.Replace(wxT("&"), wxT("&"), true);
89 doc.Replace(wxT("<"), wxT("&lt;"), true);
90 doc.Replace(wxT(">"), wxT("&gt;"), true);
2b5f62a0 91 doc2 = wxT("<HTML><BODY><PRE>\n") + doc + wxT("\n</PRE></BODY></HTML>");
5526e819
VS
92 return doc2;
93}
94
95
96
97
98
99//--------------------------------------------------------------------------------
100// wxHtmlFilterImage
101// filter for image/*
102//--------------------------------------------------------------------------------
103
104class wxHtmlFilterImage : public wxHtmlFilter
105{
106 DECLARE_DYNAMIC_CLASS(wxHtmlFilterImage)
107
108 public:
420ec58a
VS
109 virtual bool CanRead(const wxFSFile& file) const;
110 virtual wxString ReadFile(const wxFSFile& file) const;
5526e819
VS
111};
112
113IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterImage, wxHtmlFilter)
114
115
116
420ec58a 117bool wxHtmlFilterImage::CanRead(const wxFSFile& file) const
5526e819 118{
0413cec5 119 return (file.GetMimeType().Left(6) == wxT("image/"));
5526e819
VS
120}
121
122
123
420ec58a 124wxString wxHtmlFilterImage::ReadFile(const wxFSFile& file) const
5526e819 125{
2b5f62a0
VZ
126 wxString res = wxT("<HTML><BODY><IMG SRC=\"") + file.GetLocation() + wxT("\"></BODY></HTML>");
127 return res;
5526e819
VS
128}
129
130
131
132
133//--------------------------------------------------------------------------------
2b5f62a0
VZ
134// wxHtmlFilterHTML
135// filter for text/html
5526e819
VS
136//--------------------------------------------------------------------------------
137
5526e819
VS
138
139IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterHTML, wxHtmlFilter)
140
420ec58a 141bool wxHtmlFilterHTML::CanRead(const wxFSFile& file) const
5526e819 142{
f61815af
GL
143// return (file.GetMimeType() == "text/html");
144// This is true in most case but some page can return:
145// "text/html; char-encoding=...."
146// So we use Find instead
77611ad4 147 return (file.GetMimeType().Find(wxT("text/html")) == 0);
5526e819
VS
148}
149
150
151
420ec58a 152wxString wxHtmlFilterHTML::ReadFile(const wxFSFile& file) const
5526e819
VS
153{
154 wxInputStream *s = file.GetStream();
5526e819
VS
155 wxString doc;
156
04dbb646 157 if (s == NULL)
f3c82859 158 {
f6bcfd97 159 wxLogError(_("Cannot open HTML document: %s"), file.GetLocation().c_str());
f3c82859
VS
160 return wxEmptyString;
161 }
5526e819 162
d1da8872 163 // NB: We convert input file to wchar_t here in Unicode mode, based on
2b5f62a0
VZ
164 // either Content-Type header or <meta> tags. In ANSI mode, we don't
165 // do it as it is done by wxHtmlParser (for this reason, we add <meta>
166 // tag if we used Content-Type header).
167#if wxUSE_UNICODE
d1da8872 168 int charsetPos;
2b5f62a0
VZ
169 if ((charsetPos = file.GetMimeType().Find(_T("; charset="))) != wxNOT_FOUND)
170 {
171 wxString charset = file.GetMimeType().Mid(charsetPos + 10);
172 wxCSConv conv(charset);
173 ReadString(doc, s, conv);
174 }
175 else
176 {
177 wxString tmpdoc;
178 ReadString(tmpdoc, s, wxConvISO8859_1);
179 wxString charset = wxHtmlParser::ExtractCharsetInformation(tmpdoc);
180 if (charset.empty())
181 doc = tmpdoc;
182 else
183 {
184 wxCSConv conv(charset);
185 doc = wxString(tmpdoc.mb_str(wxConvISO8859_1), conv);
186 }
187 }
188#else // !wxUSE_UNICODE
189 ReadString(doc, s, wxConvLibc);
04dbb646 190 // add meta tag if we obtained this through http:
2b5f62a0 191 if (!file.GetMimeType().empty())
981e62aa 192 {
2b5f62a0
VZ
193 wxString hdr;
194 wxString mime = file.GetMimeType();
195 hdr.Printf(_T("<meta http-equiv=\"Content-Type\" content=\"%s\">"), mime.c_str());
196 return hdr+doc;
981e62aa 197 }
2b5f62a0 198#endif
981e62aa 199
3ca6a5f0 200 return doc;
5526e819
VS
201}
202
203
204
205
206///// Module:
207
208class wxHtmlFilterModule : public wxModule
209{
210 DECLARE_DYNAMIC_CLASS(wxHtmlFilterModule)
211
212 public:
213 virtual bool OnInit()
214 {
215 wxHtmlWindow::AddFilter(new wxHtmlFilterHTML);
216 wxHtmlWindow::AddFilter(new wxHtmlFilterImage);
d1da8872 217 return true;
5526e819
VS
218 }
219 virtual void OnExit() {}
220};
221
222IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterModule, wxModule)
223
8dd71e2b 224#endif