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