TerraLib and TerraView Wiki Page

This is an old revision of the document!


TerraLib.Core: URI

A classe te::core::URI fornece o suporte para manipulação de URIs (identificador uniforme de recurso) que é uma string utilizada para identificar ou denominar um recurso na Internet.

API

C++

A API para manipulação de URIs na TerraLib é definida pela classe URI, mostrada abaixo:

namespace te
{
  namespace core
  {
    /*!
      \class URI
 
      \brief A class for representing an Uniform Resource Identifier (URI).
     */
    class TECOREEXPORT URI
    {
      public:
 
        /*! \brief Default constructor. */
        URI();
 
        /*!
          \brief A constructor from a string.
 
           This constructor check the URI enconding,
           then parse it and validate.
 
           \param uri A string with the URI to be parsed.
 
           \exception URIException when the given URI isn't valid.
         */
        explicit URI(const std::string& uri);
 
        /*! \brief Copy constructor. */
        URI(const URI& other);
 
        /*! Default destructor. */
        ~URI() = default;
 
        /*! \brief Assingment operator. */
        URI& operator=(const URI& other);
 
        /*!
          \brief Retrieving the full URI
 
          \return Returns the complete URI.
         */
        const std::string& uri() const;
 
        /*!
          \brief Retrieving the scheme
 
          \return Returns the URI scheme.
         */
        std::string scheme() const;
 
        /*!
          \brief Retrieving the user information
 
          \return Returns the URI user information.
         */
        std::string user() const;
 
        /*!
          \brief Retrieving the password information
 
          \return Returns the URI password information.
         */
        std::string password() const;
 
        /*!
          \brief Retrieving the host
 
          \return Returns the URI host.
         */
        std::string host() const;
 
        /*!
          \brief Retrieving the port
 
          \return Returns the URI port.
         */
        std::string port() const;
 
        /*!
          \brief Retrieving the path
 
          \return Returns the URI path.
         */
        std::string path() const;
 
        /*!
         \brief Retrieving the query
 
         \return Returns the URI query.
         */
        std::string query() const;
 
        /*!
          \brief Retrieving the fragment
 
          \return Returns the URI fragment.
         */
        std::string fragment() const;
 
        /*!
          \brief Return if the given URI is valid or not.
 
         \return Returns true if the given URI is valid.
         */
        bool isValid() const;
 
    };
 
  }  // end namespace core
}    // end namespace te

Exemplos

A seguir alguns exemplos para ilustrar o uso da classe URI:

// TerraLib
#include <terralib/core/uri/URI.h>
 
// STL
#include <cassert>
#include <cstdlib>
#include <iostream>
 
int main(int argc, char *argv[])
{
  {
    te::core::URI uri("http://www.dpi.inpe.br/terralib5/wiki/doku.php?id=wiki:documentation:devguide#modules");
 
    if(!uri.isValid())
    {
      std::cout << "Uri isn't valid!" << std::endl;
      return EXIT_FAILURE;
    }
 
    assert(uri.scheme() == "http");
    assert(uri.user() == "");
    assert(uri.password() == "");
    assert(uri.host() == "www.dpi.inpe.br");
    assert(uri.port() == "");
    assert(uri.path() == "/terralib5/wiki/doku.php");
    assert(uri.query() == "id=wiki:documentation:devguide");
    assert(uri.fragment() == "modules");
  }
 
  {
    std::string address("xmpp:example-node@example.com?message;subject=Hello%20World");
 
    te::core::URI uri(address);
 
    if(!uri.isValid())
    {
      std::cout << "Uri isn't valid!" << std::endl;
      return EXIT_FAILURE;
    }
 
    assert(uri.scheme() == "xmpp");
    assert(uri.user() == "");
    assert(uri.password() == "");
    assert(uri.host() == "");
    assert(uri.port() == "");
    assert(uri.path() == "example-node@example.com");
    assert(uri.query() == "message;subject=Hello%20World");
    assert(uri.fragment() == "");
  }
 
  {
    te::core::URI uri("ftp://user:password@ftp.dpi.inpe.br:21/path/");
 
    if(!uri.isValid())
    {
      std::cout << "Uri isn't valid!" << std::endl;
      return EXIT_FAILURE;
    }
 
    assert(uri.scheme() == "ftp");
    assert(uri.user() == "user");
    assert(uri.password() == "password");
    assert(uri.host() == "ftp.dpi.inpe.br");
    assert(uri.port() == "21");
    assert(uri.path() == "/path/");
    assert(uri.query() == "");
    assert(uri.fragment() == "");
  }
 
  {
    te::core::URI uri("InvalidURI");
 
    if(uri.isValid())
    {
      std::cout << "Uri isn't valid!" << std::endl;
      return EXIT_FAILURE;
    }
  }
 
  return EXIT_SUCCESS;
}

Referências