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