]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmlfilt.cpp
added new files to be included in the installer
[wxWidgets.git] / src / html / htmlfilt.cpp
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 #ifdef __GNUG__
12 #pragma implementation
13 #endif
14
15 #include "wx/wxprec.h"
16
17 #include "wx/defs.h"
18 #if wxUSE_HTML && wxUSE_STREAMS
19
20 #ifdef __BORDLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WXPRECOMP
25 #include "wx/wx.h"
26 #endif
27
28 #include "wx/html/htmlfilt.h"
29 #include "wx/html/htmlwin.h"
30
31
32 /*
33
34 There is code for several default filters:
35
36 */
37
38 IMPLEMENT_ABSTRACT_CLASS(wxHtmlFilter, wxObject)
39
40 //--------------------------------------------------------------------------------
41 // wxHtmlFilterPlainText
42 // filter for text/plain or uknown
43 //--------------------------------------------------------------------------------
44
45 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterPlainText, wxHtmlFilter)
46
47 bool wxHtmlFilterPlainText::CanRead(const wxFSFile& WXUNUSED(file)) const
48 {
49 return TRUE;
50 }
51
52
53
54 wxString wxHtmlFilterPlainText::ReadFile(const wxFSFile& file) const
55 {
56 wxInputStream *s = file.GetStream();
57 char *src;
58 wxString doc, doc2;
59
60 if (s == NULL) return wxEmptyString;
61 src = new char[s->GetSize()+1];
62 src[s->GetSize()] = 0;
63 s->Read(src, s->GetSize());
64 doc = src;
65 delete [] src;
66
67 doc.Replace(wxT("<"), wxT("&lt;"), TRUE);
68 doc.Replace(wxT(">"), wxT("&gt;"), TRUE);
69 doc2 = "<HTML><BODY><PRE>\n" + doc + "\n</PRE></BODY></HTML>";
70 return doc2;
71 }
72
73
74
75
76
77 //--------------------------------------------------------------------------------
78 // wxHtmlFilterImage
79 // filter for image/*
80 //--------------------------------------------------------------------------------
81
82 class wxHtmlFilterImage : public wxHtmlFilter
83 {
84 DECLARE_DYNAMIC_CLASS(wxHtmlFilterImage)
85
86 public:
87 virtual bool CanRead(const wxFSFile& file) const;
88 virtual wxString ReadFile(const wxFSFile& file) const;
89 };
90
91 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterImage, wxHtmlFilter)
92
93
94
95 bool wxHtmlFilterImage::CanRead(const wxFSFile& file) const
96 {
97 return (file.GetMimeType().Left(6) == wxT("image/"));
98 }
99
100
101
102 wxString wxHtmlFilterImage::ReadFile(const wxFSFile& file) const
103 {
104 return ("<HTML><BODY><IMG SRC=\"" + file.GetLocation() + "\"></BODY></HTML>");
105 }
106
107
108
109
110 //--------------------------------------------------------------------------------
111 // wxHtmlFilterPlainText
112 // filter for text/plain or uknown
113 //--------------------------------------------------------------------------------
114
115 class wxHtmlFilterHTML : public wxHtmlFilter
116 {
117 DECLARE_DYNAMIC_CLASS(wxHtmlFilterHTML)
118
119 public:
120 virtual bool CanRead(const wxFSFile& file) const;
121 virtual wxString ReadFile(const wxFSFile& file) const;
122 };
123
124
125 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterHTML, wxHtmlFilter)
126
127 bool wxHtmlFilterHTML::CanRead(const wxFSFile& file) const
128 {
129 // return (file.GetMimeType() == "text/html");
130 // This is true in most case but some page can return:
131 // "text/html; char-encoding=...."
132 // So we use Find instead
133 return (file.GetMimeType().Find(wxT("text/html")) == 0);
134 }
135
136
137
138 wxString wxHtmlFilterHTML::ReadFile(const wxFSFile& file) const
139 {
140 wxInputStream *s = file.GetStream();
141 char *src;
142 wxString doc;
143
144 if (s == NULL)
145 {
146 wxLogError(_("Cannot open HTML document: %s"), file.GetLocation().c_str());
147 return wxEmptyString;
148 }
149 src = new char[s->GetSize() + 1];
150 src[s->GetSize()] = 0;
151 s->Read(src, s->GetSize());
152 doc = src;
153 delete[] src;
154
155 // add meta tag if we obtained this through http:
156 if (file.GetMimeType().Find(_T("; charset=")) == 0)
157 {
158 wxString s(_T("<meta http-equiv=\"Content-Type\" content=\""));
159 s << file.GetMimeType() << _T("\">");
160 return s;
161 }
162
163 return doc;
164 }
165
166
167
168
169 ///// Module:
170
171 class wxHtmlFilterModule : public wxModule
172 {
173 DECLARE_DYNAMIC_CLASS(wxHtmlFilterModule)
174
175 public:
176 virtual bool OnInit()
177 {
178 wxHtmlWindow::AddFilter(new wxHtmlFilterHTML);
179 wxHtmlWindow::AddFilter(new wxHtmlFilterImage);
180 return TRUE;
181 }
182 virtual void OnExit() {}
183 };
184
185 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterModule, wxModule)
186
187 #endif