• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 XML_JS_XML_DYNAMIC_H
17 #define XML_JS_XML_DYNAMIC_H
18 
19 #include <algorithm>
20 #include <cstring>
21 #include <map>
22 #include <string>
23 #include <vector>
24 #include "napi/native_api.h"
25 #include "napi/native_node_api.h"
26 #include "native_engine/native_engine.h"
27 #include "tools/log.h"
28 
29 namespace OHOS::xml {
30     constexpr uint32_t MAX_XML_LENGTH = 100000;
31     constexpr uint32_t INIT_XML_LENGTH = 8 * 1024;
32     enum ErrorCodeEnum {
33         BUFFER_OVERFLOW = 10200062,
34         ILLEGAL_POSITION = 10200063,
35         NO_ELEMENT_MATCH = 10200064
36     };
37 
38     class XmlDynamicSerializer {
39     public:
40 
41         /**
42          * Constructor for XmlDynamicSerializer.
43          *
44          * @param encoding Is the encoding format of XML serializer.
45          */
46         explicit XmlDynamicSerializer(napi_env env,
env_(env)47                                       const std::string &encoding = "utf-8") :env_(env), encoding_(encoding)
48         {
49             strXml_.reserve(INIT_XML_LENGTH);
50         }
51 
52         /**
53          * XmlDynamicSerializer destructor.
54          */
~XmlDynamicSerializer()55         ~XmlDynamicSerializer() {}
56 
57         /**
58          * Set the Attributes method.
59          *
60          * @param name The parameter is the key value of the property.
61          * @param value The parameter is the value of the property.
62          */
63         void SetAttributes(const std::string &name, const std::string &value);
64 
65         /**
66          * Writes an empty element.
67          *
68          * @param name The parameter is the element name of the empty element.
69          */
70         void AddEmptyElement(std::string name);
71 
72         /**
73          * Set the Declaration method.
74          *
75          */
76         void SetDeclaration();
77 
78         /**
79          * Writes the element start tag with the given name.
80          *
81          * @param name The parameter is the element name of the current element.
82          */
83         void StartElement(const std::string &name);
84 
85         /**
86          * Write element end tag.
87          *
88          */
89         void EndElement();
90 
91         /**
92          * The namespace into which the current element tag is written.
93          *
94          * @param prefix The parameter is the prefix of the current element and its children.
95          * @param nsTemp The parameter is the namespace of the current element and its children.
96          */
97         void SetNamespace(std::string prefix, const std::string &nsTemp);
98 
99         /**
100          * Write the comment property.
101          *
102          * @param comment The parameter is the comment content of the current element.
103          */
104         void SetComment(const std::string &comment);
105 
106         /**
107          * Write CDATA attributes.
108          *
109          * @param data The parameter is the content of the CDATA attribute.
110          */
111         void SetCData(std::string data);
112 
113         /**
114          * Write text attributes.
115          *
116          * @param text The parameter is the content between the elements.
117          */
118         void SetText(const std::string &text);
119 
120         /**
121          * Write DocType property.
122          *
123          * @param text The parameter is the content of the DocType property.
124          */
125         void SetDocType(const std::string &text);
126 
127         /**
128          * Write an escape.
129          *
130          * @param s The parameter is the passed in escaped string.
131          */
132         void WriteEscaped(std::string s);
133 
134         /**
135          * Set namespace splice.
136          */
137         void SplicNsp();
138 
139         /**
140          * Set next item.
141          */
142         void NextItem();
143 
144         /**
145          * Get XML serializer buffer length.
146          *
147          * @return XML serializer buffer length.
148          */
149         size_t GetXmlBufferLength();
150 
151         /**
152          * Get XML serializer buffer.
153          *
154          * @param data The parameter is the point of XML serializer buffer
155          * @param length The parameter is the length of XML serializer buffer.
156          * @return If the funtion copy buffer failed return false, else return true.
157          */
158         bool GetXmlBuffer(void* data, uint32_t length);
159 
160         /**
161          * Process the value of the string passed by napi.
162          *
163          * @param env The parameter is NAPI environment variables.
164          * @param napiStr The parameter is pass parameters.
165          * @param result The parameter is return the processed value.
166          * @return Native API status.
167          */
168         static napi_status DealNapiStrValue(napi_env env, const napi_value napiStr, std::string &result);
169 
170         friend class XmlTest;
171 
172     private:
173         void BufferCopy();
174         std::string Replace(std::string str, const std::string &subStr, const std::string &repStr);
175         int32_t curNspNum {0};
176         int32_t elementNum_ {0};
177         size_t depth_ {0};
178         ErrorCodeEnum errorCode_;
179         napi_env env_ {nullptr};
180         std::string xmlSerializerError_ {""};
181         std::string encoding_ {""};
182         std::string out_ {""};
183         std::string strXml_ {""};
184         std::string type_ {""};
185         std::vector<std::string> elementStack_ = { "", "", ""};
186         std::map<int, std::map<int, std::string>> multNsp_;
187     };
188 } // namespace OHOS::Xml
189 #endif // XML_JS_XML_DYNAMIC_H