StringUri.hpp
Go to the documentation of this file.
1 // @formatter:off
2 //
3 // Balau core C++ library
4 //
5 // Copyright (C) 2008 Bora Software (contact@borasoftware.com)
6 //
7 // Licensed under the Boost Software License - Version 1.0 - August 17th, 2003.
8 // See the LICENSE file for the full license text.
9 //
10 
16 
17 #ifndef COM_BORA_SOFTWARE__BALAU_RESOURCE__STRING_URI
18 #define COM_BORA_SOFTWARE__BALAU_RESOURCE__STRING_URI
19 
25 #include <Balau/Resource/Uri.hpp>
26 #include <Balau/Util/Strings.hpp>
27 
28 namespace Balau::Resource {
29 
49 class StringUri : public Uri {
55  public: StringUri() = default;
56 
60  public: explicit StringUri(std::string data_)
61  : data(std::move(data_)) {}
62 
66  public: StringUri(const StringUri & copy) = default;
67 
71  public: StringUri(StringUri && rhs) noexcept = default;
72 
79  public: StringUri & append(std::string_view text) {
80  data.append(text.data(), text.length());
81  return *this;
82  }
83 
90  public: StringUri & append(char c) {
91  data += c;
92  return *this;
93  }
94 
101  public: StringUri & append(char32_t c) {
102  Util::Strings::append(data, c, 1);
103  return *this;
104  }
105 
111  public: const std::string & getString() const {
112  return data;
113  }
114 
115  public: bool isRegularDirectory() const override {
116  return false;
117  }
118 
119  public: bool isRegularFile() const override {
120  return false;
121  }
122 
123  public: std::string toUriString() const override {
124  return "string:";
125  }
126 
127  public: std::string toRawString() const override {
128  return data;
129  }
130 
131  public: size_t hashcode() const noexcept override {
132  return std::hash<std::string>()(data);
133  }
134 
135  public: std::unique_ptr<Uri> clone() const override {
136  return std::unique_ptr<Uri>(new StringUri(*this));
137  }
138 
139  public: std::unique_ptr<Uri> append(const std::string & pathComponent) const override {
140  ThrowBalauException(Exception::UnsupportedOperationException, "StringUri does not support path appending.");
141  }
142 
143  public: std::unique_ptr<Uri> resolve(std::string_view path) const override {
144  static const std::regex scheme { "[a-zA-Z][a-zA-Z0-9+-\\.]*:" };
145 
146  auto cleanPath = Util::Strings::trim(path);
147  auto str = std::string(cleanPath);
148 
149  if (Util::Strings::startsWithRegex(str, scheme)) {
150  std::unique_ptr<Uri> uri;
151  fromString(uri, str);
152  return uri;
153  }
154 
157  , "StringUri does not support calls to the resolve method without specifying a schema in the path."
158  );
159  }
160 
161  public: bool operator == (const Uri & rhs) const override {
162  const auto * o = dynamic_cast<const StringUri *>(&rhs);
163  return o != nullptr && data == o->data;
164  }
165 
166  public: bool canReadFrom() const override {
167  return true;
168  }
169 
170  public: bool canWriteTo() const override {
171  return false;
172  }
173 
178  return StringUriByteReadResource(*this);
179  }
180 
185  return StringUriUtf8To32ReadResource(*this);
186  }
187 
188  public: std::unique_ptr<ByteReadResource> byteReadResource() const override {
189  return std::unique_ptr<ByteReadResource>(new StringUriByteReadResource(*this));
190  }
191 
192  public: std::unique_ptr<Utf8To32ReadResource> utf8To32ReadResource() const override {
193  return std::unique_ptr<Utf8To32ReadResource>(new StringUriUtf8To32ReadResource(*this));
194  }
195 
196  public: std::unique_ptr<ByteWriteResource> byteWriteResource() override {
197  return std::unique_ptr<ByteWriteResource>(new StringUriByteWriteResource(*this));
198  }
199 
200  public: std::unique_ptr<Utf32To8WriteResource> utf32To8WriteResource() override {
201  return std::unique_ptr<Utf32To8WriteResource>(new StringUriUtf32To8WriteResource(*this));
202  }
203 
204  public: bool isRecursivelyIterable() const override {
205  return false;
206  }
207 
208  public: bool isIterable() const override {
209  return false;
210  }
211 
212  public: std::unique_ptr<RecursiveUriIterator> recursiveIterator() const override {
213  ThrowBalauException(Exception::NotImplementedException, "String URIs do not have recursive iterators.");
214  }
215 
216  public: std::unique_ptr<UriIterator> iterator() const override {
217  ThrowBalauException(Exception::NotImplementedException, "String URIs do not have iterators.");
218  }
219 
220  public: void dispatch(UriDispatcher & dispatcher) const override {
221  dispatcher.dispatch(*this);
222  }
223 
224  private: std::string data;
225 };
226 
227 } // namespace Balau::Resource
228 
229 #endif // COM_BORA_SOFTWARE__BALAU_RESOURCE__STRING_URI
bool isRegularDirectory() const override
Returns true if the URI points to a file directory.
Definition: StringUri.hpp:115
virtual void dispatch(const File &object)=0
Visit a File URI.
A read-only String URI resource which is read as bytes.
Definition: StringUriByteReadResource.hpp:33
bool isRecursivelyIterable() const override
Does the URI have a recursive iterator (examples: file and zip archive URIs).
Definition: StringUri.hpp:204
StringUri(std::string data_)
Create a String URI with the supplied string data.
Definition: StringUri.hpp:60
void dispatch(UriDispatcher &dispatcher) const override
Visitor pattern dispatching.
Definition: StringUri.hpp:220
StringUri & append(std::string_view text)
Append the supplied text to the internal string data.
Definition: StringUri.hpp:79
StringUri & append(char32_t c)
Append the supplied code point to the internal string data.
Definition: StringUri.hpp:101
StringUri & append(char c)
Append the supplied ascii character to the internal string data.
Definition: StringUri.hpp:90
#define ThrowBalauException(ExceptionClass,...)
Throw a Balau style exception, with implicit file and line number, and optional stacktrace.
Definition: BalauException.hpp:45
size_t hashcode() const noexcept override
Get the URI&#39;s hash code.
Definition: StringUri.hpp:131
std::unique_ptr< Utf8To32ReadResource > utf8To32ReadResource() const override
Get a UTF-8 to UTF-32 read resource for the URI.
Definition: StringUri.hpp:192
A read-only string URI resource which is read as bytes.
void fromString(File &destination, std::string_view value)
Overwrite the supplied file URI by assignment by converting the supplied UTF-8 string to a file URI...
Definition: File.hpp:733
bool isIterable() const override
Does the URI have a non-recursive iterator (examples: file and zip archive URIs). ...
Definition: StringUri.hpp:208
An abstract universal resource identifier.
Definition: Uri.hpp:131
bool operator==(const Uri &rhs) const override
Compare the supplied URI to the current URI.
Definition: StringUri.hpp:161
STL namespace.
std::unique_ptr< ByteWriteResource > byteWriteResource() override
Get a byte write resource for the URI.
Definition: StringUri.hpp:196
bool canWriteTo() const override
Can data be written to the URI via a write resource.
Definition: StringUri.hpp:170
The unified resource class hierarchy.
Definition: ByteReadResource.hpp:24
Thrown when a feature is not yet implemented.
Definition: BalauException.hpp:162
std::unique_ptr< Utf32To8WriteResource > utf32To8WriteResource() override
Get a UTF-32 to UTF-8 write resource for the URI.
Definition: StringUri.hpp:200
std::unique_ptr< RecursiveUriIterator > recursiveIterator() const override
Get a recursive iterator.
Definition: StringUri.hpp:212
const std::string & getString() const
Get a const reference to the string data of the String URI.
Definition: StringUri.hpp:111
A read-only String UTF-8 resource which is read as UTF-32 characters.
Thrown when an illegal argument is passed to a function or method.
Definition: BalauException.hpp:138
StringUri()=default
Create an empty String URI.
std::unique_ptr< ByteReadResource > byteReadResource() const override
Get a byte read resource for the URI.
Definition: StringUri.hpp:188
Utilities for strings.
std::string toUriString() const override
Get a string representing the URI, complete with scheme.
Definition: StringUri.hpp:123
A write-only UTF-8 resource in a standard file on a file system, which is written with UTF-32 charact...
Definition: StringUriUtf32To8WriteResource.hpp:35
static std::string_view trim(const std::string_view &input)
Trim whitespace from the beginning and end of the supplied UTF-8 string.
Definition: Strings.hpp:706
An immediate string pseudo-URI.
Definition: StringUri.hpp:49
std::unique_ptr< UriIterator > iterator() const override
Get a (non-recursive) iterator.
Definition: StringUri.hpp:216
bool isRegularFile() const override
Returns true if the URI is a regular file.
Definition: StringUri.hpp:119
bool canReadFrom() const override
Can data be read from the URI via a read resource.
Definition: StringUri.hpp:166
Balau exceptions for resources.
A write only String URI that is written as bytes.
Definition: StringUriByteWriteResource.hpp:33
std::unique_ptr< Uri > append(const std::string &pathComponent) const override
Appends the path component to the supplied URI, returning a new URI.
Definition: StringUri.hpp:139
StringUriByteReadResource getByteReadResource() const
Get a byte read resource for the HTTP source.
Definition: StringUri.hpp:177
static bool startsWithRegex(const std::string &str, const std::regex &prefix)
Does the string start with the specified regular expression?
Definition: Strings.hpp:94
std::string toRawString() const override
Get a string representing the raw URI.
Definition: StringUri.hpp:127
A write resource onto a String URI that is written as bytes.
static void append(std::string &str, char c, size_t count)
Appends count times the supplied ascii character to the supplied UTF-8 string (in place version)...
Definition: Strings.hpp:507
StringUriUtf8To32ReadResource getUtf8To32ReadResource() const
Get a UTF-8 to UTF-32 read resource for the HTTP source.
Definition: StringUri.hpp:184
std::unique_ptr< Uri > clone() const override
Clone the concrete Uri.
Definition: StringUri.hpp:135
A read-only String UTF-8 resource which is read as UTF-32 characters.
Definition: StringUriUtf8To32ReadResource.hpp:37
Thrown when an operation is deliberately not implemented.
Definition: BalauException.hpp:154
The abstract URI base class.
Visitor interface for URIs.
Definition: UriDispatcher.hpp:32
std::unique_ptr< Uri > resolve(std::string_view path) const override
Resolve the relative or absolute path, in reference to the current URI.
Definition: StringUri.hpp:143
A write-only String based UTF-8 resource which is written as UTF-32 characters.