]>
git.saurik.com Git - wxWidgets.git/blob - src/html/htmlfilt.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/html/htmlfilt.cpp
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 /////////////////////////////////////////////////////////////////////////////
10 #include "wx/wxprec.h"
16 #if wxUSE_HTML && wxUSE_STREAMS
23 #include "wx/strconv.h"
24 #include "wx/html/htmlfilt.h"
25 #include "wx/html/htmlwin.h"
27 // utility function: read a wxString from a wxInputStream
28 static void ReadString(wxString
& str
, wxInputStream
* s
, wxMBConv
& conv
)
30 size_t streamSize
= s
->GetSize();
32 if (streamSize
== ~(size_t)0)
34 const size_t bufSize
= 4095;
35 char buffer
[bufSize
+1];
40 s
->Read(buffer
, bufSize
);
41 lastRead
= s
->LastRead();
43 str
.Append(wxString(buffer
, conv
));
45 while (lastRead
== bufSize
);
49 char* src
= new char[streamSize
+1];
50 s
->Read(src
, streamSize
);
52 str
= wxString(src
, conv
);
59 There is code for several default filters:
63 IMPLEMENT_ABSTRACT_CLASS(wxHtmlFilter
, wxObject
)
65 //--------------------------------------------------------------------------------
66 // wxHtmlFilterPlainText
67 // filter for text/plain or uknown
68 //--------------------------------------------------------------------------------
70 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterPlainText
, wxHtmlFilter
)
72 bool wxHtmlFilterPlainText::CanRead(const wxFSFile
& WXUNUSED(file
)) const
79 wxString
wxHtmlFilterPlainText::ReadFile(const wxFSFile
& file
) const
81 wxInputStream
*s
= file
.GetStream();
84 if (s
== NULL
) return wxEmptyString
;
85 ReadString(doc
, s
, wxConvISO8859_1
);
87 doc
.Replace(wxT("&"), wxT("&"), true);
88 doc
.Replace(wxT("<"), wxT("<"), true);
89 doc
.Replace(wxT(">"), wxT(">"), true);
90 doc2
= wxT("<HTML><BODY><PRE>\n") + doc
+ wxT("\n</PRE></BODY></HTML>");
98 //--------------------------------------------------------------------------------
100 // filter for image/*
101 //--------------------------------------------------------------------------------
103 class wxHtmlFilterImage
: public wxHtmlFilter
105 DECLARE_DYNAMIC_CLASS(wxHtmlFilterImage
)
108 virtual bool CanRead(const wxFSFile
& file
) const;
109 virtual wxString
ReadFile(const wxFSFile
& file
) const;
112 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterImage
, wxHtmlFilter
)
116 bool wxHtmlFilterImage::CanRead(const wxFSFile
& file
) const
118 return (file
.GetMimeType().Left(6) == wxT("image/"));
123 wxString
wxHtmlFilterImage::ReadFile(const wxFSFile
& file
) const
125 wxString res
= wxT("<HTML><BODY><IMG SRC=\"") + file
.GetLocation() + wxT("\"></BODY></HTML>");
132 //--------------------------------------------------------------------------------
134 // filter for text/html
135 //--------------------------------------------------------------------------------
138 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterHTML
, wxHtmlFilter
)
140 bool wxHtmlFilterHTML::CanRead(const wxFSFile
& file
) const
142 // return (file.GetMimeType() == "text/html");
143 // This is true in most case but some page can return:
144 // "text/html; char-encoding=...."
145 // So we use Find instead
146 return (file
.GetMimeType().Find(wxT("text/html")) == 0);
151 wxString
wxHtmlFilterHTML::ReadFile(const wxFSFile
& file
) const
153 wxInputStream
*s
= file
.GetStream();
158 wxLogError(_("Cannot open HTML document: %s"), file
.GetLocation().c_str());
159 return wxEmptyString
;
162 // NB: We convert input file to wchar_t here in Unicode mode, based on
163 // either Content-Type header or <meta> tags. In ANSI mode, we don't
164 // do it as it is done by wxHtmlParser (for this reason, we add <meta>
165 // tag if we used Content-Type header).
168 if ((charsetPos
= file
.GetMimeType().Find(_T("; charset="))) != wxNOT_FOUND
)
170 wxString charset
= file
.GetMimeType().Mid(charsetPos
+ 10);
171 wxCSConv
conv(charset
);
172 ReadString(doc
, s
, conv
);
177 ReadString(tmpdoc
, s
, wxConvISO8859_1
);
178 wxString charset
= wxHtmlParser::ExtractCharsetInformation(tmpdoc
);
183 wxCSConv
conv(charset
);
184 doc
= wxString(tmpdoc
.mb_str(wxConvISO8859_1
), conv
);
187 #else // !wxUSE_UNICODE
188 ReadString(doc
, s
, wxConvLibc
);
189 // add meta tag if we obtained this through http:
190 if (!file
.GetMimeType().empty())
193 wxString mime
= file
.GetMimeType();
194 hdr
.Printf(_T("<meta http-equiv=\"Content-Type\" content=\"%s\">"), mime
.c_str());
207 class wxHtmlFilterModule
: public wxModule
209 DECLARE_DYNAMIC_CLASS(wxHtmlFilterModule
)
212 virtual bool OnInit()
214 wxHtmlWindow::AddFilter(new wxHtmlFilterHTML
);
215 wxHtmlWindow::AddFilter(new wxHtmlFilterImage
);
218 virtual void OnExit() {}
221 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterModule
, wxModule
)