1 /* 2 * Copyright (c) 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 CONVERTXML_JS_CONVERTXML_H 17 #define CONVERTXML_JS_CONVERTXML_H 18 19 #include <string> 20 #include <vector> 21 #include "libxml/parser.h" 22 #include "libxml/tree.h" 23 #include "napi/native_api.h" 24 #include "napi/native_node_api.h" 25 26 namespace OHOS::Xml { 27 enum class SpaceType { 28 T_INT32, 29 T_STRING, 30 T_INIT = -1 31 }; 32 33 struct Options { 34 std::string declaration = "_declaration"; 35 std::string instruction = "_instruction"; 36 std::string attributes = "_attributes"; 37 std::string text = "_text"; 38 std::string cdata = "_cdata"; 39 std::string doctype = "_doctype"; 40 std::string comment = "_comment"; 41 std::string parent = "_parent"; 42 std::string type = "_type"; 43 std::string name = "_name"; 44 std::string elements = "_elements"; 45 bool compact = false; 46 bool trim = false; 47 bool nativetype = false; 48 bool nativetypeattributes = false; 49 bool addparent = false; 50 bool alwaysArray = false; 51 bool alwaysChildren = false; 52 bool instructionHasAttributes = false; 53 bool ignoreDeclaration = false; 54 bool ignoreInstruction = false; 55 bool ignoreAttributes = false; 56 bool ignoreComment = false; 57 bool ignoreCdata = false; 58 bool ignoreDoctype = false; 59 bool ignoreText = false; 60 bool spaces = false; 61 }; 62 63 struct XmlInfo { 64 bool bXml = false; 65 bool bVersion = false; 66 std::string strVersion = ""; 67 bool bEncoding = false; 68 std::string strEncoding = ""; 69 }; 70 71 class ConvertXml { 72 public: 73 /** 74 * To convert XML text to JavaScript object. 75 */ 76 explicit ConvertXml(); 77 78 /** 79 * The destructor of the ConvertXml. 80 */ ~ConvertXml()81 virtual ~ConvertXml() {} 82 83 /** 84 * To convert XML text to JavaScript object. 85 * 86 * @param env NAPI environment parameters. 87 * @param strXml A string of XML text. 88 */ 89 napi_value Convert(napi_env env, std::string strXml); 90 91 /** 92 * Converts the string of js to string of C++. 93 * 94 * @param env NAPI environment parameters. 95 * @param napi_StrValue JS layer incoming stringing. 96 * @param result The C++ layer accepts stringing. 97 */ 98 napi_status DealNapiStrValue(napi_env env, const napi_value napi_StrValue, std::string &result) const; 99 100 /** 101 * Handles user input of optional information 102 * 103 * @param env NAPI environment parameters. 104 * @param napiObj Get the option parameter from js to the napi layer 105 */ 106 void DealOptions(napi_env env, const napi_value napiObj); 107 108 friend class CxmlTest; 109 110 private: 111 void SetAttributes(napi_env env, xmlNodePtr curNode, const napi_value &elementsObject) const; 112 void SetXmlElementType(napi_env env, xmlNodePtr curNode, const napi_value &elementsObject, bool &bFlag) const; 113 void SetNodeInfo(napi_env env, xmlNodePtr curNode, const napi_value &elementsObject) const; 114 void SetEndInfo(napi_env env, xmlNodePtr curNode, const napi_value &elementsObject, bool &bFlag) const; 115 void GetXMLInfo(napi_env env, xmlNodePtr curNode, const napi_value &object, int flag = 0); 116 std::string GetNodeType(const xmlElementType enumType) const; 117 void SetKeyValue(napi_env env, const napi_value &object, const std::string strKey, 118 const std::string strValue) const; 119 std::string Trim(std::string strXmltrim) const; 120 void GetPrevNodeList(napi_env env, xmlNodePtr curNode); 121 void DealSpaces(napi_env env, const napi_value napiObj); 122 void DealIgnore(napi_env env, const napi_value napiObj); 123 void SetPrevInfo(napi_env env, const napi_value &recvElement, int flag, int32_t &index1) const; 124 void SetDefaultKey(size_t i, const std::string strRecv); 125 void SetSpacesInfo(napi_env env, const napi_value &object) const; 126 void DealSingleLine(napi_env env, std::string &strXml, const napi_value &object); 127 void DealComplex(napi_env env, std::string &strXml, const napi_value &object) const; 128 void Replace(std::string &str, const std::string src, const std::string dst) const; 129 void DealCDataInfo(bool bCData, xmlNodePtr &curNode) const; 130 131 SpaceType spaceType_; 132 int32_t iSpace_; 133 std::string strSpace_; 134 Options options_; 135 std::vector<napi_value> prevObj_; 136 XmlInfo xmlInfo_; 137 }; 138 } // namespace OHOS::Xml 139 #endif // CONVERTXML_JS_CONVERTXML_H 140