• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_RESTOOL_SOLID_XML_H
17 #define OHOS_RESTOOL_SOLID_XML_H
18 
19 #include<fstream>
20 #include<memory>
21 #include<string>
22 #include<vector>
23 #include "libxml/parser.h"
24 #include "xml_key_node.h"
25 
26 namespace OHOS {
27 namespace Global {
28 namespace Restool {
29 class SolidXml {
30 public:
31     explicit SolidXml(const std::string &xmlPath, std::map<XmlKeyNode::KeyType, std::shared_ptr<XmlKeyNode>> &keys);
32     virtual ~SolidXml();
33     bool GenerateSolidXml(const std::string &filePath);
34     bool FlushNodeKeys(const std::string &filePath,
35         std::map<XmlKeyNode::KeyType, std::shared_ptr<XmlKeyNode>> &newKeys);
36 private:
37     class Node {
38     public:
Node()39         Node(): name_(-1), value_(-1), nameSpace_(-1) {};
~Node()40         virtual ~Node() {};
41         virtual void RawData(std::ofstream &out) const;
42         virtual bool LoadFrom(std::ifstream &in);
43         virtual bool FlushIndex(const std::map<XmlKeyNode::KeyType, std::shared_ptr<XmlKeyNode>> &oldKeys,
44             std::map<XmlKeyNode::KeyType, std::shared_ptr<XmlKeyNode>> &newKeys);
SetName(int32_t index)45         void SetName(int32_t index) { name_ = index; };
SetValue(int32_t index)46         void SetValue(int32_t index) { value_ = index; };
SetNameSpace(int32_t index)47         void SetNameSpace(int32_t index) { nameSpace_ = index; };
48     protected:
49         int32_t name_;
50         int32_t value_;
51         int32_t nameSpace_;
52     };
53 
54     class XmlNode : public Node {
55     public:
XmlNode()56         XmlNode() : child_(-1), brother_(-1) {};
~XmlNode()57         virtual ~XmlNode() {};
58         void RawData(std::ofstream &out) const override;
59         bool LoadFrom(std::ifstream &in) override;
60         bool FlushIndex(const std::map<XmlKeyNode::KeyType, std::shared_ptr<XmlKeyNode>> &oldKeys,
61             std::map<XmlKeyNode::KeyType, std::shared_ptr<XmlKeyNode>> &newKeys) override;
SetChild(int32_t index)62         void SetChild(int32_t index) { child_ = index; };
SetBrother(int32_t index)63         void SetBrother(int32_t index) { brother_ = index; };
AddAttribute(int32_t index)64         void AddAttribute(int32_t index) { attributes_.push_back(index); };
65     private:
66         int32_t child_;
67         int32_t brother_;
68         std::vector<int32_t> attributes_;
69     };
70 
71     void Compile(const xmlNodePtr nodePtr, std::shared_ptr<XmlNode> &node);
72     void CompileAttr(const xmlAttrPtr attrPtr, std::shared_ptr<XmlNode> &node);
73     void CompileNameSpace(const xmlNodePtr nodePtr, std::shared_ptr<XmlNode> &node);
74     void AddNampeSpaceDef(int32_t nameSpace, int32_t href);
75     bool SaveToFile(const std::string &filePath) const;
76     void PretreatmentAttr(std::string &value) const;
77     bool LoadFromFile(const std::string &sxmlPath);
78     static bool ChangeToNewKey(const std::map<XmlKeyNode::KeyType, std::shared_ptr<XmlKeyNode>> &oldKeys,
79         std::map<XmlKeyNode::KeyType, std::shared_ptr<XmlKeyNode>> &newKeys,
80         XmlKeyNode::KeyType keyType, int32_t &keyId);
81     bool FlushXmlnsKey(std::map<XmlKeyNode::KeyType, std::shared_ptr<XmlKeyNode>> &newKeys);
82     const std::string &xmlPath_;
83     std::map<XmlKeyNode::KeyType, std::shared_ptr<XmlKeyNode>> &keys_;
84     std::vector<std::shared_ptr<Node>> attributes_;
85     std::vector<std::shared_ptr<XmlNode>> nodes_;
86     std::vector<int32_t> nameSpaces_;
87     std::vector<int32_t> hrefs_;
88 
89     static const uint8_t SOLID_XML_MAGIC[];
90     static const uint32_t SOLID_XML_MAGIC_LENGTH;
91     #define CHECK_IO(stream) \
92         if (!stream) { \
93             return false; \
94         }
95 };
96 }
97 }
98 }
99 #endif