examples/core/uri/main.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/examples/core/uri/main.cpp
22 
23  \brief Examples for the Terralib URI API
24 
25  \author Vinicius Campanha
26 */
27 
28 // TerraLib
29 #include <terralib/Exception.h>
30 #include <terralib/core/uri/URI.h>
32 
33 // STL
34 #include <cassert>
35 #include <cstdlib>
36 #include <iostream>
37 
38 int main(int argc, char *argv[])
39 {
40  {
41  te::core::URI uri("http://www.dpi.inpe.br/terralib5/wiki/doku.php?id=wiki:documentation:devguide#modules");
42 
43  if(!uri.isValid())
44  {
45  std::cout << "Uri isn't valid!" << std::endl;
46  return EXIT_FAILURE;
47  }
48 
49  assert(uri.scheme() == "http");
50  assert(uri.user() == "");
51  assert(uri.password() == "");
52  assert(uri.host() == "www.dpi.inpe.br");
53  assert(uri.port() == "");
54  assert(uri.path() == "/terralib5/wiki/doku.php");
55  assert(uri.query() == "id=wiki:documentation:devguide");
56  assert(uri.fragment() == "modules");
57  }
58 
59  {
60  std::string address("xmpp:example-node@example.com?message;subject=Hello%20World");
61 
62  te::core::URI uri(address);
63 
64  if(!uri.isValid())
65  {
66  std::cout << "Uri isn't valid!" << std::endl;
67  return EXIT_FAILURE;
68  }
69 
70  assert(uri.scheme() == "xmpp");
71  assert(uri.user() == "");
72  assert(uri.password() == "");
73  assert(uri.host() == "");
74  assert(uri.port() == "");
75  assert(uri.path() == "example-node@example.com");
76  assert(uri.query() == "message;subject=Hello%20World");
77  assert(uri.fragment() == "");
78  }
79 
80  {
81  te::core::URI uri("ftp://user:password@ftp.ftp.inpe.br:21/path/");
82 
83  if(!uri.isValid())
84  {
85  std::cout << "Uri isn't valid!" << std::endl;
86  return EXIT_FAILURE;
87  }
88 
89  assert(uri.scheme() == "ftp");
90  assert(uri.user() == "user");
91  assert(uri.password() == "password");
92  assert(uri.host() == "ftp.ftp.inpe.br");
93  assert(uri.port() == "21");
94  assert(uri.path() == "/path/");
95  assert(uri.query() == "");
96  assert(uri.fragment() == "");
97  }
98 
99  {
100  te::core::URI uri("InvalidURI");
101 
102  if(uri.isValid())
103  {
104  std::cout << "Uri isn't valid!" << std::endl;
105  return EXIT_FAILURE;
106  }
107  }
108 
109  {
110  std::string uri("http%3A%2F%2Fmapas.mma.gov.br%2Fi3geo%2Fogc.php%3Ftema%3Dtransporte%26");
111 
112  std::string decodedUri = te::core::URIDecode(uri);
113 
114  std::string encodedUri = te::core::URIEncode(decodedUri);
115 
116  assert(decodedUri == "http://mapas.mma.gov.br/i3geo/ogc.php?tema=transporte&");
117  assert(encodedUri == "http%3A%2F%2Fmapas.mma.gov.br%2Fi3geo%2Fogc.php%3Ftema%3Dtransporte%26");
118  }
119 
120  return EXIT_SUCCESS;
121 }
std::string path() const
Retrieving the path.
Definition: URI.cpp:118
std::string scheme() const
Retrieving the scheme.
Definition: URI.cpp:93
TECOREEXPORT std::string URIEncode(const std::string &srcUri)
Encodes an decoded URI. The algorithm implementation is based on http://www.codeguru.com/cpp/cpp/algorithms/strings/article.php/c12759/URI-Encoding-and-Decoding.htm.
std::string fragment() const
Retrieving the fragment.
Definition: URI.cpp:128
std::string password() const
Retrieving the password information.
Definition: URI.cpp:103
bool isValid() const
Return if the given URI is valid or not.
Definition: URI.cpp:133
std::string query() const
Retrieving the query.
Definition: URI.cpp:123
std::string port() const
Retrieving the port.
Definition: URI.cpp:113
std::string host() const
Retrieving the host.
Definition: URI.cpp:108
A class for representing an Uniform Resource Identifier (URI).
Definition: URI.h:49
This file contains utility functions used to manipulate data from a URI.
int main(int argc, char *argv[])
A class for representing an Uniform Resource Identifier (URI).
TECOREEXPORT std::string URIDecode(const std::string &srcUri)
Decodes an encoded URI. The algorithm implementation is based on http://www.codeguru.com/cpp/cpp/algorithms/strings/article.php/c12759/URI-Encoding-and-Decoding.htm.
std::string user() const
Retrieving the user information.
Definition: URI.cpp:98