1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_ABILITY_RUNTIME_DATA_URI_UTILS_H
17 #define OHOS_ABILITY_RUNTIME_DATA_URI_UTILS_H
18 
19 #include <cstring>
20 #include "uri.h"
21 
22 using string = std::string;
23 using Uri = OHOS::Uri;
24 
25 namespace OHOS {
26 namespace AppExecFwk {
27 class DataUriUtils final {
28 public:
29     /**
30      * @brief Default constructor of DataUriUtils class
31      * @return None
32      */
33     DataUriUtils();
34 
35     /**
36      * @brief Default deconstructor of DataUriUtils class
37      * @return None
38      */
39     ~DataUriUtils();
40 
41     /**
42      * @brief Attaches the given ID to the end of the path component of the given URI.
43      * @param dataUri based on RFC 2396( Uniform Resource Identifier ).
44      * @param id
45      * @return Uri( scheme://authority/path1/path2/path3/updateIDNumber....)
46      */
47     static Uri AttachId(const Uri &dataUri, long long id);
48 
49     /**
50      * @brief Obtains the ID attached to the end of the path component of the given URI.
51      * @param dataUri based on RFC 2396( Uniform Resource Identifier ).
52      * @return long ID
53      */
54     static long long GetId(const Uri &dataUri);
55 
56     /**
57      * @brief Deletes the ID from the end of the path component of the given URI.
58      * @param dataUri based on RFC 2396( Uniform Resource Identifier ).
59      * @return long ID
60      */
61     static Uri DeleteId(const Uri &dataUri);
62 
63     /**
64      * @brief Updates the ID in the specified dataUri
65      * @param dataUri based on RFC 2396( Uniform Resource Identifier ).
66      * @param id indicates Update attached to the end of the path component of the given URI
67      * @return Uri return is the URI after path is updated
68      */
69     static Uri UpdateId(const Uri &dataUri, long long id);
70 
71     /**
72      * @brief Does the end path of the path component of the given URI have an ID attached to it?
73      * @param dataUri based on RFC 2396( Uniform Resource Identifier ).
74      * @return bool
75      */
76     static bool IsAttachedId(const Uri &dataUri);
77 
78 private:
79     /**
80      * @brief Determine whether the string content is a numeric string
81      * @param str indicates string.
82      * @return bool
83      */
84     static bool IsNumber(const string &str);
85 
86     /**
87      * @brief Determine whether the string content is a numeric string
88      * @param dataUri indicates Uri object
89      * @return Uri return is the URI after path is updated
90      */
91     static Uri UriUpateLastPath(const Uri &dataUri, const string &updateLastPath);
92 };
93 }  // namespace AppExecFwk
94 }  // namespace OHOS
95 
96 #endif
97