| 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 |
| 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("<"), TRUE); |
| 68 | doc.Replace(wxT(">"), wxT(">"), 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) == "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) return wxEmptyString; |
| 145 | src = new char[s -> GetSize() + 1]; |
| 146 | src[s -> GetSize()] = 0; |
| 147 | s -> Read(src, s -> GetSize()); |
| 148 | doc = src; |
| 149 | delete[] src; |
| 150 | |
| 151 | return doc; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | ///// Module: |
| 158 | |
| 159 | class wxHtmlFilterModule : public wxModule |
| 160 | { |
| 161 | DECLARE_DYNAMIC_CLASS(wxHtmlFilterModule) |
| 162 | |
| 163 | public: |
| 164 | virtual bool OnInit() |
| 165 | { |
| 166 | wxHtmlWindow::AddFilter(new wxHtmlFilterHTML); |
| 167 | wxHtmlWindow::AddFilter(new wxHtmlFilterImage); |
| 168 | return TRUE; |
| 169 | } |
| 170 | virtual void OnExit() {} |
| 171 | }; |
| 172 | |
| 173 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterModule, wxModule) |
| 174 | |
| 175 | #endif |