]>
Commit | Line | Data |
---|---|---|
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 VS |
6 | // Copyright: (c) 1999 Vaclav Slavik |
7 | // Licence: wxWindows Licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | ||
11 | #ifdef __GNUG__ | |
69941f05 | 12 | #pragma implementation |
5526e819 VS |
13 | #endif |
14 | ||
4dcaf11a | 15 | #include "wx/wxprec.h" |
5526e819 | 16 | |
314260fb | 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 | |
04dbb646 VZ |
25 | #include "wx/log.h" |
26 | #include "wx/intl.h" | |
5526e819 VS |
27 | #endif |
28 | ||
69941f05 | 29 | #include "wx/html/htmlfilt.h" |
4dcaf11a | 30 | #include "wx/html/htmlwin.h" |
5526e819 | 31 | |
eb37e1d2 MB |
32 | // utility function: read a wxString from a wxInputStream |
33 | void wxPrivate_ReadString(wxString& str, wxInputStream* s) | |
34 | { | |
35 | size_t streamSize = s->GetSize(); | |
36 | ||
37 | if(streamSize == ~(size_t)0) | |
38 | { | |
39 | const size_t bufSize = 4095; | |
40 | char buffer[bufSize+1]; | |
41 | size_t lastRead; | |
42 | ||
43 | do | |
44 | { | |
45 | s->Read(buffer, bufSize); | |
46 | lastRead = s->LastRead(); | |
47 | buffer[lastRead] = 0; | |
48 | str.Append(buffer); | |
49 | } | |
50 | while(lastRead == bufSize); | |
51 | } | |
52 | else | |
53 | { | |
54 | char* src = new char[streamSize+1]; | |
55 | s->Read(src, streamSize); | |
56 | src[streamSize] = 0; | |
57 | str = src; | |
58 | delete [] src; | |
59 | } | |
60 | } | |
5526e819 VS |
61 | |
62 | /* | |
63 | ||
64 | There is code for several default filters: | |
65 | ||
66 | */ | |
67 | ||
68 | IMPLEMENT_ABSTRACT_CLASS(wxHtmlFilter, wxObject) | |
69 | ||
70 | //-------------------------------------------------------------------------------- | |
71 | // wxHtmlFilterPlainText | |
72 | // filter for text/plain or uknown | |
73 | //-------------------------------------------------------------------------------- | |
74 | ||
75 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterPlainText, wxHtmlFilter) | |
76 | ||
a4c97004 | 77 | bool wxHtmlFilterPlainText::CanRead(const wxFSFile& WXUNUSED(file)) const |
5526e819 VS |
78 | { |
79 | return TRUE; | |
80 | } | |
81 | ||
82 | ||
83 | ||
420ec58a | 84 | wxString wxHtmlFilterPlainText::ReadFile(const wxFSFile& file) const |
5526e819 VS |
85 | { |
86 | wxInputStream *s = file.GetStream(); | |
5526e819 VS |
87 | wxString doc, doc2; |
88 | ||
89 | if (s == NULL) return wxEmptyString; | |
eb37e1d2 | 90 | wxPrivate_ReadString(doc, s); |
5526e819 | 91 | |
eb37e1d2 | 92 | doc.Replace(wxT("&"), wxT("&"), TRUE); |
66a77a74 OK |
93 | doc.Replace(wxT("<"), wxT("<"), TRUE); |
94 | doc.Replace(wxT(">"), wxT(">"), TRUE); | |
5526e819 VS |
95 | doc2 = "<HTML><BODY><PRE>\n" + doc + "\n</PRE></BODY></HTML>"; |
96 | return doc2; | |
97 | } | |
98 | ||
99 | ||
100 | ||
101 | ||
102 | ||
103 | //-------------------------------------------------------------------------------- | |
104 | // wxHtmlFilterImage | |
105 | // filter for image/* | |
106 | //-------------------------------------------------------------------------------- | |
107 | ||
108 | class wxHtmlFilterImage : public wxHtmlFilter | |
109 | { | |
110 | DECLARE_DYNAMIC_CLASS(wxHtmlFilterImage) | |
111 | ||
112 | public: | |
420ec58a VS |
113 | virtual bool CanRead(const wxFSFile& file) const; |
114 | virtual wxString ReadFile(const wxFSFile& file) const; | |
5526e819 VS |
115 | }; |
116 | ||
117 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterImage, wxHtmlFilter) | |
118 | ||
119 | ||
120 | ||
420ec58a | 121 | bool wxHtmlFilterImage::CanRead(const wxFSFile& file) const |
5526e819 | 122 | { |
0413cec5 | 123 | return (file.GetMimeType().Left(6) == wxT("image/")); |
5526e819 VS |
124 | } |
125 | ||
126 | ||
127 | ||
420ec58a | 128 | wxString wxHtmlFilterImage::ReadFile(const wxFSFile& file) const |
5526e819 VS |
129 | { |
130 | return ("<HTML><BODY><IMG SRC=\"" + file.GetLocation() + "\"></BODY></HTML>"); | |
131 | } | |
132 | ||
133 | ||
134 | ||
135 | ||
136 | //-------------------------------------------------------------------------------- | |
137 | // wxHtmlFilterPlainText | |
138 | // filter for text/plain or uknown | |
139 | //-------------------------------------------------------------------------------- | |
140 | ||
141 | class wxHtmlFilterHTML : public wxHtmlFilter | |
142 | { | |
143 | DECLARE_DYNAMIC_CLASS(wxHtmlFilterHTML) | |
144 | ||
145 | public: | |
420ec58a VS |
146 | virtual bool CanRead(const wxFSFile& file) const; |
147 | virtual wxString ReadFile(const wxFSFile& file) const; | |
5526e819 VS |
148 | }; |
149 | ||
150 | ||
151 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterHTML, wxHtmlFilter) | |
152 | ||
420ec58a | 153 | bool wxHtmlFilterHTML::CanRead(const wxFSFile& file) const |
5526e819 | 154 | { |
f61815af GL |
155 | // return (file.GetMimeType() == "text/html"); |
156 | // This is true in most case but some page can return: | |
157 | // "text/html; char-encoding=...." | |
158 | // So we use Find instead | |
77611ad4 | 159 | return (file.GetMimeType().Find(wxT("text/html")) == 0); |
5526e819 VS |
160 | } |
161 | ||
162 | ||
163 | ||
420ec58a | 164 | wxString wxHtmlFilterHTML::ReadFile(const wxFSFile& file) const |
5526e819 VS |
165 | { |
166 | wxInputStream *s = file.GetStream(); | |
5526e819 VS |
167 | wxString doc; |
168 | ||
04dbb646 | 169 | if (s == NULL) |
f3c82859 | 170 | { |
f6bcfd97 | 171 | wxLogError(_("Cannot open HTML document: %s"), file.GetLocation().c_str()); |
f3c82859 VS |
172 | return wxEmptyString; |
173 | } | |
eb37e1d2 | 174 | wxPrivate_ReadString(doc, s); |
5526e819 | 175 | |
04dbb646 | 176 | // add meta tag if we obtained this through http: |
981e62aa VS |
177 | if (file.GetMimeType().Find(_T("; charset=")) == 0) |
178 | { | |
179 | wxString s(_T("<meta http-equiv=\"Content-Type\" content=\"")); | |
180 | s << file.GetMimeType() << _T("\">"); | |
7ee77720 | 181 | return s+doc; |
981e62aa VS |
182 | } |
183 | ||
3ca6a5f0 | 184 | return doc; |
5526e819 VS |
185 | } |
186 | ||
187 | ||
188 | ||
189 | ||
190 | ///// Module: | |
191 | ||
192 | class wxHtmlFilterModule : public wxModule | |
193 | { | |
194 | DECLARE_DYNAMIC_CLASS(wxHtmlFilterModule) | |
195 | ||
196 | public: | |
197 | virtual bool OnInit() | |
198 | { | |
199 | wxHtmlWindow::AddFilter(new wxHtmlFilterHTML); | |
200 | wxHtmlWindow::AddFilter(new wxHtmlFilterImage); | |
201 | return TRUE; | |
202 | } | |
203 | virtual void OnExit() {} | |
204 | }; | |
205 | ||
206 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterModule, wxModule) | |
207 | ||
8dd71e2b | 208 | #endif |