]>
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/sstream.h" 
  25 #include "wx/html/htmlfilt.h" 
  26 #include "wx/html/htmlwin.h" 
  28 // utility function: read entire contents of an wxInputStream into a wxString 
  30 // TODO: error handling? 
  31 static void ReadString(wxString
& str
, wxInputStream
* s
, wxMBConv
& conv
) 
  33     wxStringOutputStream 
out(&str
, conv
); 
  39 There is code for several default filters: 
  43 IMPLEMENT_ABSTRACT_CLASS(wxHtmlFilter
, wxObject
) 
  45 //-------------------------------------------------------------------------------- 
  46 // wxHtmlFilterPlainText 
  47 //          filter for text/plain or uknown 
  48 //-------------------------------------------------------------------------------- 
  50 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterPlainText
, wxHtmlFilter
) 
  52 bool wxHtmlFilterPlainText::CanRead(const wxFSFile
& WXUNUSED(file
)) const 
  59 wxString 
wxHtmlFilterPlainText::ReadFile(const wxFSFile
& file
) const 
  61     wxInputStream 
*s 
= file
.GetStream(); 
  64     if (s 
== NULL
) return wxEmptyString
; 
  65     ReadString(doc
, s
, wxConvISO8859_1
); 
  67     doc
.Replace(wxT("&"), wxT("&"), true); 
  68     doc
.Replace(wxT("<"), wxT("<"), true); 
  69     doc
.Replace(wxT(">"), wxT(">"), true); 
  70     doc2 
= wxT("<HTML><BODY><PRE>\n") + doc 
+ wxT("\n</PRE></BODY></HTML>"); 
  78 //-------------------------------------------------------------------------------- 
  81 //-------------------------------------------------------------------------------- 
  83 class wxHtmlFilterImage 
: public wxHtmlFilter
 
  85     DECLARE_DYNAMIC_CLASS(wxHtmlFilterImage
) 
  88         virtual bool CanRead(const wxFSFile
& file
) const; 
  89         virtual wxString 
ReadFile(const wxFSFile
& file
) const; 
  92 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterImage
, wxHtmlFilter
) 
  96 bool wxHtmlFilterImage::CanRead(const wxFSFile
& file
) const 
  98     return (file
.GetMimeType().Left(6) == wxT("image/")); 
 103 wxString 
wxHtmlFilterImage::ReadFile(const wxFSFile
& file
) const 
 105     wxString res 
= wxT("<HTML><BODY><IMG SRC=\"") + file
.GetLocation() + wxT("\"></BODY></HTML>"); 
 112 //-------------------------------------------------------------------------------- 
 114 //          filter for text/html 
 115 //-------------------------------------------------------------------------------- 
 118 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterHTML
, wxHtmlFilter
) 
 120 bool wxHtmlFilterHTML::CanRead(const wxFSFile
& file
) const 
 122 //    return (file.GetMimeType() == "text/html"); 
 123 // This is true in most case but some page can return: 
 124 // "text/html; char-encoding=...." 
 125 // So we use Find instead 
 126   return (file
.GetMimeType().Find(wxT("text/html")) == 0); 
 131 wxString 
wxHtmlFilterHTML::ReadFile(const wxFSFile
& file
) const 
 133     wxInputStream 
*s 
= file
.GetStream(); 
 138         wxLogError(_("Cannot open HTML document: %s"), file
.GetLocation().c_str()); 
 139         return wxEmptyString
; 
 142     // NB: We convert input file to wchar_t here in Unicode mode, based on 
 143     //     either Content-Type header or <meta> tags. In ANSI mode, we don't 
 144     //     do it as it is done by wxHtmlParser (for this reason, we add <meta> 
 145     //     tag if we used Content-Type header). 
 148     if ((charsetPos 
= file
.GetMimeType().Find(wxT("; charset="))) != wxNOT_FOUND
) 
 150         wxString charset 
= file
.GetMimeType().Mid(charsetPos 
+ 10); 
 151         wxCSConv 
conv(charset
); 
 152         ReadString(doc
, s
, conv
); 
 156         size_t size 
= s
->GetSize(); 
 157         wxCharBuffer 
buf( size
+1 ); 
 158         s
->Read( buf
.data(), size 
); 
 159         *(buf
.data() + size
) = 0; 
 160         wxString 
tmpdoc( buf
, wxConvISO8859_1
); 
 162         wxString charset 
= wxHtmlParser::ExtractCharsetInformation(tmpdoc
); 
 167             wxCSConv 
conv(charset
); 
 168             doc 
= wxString( buf
, conv 
); 
 171 #else // !wxUSE_UNICODE 
 172     ReadString(doc
, s
, wxConvLibc
); 
 173     // add meta tag if we obtained this through http: 
 174     if (!file
.GetMimeType().empty()) 
 177         wxString mime 
= file
.GetMimeType(); 
 178         hdr
.Printf(wxT("<meta http-equiv=\"Content-Type\" content=\"%s\">"), mime
.c_str()); 
 191 class wxHtmlFilterModule 
: public wxModule
 
 193     DECLARE_DYNAMIC_CLASS(wxHtmlFilterModule
) 
 196         virtual bool OnInit() 
 198             wxHtmlWindow::AddFilter(new wxHtmlFilterHTML
); 
 199             wxHtmlWindow::AddFilter(new wxHtmlFilterImage
); 
 202         virtual void OnExit() {} 
 205 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterModule
, wxModule
)