Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

WebResourceProvider

, 23 Mar 2007 CPOL
A framework to allow public web services to be used as objects in your application.
// WebResourceProvider.h : header file
//
// Written by Ravi Bhavnani <ravib@ravib.com>
// Copyright (c) 2002.  All Rights Reserved.
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is 
// not sold for profit without the author's written consent, and 
// providing that this notice and the author's name and all copyright 
// notices remains intact. 
//
// An email letting me know how you are using it would be nice as well. 
//
// This code is provided "as is" with no expressed or implied warranty.
// The author accepts no liability for any damage/loss of business that
// this product may cause.
//
// A description of this object and instructions on how to use it are
// provided at http://www.codeproject.com/internet/WebResourceProvider.asp.
//
// Revision history:
//    See WebResourceProvider.cpp
//
//////////////////////////////////////////////////////////////////////

#ifndef _WebResourceProvider_h_
#define _WebResourceProvider_h_

//////////////////////////////////////////////////////////////////////
// CWebResourceProvider object
//

class CWebResourceProvider
{
/////////////////////////////////////////////////////////////////////////////
// Construction
public:
	CWebResourceProvider();
	virtual ~CWebResourceProvider();

/////////////////////////////////////////////////////////////////////////////
// Public methods (API)
public:
  void setAgent                       // Set agent name
    (CString strAgent)
    { m_strAgent = strAgent; }

  void setStatusWnd                   // Set status window
    (CWnd* pWndStatus)
    { m_pWndStatus = pWndStatus; }

  void fetchResource();               // Fetch resource

  DWORD getFetchStatus()              // Get fetch status
    { return m_dwFetchStatus; }

  CString getFetchError()             // Get fetch error message
    { return m_strFetchError; }

  CTime getFetchTime()                // Get time of last successful fetch
    { return m_tmFetchTime; }

  static bool urlExists               // checks if a URL exists
    (CString  strUrl);

/////////////////////////////////////////////////////////////////////////////
// Methods overridden by derived classes
public:
  virtual CString getName()           // Get provider name
    { return ""; }
  virtual CString getVersion()        // Get provider version
    { return ""; }
  virtual CString getCopyright()      // Get provider copyright
    { return ""; }
  virtual CString getOtherInfo()      // Get optional provider information
    { return ""; }

protected:
  virtual void constructUrl           // Construct the URL to be fetched
    (CString& strUrl) = 0;

  virtual bool isPost();              // Test if this is a POST request

  virtual void getPostData            // Get post data
    (CString& strPostData);

  virtual bool init();                // Initialize the provider

  virtual bool moreAvailable();       // Test if more data is available

  virtual void parseContent();        // Parse the fetched content

/////////////////////////////////////////////////////////////////////////////
// Helper methods used by derived classes
protected:
  bool at                             // check whether index is positioned
    (CString strText);                // at a string (case insensitive)

  bool atExact                        // check whether index is positioned
    (CString strText);                // at a string (case insensitive)

  bool skipTo                         // advance index to the next occurence
    (CString strText);                // of a string (case insensitive)

  bool skipToExact                    // advance index to the next occurence
    (CString strText);                // of a string (case sensitive)

  bool skipBackTo                     // retreat index to the previous occurence
    (CString strText);                // of a string (case insensitive)

  bool skipBackToExact                // retreat index to the previous occurence
    (CString strText);                // of a string (case sensitive)

  bool extractTo                      // extract text from current location to
    (CString  strTerminator,          // the start of a string and position index
     CString& strResult);             // just after string (case insensitive)

  bool extractToExact                 // extract text from current location to
    (CString  strTerminator,          // the start of a string and position index
     CString& strResult);             // just after string (case sensitive)

  void extractToEnd                   // extract text from current location to
    (CString& strResult);             // end of content

  long getIndex()                     // get current position
    { return m_nIndex; }

  void getLinks                       // get list of documents and images linked in content
    (CStringArray& documents,
     CStringArray& images);

  void resetIndex();                  // reset index to start of content

  CString replaceEvery                // case-sensitive text replacement
    (LPCTSTR  strTarget,
     LPCTSTR  strOccurence,
     LPCTSTR  strWith);

  void removeComments();              // remove comments from content

  void removeScripts();               // remove scripts from content

  void removeEnclosingAnchorTag       // remove enclosing <A> tag from string
    (CString& strText);

  void removeEnclosingQuotes          // remove enclosing quotes from string
    (CString& strText);

  void removeHtml                     // remove all HTML from string
    (CString& strText);

  void trim                           // remove leading and trailing whitespace
    (CString& strText);               // from string

/////////////////////////////////////////////////////////////////////////////
// Implementation
protected:
  static long findNoCase              // searches (case-insensitively) for a
    (CString  strString,              // substring within a string
     CString  strSubstring,
     long     nStart);

  long findStringInArray              // searches a string array for an exact match
    (CStringArray&  stringArray,
     CString        string);

  void getContent                     // retrieves the URL from the web
    (CString strUrl);

/////////////////////////////////////////////////////////////////////////////
// Members
protected:
  DWORD     m_dwFetchStatus;    // fetch status
  long      m_nIndex;           // current location
  CString   m_strAgent;         // agent name
  CString   m_strContent;       // fetched content
  CString   m_strFetchError;    // fetch error
  CString   m_strUrl;           // target url
  CWnd*     m_pWndStatus;       // status window
  CTime     m_tmFetchTime;      // fetch timestamp
};

#endif

// End WebResourceProvider.h

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ravi Bhavnani
Technical Lead
Canada Canada
Ravi Bhavnani is an ardent fan of Microsoft technologies who loves building Windows apps, especially PIMs, system utilities, and things that go bump on the Internet. During his career, Ravi has developed expert systems, desktop imaging apps, marketing automation software, EDA tools, a platform to help people find, analyze and understand information, trading software for institutional investors and advanced data visualization solutions. He currently works for a company that provides enterprise workforce management solutions to large clients.
 
His interests include the .NET framework, reasoning systems, financial analysis and algorithmic trading, NLP, HCI and UI design. Ravi holds a BS in Physics and Math and an MS in Computer Science and was a Microsoft MVP (C++ and C# in 2006 and 2007). He is also the co-inventor of 2 patents on software security and generating data visualization dashboards. His claim to fame is that he crafted CodeProject's "joke" forum post icon.
 
Ravi's biggest fear is that one day he might actually get a life, although the chances of that happening seem extremely remote.
Follow on   Google+   LinkedIn

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141216.1 | Last Updated 23 Mar 2007
Article Copyright 2002 by Ravi Bhavnani
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid