]>
git.saurik.com Git - wxWidgets.git/blob - src/html/htmlfilt.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtmlFilter - input filter for translating into HTML format
4 // Author: Vaclav Slavik
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation
15 #include "wx/wxprec.h"
18 #if wxUSE_HTML && wxUSE_STREAMS
29 #include "wx/html/htmlfilt.h"
30 #include "wx/html/htmlwin.h"
32 // utility function: read a wxString from a wxInputStream
33 void wxPrivate_ReadString(wxString
& str
, wxInputStream
* s
)
35 size_t streamSize
= s
->GetSize();
37 if(streamSize
== ~(size_t)0)
39 const size_t bufSize
= 4095;
40 char buffer
[bufSize
+1];
45 s
->Read(buffer
, bufSize
);
46 lastRead
= s
->LastRead();
50 while(lastRead
== bufSize
);
54 char* src
= new char[streamSize
+1];
55 s
->Read(src
, streamSize
);
64 There is code for several default filters:
68 IMPLEMENT_ABSTRACT_CLASS(wxHtmlFilter
, wxObject
)
70 //--------------------------------------------------------------------------------
71 // wxHtmlFilterPlainText
72 // filter for text/plain or uknown
73 //--------------------------------------------------------------------------------
75 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterPlainText
, wxHtmlFilter
)
77 bool wxHtmlFilterPlainText::CanRead(const wxFSFile
& WXUNUSED(file
)) const
84 wxString
wxHtmlFilterPlainText::ReadFile(const wxFSFile
& file
) const
86 wxInputStream
*s
= file
.GetStream();
89 if (s
== NULL
) return wxEmptyString
;
90 wxPrivate_ReadString(doc
, s
);
92 doc
.Replace(wxT("&"), wxT("&"), TRUE
);
93 doc
.Replace(wxT("<"), wxT("<"), TRUE
);
94 doc
.Replace(wxT(">"), wxT(">"), TRUE
);
95 doc2
= "<HTML><BODY><PRE>\n" + doc
+ "\n</PRE></BODY></HTML>";
103 //--------------------------------------------------------------------------------
105 // filter for image/*
106 //--------------------------------------------------------------------------------
108 class wxHtmlFilterImage
: public wxHtmlFilter
110 DECLARE_DYNAMIC_CLASS(wxHtmlFilterImage
)
113 virtual bool CanRead(const wxFSFile
& file
) const;
114 virtual wxString
ReadFile(const wxFSFile
& file
) const;
117 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterImage
, wxHtmlFilter
)
121 bool wxHtmlFilterImage::CanRead(const wxFSFile
& file
) const
123 return (file
.GetMimeType().Left(6) == wxT("image/"));
128 wxString
wxHtmlFilterImage::ReadFile(const wxFSFile
& file
) const
130 return ("<HTML><BODY><IMG SRC=\"" + file
.GetLocation() + "\"></BODY></HTML>");
136 //--------------------------------------------------------------------------------
137 // wxHtmlFilterPlainText
138 // filter for text/plain or uknown
139 //--------------------------------------------------------------------------------
141 class wxHtmlFilterHTML
: public wxHtmlFilter
143 DECLARE_DYNAMIC_CLASS(wxHtmlFilterHTML
)
146 virtual bool CanRead(const wxFSFile
& file
) const;
147 virtual wxString
ReadFile(const wxFSFile
& file
) const;
151 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterHTML
, wxHtmlFilter
)
153 bool wxHtmlFilterHTML::CanRead(const wxFSFile
& file
) const
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
159 return (file
.GetMimeType().Find(wxT("text/html")) == 0);
164 wxString
wxHtmlFilterHTML::ReadFile(const wxFSFile
& file
) const
166 wxInputStream
*s
= file
.GetStream();
171 wxLogError(_("Cannot open HTML document: %s"), file
.GetLocation().c_str());
172 return wxEmptyString
;
174 wxPrivate_ReadString(doc
, s
);
176 // add meta tag if we obtained this through http:
177 if (file
.GetMimeType().Find(_T("; charset=")) == 0)
179 wxString
s(_T("<meta http-equiv=\"Content-Type\" content=\""));
180 s
<< file
.GetMimeType() << _T("\">");
192 class wxHtmlFilterModule
: public wxModule
194 DECLARE_DYNAMIC_CLASS(wxHtmlFilterModule
)
197 virtual bool OnInit()
199 wxHtmlWindow::AddFilter(new wxHtmlFilterHTML
);
200 wxHtmlWindow::AddFilter(new wxHtmlFilterImage
);
203 virtual void OnExit() {}
206 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterModule
, wxModule
)