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