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