URIStreamOpener.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // URIStreamOpener.h
  3. //
  4. // Library: Foundation
  5. // Package: URI
  6. // Module: URIStreamOpener
  7. //
  8. // Definition of the URIStreamOpener class.
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Foundation_URIStreamOpener_INCLUDED
  16. #define Foundation_URIStreamOpener_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include "Poco/Mutex.h"
  19. #include <istream>
  20. #include <map>
  21. namespace Poco {
  22. class URI;
  23. class URIStreamFactory;
  24. class Path;
  25. class Foundation_API URIStreamOpener
  26. /// The URIStreamOpener class is used to create and open input streams
  27. /// for resourced identified by Uniform Resource Identifiers.
  28. ///
  29. /// For every URI scheme used, a URIStreamFactory must be registered.
  30. /// A FileStreamFactory is automatically registered for file URIs.
  31. {
  32. public:
  33. enum
  34. {
  35. MAX_REDIRECTS = 10
  36. };
  37. URIStreamOpener();
  38. /// Creates the URIStreamOpener and registers a FileStreamFactory
  39. /// for file URIs.
  40. ~URIStreamOpener();
  41. /// Destroys the URIStreamOpener and deletes all registered
  42. /// URI stream factories.
  43. std::istream* open(const URI& uri) const;
  44. /// Tries to create and open an input stream for the resource specified
  45. /// by the given uniform resource identifier.
  46. ///
  47. /// If no URIStreamFactory has been registered for the URI's
  48. /// scheme, a UnknownURIScheme exception is thrown.
  49. /// If the stream cannot be opened for any reason, an
  50. /// IOException is thrown.
  51. ///
  52. /// The given URI must be a valid one. This excludes file system paths.
  53. ///
  54. /// Whoever calls the method is responsible for deleting
  55. /// the returned stream.
  56. std::istream* open(const std::string& pathOrURI) const;
  57. /// Tries to create and open an input stream for the resource specified
  58. /// by the given path or uniform resource identifier.
  59. ///
  60. /// If the stream cannot be opened for any reason, an
  61. /// Exception is thrown.
  62. ///
  63. /// The method first tries to interpret the given pathOrURI as an URI.
  64. /// If this fails, the pathOrURI is treated as local filesystem path.
  65. /// If this also fails, an exception is thrown.
  66. ///
  67. /// Whoever calls the method is responsible for deleting
  68. /// the returned stream.
  69. std::istream* open(const std::string& basePathOrURI, const std::string& pathOrURI) const;
  70. /// Tries to create and open an input stream for the resource specified
  71. /// by the given path or uniform resource identifier.
  72. ///
  73. /// pathOrURI is resolved against basePathOrURI (see URI::resolve() and
  74. /// Path::resolve() for more information).
  75. ///
  76. /// If the stream cannot be opened for any reason, an
  77. /// Exception is thrown.
  78. ///
  79. /// Whoever calls the method is responsible for deleting
  80. /// the returned stream.
  81. void registerStreamFactory(const std::string& scheme, URIStreamFactory* pFactory);
  82. /// Registers a URIStreamFactory for the given scheme. If another factory
  83. /// has already been registered for the scheme, an ExistsException is thrown.
  84. ///
  85. /// The URIStreamOpener takes ownership of the factory and deletes it when it is
  86. /// no longer needed (in other words, when the URIStreamOpener is deleted).
  87. void unregisterStreamFactory(const std::string& scheme);
  88. /// Unregisters and deletes the URIStreamFactory for the given scheme.
  89. ///
  90. /// Throws a NotFoundException if no URIStreamFactory has been registered
  91. /// for the given scheme.
  92. bool supportsScheme(const std::string& scheme);
  93. /// Returns true iff a URIStreamFactory for the given scheme
  94. /// has been registered.
  95. static URIStreamOpener& defaultOpener();
  96. /// Returns a reference to the default URIStreamOpener.
  97. protected:
  98. std::istream* openFile(const Path& path) const;
  99. std::istream* openURI(const std::string& scheme, const URI& uri) const;
  100. private:
  101. URIStreamOpener(const URIStreamOpener&);
  102. URIStreamOpener& operator = (const URIStreamOpener&);
  103. typedef std::map<std::string, URIStreamFactory*> FactoryMap;
  104. FactoryMap _map;
  105. mutable FastMutex _mutex;
  106. };
  107. } // namespace Poco
  108. #endif // Foundation_URIStreamOpener_INCLUDED